Skip to content

Commit

Permalink
Optimize to-do list
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Jan 30, 2024
1 parent 9e8372f commit 6d17873
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 32 deletions.
12 changes: 12 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/views/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
29 changes: 8 additions & 21 deletions app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

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

0 comments on commit 6d17873

Please sign in to comment.