Skip to content

Commit

Permalink
refactor(ui): reorganize preference screen
Browse files Browse the repository at this point in the history
- use Navigation component to navigate to different fragments
- polish some preference names and their summaries
- replace some preference icons with more appropriate ones
- reorder preference entries
- use one Activity to navigate to all fragments
  • Loading branch information
WhiredPlanck committed Aug 6, 2022
1 parent 545a392 commit be95e64
Show file tree
Hide file tree
Showing 20 changed files with 307 additions and 341 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ dependencies {
implementation "androidx.constraintlayout:constraintlayout:2.1.2"
implementation "androidx.viewpager2:viewpager2:1.0.0"
implementation "androidx.fragment:fragment-ktx:1.4.0"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
Expand Down
77 changes: 77 additions & 0 deletions app/src/main/java/com/osfans/trime/ui/fragments/AboutFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.osfans.trime.ui.fragments

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.webkit.WebView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.activityViewModels
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.get
import com.blankj.utilcode.util.ToastUtils
import com.osfans.trime.BuildConfig
import com.osfans.trime.R
import com.osfans.trime.core.Rime
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.util.AppVersionUtils.writeLibraryVersionToSummary

class AboutFragment : PreferenceFragmentCompat() {
private val viewModel: MainViewModel by activityViewModels()

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.about_preference, rootKey)
with(preferenceScreen) {
get<Preference>("about__changelog")
?.writeLibraryVersionToSummary(BuildConfig.BUILD_VERSION)
get<Preference>("about__buildinfo")?.apply {
writeLibraryVersionToSummary(BuildConfig.BUILD_INFO)
setOnPreferenceClickListener {
val cbm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val info = ClipData.newPlainText("BuildInfo", BuildConfig.BUILD_INFO)
cbm.setPrimaryClip(info)
ToastUtils.showLong(R.string.copy_done)
true
}
}
get<Preference>("about__librime_version")
?.writeLibraryVersionToSummary(Rime.get_librime_version())
get<Preference>("about__opencc_version")
?.writeLibraryVersionToSummary(Rime.get_opencc_version())
get<Preference>("pref_trime_custom_qq")
?.hidden()
get<Preference>("about__licensing")?.setOnPreferenceClickListener {
val webView = WebView(requireContext())
webView.loadUrl("file:///android_asset/license/open_source_license.html")
AlertDialog.Builder(requireContext())
.setTitle(R.string.about__licensing_title)
.setView(webView)
.setPositiveButton(android.R.string.ok, null)
.show()
true
}
get<Preference>("about__used_libraries")?.setOnPreferenceClickListener {
val webView = WebView(requireContext())
webView.loadUrl("file:///android_asset/license/library_licenses.html")
AlertDialog.Builder(requireContext())
.setTitle(R.string.about__used_library_dialog_title)
.setView(webView)
.setPositiveButton(android.R.string.ok, null)
.show()
true
}
}
}

override fun onResume() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.pref_about))
}

private fun Preference.hidden() {
if (this.summary.isBlank() || this.intent.data.toString().isBlank()) {
this.isVisible = false
}
}
}
30 changes: 15 additions & 15 deletions app/src/main/java/com/osfans/trime/ui/fragments/ConfFragment.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
@file:Suppress("DEPRECATION")

package com.osfans.trime.ui.fragments

import android.app.ProgressDialog
import android.content.Context
import android.os.Bundle
import android.view.Menu
import androidx.core.view.forEach
import androidx.fragment.app.activityViewModels
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import androidx.preference.get
import com.osfans.trime.R
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.ui.components.ResetAssetsDialog
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.util.RimeUtils
import com.osfans.trime.util.createLoadingDialog
import com.osfans.trime.util.formatDateTime
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
Expand All @@ -22,42 +23,41 @@ import timber.log.Timber

class ConfFragment : PreferenceFragmentCompat(), CoroutineScope by MainScope() {

private val viewModel : MainViewModel by activityViewModels()
private val viewModel: MainViewModel by activityViewModels()
private val prefs get() = AppPrefs.defaultInstance()

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.conf_preference)
}

override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
"conf__synchronize" -> {
val progressDialog = createLoadingDialog(requireContext(), R.string.sync_progress)
with(preferenceScreen) {
get<Preference>("conf_synchronize")?.setOnPreferenceClickListener {
val progress = ProgressDialog(context)
progress.setMessage(getString(R.string.sync_progress))
progress.setCancelable(false)
launch {
try {
RimeUtils.sync(requireContext())
} catch (ex: Exception) {
Timber.e(ex, "Sync Exception")
} finally {
progressDialog.dismiss()
progress.dismiss()
}
}
true
}
"conf__synchronize_background" -> {
get<Preference>("conf__synchronize_background")?.setOnPreferenceClickListener {
setBackgroundSyncSummary(context)
true
}
"conf__reset" -> {
ResetAssetsDialog(requireContext()).show()
get<Preference>("conf__reset")?.setOnPreferenceClickListener {
ResetAssetsDialog(context).show()
true
}
else -> super.onPreferenceTreeClick(preference)
}
}

override fun onResume() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.pref_user_data))
viewModel.disableTopOptionsMenu()
setBackgroundSyncSummary(context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.osfans.trime.ui.fragments

import android.content.SharedPreferences
import android.os.Bundle
import android.view.Menu
import androidx.core.view.forEach
import androidx.fragment.app.activityViewModels
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
Expand All @@ -17,7 +15,7 @@ import com.osfans.trime.ui.main.MainViewModel
class KeyboardFragment :
PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener {
private val viewModel : MainViewModel by activityViewModels()
private val viewModel: MainViewModel by activityViewModels()
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.keyboard_preference)
}
Expand Down Expand Up @@ -57,6 +55,7 @@ class KeyboardFragment :
}
override fun onResume() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.pref_keyboard))
viewModel.disableTopOptionsMenu()
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
Expand Down
30 changes: 13 additions & 17 deletions app/src/main/java/com/osfans/trime/ui/fragments/LooksFragment.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
package com.osfans.trime.ui.fragments

