From c1faf2fe060ae67c265cd9cfca1ff55a43741219 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 16 May 2020 09:07:09 +0300 Subject: [PATCH] Remember last opened section --- .../koitharu/kotatsu/core/prefs/AppSection.kt | 6 ++++ .../kotatsu/core/prefs/AppSettings.kt | 7 +++- .../koitharu/kotatsu/core/prefs/ListMode.kt | 11 ++---- .../koitharu/kotatsu/ui/main/MainActivity.kt | 36 ++++++++++++++++--- app/src/main/res/values/constants.xml | 1 + 5 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSection.kt diff --git a/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSection.kt b/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSection.kt new file mode 100644 index 000000000..56ba0b401 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSection.kt @@ -0,0 +1,6 @@ +package org.koitharu.kotatsu.core.prefs + +enum class AppSection { + + LOCAL, FAVOURITES, HISTORY; +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt index bc91bacf9..d1bf1a919 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -3,7 +3,6 @@ package org.koitharu.kotatsu.core.prefs import android.content.Context import android.content.SharedPreferences import android.content.res.Resources -import android.os.StatFs import android.provider.Settings import androidx.appcompat.app.AppCompatDelegate import androidx.core.content.edit @@ -27,6 +26,12 @@ class AppSettings private constructor(resources: Resources, private val prefs: S ListMode.DETAILED_LIST ) + var defaultSection by EnumPreferenceDelegate( + AppSection::class.java, + resources.getString(R.string.key_app_section), + AppSection.HISTORY + ) + val theme by StringIntPreferenceDelegate( resources.getString(R.string.key_theme), AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM diff --git a/app/src/main/java/org/koitharu/kotatsu/core/prefs/ListMode.kt b/app/src/main/java/org/koitharu/kotatsu/core/prefs/ListMode.kt index 5934c49f8..dc8269cad 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/prefs/ListMode.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/prefs/ListMode.kt @@ -1,13 +1,6 @@ package org.koitharu.kotatsu.core.prefs -enum class ListMode(val id: Int) { +enum class ListMode { - LIST(0), - DETAILED_LIST(1), - GRID(2); - - companion object { - - fun valueOf(id: Int) = values().firstOrNull { it.id == id } - } + LIST, DETAILED_LIST, GRID; } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt b/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt index 75ac835c0..cdc4c4ec1 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt @@ -21,6 +21,7 @@ import moxy.ktx.moxyPresenter import org.koin.core.inject import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.model.MangaSource +import org.koitharu.kotatsu.core.prefs.AppSection import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.domain.MangaProviderFactory import org.koitharu.kotatsu.ui.common.BaseActivity @@ -65,8 +66,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList supportFragmentManager.findFragmentById(R.id.container)?.let { fab.isVisible = it is HistoryListFragment } ?: run { - navigationView.setCheckedItem(R.id.nav_history) - setPrimaryFragment(HistoryListFragment.newInstance()) + openDefaultSection() } drawer.postDelayed(2000) { AppUpdateService.startIfRequired(applicationContext) @@ -106,9 +106,18 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList val source = MangaSource.values().getOrNull(item.itemId) ?: return false setPrimaryFragment(RemoteListFragment.newInstance(source)) } else when (item.itemId) { - R.id.nav_history -> setPrimaryFragment(HistoryListFragment.newInstance()) - R.id.nav_favourites -> setPrimaryFragment(FavouritesContainerFragment.newInstance()) - R.id.nav_local_storage -> setPrimaryFragment(LocalListFragment.newInstance()) + R.id.nav_history -> { + settings.defaultSection = AppSection.HISTORY + setPrimaryFragment(HistoryListFragment.newInstance()) + } + R.id.nav_favourites -> { + settings.defaultSection = AppSection.FAVOURITES + setPrimaryFragment(FavouritesContainerFragment.newInstance()) + } + R.id.nav_local_storage -> { + settings.defaultSection = AppSection.LOCAL + setPrimaryFragment(LocalListFragment.newInstance()) + } R.id.nav_action_settings -> { startActivity(SettingsActivity.newIntent(this)) return true @@ -167,6 +176,23 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } } + private fun openDefaultSection() { + when(settings.defaultSection) { + AppSection.LOCAL -> { + navigationView.setCheckedItem(R.id.nav_local_storage) + setPrimaryFragment(LocalListFragment.newInstance()) + } + AppSection.FAVOURITES -> { + navigationView.setCheckedItem(R.id.nav_favourites) + setPrimaryFragment(FavouritesContainerFragment.newInstance()) + } + AppSection.HISTORY -> { + navigationView.setCheckedItem(R.id.nav_history) + setPrimaryFragment(HistoryListFragment.newInstance()) + } + } + } + private fun setPrimaryFragment(fragment: Fragment) { supportFragmentManager.beginTransaction() .replace(R.id.container, fragment) diff --git a/app/src/main/res/values/constants.xml b/app/src/main/res/values/constants.xml index bc69568ca..92c2d4f00 100644 --- a/app/src/main/res/values/constants.xml +++ b/app/src/main/res/values/constants.xml @@ -1,6 +1,7 @@ list_mode + app_section theme sources_order sources_hidden