mirror of
https://github.com/fcitx5-android/fcitx5-android.git
synced 2026-08-02 20:16:06 +08:00
Extract LongClickPreference to utils
This commit is contained in:
parent
669942063d
commit
4128b6e6e4
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
* SPDX-FileCopyrightText: Copyright 2021-2023 Fcitx5 for Android Contributors
|
||||
* SPDX-FileCopyrightText: Copyright 2021-2024 Fcitx5 for Android Contributors
|
||||
*/
|
||||
package org.fcitx.fcitx5.android.ui.main.settings
|
||||
|
||||
@ -20,7 +20,6 @@ import androidx.preference.PreferenceCategory
|
||||
import androidx.preference.PreferenceDataStore
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.preference.PreferenceScreen
|
||||
import androidx.preference.PreferenceViewHolder
|
||||
import arrow.core.getOrElse
|
||||
import org.fcitx.fcitx5.android.R
|
||||
import org.fcitx.fcitx5.android.core.Key
|
||||
@ -30,6 +29,7 @@ import org.fcitx.fcitx5.android.ui.main.modified.MySwitchPreference
|
||||
import org.fcitx.fcitx5.android.ui.main.settings.addon.AddonConfigFragment
|
||||
import org.fcitx.fcitx5.android.ui.main.settings.global.GlobalConfigFragment
|
||||
import org.fcitx.fcitx5.android.ui.main.settings.im.InputMethodConfigFragment
|
||||
import org.fcitx.fcitx5.android.utils.LongClickPreference
|
||||
import org.fcitx.fcitx5.android.utils.buildDocumentsProviderIntent
|
||||
import org.fcitx.fcitx5.android.utils.buildPrimaryStorageIntent
|
||||
import org.fcitx.fcitx5.android.utils.config.ConfigDescriptor
|
||||
@ -166,36 +166,30 @@ object PreferenceScreenFactory {
|
||||
}
|
||||
}
|
||||
|
||||
fun rimeUserDataDir(title: String): Preference = object : Preference(context) {
|
||||
init {
|
||||
setOnPreferenceClickListener {
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(title)
|
||||
.setMessage(R.string.open_rime_user_data_dir)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
try {
|
||||
context.startActivity(buildDocumentsProviderIntent())
|
||||
} catch (e: Exception) {
|
||||
context.toast(e)
|
||||
}
|
||||
}
|
||||
.show()
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
// make it a hidden option, because of compatibility issues
|
||||
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
||||
super.onBindViewHolder(holder)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
holder.itemView.setOnLongClickListener {
|
||||
fun rimeUserDataDir(title: String): Preference = LongClickPreference(context).apply {
|
||||
setOnPreferenceClickListener {
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(title)
|
||||
.setMessage(R.string.open_rime_user_data_dir)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
try {
|
||||
context.startActivity(buildPrimaryStorageIntent("data/rime"))
|
||||
context.startActivity(buildDocumentsProviderIntent())
|
||||
} catch (e: Exception) {
|
||||
context.toast(e)
|
||||
}
|
||||
true
|
||||
}
|
||||
.show()
|
||||
true
|
||||
}
|
||||
|
||||
// make it a hidden option, because of compatibility issues
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
setOnPreferenceLongClickListener {
|
||||
try {
|
||||
context.startActivity(buildPrimaryStorageIntent("data/rime"))
|
||||
} catch (e: Exception) {
|
||||
context.toast(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
* SPDX-FileCopyrightText: Copyright 2021-2023 Fcitx5 for Android Contributors
|
||||
* SPDX-FileCopyrightText: Copyright 2021-2024 Fcitx5 for Android Contributors
|
||||
*/
|
||||
package org.fcitx.fcitx5.android.ui.main.settings.behavior
|
||||
|
||||
@ -11,9 +11,7 @@ import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceScreen
|
||||
import androidx.preference.PreferenceViewHolder
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.delay
|
||||
@ -71,12 +69,8 @@ class AdvancedSettingsFragment : ManagedPreferenceFragment(AppPrefs.getInstance(
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
AppUtil.showRestartNotification(ctx)
|
||||
ctx.toast(
|
||||
getString(
|
||||
R.string.user_data_imported,
|
||||
formatDateTime(metadata.exportTime)
|
||||
)
|
||||
)
|
||||
val exportTime = formatDateTime(metadata.exportTime)
|
||||
ctx.toast(getString(R.string.user_data_imported, exportTime))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// re-start fcitx in case importing failed
|
||||
@ -107,37 +101,25 @@ class AdvancedSettingsFragment : ManagedPreferenceFragment(AppPrefs.getInstance(
|
||||
}
|
||||
|
||||
override fun onPreferenceUiCreated(screen: PreferenceScreen) {
|
||||
screen.addPreference(object : Preference(requireContext()) {
|
||||
init {
|
||||
setTitle(R.string.browse_user_data_dir)
|
||||
isSingleLineTitle = false
|
||||
isIconSpaceReserved = false
|
||||
setOnPreferenceClickListener {
|
||||
try {
|
||||
context.startActivity(buildDocumentsProviderIntent())
|
||||
} catch (e: Exception) {
|
||||
context.toast(e)
|
||||
}
|
||||
true
|
||||
val ctx = requireContext()
|
||||
screen.addPreference(
|
||||
R.string.browse_user_data_dir,
|
||||
onClick = {
|
||||
try {
|
||||
ctx.startActivity(buildDocumentsProviderIntent())
|
||||
} catch (e: Exception) {
|
||||
ctx.toast(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
||||
super.onBindViewHolder(holder)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
holder.itemView.setOnLongClickListener {
|
||||
try {
|
||||
context.startActivity(buildPrimaryStorageIntent())
|
||||
} catch (e: Exception) {
|
||||
context.toast(e)
|
||||
}
|
||||
true
|
||||
}
|
||||
},
|
||||
onLongClick = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ({
|
||||
try {
|
||||
ctx.startActivity(buildPrimaryStorageIntent())
|
||||
} catch (e: Exception) {
|
||||
ctx.toast(e)
|
||||
}
|
||||
}
|
||||
})
|
||||
}) else null
|
||||
)
|
||||
screen.addPreference(R.string.export_user_data) {
|
||||
val ctx = requireContext()
|
||||
lifecycleScope.launch {
|
||||
lifecycleScope.withLoadingDialog(ctx) {
|
||||
viewModel.fcitx.runOnReady {
|
||||
@ -149,7 +131,6 @@ class AdvancedSettingsFragment : ManagedPreferenceFragment(AppPrefs.getInstance(
|
||||
}
|
||||
}
|
||||
screen.addPreference(R.string.import_user_data) {
|
||||
val ctx = requireContext()
|
||||
AlertDialog.Builder(ctx)
|
||||
.setIconAttribute(android.R.attr.alertDialogIcon)
|
||||
.setTitle(R.string.import_user_data)
|
||||
|
||||
@ -1,15 +1,17 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
* SPDX-FileCopyrightText: Copyright 2021-2023 Fcitx5 for Android Contributors
|
||||
* SPDX-FileCopyrightText: Copyright 2021-2024 Fcitx5 for Android Contributors
|
||||
*/
|
||||
package org.fcitx.fcitx5.android.utils
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceCategory
|
||||
import androidx.preference.PreferenceGroup
|
||||
import androidx.preference.PreferenceScreen
|
||||
import androidx.preference.PreferenceViewHolder
|
||||
import splitties.resources.drawable
|
||||
import splitties.resources.styledColor
|
||||
|
||||
@ -22,8 +24,31 @@ fun PreferenceScreen.addCategory(title: String, block: PreferenceCategory.() ->
|
||||
}
|
||||
|
||||
fun PreferenceScreen.addCategory(@StringRes title: Int, block: PreferenceCategory.() -> Unit) {
|
||||
val ctx = context
|
||||
addCategory(ctx.getString(title), block)
|
||||
addCategory(context.getString(title), block)
|
||||
}
|
||||
|
||||
fun Preference.setup(
|
||||
title: String,
|
||||
summary: String? = null,
|
||||
@DrawableRes icon: Int? = null,
|
||||
onClick: (() -> Unit)? = null
|
||||
) {
|
||||
isSingleLineTitle = false
|
||||
setTitle(title)
|
||||
setSummary(summary)
|
||||
if (icon == null) {
|
||||
isIconSpaceReserved = false
|
||||
} else {
|
||||
setIcon(context.drawable(icon)?.apply {
|
||||
setTint(context.styledColor(android.R.attr.colorControlNormal))
|
||||
})
|
||||
}
|
||||
onClick?.also {
|
||||
setOnPreferenceClickListener { _ ->
|
||||
it.invoke()
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun PreferenceGroup.addPreference(
|
||||
@ -32,24 +57,7 @@ fun PreferenceGroup.addPreference(
|
||||
@DrawableRes icon: Int? = null,
|
||||
onClick: (() -> Unit)? = null
|
||||
) {
|
||||
addPreference(Preference(context).apply {
|
||||
isSingleLineTitle = false
|
||||
setTitle(title)
|
||||
setSummary(summary)
|
||||
if (icon == null) {
|
||||
isIconSpaceReserved = false
|
||||
} else {
|
||||
setIcon(context.drawable(icon)?.apply {
|
||||
setTint(context.styledColor(android.R.attr.colorControlNormal))
|
||||
})
|
||||
}
|
||||
onClick?.also {
|
||||
setOnPreferenceClickListener { _ ->
|
||||
it.invoke()
|
||||
true
|
||||
}
|
||||
}
|
||||
})
|
||||
addPreference(Preference(context).apply { setup(title, summary, icon, onClick) })
|
||||
}
|
||||
|
||||
fun PreferenceGroup.addPreference(
|
||||
@ -58,8 +66,7 @@ fun PreferenceGroup.addPreference(
|
||||
@DrawableRes icon: Int? = null,
|
||||
onClick: (() -> Unit)? = null
|
||||
) {
|
||||
val ctx = context
|
||||
addPreference(ctx.getString(title), summary, icon, onClick)
|
||||
addPreference(context.getString(title), summary, icon, onClick)
|
||||
}
|
||||
|
||||
fun PreferenceGroup.addPreference(
|
||||
@ -68,6 +75,34 @@ fun PreferenceGroup.addPreference(
|
||||
@DrawableRes icon: Int? = null,
|
||||
onClick: (() -> Unit)? = null
|
||||
) {
|
||||
val ctx = context
|
||||
addPreference(ctx.getString(title), summary?.let(ctx::getString), icon, onClick)
|
||||
addPreference(context.getString(title), summary?.let(context::getString), icon, onClick)
|
||||
}
|
||||
|
||||
class LongClickPreference(context: Context) : Preference(context) {
|
||||
private var onLongClick: (() -> Unit)? = null
|
||||
|
||||
fun setOnPreferenceLongClickListener(callback: (() -> Unit)? = null) {
|
||||
onLongClick = callback
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
||||
super.onBindViewHolder(holder)
|
||||
holder.itemView.setOnLongClickListener {
|
||||
onLongClick?.invoke()
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun PreferenceGroup.addPreference(
|
||||
@StringRes title: Int,
|
||||
@StringRes summary: Int? = null,
|
||||
@DrawableRes icon: Int? = null,
|
||||
onClick: (() -> Unit)? = null,
|
||||
onLongClick: (() -> Unit)? = null,
|
||||
) {
|
||||
addPreference(LongClickPreference(context).apply {
|
||||
setup(context.getString(title), summary?.let { context.getString(it) }, icon, onClick)
|
||||
setOnPreferenceLongClickListener(onLongClick)
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user