Skip to content

Commit

Permalink
New feature: view all tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Feb 21, 2024
1 parent 26e2c04 commit 0bfc7f5
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 27 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
android:exported="false"
android:label="@string/settings_label"
android:launchMode="singleTask" />
<activity
android:name=".views.all.AllTasksActivity"
android:exported="false"
android:label="@string/all_tasks_label"
android:launchMode="singleTask" />
<activity
android:name=".views.about.AboutActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cn.super12138.todo.views.all

import android.os.Bundle
import android.view.View
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.databinding.ActivityAllTasksBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.views.BaseActivity
import me.zhanghai.android.fastscroll.FastScrollerBuilder

class AllTasksActivity : BaseActivity() {
private lateinit var binding: ActivityAllTasksBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val isSecureMode = Repository.getPreferenceBoolean(this, "secure_mode", false)
when (isSecureMode) {
true -> window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)

false -> window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

binding = ActivityAllTasksBinding.inflate(layoutInflater)
setContentView(binding.root)

val viewModel = ViewModelProvider(this)[AllTasksViewModel::class.java]

val layoutManager = LinearLayoutManager(ToDoApplication.context)
binding.allTasksList.layoutManager = layoutManager
val adapter = AllTasksAdapter(viewModel.todoListAll)
binding.allTasksList.adapter = adapter

FastScrollerBuilder(binding.allTasksList).apply {
useMd2Style()
build()
}

binding.toolbar.setNavigationOnClickListener {
finish()
}

viewModel.emptyTipVis.observe(this, Observer { visibility ->
if (visibility == View.VISIBLE) {
binding.allTasksList.alpha = 1f
binding.allTasksList.animate().alpha(0f).duration = 200
binding.allTasksList.visibility = View.GONE

binding.emptyTip.alpha = 0f
binding.emptyTip.visibility = View.VISIBLE
binding.emptyTip.animate().alpha(1f).duration = 200
} else {
binding.allTasksList.alpha = 0f
binding.allTasksList.visibility = View.VISIBLE
binding.allTasksList.animate().alpha(1f).duration = 200

binding.emptyTip.alpha = 1f
binding.emptyTip.animate().alpha(0f).duration = 200
binding.emptyTip.visibility = View.GONE
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.super12138.todo.views.all

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.super12138.todo.R
import cn.super12138.todo.logic.model.ToDo

class AllTasksAdapter(private val todoList: MutableList<ToDo>) :
RecyclerView.Adapter<AllTasksAdapter.ViewHolder>() {

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val todoContext: TextView = view.findViewById(R.id.todo_content_all)
val todoSubject: TextView = view.findViewById(R.id.todo_subject_all)
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_all_tasks, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val todo = todoList[position]
holder.todoContext.text = todo.content
holder.todoSubject.text = todo.subject
if (!todo.isAnimated) {
holder.itemView.alpha = 0f
holder.itemView.animate().alpha(1f).duration = 200
todo.isAnimated = true
}
}

override fun getItemCount() = todoList.size
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.super12138.todo.views.all

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 AllTasksViewModel : ViewModel() {
val emptyTipVis = MutableLiveData<Int>(View.VISIBLE)
val todoListAll = ArrayList<ToDo>()

init {
loadToDos()
}

private fun loadToDos() {
viewModelScope.launch {
val todos = Repository.getAll()
var count = 0
for (todo in todos) {
todoListAll.add(ToDo(todo.uuid, todo.content, todo.subject))
count++
}
if (count == 0) {
emptyTipVis.value = View.VISIBLE
} else {
emptyTipVis.value = View.GONE
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.super12138.todo.views.settings

import android.content.Intent
import android.os.Bundle
import android.os.Process
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.lifecycleScope
Expand All @@ -18,6 +18,7 @@ import cn.super12138.todo.databinding.DialogRestoreBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.logic.dao.ToDoRoom
import cn.super12138.todo.views.BaseActivity
import cn.super12138.todo.views.main.MainActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.gson.Gson
Expand Down Expand Up @@ -72,20 +73,15 @@ class SettingsActivity : BaseActivity() {
}

findPreference<SwitchPreferenceCompat>("secure_mode")?.apply {
setOnPreferenceChangeListener { _, newValue ->
when (newValue) {
true -> activity?.window?.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)

false -> activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
setOnPreferenceChangeListener { _, _ ->
view?.let {
Snackbar.make(it, R.string.need_restart_app, Snackbar.LENGTH_LONG)
.setAction(R.string.restart_app_now) {
Process.killProcess(Process.myPid())
exitProcess(10)
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
context.startActivity(intent)
exitProcess(0)
}
.show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class ToDoFragment : Fragment() {
}*/

val progressViewModel =
ViewModelProvider(requireActivity()).get(ProgressFragmentViewModel::class.java)
ViewModelProvider(requireActivity())[ProgressFragmentViewModel::class.java]
val todoViewModel =
ViewModelProvider(requireActivity()).get(ToDoFragmentViewModel::class.java)
ViewModelProvider(requireActivity())[ToDoFragmentViewModel::class.java]

val todoList = todoViewModel.todoList

Expand Down Expand Up @@ -89,7 +89,7 @@ class ToDoFragment : Fragment() {
ToDoDialogBinding.todoContent.error =
getString(R.string.content_cannot_be_empty)
} else {
if (todoContent == "/DEV MODE") {
if (todoContent == "/DEV_MODE") {
if (Repository.getPreferenceBoolean(
ToDoApplication.context,
"dev_mode",
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_all_todos.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M4,10.5c-0.83,0 -1.5,0.67 -1.5,1.5s0.67,1.5 1.5,1.5 1.5,-0.67 1.5,-1.5 -0.67,-1.5 -1.5,-1.5zM4,4.5c-0.83,0 -1.5,0.67 -1.5,1.5S3.17,7.5 4,7.5 5.5,6.83 5.5,6 4.83,4.5 4,4.5zM4,16.5c-0.83,0 -1.5,0.68 -1.5,1.5s0.68,1.5 1.5,1.5 1.5,-0.68 1.5,-1.5 -0.67,-1.5 -1.5,-1.5zM7,19h14v-2L7,17v2zM7,13h14v-2L7,11v2zM7,5v2h14L21,5L7,5z" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:liftOnScrollTargetViewId="@id/progress_frag">
app:liftOnScroll="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_about.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
android:fitsSystemWindows="true"
app:liftOnScroll="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/res/layout/activity_all_tasks.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".views.about.AboutActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:liftOnScroll="true"
app:liftOnScrollTargetViewId="@+id/all_tasks_list">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_arrow_back"
app:title="@string/all_tasks_label" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/all_tasks_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:visibility="gone"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

</androidx.recyclerview.widget.RecyclerView>


<TextView
android:id="@+id/empty_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@string/no_tasks"
android:textSize="14sp"
android:visibility="visible" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_todo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@string/no_tasks"
android:textSize="14sp"
android:visibility="visible" />

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/res/layout/item_all_tasks.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/materialCardViewElevatedStyle"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/todo_content_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="18dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="?android:attr/textColorPrimary"
android:textSize="20sp" />

<TextView
android:id="@+id/todo_subject_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:singleLine="true"
android:textColor="?android:attr/textColorPrimary"
android:textSize="11sp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@
<string name="restore_need_restart_app">需要重启应用以应用恢复的数据</string>
<string name="json_data_incorrect">JSON 数据格式错误</string>
<string name="restore_failed">恢复失败,请检查是否重复恢复同一次备份的数据</string>
<string name="view_all_tasks_label">查看全部待办</string>
<string name="all_tasks_label">全部代办</string>
<string name="view_all_tasks_summary">查看包含已完成的待办在内的全部待办</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@
<string name="restore_need_restart_app">Need to restart the app to apply the recovered data</string>
<string name="json_data_incorrect">JSON data format is incorrect</string>
<string name="restore_failed">Restore failed, please check if you are attempting to restore the same backup data again.</string>
<string name="view_all_tasks_label">View all tasks</string>
<string name="all_tasks_label">All tasks</string>
<string name="view_all_tasks_summary">View all tasks including those that have been checked as completed</string>
</resources>
Loading

0 comments on commit 0bfc7f5

Please sign in to comment.