Refine code

This commit is contained in:
Super12138 2024-02-23 10:46:21 +08:00
parent eade595899
commit 25220b716e
18 changed files with 137 additions and 128 deletions

View file

@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".ToDoApplication"
android:name=".ToDoApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:enableOnBackInvokedCallback="true"

View file

@ -7,7 +7,7 @@ import cn.super12138.todo.logic.dao.ToDoRoomDB
import cn.super12138.todo.views.crash.CrashHandler
import com.google.android.material.color.DynamicColors
class ToDoApplication : Application() {
class ToDoApp : Application() {
private val database by lazy { ToDoRoomDB.getDatabase(this) }
companion object {

View file

@ -4,4 +4,22 @@ object Constants {
const val AUTHOR_GITHUB_URL = "https://github.com/Super12138/"
const val REPO_GITHUB_URL = "https://github.com/Super12138/ToDo"
const val UPDATE_URL = "https://github.com/Super12138/ToDo/releases"
const val SP_NAME = "cn.super12138.todo_preferences"
const val PREF_DARK_MODE = "dark_mode"
const val PREF_SECURE_MODE = "secure_mode"
const val PREF_DEV_MODE = "dev_mode"
const val PREF_SPRING_FESTIVAL_THEME = "spring_festival_theme"
const val PREF_BACKUP_DB = "backup_db"
const val PREF_RESTORE_DB = "restore_db"
const val STRING_DEV_MODE = "/DEV_MODE"
const val TAG_INFO_BOTTOM_SHEET = "InfoBottomSheet"
const val BUNDLE_TODO_CONTENT = "todoContent"
const val BUNDLE_TODO_SUBJECT = "todoSubject"
const val BUNDLE_TODO_STATE = "todoState"
const val BUNDLE_TODO_UUID = "todoUUID"
}

View file

@ -0,0 +1,11 @@
package cn.super12138.todo.constant
import cn.super12138.todo.logic.database.SPDelegates
object GlobalValues {
var darkMode: String by SPDelegates(Constants.PREF_DARK_MODE, "0")
var devMode: Boolean by SPDelegates(Constants.PREF_DEV_MODE, false)
var springFestivalTheme: Boolean by SPDelegates(Constants.PREF_SPRING_FESTIVAL_THEME, false)
var secureMode: Boolean by SPDelegates(Constants.PREF_SECURE_MODE, false)
}

View file

@ -1,33 +1,14 @@
package cn.super12138.todo.logic
import android.content.Context
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.logic.dao.ToDoRoom
import cn.super12138.todo.logic.database.SPHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
object Repository {
/**
* 设置数据到应用设置中
*/
fun setPreference(context: Context, key: String, value: Any) =
SPHelper.setPreference(context, key, value)
/**
* 获取应用设置里的数据
*/
fun getPreferenceString(context: Context, key: String, defaultValue: String) =
SPHelper.getPreferenceString(context, key, defaultValue)
/**
* 获取应用设置的布尔值
*/
fun getPreferenceBoolean(context: Context, key: String, defaultValue: Boolean) =
SPHelper.getPreferenceBoolean(context, key, defaultValue)
// Room
private val db get() = ToDoApplication.db
private val db get() = ToDoApp.db
val todoDao = db.toDoRoomDao()
/**

View file

@ -0,0 +1,14 @@
package cn.super12138.todo.logic.database
import cn.super12138.todo.utils.SPUtils
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class SPDelegates<T>(private val key: String, private val default: T) : ReadWriteProperty<Any?, T> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return SPUtils.getValue(key, default)
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
SPUtils.putValue(key, value)
}
}

View file

@ -1,34 +0,0 @@
package cn.super12138.todo.logic.database
import android.content.Context
import androidx.preference.PreferenceManager
object SPHelper {
fun getPreferenceString(context: Context, name: String, defaultValue: String): String? {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context /* Activity context */)
return sharedPreferences.getString(name, defaultValue)
}
fun getPreferenceBoolean(context: Context, name: String, defaultValue: Boolean): Boolean {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context /* Activity context */)
return sharedPreferences.getBoolean(name, defaultValue)
}
fun setPreference(context: Context, name: String, value: Any) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val editor = sharedPreferences.edit()
when (value) {
is String -> editor.putString(name, value)
is Boolean -> editor.putBoolean(name, value)
is Int -> editor.putInt(name, value)
is Float -> editor.putFloat(name, value)
is Long -> editor.putLong(name, value)
else -> throw IllegalArgumentException("Unsupported data type")
}
editor.apply()
}
}

