Optimize to-do list
This commit is contained in:
parent
9e8372fb3d
commit
6d17873cca
5 changed files with 47 additions and 32 deletions
|
|
@ -4,6 +4,7 @@ import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import cn.super12138.todo.R
|
import cn.super12138.todo.R
|
||||||
import cn.super12138.todo.ToDoApplication
|
import cn.super12138.todo.ToDoApplication
|
||||||
import cn.super12138.todo.logic.Repository
|
import cn.super12138.todo.logic.Repository
|
||||||
|
|
@ -17,6 +18,17 @@ open class BaseActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
// enableEdgeToEdge()
|
// enableEdgeToEdge()
|
||||||
super.onCreate(savedInstanceState)
|
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
|
val lp = window.attributes
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||||
|
|
|
||||||
|
|
@ -47,16 +47,7 @@ class MainActivity : BaseActivity() {
|
||||||
}
|
}
|
||||||
.show()
|
.show()
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
val isDarkMode = Repository.getPreferenceString(this, "dark_mode", "0")
|
|
||||||
val isSecureMode = Repository.getPreferenceBoolean(this, "secure_mode", false)
|
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) {
|
when (isSecureMode) {
|
||||||
true -> window.setFlags(
|
true -> window.setFlags(
|
||||||
WindowManager.LayoutParams.FLAG_SECURE,
|
WindowManager.LayoutParams.FLAG_SECURE,
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ class SettingsActivity : BaseActivity() {
|
||||||
Repository.getPreferenceBoolean(ToDoApplication.context, "dev_mode", false)
|
Repository.getPreferenceBoolean(ToDoApplication.context, "dev_mode", false)
|
||||||
|
|
||||||
findPreference<ListPreference>("dark_mode")?.apply {
|
findPreference<ListPreference>("dark_mode")?.apply {
|
||||||
setOnPreferenceChangeListener { preference, newValue ->
|
setOnPreferenceChangeListener { _, newValue ->
|
||||||
when (newValue) {
|
when (newValue) {
|
||||||
"0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
"0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
||||||
|
|
||||||
|
|
@ -72,7 +72,7 @@ class SettingsActivity : BaseActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
findPreference<SwitchPreferenceCompat>("secure_mode")?.apply {
|
findPreference<SwitchPreferenceCompat>("secure_mode")?.apply {
|
||||||
setOnPreferenceChangeListener { preference, newValue ->
|
setOnPreferenceChangeListener { _, newValue ->
|
||||||
when (newValue) {
|
when (newValue) {
|
||||||
true -> activity?.window?.setFlags(
|
true -> activity?.window?.setFlags(
|
||||||
WindowManager.LayoutParams.FLAG_SECURE,
|
WindowManager.LayoutParams.FLAG_SECURE,
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ class ToDoFragment : Fragment() {
|
||||||
|
|
||||||
private lateinit var binding: FragmentTodoBinding
|
private lateinit var binding: FragmentTodoBinding
|
||||||
private lateinit var ToDoDialogBinding: DialogAddTodoBinding
|
private lateinit var ToDoDialogBinding: DialogAddTodoBinding
|
||||||
private val todoList = ArrayList<ToDo>()
|
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
|
|
@ -52,31 +51,19 @@ class ToDoFragment : Fragment() {
|
||||||
WindowInsetsCompat.CONSUMED
|
WindowInsetsCompat.CONSUMED
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
val layoutManager = LinearLayoutManager(ToDoApplication.context)
|
|
||||||
binding.todoList.layoutManager = layoutManager
|
|
||||||
val adapter = ToDoAdapter(todoList, requireActivity())
|
|
||||||
binding.todoList.adapter = adapter
|
|
||||||
|
|
||||||
val progressViewModel =
|
val progressViewModel =
|
||||||
ViewModelProvider(requireActivity()).get(ProgressFragmentViewModel::class.java)
|
ViewModelProvider(requireActivity()).get(ProgressFragmentViewModel::class.java)
|
||||||
val todoViewModel =
|
val todoViewModel =
|
||||||
ViewModelProvider(requireActivity()).get(ToDoFragmentViewModel::class.java)
|
ViewModelProvider(requireActivity()).get(ToDoFragmentViewModel::class.java)
|
||||||
|
|
||||||
lifecycleScope.launch {
|
val todoList = todoViewModel.todoList
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (todoList.size == 0) {
|
val layoutManager = LinearLayoutManager(ToDoApplication.context)
|
||||||
|
binding.todoList.layoutManager = layoutManager
|
||||||
|
val adapter = ToDoAdapter(todoList, requireActivity())
|
||||||
|
binding.todoList.adapter = adapter
|
||||||
|
|
||||||
|
if (todoList.isEmpty()) {
|
||||||
todoViewModel.emptyTipVis.value = View.VISIBLE
|
todoViewModel.emptyTipVis.value = View.VISIBLE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,7 +145,7 @@ class ToDoFragment : Fragment() {
|
||||||
MaterialAlertDialogBuilder(it1)
|
MaterialAlertDialogBuilder(it1)
|
||||||
.setTitle(R.string.warning)
|
.setTitle(R.string.warning)
|
||||||
.setMessage(R.string.delete_confirm)
|
.setMessage(R.string.delete_confirm)
|
||||||
.setPositiveButton(R.string.ok) { dialog, which ->
|
.setPositiveButton(R.string.ok) { _, _ ->
|
||||||
todoList.clear()
|
todoList.clear()
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
Repository.deleteAll()
|
Repository.deleteAll()
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,33 @@ package cn.super12138.todo.views.todo
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
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() {
|
class ToDoFragmentViewModel : ViewModel() {
|
||||||
val emptyTipVis = MutableLiveData<Int>(View.GONE)
|
val emptyTipVis = MutableLiveData<Int>(View.GONE)
|
||||||
val refreshData = MutableLiveData<Int>(0)
|
val refreshData = MutableLiveData<Int>(0)
|
||||||
|
val todoList = ArrayList<ToDo>()
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue