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.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
}
}

View File

@ -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
}
}

View File

@ -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)
}

View File

@ -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)

View File

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