Back button now returns to start screen. Also fix #356

This commit is contained in:
len 2016-07-20 19:09:28 +02:00
parent 1090c04fe3
commit 8c1bedf796
5 changed files with 82 additions and 33 deletions

View file

@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.main
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v4.app.TaskStackBuilder
import android.support.v4.view.GravityCompat import android.support.v4.view.GravityCompat
import android.view.MenuItem import android.view.MenuItem
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
@ -23,6 +24,15 @@ class MainActivity : BaseActivity() {
val preferences: PreferencesHelper by injectLazy() val preferences: PreferencesHelper by injectLazy()
private val startScreenId by lazy {
when (preferences.startScreen()) {
1 -> R.id.nav_drawer_library
2 -> R.id.nav_drawer_recently_read
3 -> R.id.nav_drawer_recent_updates
else -> R.id.nav_drawer_library
}
}
override fun onCreate(savedState: Bundle?) { override fun onCreate(savedState: Bundle?) {
setAppTheme() setAppTheme()
super.onCreate(savedState) super.onCreate(savedState)
@ -45,28 +55,28 @@ class MainActivity : BaseActivity() {
// Make information view invisible // Make information view invisible
empty_view.hide() empty_view.hide()
when (item.itemId) { val id = item.itemId
R.id.nav_drawer_library -> setFragment(LibraryFragment.newInstance()) when (id) {
R.id.nav_drawer_recent_updates -> setFragment(RecentChaptersFragment.newInstance()) R.id.nav_drawer_library -> setFragment(LibraryFragment.newInstance(), id)
R.id.nav_drawer_recent_manga -> setFragment(RecentlyReadFragment.newInstance()) R.id.nav_drawer_recently_read -> setFragment(RecentlyReadFragment.newInstance(), id)
R.id.nav_drawer_catalogues -> setFragment(CatalogueFragment.newInstance()) R.id.nav_drawer_recent_updates -> setFragment(RecentChaptersFragment.newInstance(), id)
R.id.nav_drawer_downloads -> setFragment(DownloadFragment.newInstance()) R.id.nav_drawer_catalogues -> setFragment(CatalogueFragment.newInstance(), id)
R.id.nav_drawer_settings -> startActivity(Intent(this, SettingsActivity::class.java)) R.id.nav_drawer_downloads -> setFragment(DownloadFragment.newInstance(), id)
R.id.nav_drawer_backup -> setFragment(BackupFragment.newInstance()) R.id.nav_drawer_settings -> {
val intent = Intent(this, SettingsActivity::class.java)
startActivityForResult(intent, REQUEST_OPEN_SETTINGS)
}
R.id.nav_drawer_backup -> setFragment(BackupFragment.newInstance(), id)
} }
drawer.closeDrawer(GravityCompat.START) drawer.closeDrawer(GravityCompat.START)
true true
} }
if (savedState == null) { if (savedState == null) {
// Set start screen
setSelectedDrawerItem(startScreenId)
when (preferences.startScreen()) { // Show changelog if needed
1 -> setFragment(LibraryFragment.newInstance())
2 -> setFragment(RecentlyReadFragment.newInstance())
3 -> setFragment(RecentChaptersFragment.newInstance())
else -> setFragment(LibraryFragment.newInstance())
}
ChangelogDialogFragment.show(preferences, supportFragmentManager) ChangelogDialogFragment.show(preferences, supportFragmentManager)
} }
} }
@ -80,23 +90,45 @@ class MainActivity : BaseActivity() {
} }
override fun onBackPressed() { override fun onBackPressed() {
supportFragmentManager.findFragmentById(R.id.frame_container)?.let { val fragment = supportFragmentManager.findFragmentById(R.id.frame_container)
if (it !is LibraryFragment) { if (fragment != null && fragment.tag.toInt() != startScreenId) {
nav_view.setCheckedItem(R.id.nav_drawer_library) setSelectedDrawerItem(startScreenId)
nav_view.menu.performIdentifierAction(R.id.nav_drawer_library, 0)
} else { } else {
super.onBackPressed() super.onBackPressed()
} }
} ?: super.onBackPressed()
} }
fun setFragment(fragment: Fragment) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_OPEN_SETTINGS && resultCode != 0) {
if (resultCode and SettingsActivity.FLAG_DATABASE_CLEARED != 0) {
// If database is cleared avoid undefined behavior by recreating the stack.
TaskStackBuilder.create(this)
.addNextIntent(Intent(this, MainActivity::class.java))
.startActivities()
} else if (resultCode and SettingsActivity.FLAG_THEME_CHANGED != 0) {
recreate()
}
}
}
private fun setSelectedDrawerItem(itemId: Int, triggerAction: Boolean = true) {
nav_view.setCheckedItem(itemId)
if (triggerAction) {
nav_view.menu.performIdentifierAction(itemId, 0)
}
}
private fun setFragment(fragment: Fragment, itemId: Int) {
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment) .replace(R.id.frame_container, fragment, "$itemId")
.commit() .commit()
} }
fun updateEmptyView(show: Boolean, textResource: Int, drawable: Int) { fun updateEmptyView(show: Boolean, textResource: Int, drawable: Int) {
if (show) empty_view.show(drawable, textResource) else empty_view.hide() if (show) empty_view.show(drawable, textResource) else empty_view.hide()
} }
companion object {
private const val REQUEST_OPEN_SETTINGS = 200
}
} }

