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.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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ class SettingsActivity : BaseActivity() {
|
|||
Repository.getPreferenceBoolean(ToDoApplication.context, "dev_mode", false)
|
||||
|
||||
findPreference<ListPreference>("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<SwitchPreferenceCompat>("secure_mode")?.apply {
|
||||
setOnPreferenceChangeListener { preference, newValue ->
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
when (newValue) {
|
||||
true -> activity?.window?.setFlags(
|
||||
WindowManager.LayoutParams.FLAG_SECURE,
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ class ToDoFragment : Fragment() {
|
|||
|
||||
private lateinit var binding: FragmentTodoBinding
|
||||
private lateinit var ToDoDialogBinding: DialogAddTodoBinding
|
||||
private val todoList = ArrayList<ToDo>()
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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<Int>(View.GONE)
|
||||
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