View file

@ -0,0 +1,36 @@
package cn.super12138.todo.utils
import android.content.Context
import android.content.SharedPreferences
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.Constants
object SPUtils {
private val sp: SharedPreferences by lazy {
ToDoApp.context.getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE)
}
fun <T> getValue(name: String, default: T): T = with(sp) {
val res: Any = when (default) {
is Long -> getLong(name, default)
is String -> getString(name, default).orEmpty()
is Int -> getInt(name, default)
is Boolean -> getBoolean(name, default)
is Float -> getFloat(name, default)
else -> throw java.lang.IllegalArgumentException()
}
@Suppress("UNCHECKED_CAST")
res as T
}
fun <T> putValue(name: String, value: T) = with(sp.edit()) {
when (value) {
is Long -> putLong(name, value)
is String -> putString(name, value)
is Int -> putInt(name, value)
is Boolean -> putBoolean(name, value)
is Float -> putFloat(name, value)
else -> throw IllegalArgumentException("This type can't be saved into Preferences")
}.apply()
}
}

View file

@ -1,14 +1,16 @@
package cn.super12138.todo.utils
import android.content.Context
import android.widget.Toast
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.ToDoApp
fun String.showToast(
context: Context = ToDoApplication.context,
context: Context = ToDoApp.context,
duration: Int = Toast.LENGTH_SHORT
) {
Toast.makeText(context, this, duration).show()
}
fun Int.showToast(context: Context = ToDoApplication.context, duration: Int = Toast.LENGTH_SHORT) {
fun Int.showToast(context: Context = ToDoApp.context, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(context, this, duration).show()
}

View file

@ -6,22 +6,21 @@ import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.logic.Repository
open class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val springFestivalTheme =
Repository.getPreferenceBoolean(ToDoApplication.context, "spring_festival_theme", false)
if (springFestivalTheme) {
if (GlobalValues.springFestivalTheme) {
setTheme(R.style.Theme_SpringFestival)
}
// enableEdgeToEdge()
super.onCreate(savedInstanceState)
// 深色模式
val isDarkMode = Repository.getPreferenceString(this, "dark_mode", "0")
when (isDarkMode) {
when (GlobalValues.darkMode) {
"0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
"1" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

View file

@ -4,12 +4,11 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.databinding.ActivityAboutBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.utils.showToast
import cn.super12138.todo.views.BaseActivity
import showToast
class AboutActivity : BaseActivity() {
private lateinit var binding: ActivityAboutBinding
@ -61,24 +60,7 @@ class AboutActivity : BaseActivity() {
when (clickCount) {
5 -> {
clickCount = 0
val isSpringFestivalTheme = Repository.getPreferenceBoolean(
ToDoApplication.context,
"spring_festival_theme",
false
)
if (isSpringFestivalTheme) {
Repository.setPreference(
ToDoApplication.context,
"spring_festival_theme",
false
)
} else {
Repository.setPreference(
ToDoApplication.context,
"spring_festival_theme",
true
)
}
GlobalValues.springFestivalTheme = !GlobalValues.springFestivalTheme
"🧧".showToast()
}
}

View file

@ -6,7 +6,9 @@ import android.view.WindowManager
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.databinding.ActivityAllTasksBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.views.BaseActivity
@ -17,8 +19,7 @@ class AllTasksActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val isSecureMode = Repository.getPreferenceBoolean(this, "secure_mode", false)
when (isSecureMode) {
when (GlobalValues.secureMode) {
true -> window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
@ -32,7 +33,7 @@ class AllTasksActivity : BaseActivity() {
val viewModel = ViewModelProvider(this)[AllTasksViewModel::class.java]
val layoutManager = LinearLayoutManager(ToDoApplication.context)
val layoutManager = LinearLayoutManager(ToDoApp.context)
binding.allTasksList.layoutManager = layoutManager
val adapter = AllTasksAdapter(viewModel.todoListAll, supportFragmentManager)
binding.allTasksList.adapter = adapter

View file

@ -5,7 +5,9 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.databinding.BottomSheetInfoBinding
import cn.super12138.todo.logic.Repository
import com.google.android.material.bottomsheet.BottomSheetBehavior
@ -21,7 +23,7 @@ class InfoBottomSheet : BottomSheetDialogFragment() {
private lateinit var todoUUID: String
companion object {
const val TAG = "InfoBtmSheet"
const val TAG = Constants.TAG_INFO_BOTTOM_SHEET
fun newInstance(
todoContent: String,
todoSubject: String,
@ -29,10 +31,10 @@ class InfoBottomSheet : BottomSheetDialogFragment() {
todoUUID: String
) = InfoBottomSheet().apply {
arguments = Bundle().apply {
putString("todoContent", todoContent)
putString("todoSubject", todoSubject)
putInt("todoState", todoState)
putString("todoUUID", todoUUID)
putString(Constants.BUNDLE_TODO_CONTENT, todoContent)
putString(Constants.BUNDLE_TODO_SUBJECT, todoSubject)
putInt(Constants.BUNDLE_TODO_STATE, todoState)
putString(Constants.BUNDLE_TODO_UUID, todoUUID)
}
}
}
@ -40,10 +42,10 @@ class InfoBottomSheet : BottomSheetDialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
todoContent = it.getString("todoContent", "")
todoSubject = it.getString("todoSubject", "")
todoState = it.getInt("todoState", 0)
todoUUID = it.getString("todoUUID", "")
todoContent = it.getString(Constants.BUNDLE_TODO_CONTENT, "")
todoSubject = it.getString(Constants.BUNDLE_TODO_SUBJECT, "")
todoState = it.getInt(Constants.BUNDLE_TODO_STATE, 0)
todoUUID = it.getString(Constants.BUNDLE_TODO_UUID, "")
}
}
@ -57,7 +59,6 @@ class InfoBottomSheet : BottomSheetDialogFragment() {
return binding.root
}
@androidx.annotation.OptIn(com.google.android.material.badge.ExperimentalBadgeUtils::class)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -72,7 +73,7 @@ class InfoBottomSheet : BottomSheetDialogFragment() {
} else {
binding.todoState.text = getString(R.string.info_state_incomplete)
}
if (Repository.getPreferenceBoolean(ToDoApplication.context, "dev_mode", false)) {
if (GlobalValues.devMode) {
binding.todoUuid.apply {
visibility = View.VISIBLE
text = String.format(getString(R.string.info_uuid), todoUUID)

View file

@ -3,9 +3,10 @@ package cn.super12138.todo.views.main
import android.content.Intent
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDelegate
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.databinding.ActivityMainBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.views.BaseActivity
@ -23,7 +24,7 @@ class MainActivity : BaseActivity() {
binding.toolbar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.item_settings -> {
val intent = Intent(ToDoApplication.context, SettingsActivity::class.java)
val intent = Intent(ToDoApp.context, SettingsActivity::class.java)
startActivity(intent)
true
}
@ -47,8 +48,7 @@ class MainActivity : BaseActivity() {
}
.show()
}*/
val isSecureMode = Repository.getPreferenceBoolean(this, "secure_mode", false)
when (isSecureMode) {
when (GlobalValues.secureMode) {
true -> window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE

View file

@ -3,7 +3,6 @@ package cn.super12138.todo.views.settings
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Process
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.lifecycleScope
@ -12,7 +11,8 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.databinding.ActivitySettingsBinding
import cn.super12138.todo.databinding.DialogBackupBinding
import cn.super12138.todo.databinding.DialogRestoreBinding
@ -56,10 +56,8 @@ class SettingsActivity : BaseActivity() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
val gson = Gson()
val isDevMode =
Repository.getPreferenceBoolean(ToDoApplication.context, "dev_mode", false)
findPreference<ListPreference>("dark_mode")?.apply {
findPreference<ListPreference>(Constants.PREF_DARK_MODE)?.apply {
setOnPreferenceChangeListener { _, newValue ->
when (newValue) {
"0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
@ -73,7 +71,7 @@ class SettingsActivity : BaseActivity() {
}
}
findPreference<SwitchPreferenceCompat>("secure_mode")?.apply {
findPreference<SwitchPreferenceCompat>(Constants.PREF_SECURE_MODE)?.apply {
setOnPreferenceChangeListener { _, _ ->
view?.let {
Snackbar.make(it, R.string.need_restart_app, Snackbar.LENGTH_LONG)
@ -87,7 +85,7 @@ class SettingsActivity : BaseActivity() {
}
}
findPreference<Preference>("backupDB")?.apply {
findPreference<Preference>(Constants.PREF_BACKUP_DB)?.apply {
setOnPreferenceClickListener {
lifecycleScope.launch {
val data = Repository.getAll()
@ -107,7 +105,7 @@ class SettingsActivity : BaseActivity() {
}
}
findPreference<Preference>("restoreDB")?.apply {
findPreference<Preference>(Constants.PREF_RESTORE_DB)?.apply {
setOnPreferenceClickListener {
restoreBinding = DialogRestoreBinding.inflate(layoutInflater)
activity?.let { it1 ->
@ -149,7 +147,7 @@ class SettingsActivity : BaseActivity() {
restoreBinding.jsonInput.error =
getString(R.string.json_data_incorrect)
} catch (e: Exception) {
if (isDevMode) {
if (GlobalValues.devMode) {
restoreBinding.jsonInput.error = e.toString()
} else {
restoreBinding.jsonInput.error =

View file

@ -12,7 +12,8 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.databinding.DialogAddTodoBinding
import cn.super12138.todo.databinding.FragmentTodoBinding
import cn.super12138.todo.logic.Repository
@ -22,7 +23,7 @@ import cn.super12138.todo.views.progress.ProgressFragmentViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.launch
import me.zhanghai.android.fastscroll.FastScrollerBuilder
import showToast
import cn.super12138.todo.utils.showToast
import java.util.UUID
class ToDoFragment : Fragment() {
@ -59,7 +60,7 @@ class ToDoFragment : Fragment() {
val todoList = todoViewModel.todoList
val layoutManager = LinearLayoutManager(ToDoApplication.context)
val layoutManager = LinearLayoutManager(ToDoApp.context)
binding.todoList.layoutManager = layoutManager
val adapter = ToDoAdapter(todoList, requireActivity())
binding.todoList.adapter = adapter
@ -89,16 +90,16 @@ class ToDoFragment : Fragment() {
toDoDialogBinding.todoContent.error =
getString(R.string.content_cannot_be_empty)
} else {
if (todoContent == "/DEV_MODE") {
if (todoContent == Constants.STRING_DEV_MODE) {
if (Repository.getPreferenceBoolean(
ToDoApplication.context,
"dev_mode",
ToDoApp.context,
Constants.PREF_DEV_MODE,
true
)
) {
Repository.setPreference(ToDoApplication.context, "dev_mode", false)
Repository.setPreference(ToDoApp.context, Constants.PREF_DEV_MODE, false)
} else {
Repository.setPreference(ToDoApplication.context, "dev_mode", true)
Repository.setPreference(ToDoApp.context, Constants.PREF_DEV_MODE, true)
"Dev Mode".showToast()
}
dialog.dismiss()

View file

@ -35,7 +35,6 @@
android:layout_height="match_parent"
android:visibility="gone" />
<TextView
android:id="@+id/empty_tip"
android:layout_width="match_parent"

View file

@ -28,12 +28,12 @@
app:title="@string/secure_mode" />
<Preference
android:icon="@drawable/ic_backup"
app:key="backupDB"
app:key="backup_db"
app:summary="@string/backup_summary"
app:title="@string/backup" />
<Preference
app:icon="@drawable/ic_restore"
app:key="restoreDB"
app:key="restore_db"
app:summary="@string/restore_summary"
app:title="@string/restore" />
</PreferenceCategory>