-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHistoryActivity.kt
More file actions
106 lines (88 loc) · 3.12 KB
/
HistoryActivity.kt
File metadata and controls
106 lines (88 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.example.todoapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_history.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class HistoryActivity : AppCompatActivity() {
val db by lazy {
AppDatabase.getDatabase(this)
}
val list = arrayListOf<TodoModel>()
var com_adapter = TodoAdapter(list)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_history)
setSupportActionBar(toolbar)
completedRv.apply {
layoutManager = LinearLayoutManager(this@HistoryActivity)
adapter = this@HistoryActivity.com_adapter
}
db.todoDao().getFinishedTask().observe(this, Observer {
if (!it.isNullOrEmpty()) {
list.clear()
list.addAll(it)
com_adapter.notifyDataSetChanged()
}else{
list.clear()
com_adapter.notifyDataSetChanged()
}
})
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.history, menu)
val item = menu.findItem(R.id.search)
val searchView = item.actionView as SearchView
item.setOnActionExpandListener(object :MenuItem.OnActionExpandListener{
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
displayTodo()
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
displayTodo()
return true
}
})
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
if(!newText.isNullOrEmpty()){
displayTodo(newText)
}
return true
}
})
return super.onCreateOptionsMenu(menu)
}
fun displayTodo(newText: String = "") {
db.todoDao().getFinishedTask().observe(this, Observer {
if(it.isNotEmpty()){
list.clear()
list.addAll(
it.filter { todo ->
todo.title.contains(newText,true)
}
)
com_adapter.notifyDataSetChanged()
}
})
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.delete -> {
GlobalScope.launch(Dispatchers.IO) {
db.todoDao().delete_finished_task()
}
}
}
return super.onOptionsItemSelected(item)
}
}