From ef5818673cb7afe26b8b9d347cf6d75bbab8c62e Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Sun, 9 Jun 2024 13:24:52 +0800 Subject: [PATCH] Refine code --- .../cn/super12138/todo/utils/VibrationUtils.kt | 13 +++++++++++++ .../todo/views/about/AboutActivity.kt | 17 +++++------------ .../todo/views/all/AllTasksActivity.kt | 5 ++--- .../todo/views/all/AllTasksAdapter.kt | 5 ++--- .../todo/views/bottomsheet/ToDoBottomSheet.kt | 14 ++++---------- .../todo/views/crash/CrashActivity.kt | 7 +++---- .../super12138/todo/views/main/MainActivity.kt | 7 ++++--- .../todo/views/settings/SettingsActivity.kt | 7 ++----- .../super12138/todo/views/todo/ToDoAdapter.kt | 2 ++ .../super12138/todo/views/todo/ToDoFragment.kt | 10 +++++----- 10 files changed, 42 insertions(+), 45 deletions(-) create mode 100644 app/src/main/kotlin/cn/super12138/todo/utils/VibrationUtils.kt diff --git a/app/src/main/kotlin/cn/super12138/todo/utils/VibrationUtils.kt b/app/src/main/kotlin/cn/super12138/todo/utils/VibrationUtils.kt new file mode 100644 index 0000000..cbc7dfc --- /dev/null +++ b/app/src/main/kotlin/cn/super12138/todo/utils/VibrationUtils.kt @@ -0,0 +1,13 @@ +package cn.super12138.todo.utils + +import android.view.HapticFeedbackConstants +import android.view.View +import cn.super12138.todo.constant.GlobalValues + +object VibrationUtils { + fun performHapticFeedback(view: View, hapticFeedbackConstants: Int=HapticFeedbackConstants.CONTEXT_CLICK) { + if (GlobalValues.hapticFeedback) { + view.performHapticFeedback(hapticFeedbackConstants) + } + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt index e3e4bce..35c41a7 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/about/AboutActivity.kt @@ -8,6 +8,7 @@ import cn.super12138.todo.constant.Constants import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.databinding.ActivityAboutBinding import cn.super12138.todo.utils.VersionUtils +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.utils.showToast import cn.super12138.todo.views.BaseActivity @@ -20,17 +21,13 @@ class AboutActivity : BaseActivity<ActivityAboutBinding>() { binding.appVersion.text = VersionUtils.getAppVersion(this) binding.toolBar.setNavigationOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) finish() } binding.checkUpdate.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) val intent = Intent(Intent.ACTION_VIEW).apply { data = Uri.parse(Constants.UPDATE_URL) @@ -39,9 +36,7 @@ class AboutActivity : BaseActivity<ActivityAboutBinding>() { } binding.openSource.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) val intent = Intent(Intent.ACTION_VIEW).apply { data = Uri.parse(Constants.REPO_GITHUB_URL) @@ -51,9 +46,7 @@ class AboutActivity : BaseActivity<ActivityAboutBinding>() { binding.developerInfo.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) val intent = Intent(Intent.ACTION_VIEW).apply { data = Uri.parse(Constants.AUTHOR_GITHUB_URL) diff --git a/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt index 242fe4a..cb25644 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt @@ -10,6 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import cn.super12138.todo.ToDoApp import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.databinding.ActivityAllTasksBinding +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.views.BaseActivity import me.zhanghai.android.fastscroll.FastScrollerBuilder @@ -38,9 +39,7 @@ class AllTasksActivity : BaseActivity<ActivityAllTasksBinding>() { } binding.toolBar.setNavigationOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) finish() } diff --git a/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksAdapter.kt b/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksAdapter.kt index a7bd3e1..f4d014c 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksAdapter.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksAdapter.kt @@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView import cn.super12138.todo.R import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.logic.model.ToDo +import cn.super12138.todo.utils.VibrationUtils class AllTasksAdapter( private val todoList: MutableList<ToDo>, @@ -35,9 +36,7 @@ class AllTasksAdapter( holder.todoSubject.text = todo.subject holder.itemView.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) val infoBottomSheet = InfoBottomSheet.newInstance( todo.content, diff --git a/app/src/main/kotlin/cn/super12138/todo/views/bottomsheet/ToDoBottomSheet.kt b/app/src/main/kotlin/cn/super12138/todo/views/bottomsheet/ToDoBottomSheet.kt index c1a58be..c0503f2 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/bottomsheet/ToDoBottomSheet.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/bottomsheet/ToDoBottomSheet.kt @@ -1,7 +1,6 @@ package cn.super12138.todo.views.bottomsheet import android.os.Bundle -import android.view.HapticFeedbackConstants import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -13,6 +12,7 @@ import cn.super12138.todo.databinding.BottomSheetTodoBinding import cn.super12138.todo.logic.dao.ToDoRoom import cn.super12138.todo.logic.model.ToDo import cn.super12138.todo.utils.TextUtils +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.utils.showToast import cn.super12138.todo.utils.toEditable import cn.super12138.todo.views.progress.ProgressFragmentViewModel @@ -104,9 +104,7 @@ class ToDoBottomSheet : BottomSheetDialogFragment() { } binding.btnSave.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) val todoContent = binding.todoContent.editText?.text.toString() // 内容判空 @@ -168,17 +166,13 @@ class ToDoBottomSheet : BottomSheetDialogFragment() { } binding.btnCancel.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) dismiss() } binding.btnDelete.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) todoViewModel.deleteTask(todoPosition, todoUUID) progressViewModel.updateProgress() diff --git a/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt index c86de0b..adae620 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/crash/CrashActivity.kt @@ -11,6 +11,7 @@ import androidx.core.view.updateLayoutParams import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.databinding.ActivityCrashBinding import cn.super12138.todo.utils.VersionUtils +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.views.BaseActivity import java.text.SimpleDateFormat import java.util.Calendar @@ -18,7 +19,6 @@ import java.util.Locale class CrashActivity : BaseActivity<ActivityCrashBinding>() { override fun onCreate(savedInstanceState: Bundle?) { - enableEdgeToEdge() super.onCreate(savedInstanceState) ViewCompat.setOnApplyWindowInsetsListener(binding.exitApp) { view, windowInsets -> @@ -56,9 +56,8 @@ class CrashActivity : BaseActivity<ActivityCrashBinding>() { } binding.exitApp.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) + this.finishAffinity() } } diff --git a/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt index 58ff13a..e61c7a7 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt @@ -8,6 +8,7 @@ import cn.super12138.todo.R import cn.super12138.todo.ToDoApp import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.databinding.ActivityMainBinding +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.views.BaseActivity import cn.super12138.todo.views.settings.SettingsActivity @@ -19,9 +20,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() { R.id.item_settings -> { val intent = Intent(ToDoApp.context, SettingsActivity::class.java) startActivity(intent) - if (GlobalValues.hapticFeedback) { - binding.toolBar.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + + VibrationUtils.performHapticFeedback(binding.toolBar) + true } diff --git a/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt index 481f98d..6935520 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt @@ -1,10 +1,9 @@ package cn.super12138.todo.views.settings import android.os.Bundle -import android.view.HapticFeedbackConstants import cn.super12138.todo.R -import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.databinding.ActivitySettingsBinding +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.views.BaseActivity class SettingsActivity : BaseActivity<ActivitySettingsBinding>() { @@ -20,9 +19,7 @@ class SettingsActivity : BaseActivity<ActivitySettingsBinding>() { supportActionBar?.setDisplayHomeAsUpEnabled(true) binding.toolBar.setNavigationOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) finish() } diff --git a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt index e04bd60..1621a0b 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.RecyclerView import cn.super12138.todo.R import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.logic.model.ToDo +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.utils.showToast import cn.super12138.todo.views.bottomsheet.ToDoBottomSheet import cn.super12138.todo.views.progress.ProgressFragmentViewModel @@ -78,6 +79,7 @@ class ToDoAdapter( holder.itemView.setOnClickListener { if (GlobalValues.devMode) { + VibrationUtils.performHapticFeedback(it) "Current position: $position".showToast() } } diff --git a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt index 8da216d..3d086f1 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt @@ -1,7 +1,6 @@ package cn.super12138.todo.views.todo import android.os.Bundle -import android.view.HapticFeedbackConstants import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -13,9 +12,9 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import cn.super12138.todo.R import cn.super12138.todo.ToDoApp -import cn.super12138.todo.constant.GlobalValues import cn.super12138.todo.databinding.FragmentTodoBinding import cn.super12138.todo.logic.Repository +import cn.super12138.todo.utils.VibrationUtils import cn.super12138.todo.views.bottomsheet.ToDoBottomSheet import cn.super12138.todo.views.progress.ProgressFragmentViewModel import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -66,9 +65,8 @@ class ToDoFragment : Fragment() { } binding.addItem.setOnClickListener { - if (GlobalValues.hapticFeedback) { - it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) - } + VibrationUtils.performHapticFeedback(it) + val toDoBottomSheet = ToDoBottomSheet() toDoBottomSheet.show(parentFragmentManager, ToDoBottomSheet.TAG) } @@ -79,6 +77,8 @@ class ToDoFragment : Fragment() { .setTitle(R.string.warning) .setMessage(R.string.delete_confirm) .setPositiveButton(R.string.ok) { _, _ -> + VibrationUtils.performHapticFeedback(it) + // 清除 Recycler View todoList.clear() // 清除数据库