This commit is contained in:
Admin
2020-02-20 20:38:05 +02:00
parent a552328888
commit 21c2a4aa9a
12 changed files with 241 additions and 3 deletions

View File

@@ -0,0 +1,24 @@
package org.koitharu.kotatsu.ui.common
import android.content.Context
import androidx.annotation.StringRes
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import org.koin.core.KoinComponent
import org.koin.core.inject
import org.koitharu.kotatsu.core.prefs.AppSettings
abstract class BasePreferenceFragment(@StringRes private val titleId: Int) :
PreferenceFragmentCompat(), KoinComponent {
protected val settings by inject<AppSettings>()
override fun onAttach(context: Context) {
super.onAttach(context)
activity?.setTitle(titleId)
}
fun <T : Preference> findPreference(@StringRes keyId: Int): T? =
findPreference(getString(keyId))
}

View File

@@ -15,6 +15,7 @@ import org.koitharu.kotatsu.ui.main.list.favourites.FavouritesListFragment
import org.koitharu.kotatsu.ui.main.list.history.HistoryListFragment
import org.koitharu.kotatsu.ui.main.list.local.LocalListFragment
import org.koitharu.kotatsu.ui.main.list.remote.RemoteListFragment
import org.koitharu.kotatsu.ui.settings.SettingsActivity
import org.koitharu.kotatsu.utils.SearchHelper
class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {
@@ -70,6 +71,10 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
R.id.nav_history -> setPrimaryFragment(HistoryListFragment.newInstance())
R.id.nav_favourites -> setPrimaryFragment(FavouritesListFragment.newInstance())
R.id.nav_local_storage -> setPrimaryFragment(LocalListFragment.newInstance())
R.id.nav_action_settings -> {
startActivity(SettingsActivity.newIntent(this))
return true
}
else -> return false
}
drawer.closeDrawers()

View File

@@ -0,0 +1,58 @@
package org.koitharu.kotatsu.ui.settings
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import androidx.collection.arrayMapOf
import androidx.preference.Preference
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.prefs.ListMode
import org.koitharu.kotatsu.ui.common.BasePreferenceFragment
import org.koitharu.kotatsu.ui.main.list.ListModeSelectDialog
class AppearanceSettingsFragment : BasePreferenceFragment(R.string.appearance),
SharedPreferences.OnSharedPreferenceChangeListener {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_appearance)
findPreference<Preference>(R.string.key_list_mode)?.summary =
listModes[settings.listMode]?.let(::getString)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
settings.subscribe(this)
}
override fun onDestroyView() {
settings.unsubscribe(this)
super.onDestroyView()
}
override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
getString(R.string.key_list_mode) -> {
ListModeSelectDialog.show(childFragmentManager)
true
}
else -> super.onPreferenceTreeClick(preference)
}
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
getString(R.string.key_list_mode) -> findPreference<Preference>(R.string.key_list_mode)?.summary =
listModes[settings.listMode]?.let(::getString)
}
}
private companion object {
val listModes = arrayMapOf(
ListMode.DETAILED_LIST to R.string.detailed_list,
ListMode.GRID to R.string.grid,
ListMode.LIST to R.string.list
)
}
}

View File

@@ -0,0 +1,27 @@
package org.koitharu.kotatsu.ui.settings
import android.content.Context
import android.content.Intent
import android.os.Bundle
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.ui.common.BaseActivity
class SettingsActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
if (supportFragmentManager.findFragmentById(R.id.container) == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, SettingsHeadersFragment())
.commit()
}
}
companion object {
fun newIntent(context: Context) = Intent(context, SettingsActivity::class.java)
}
}

View File

@@ -0,0 +1,12 @@
package org.koitharu.kotatsu.ui.settings
import android.os.Bundle
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.ui.common.BasePreferenceFragment
class SettingsHeadersFragment : BasePreferenceFragment(R.string.settings) {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_headers)
}
}

View File

@@ -0,0 +1,33 @@
package org.koitharu.kotatsu.utils.ext
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
fun ListPreference.bindSummary(listener: (String) -> Boolean = { true }) {
summary = entries.getOrNull(findIndexOfValue(value))
this.setOnPreferenceChangeListener { preference, newValue ->
newValue as String
preference as ListPreference
val res = listener(newValue)
if (res) {
preference.summary = preference.entries.getOrNull(preference.findIndexOfValue(newValue))
}
res
}
}
fun EditTextPreference.bindSummary(
formatter: (String) -> String = { it },
listener: (String) -> Boolean = { true }
) {
summary = formatter(text)
this.setOnPreferenceChangeListener { preference, newValue ->
newValue as String
preference as EditTextPreference
val res = listener(newValue)
if (res) {
preference.summary = formatter(newValue)
}
res
}
}