Add more haptic feedback & refine code

This commit is contained in:
Super12138 2024-06-30 21:02:22 +08:00
parent fca776f8e6
commit cdb0d030d6
5 changed files with 64 additions and 57 deletions

View file

@ -68,7 +68,8 @@ android {
dependencies {
// AndroidX
// Android X
implementation(libs.androidx.core)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.activity)

View file

@ -6,11 +6,11 @@ import cn.super12138.todo.constant.GlobalValues
object VibrationUtils {
fun performHapticFeedback(
view: View,
view: View?,
hapticFeedbackConstants: Int = HapticFeedbackConstants.CONTEXT_CLICK
) {
if (GlobalValues.hapticFeedback) {
view.performHapticFeedback(hapticFeedbackConstants)
view?.performHapticFeedback(hapticFeedbackConstants)
}
}
}

View file

@ -92,6 +92,10 @@ class ToDoBottomSheet : BottomSheetDialogFragment() {
val todoList = todoViewModel.todoList
binding.todoSubject.setOnCheckedStateChangeListener { _, _ ->
VibrationUtils.performHapticFeedback(binding.todoSubject)
}
// 编辑模式
if (editMode) {
binding.btnCancel.visibility = View.GONE

View file

@ -21,6 +21,7 @@ import cn.super12138.todo.databinding.DialogBackupBinding
import cn.super12138.todo.databinding.DialogRestoreBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.logic.dao.ToDoRoom
import cn.super12138.todo.utils.VibrationUtils
import cn.super12138.todo.views.about.AboutActivity
import cn.super12138.todo.views.all.AllTasksActivity
import cn.super12138.todo.views.main.MainActivity
@ -42,9 +43,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<ListPreference>(Constants.PREF_DARK_MODE)?.apply {
setOnPreferenceClickListener {
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
true
}
setOnPreferenceChangeListener { _, newValue ->
@ -62,16 +61,13 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<SwitchPreferenceCompat>(Constants.PREF_SECURE_MODE)?.apply {
setOnPreferenceChangeListener { _, _ ->
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
view?.let {
Snackbar.make(it, R.string.need_restart_app, Snackbar.LENGTH_LONG)
.setAction(R.string.restart_app_now) {
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
VibrationUtils.performHapticFeedback(view)
restartApp(context)
}
.show()
@ -84,7 +80,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<SwitchPreferenceCompat>(Constants.PREF_HAPTIC_FEEDBACK)?.apply {
setOnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
VibrationUtils.performHapticFeedback(view)
}
true
@ -93,10 +89,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<Preference>(Constants.PREF_BACKUP_DB)?.apply {
setOnPreferenceClickListener {
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
VibrationUtils.performHapticFeedback(view)
lifecycleScope.launch {
val data = Repository.getAll()
val jsonData = gson.toJson(data)
@ -107,7 +100,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
MaterialAlertDialogBuilder(it)
.setTitle(R.string.export_data)
.setView(backupBinding.root)
.setPositiveButton(R.string.ok, null)
.setPositiveButton(R.string.ok) { _, _ ->
VibrationUtils.performHapticFeedback(view)
}
.show()
}
}
@ -117,9 +112,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<Preference>(Constants.PREF_RESTORE_DB)?.apply {
setOnPreferenceClickListener {
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
VibrationUtils.performHapticFeedback(view)
restoreBinding = DialogRestoreBinding.inflate(layoutInflater)
activity?.let { it1 ->
@ -131,6 +125,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
.show()
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
VibrationUtils.performHapticFeedback(view)
val jsonInput = restoreBinding.jsonInput.editText?.text.toString()
if (jsonInput.isEmpty()) {
restoreBinding.jsonInput.error =
@ -152,9 +148,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
.setTitle(R.string.restore_successful)
.setMessage(R.string.restore_need_restart_app)
.setPositiveButton(R.string.ok) { _, _ ->
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
VibrationUtils.performHapticFeedback(view)
restartApp(context)
}
@ -182,9 +176,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<Preference>(Constants.PREF_ALL_TASKS)?.apply {
setOnPreferenceClickListener {
startActivity(Intent(context, AllTasksActivity::class.java))
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
true
}
}
@ -192,9 +185,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<Preference>(Constants.PREF_ABOUT)?.apply {
setOnPreferenceClickListener {
startActivity(Intent(context, AboutActivity::class.java))
if (GlobalValues.hapticFeedback) {
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
view?.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
true
}
}

View file

@ -1,4 +1,6 @@
[versions]
# Android X
core = "1.13.1"
coreKtx = "1.13.1"
appcompat = "1.7.0"
activity = "1.9.0"
@ -6,47 +8,55 @@ activityKtx = "1.9.0"
constraintlayout = "2.1.4"
preference = "1.2.1"
preferenceKtx = "1.2.1"
lifecycleViewmodelKtx = "2.8.2"
lifecycleViewmodel = "2.8.2"
lifecycleViewmodelKtx = "2.8.2"
recyclerview = "1.3.2"
fragmentKtx = "1.8.1"
fragment = "1.8.1"
fragmentKtx = "1.8.1"
roomRuntime = "2.6.1"
fastScroll = "1.3.0"
# Material
material = "1.13.0-alpha03"
# Fast Scroll
fastScroll = "1.3.0"
# Gson
gson = "2.11.0"
# Test
espressoCore = "3.6.1"
junit = "4.13.2"
junitVersion = "1.2.1"
# Plugins
agp = "8.5.0"
kotlin-android = "2.0.0"
ksp = "2.0.0-1.0.22"
[libraries]
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
androidx-activity = { module = "androidx.activity:activity", version.ref = "activity" }
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "activityKtx" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" }
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" }
androidx-preference-ktx = { module = "androidx.preference:preference-ktx", version.ref = "preferenceKtx" }
androidx-preference = { module = "androidx.preference:preference", version.ref = "preference" }
androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" }
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "lifecycleViewmodel" }
androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version.ref = "recyclerview" }
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "fragmentKtx" }
androidx-fragment = { module = "androidx.fragment:fragment", version.ref = "fragment" }
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomRuntime" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "roomRuntime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
junit = { module = "junit:junit", version.ref = "junit" }
fast-scroll = { module = "me.zhanghai.android.fastscroll:library", version.ref = "fastScroll" }
material = { module = "com.google.android.material:material", version.ref = "material" }
# Android X
androidx-core = { group = "androidx.core", name = "core", version.ref = "core" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-activity-ktx = { group = "androidx.activity", name = "activity-ktx", version.ref = "activityKtx" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
androidx-preference = { group = "androidx.preference", name = "preference", version.ref = "preference" }
androidx-preference-ktx = { group = "androidx.preference", name = "preference-ktx", version.ref = "preferenceKtx" }
androidx-lifecycle-viewmodel = { group = "androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "lifecycleViewmodel" }
androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" }
androidx-recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "recyclerview" }
androidx-fragment = { group = "androidx.fragment", name = "fragment", version.ref = "fragment" }
androidx-fragment-ktx = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "fragmentKtx" }
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "roomRuntime" }
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomRuntime" }
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "roomRuntime" }
# Material
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
# Fast Scroll
fast-scroll = { group = "me.zhanghai.android.fastscroll", name = "library", version.ref = "fastScroll" }
# Gson
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
# Test
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }