Skip to content

Commit

Permalink
Refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Jun 9, 2024
1 parent 5a15826 commit ef58186
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 45 deletions.
13 changes: 13 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/utils/VibrationUtils.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -38,9 +39,7 @@ class AllTasksActivity : BaseActivity<ActivityAllTasksBinding>() {
}

binding.toolBar.setNavigationOnClickListener {
if (GlobalValues.hapticFeedback) {
it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
VibrationUtils.performHapticFeedback(it)

finish()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
// 内容判空
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ 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
import java.util.Locale

class CrashActivity : BaseActivity<ActivityCrashBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)

ViewCompat.setOnApplyWindowInsetsListener(binding.exitApp) { view, windowInsets ->
Expand Down Expand Up @@ -56,9 +56,8 @@ class CrashActivity : BaseActivity<ActivityCrashBinding>() {
}

binding.exitApp.setOnClickListener {
if (GlobalValues.hapticFeedback) {
it.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}
VibrationUtils.performHapticFeedback(it)

this.finishAffinity()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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>() {
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,6 +79,7 @@ class ToDoAdapter(

holder.itemView.setOnClickListener {
if (GlobalValues.devMode) {
VibrationUtils.performHapticFeedback(it)
"Current position: $position".showToast()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()
// 清除数据库
Expand Down

0 comments on commit ef58186

Please sign in to comment.