From 8c1bedf7963e9274a05e4f55b579f80d0a73883a Mon Sep 17 00:00:00 2001 From: len Date: Wed, 20 Jul 2016 19:09:28 +0200 Subject: [PATCH] Back button now returns to start screen. Also fix #356 --- .../kanade/tachiyomi/ui/main/MainActivity.kt | 82 +++++++++++++------ .../tachiyomi/ui/setting/SettingsActivity.kt | 20 +++++ .../ui/setting/SettingsAdvancedFragment.kt | 1 + .../ui/setting/SettingsGeneralFragment.kt | 10 +-- app/src/main/res/menu/menu_navigation.xml | 2 +- 5 files changed, 82 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 7c1c98a6c..c93e292ee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.main import android.content.Intent import android.os.Bundle import android.support.v4.app.Fragment +import android.support.v4.app.TaskStackBuilder import android.support.v4.view.GravityCompat import android.view.MenuItem import eu.kanade.tachiyomi.R @@ -23,6 +24,15 @@ class MainActivity : BaseActivity() { 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?) { setAppTheme() super.onCreate(savedState) @@ -45,28 +55,28 @@ class MainActivity : BaseActivity() { // Make information view invisible empty_view.hide() - when (item.itemId) { - R.id.nav_drawer_library -> setFragment(LibraryFragment.newInstance()) - R.id.nav_drawer_recent_updates -> setFragment(RecentChaptersFragment.newInstance()) - R.id.nav_drawer_recent_manga -> setFragment(RecentlyReadFragment.newInstance()) - R.id.nav_drawer_catalogues -> setFragment(CatalogueFragment.newInstance()) - R.id.nav_drawer_downloads -> setFragment(DownloadFragment.newInstance()) - R.id.nav_drawer_settings -> startActivity(Intent(this, SettingsActivity::class.java)) - R.id.nav_drawer_backup -> setFragment(BackupFragment.newInstance()) + val id = item.itemId + when (id) { + R.id.nav_drawer_library -> setFragment(LibraryFragment.newInstance(), id) + R.id.nav_drawer_recently_read -> setFragment(RecentlyReadFragment.newInstance(), id) + R.id.nav_drawer_recent_updates -> setFragment(RecentChaptersFragment.newInstance(), id) + R.id.nav_drawer_catalogues -> setFragment(CatalogueFragment.newInstance(), id) + R.id.nav_drawer_downloads -> setFragment(DownloadFragment.newInstance(), id) + 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) true } if (savedState == null) { + // Set start screen + setSelectedDrawerItem(startScreenId) - when (preferences.startScreen()) { - 1 -> setFragment(LibraryFragment.newInstance()) - 2 -> setFragment(RecentlyReadFragment.newInstance()) - 3 -> setFragment(RecentChaptersFragment.newInstance()) - else -> setFragment(LibraryFragment.newInstance()) - } - + // Show changelog if needed ChangelogDialogFragment.show(preferences, supportFragmentManager) } } @@ -80,23 +90,45 @@ class MainActivity : BaseActivity() { } override fun onBackPressed() { - supportFragmentManager.findFragmentById(R.id.frame_container)?.let { - if (it !is LibraryFragment) { - nav_view.setCheckedItem(R.id.nav_drawer_library) - nav_view.menu.performIdentifierAction(R.id.nav_drawer_library, 0) - } else { - super.onBackPressed() - } - } ?: super.onBackPressed() + val fragment = supportFragmentManager.findFragmentById(R.id.frame_container) + if (fragment != null && fragment.tag.toInt() != startScreenId) { + setSelectedDrawerItem(startScreenId) + } else { + 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() - .replace(R.id.frame_container, fragment) + .replace(R.id.frame_container, fragment, "$itemId") .commit() } fun updateEmptyView(show: Boolean, textResource: Int, drawable: Int) { if (show) empty_view.show(drawable, textResource) else empty_view.hide() } + + companion object { + private const val REQUEST_OPEN_SETTINGS = 200 + } } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt index 870ce706b..331e63d0b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt @@ -16,6 +16,15 @@ class SettingsActivity : BaseActivity(), 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?) { setAppTheme() super.onCreate(savedState) @@ -29,10 +38,16 @@ class SettingsActivity : BaseActivity(), supportFragmentManager.beginTransaction() .add(R.id.settings_content, SettingsFragment.newInstance(null), "Settings") .commit() + } else { + parentFlags = savedState.getInt(SettingsActivity::parentFlags.name) } setupToolbar(toolbar, backNavigation = false) + } + override fun onSaveInstanceState(outState: Bundle) { + outState.putInt(SettingsActivity::parentFlags.name, parentFlags) + super.onSaveInstanceState(outState) } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -60,4 +75,9 @@ class SettingsActivity : BaseActivity(), return true } + companion object { + const val FLAG_THEME_CHANGED = 0x1 + const val FLAG_DATABASE_CLEARED = 0x2 + } + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt index 9d2259a67..d4585d837 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt @@ -97,6 +97,7 @@ class SettingsAdvancedFragment : SettingsFragment() { .positiveText(android.R.string.yes) .negativeText(android.R.string.no) .onPositive { dialog, which -> + (activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_DATABASE_CLEARED db.deleteMangasNotInLibrary().executeAsBlocking() activity.toast(R.string.clear_database_completed) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt index 139106c77..49d0ed2e5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt @@ -1,8 +1,6 @@ package eu.kanade.tachiyomi.ui.setting -import android.content.Intent import android.os.Bundle -import android.support.v4.app.TaskStackBuilder import android.support.v7.preference.Preference import android.support.v7.preference.PreferenceFragmentCompat import android.support.v7.preference.XpPreferenceFragment @@ -67,16 +65,14 @@ class SettingsGeneralFragment : SettingsFragment(), } themePreference.setOnPreferenceChangeListener { preference, newValue -> - // Rebuild activity's to apply themes. - TaskStackBuilder.create(context) - .addNextIntentWithParentStack(Intent(activity.intent)) - .startActivities() + (activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_THEME_CHANGED + activity.recreate() true } } 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) fragment.setTargetFragment(this, 0) fragment.show(fragmentManager, null) diff --git a/app/src/main/res/menu/menu_navigation.xml b/app/src/main/res/menu/menu_navigation.xml index f236741f5..cdf4cea36 100644 --- a/app/src/main/res/menu/menu_navigation.xml +++ b/app/src/main/res/menu/menu_navigation.xml @@ -8,7 +8,7 @@ android:icon="@drawable/ic_book_black_24dp" android:title="@string/label_library" />