From 6d17873ccae69a5be44427b44f942e6ef14e9ead Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:47:41 +0800 Subject: [PATCH] Optimize to-do list --- .../cn/super12138/todo/views/BaseActivity.kt | 12 ++++++++ .../todo/views/main/MainActivity.kt | 9 ------ .../todo/views/settings/SettingsActivity.kt | 4 +-- .../todo/views/todo/ToDoFragment.kt | 29 +++++-------------- .../todo/views/todo/ToDoFragmentViewModel.kt | 25 ++++++++++++++++ 5 files changed, 47 insertions(+), 32 deletions(-) diff --git a/app/src/main/kotlin/cn/super12138/todo/views/BaseActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/BaseActivity.kt index 250722f..ae58b16 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/BaseActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/BaseActivity.kt @@ -4,6 +4,7 @@ import android.os.Build import android.os.Bundle 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.logic.Repository @@ -17,6 +18,17 @@ open class BaseActivity : AppCompatActivity() { } // enableEdgeToEdge() super.onCreate(savedInstanceState) + + // 深色模式 + val isDarkMode = Repository.getPreferenceString(this, "dark_mode", "0") + when (isDarkMode) { + "0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) + + "1" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) + + "2" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) + } + // 适配刘海屏 val lp = window.attributes if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { 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 656115f..9af2fcb 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 @@ -47,16 +47,7 @@ class MainActivity : BaseActivity() { } .show() }*/ - - val isDarkMode = Repository.getPreferenceString(this, "dark_mode", "0") val isSecureMode = Repository.getPreferenceBoolean(this, "secure_mode", false) - when (isDarkMode) { - "0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) - - "1" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) - - "2" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) - } when (isSecureMode) { true -> window.setFlags( WindowManager.LayoutParams.FLAG_SECURE, 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 f168e65..ff61a8d 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 @@ -58,7 +58,7 @@ class SettingsActivity : BaseActivity() { Repository.getPreferenceBoolean(ToDoApplication.context, "dev_mode", false) findPreference("dark_mode")?.apply { - setOnPreferenceChangeListener { preference, newValue -> + setOnPreferenceChangeListener { _, newValue -> when (newValue) { "0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) @@ -72,7 +72,7 @@ class SettingsActivity : BaseActivity() { } findPreference("secure_mode")?.apply { - setOnPreferenceChangeListener { preference, newValue -> + setOnPreferenceChangeListener { _, newValue -> when (newValue) { true -> activity?.window?.setFlags( WindowManager.LayoutParams.FLAG_SECURE, 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 00dac8e..b714676 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 @@ -28,7 +28,6 @@ class ToDoFragment : Fragment() { private lateinit var binding: FragmentTodoBinding private lateinit var ToDoDialogBinding: DialogAddTodoBinding - private val todoList = ArrayList() override fun onCreateView( inflater: LayoutInflater, @@ -52,31 +51,19 @@ class ToDoFragment : Fragment() { WindowInsetsCompat.CONSUMED }*/ - val layoutManager = LinearLayoutManager(ToDoApplication.context) - binding.todoList.layoutManager = layoutManager - val adapter = ToDoAdapter(todoList, requireActivity()) - binding.todoList.adapter = adapter - val progressViewModel = ViewModelProvider(requireActivity()).get(ProgressFragmentViewModel::class.java) val todoViewModel = ViewModelProvider(requireActivity()).get(ToDoFragmentViewModel::class.java) - lifecycleScope.launch { - val todos = Repository.getAllUncomplete() - var count = 0 - for (todo in todos) { - todoList.add(ToDo(todo.uuid, todo.content, todo.subject)) - count++ - } - if (count == 0) { - todoViewModel.emptyTipVis.value = View.VISIBLE - } else { - todoViewModel.emptyTipVis.value = View.GONE - } - } + val todoList = todoViewModel.todoList + + val layoutManager = LinearLayoutManager(ToDoApplication.context) + binding.todoList.layoutManager = layoutManager + val adapter = ToDoAdapter(todoList, requireActivity()) + binding.todoList.adapter = adapter - if (todoList.size == 0) { + if (todoList.isEmpty()) { todoViewModel.emptyTipVis.value = View.VISIBLE } @@ -158,7 +145,7 @@ class ToDoFragment : Fragment() { MaterialAlertDialogBuilder(it1) .setTitle(R.string.warning) .setMessage(R.string.delete_confirm) - .setPositiveButton(R.string.ok) { dialog, which -> + .setPositiveButton(R.string.ok) { _, _ -> todoList.clear() lifecycleScope.launch { Repository.deleteAll() diff --git a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragmentViewModel.kt b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragmentViewModel.kt index 1902b67..7d315b3 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragmentViewModel.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragmentViewModel.kt @@ -3,8 +3,33 @@ package cn.super12138.todo.views.todo import android.view.View import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import cn.super12138.todo.logic.Repository +import cn.super12138.todo.logic.model.ToDo +import kotlinx.coroutines.launch class ToDoFragmentViewModel : ViewModel() { val emptyTipVis = MutableLiveData(View.GONE) val refreshData = MutableLiveData(0) + val todoList = ArrayList() + + init { + loadToDos() + } + + private fun loadToDos(){ + viewModelScope.launch { + val todos = Repository.getAllUncomplete() + var count = 0 + for (todo in todos) { + todoList.add(ToDo(todo.uuid, todo.content, todo.subject)) + count++ + } + if (count == 0) { + emptyTipVis.value = View.VISIBLE + } else { + emptyTipVis.value = View.GONE + } + } + } } \ No newline at end of file