import android.os.Bundle
import android.view.Menu
import androidx.core.view.forEach
import androidx.fragment.app.activityViewModels
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.get
import com.osfans.trime.R
import com.osfans.trime.ui.components.ColorPickerDialog
import com.osfans.trime.ui.components.ThemePickerDialog
import com.osfans.trime.ui.main.MainViewModel

class LooksFragment : PreferenceFragmentCompat() {
private val viewModel : MainViewModel by activityViewModels()
private val viewModel: MainViewModel by activityViewModels()

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.looks_preference)
}

override fun onResume() {
super.onResume()
viewModel.disableTopOptionsMenu()
}

override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
"looks__selected_theme" -> {
ThemePickerDialog(requireContext()).show()
with(preferenceScreen) {
get<Preference>("looks__selected_theme")?.setOnPreferenceClickListener {
ThemePickerDialog(context).show()
true
}
"looks__selected_color" -> {
ColorPickerDialog(requireContext()).show()
get<Preference>("looks__selected_color")?.setOnPreferenceClickListener {
ColorPickerDialog(context).show()
true
}
else -> super.onPreferenceTreeClick(preference)
}
}

override fun onResume() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.pref_theme_and_color))
viewModel.disableTopOptionsMenu()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.Menu
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.forEach
import androidx.fragment.app.activityViewModels
import androidx.preference.ListPreference
import androidx.preference.PreferenceFragmentCompat
Expand All @@ -20,7 +18,7 @@ import com.osfans.trime.ui.main.MainViewModel
class OtherFragment :
PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener {
private val viewModel : MainViewModel by activityViewModels()
private val viewModel: MainViewModel by activityViewModels()
private val prefs get() = AppPrefs.defaultInstance()
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.other_preference)
Expand Down Expand Up @@ -67,6 +65,7 @@ class OtherFragment :

override fun onResume() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.pref_other))
viewModel.disableTopOptionsMenu()
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
Expand Down
49 changes: 26 additions & 23 deletions app/src/main/java/com/osfans/trime/ui/fragments/PrefFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package com.osfans.trime.ui.fragments

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.get
import com.osfans.trime.R
import com.osfans.trime.ui.components.SchemaPickerDialog
import com.osfans.trime.ui.main.MainViewModel

class PrefFragment : PreferenceFragmentCompat() {

private val viewModel : MainViewModel by activityViewModels()
private val viewModel: MainViewModel by activityViewModels()

override fun onResume() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.trime_app_name))
viewModel.enableTopOptionsMenu()
}

Expand All @@ -24,31 +26,32 @@ class PrefFragment : PreferenceFragmentCompat() {
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val context = preferenceManager.context
setPreferencesFromResource(R.xml.prefs, rootKey)
val systemCategory = PreferenceCategory(context).apply {
setTitle(R.string.system)
isIconSpaceReserved = false
order = 18
}
preferenceScreen.addPreference(systemCategory)
systemCategory.addPreference(
Preference(context).apply {
setTitle(R.string.settings__system_toolkit)
setIcon(R.drawable.ic_iconpark_toolkit_24)
fragment = "com.osfans.trime.ui.fragments.ToolkitFragment"
order = 19
with(preferenceScreen) {
get<Preference>("pref_schemas")?.setOnPreferenceClickListener {
SchemaPickerDialog(context).show()
true
}
)
}

override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
"pref_schemas" -> {
SchemaPickerDialog(requireContext()).show()
get<Preference>("pref_user_data")?.setOnPreferenceClickListener {
findNavController().navigate(R.id.action_prefFragment_to_confFragment)
true
}
get<Preference>("pref_keyboard")?.setOnPreferenceClickListener {
findNavController().navigate(R.id.action_prefFragment_to_keyboardFragment)
true
}
get<Preference>("pref_theme_and_color")?.setOnPreferenceClickListener {
findNavController().navigate(R.id.action_prefFragment_to_looksFragment)
true
}
get<Preference>("pref_toolkit")?.setOnPreferenceClickListener {
findNavController().navigate(R.id.action_prefFragment_to_toolkitFragment)
true
}
get<Preference>("pref_others")?.setOnPreferenceClickListener {
findNavController().navigate(R.id.action_prefFragment_to_otherFragment)
true
}
else -> super.onPreferenceTreeClick(preference)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.osfans.trime.ui.fragments

import android.os.Bundle
import android.view.Menu
import androidx.core.view.forEach
import androidx.fragment.app.activityViewModels
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.osfans.trime.R
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.util.ShortcutUtils

@Suppress("unused")
class ToolkitFragment : PreferenceFragmentCompat() {

private val viewModel : MainViewModel by activityViewModels()
private val viewModel: MainViewModel by activityViewModels()

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val context = preferenceManager.context
Expand All @@ -33,6 +30,7 @@ class ToolkitFragment : PreferenceFragmentCompat() {

override fun onResume() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.pref_toolkit))
viewModel.disableTopOptionsMenu()
}
}
Loading

0 comments on commit be95e64

Please sign in to comment.