View file

@ -16,6 +16,15 @@ class SettingsActivity : BaseActivity(),
private lateinit var replaceFragmentStrategy: ReplaceFragment private lateinit var replaceFragmentStrategy: ReplaceFragment
/**
* Flags to send to the parent activity for reacting to preference changes.
*/
var parentFlags = 0
set(value) {
field = field or value
setResult(field)
}
override fun onCreate(savedState: Bundle?) { override fun onCreate(savedState: Bundle?) {
setAppTheme() setAppTheme()
super.onCreate(savedState) super.onCreate(savedState)
@ -29,10 +38,16 @@ class SettingsActivity : BaseActivity(),
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction()
.add(R.id.settings_content, SettingsFragment.newInstance(null), "Settings") .add(R.id.settings_content, SettingsFragment.newInstance(null), "Settings")
.commit() .commit()
} else {
parentFlags = savedState.getInt(SettingsActivity::parentFlags.name)
} }
setupToolbar(toolbar, backNavigation = false) setupToolbar(toolbar, backNavigation = false)
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(SettingsActivity::parentFlags.name, parentFlags)
super.onSaveInstanceState(outState)
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
@ -60,4 +75,9 @@ class SettingsActivity : BaseActivity(),
return true return true
} }
companion object {
const val FLAG_THEME_CHANGED = 0x1
const val FLAG_DATABASE_CLEARED = 0x2
}
} }

View file

@ -97,6 +97,7 @@ class SettingsAdvancedFragment : SettingsFragment() {
.positiveText(android.R.string.yes) .positiveText(android.R.string.yes)
.negativeText(android.R.string.no) .negativeText(android.R.string.no)
.onPositive { dialog, which -> .onPositive { dialog, which ->
(activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_DATABASE_CLEARED
db.deleteMangasNotInLibrary().executeAsBlocking() db.deleteMangasNotInLibrary().executeAsBlocking()
activity.toast(R.string.clear_database_completed) activity.toast(R.string.clear_database_completed)
} }

View file

@ -1,8 +1,6 @@
package eu.kanade.tachiyomi.ui.setting package eu.kanade.tachiyomi.ui.setting
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.TaskStackBuilder
import android.support.v7.preference.Preference import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v7.preference.XpPreferenceFragment import android.support.v7.preference.XpPreferenceFragment
@ -67,16 +65,14 @@ class SettingsGeneralFragment : SettingsFragment(),
} }
themePreference.setOnPreferenceChangeListener { preference, newValue -> themePreference.setOnPreferenceChangeListener { preference, newValue ->
// Rebuild activity's to apply themes. (activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_THEME_CHANGED
TaskStackBuilder.create(context) activity.recreate()
.addNextIntentWithParentStack(Intent(activity.intent))
.startActivities()
true true
} }
} }
override fun onPreferenceDisplayDialog(p0: PreferenceFragmentCompat?, p: Preference): Boolean { override fun onPreferenceDisplayDialog(p0: PreferenceFragmentCompat?, p: Preference): Boolean {
if (p.key == getString(R.string.pref_library_columns_dialog_key)) { if (p === columnsPreference) {
val fragment = LibraryColumnsDialog.newInstance(p) val fragment = LibraryColumnsDialog.newInstance(p)
fragment.setTargetFragment(this, 0) fragment.setTargetFragment(this, 0)
fragment.show(fragmentManager, null) fragment.show(fragmentManager, null)

View file

@ -8,7 +8,7 @@
android:icon="@drawable/ic_book_black_24dp" android:icon="@drawable/ic_book_black_24dp"
android:title="@string/label_library" /> android:title="@string/label_library" />
<item <item
android:id="@+id/nav_drawer_recent_manga" android:id="@+id/nav_drawer_recently_read"
android:icon="@drawable/ic_glasses_black_24dp" android:icon="@drawable/ic_glasses_black_24dp"
android:title="@string/label_recent_manga"/> android:title="@string/label_recent_manga"/>
<item <item