@@ -13,69 +13,66 @@ import com.simplemobiletools.notes.pro.R
13
13
import com.simplemobiletools.notes.pro.activities.SimpleActivity
14
14
import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter
15
15
import com.simplemobiletools.notes.pro.dialogs.NewChecklistItemDialog
16
- import com.simplemobiletools.notes.pro.extensions.config
17
- import com.simplemobiletools.notes.pro.extensions.notesDB
18
- import com.simplemobiletools.notes.pro.extensions.requiredActivity
19
- import com.simplemobiletools.notes.pro.extensions.updateWidgets
16
+ import com.simplemobiletools.notes.pro.extensions.*
20
17
import com.simplemobiletools.notes.pro.helpers.NOTE_ID
21
- import com.simplemobiletools.notes.pro.helpers.NoteType
22
18
import com.simplemobiletools.notes.pro.helpers.NotesHelper
23
19
import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener
24
20
import com.simplemobiletools.notes.pro.models.ChecklistItem
25
21
import com.simplemobiletools.notes.pro.models.Note
26
22
import kotlinx.android.synthetic.main.fragment_checklist.view.*
27
23
28
24
class ChecklistFragment : NoteFragment (), ChecklistItemsListener {
29
- private var noteId = 0L
25
+
26
+ private var noteId = 0L
27
+ private var items = ArrayList <ChecklistItem >()
30
28
private var note: Note ? = null
31
- private var items = ArrayList <ChecklistItem >()
32
29
33
30
lateinit var view: ViewGroup
34
31
32
+ val checklistItems get(): String = Gson ().toJson(items)
33
+
35
34
override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
36
- view = inflater.inflate(R .layout.fragment_checklist, container, false ) as ViewGroup
35
+ view = inflater.inflate(R .layout.fragment_checklist, container, false ) as ViewGroup
37
36
noteId = arguments!! .getLong(NOTE_ID , 0L )
38
37
return view
39
38
}
40
39
41
40
override fun onResume () {
42
41
super .onResume()
43
42
44
- NotesHelper (requiredActivity).getNoteWithId(noteId) {
45
- if (it != null && activity?.isDestroyed == false ) {
46
- note = it
43
+ loadNoteById(noteId)
44
+ }
45
+
46
+ override fun setMenuVisibility (menuVisible : Boolean ) {
47
+ super .setMenuVisibility(menuVisible)
48
+
49
+ if (menuVisible) activity?.hideKeyboard()
50
+ }
51
+
52
+ private fun loadNoteById (noteId : Long ) {
53
+ NotesHelper (requiredActivity).getNoteWithId(noteId) { storedNote ->
54
+ if (storedNote != null && activity?.isDestroyed == false ) {
55
+ note = storedNote
47
56
48
57
try {
49
- val checklistItemType = object : TypeToken <List <ChecklistItem >>() {}.type
50
- items = Gson ().fromJson<ArrayList <ChecklistItem >>(note !! .value, checklistItemType) ? : ArrayList (1 )
58
+ val checklistItemType = object : TypeToken <List <ChecklistItem >>(){}.type
59
+ items = Gson ().fromJson<ArrayList <ChecklistItem >>(storedNote .value, checklistItemType) ? : ArrayList (1 )
51
60
} catch (e: Exception ) {
52
- note?.run { migrateCheckListOnFailure(it) }
53
- e.printStackTrace()
54
- }
55
-
56
- if (config!! .moveUndoneChecklistItems) {
57
- items.sortBy { it.isDone }
61
+ migrateCheckListOnFailure(storedNote)
62
+ if (isDebug) e.printStackTrace()
58
63
}
59
64
65
+ if (config?.moveUndoneChecklistItems == true ) items.sortBy { it.isDone }
60
66
requiredActivity.updateTextColors(view.checklist_holder)
61
67
setupFragment()
62
68
}
63
69
}
64
70
}
65
71
66
- override fun setMenuVisibility (menuVisible : Boolean ) {
67
- super .setMenuVisibility(menuVisible)
68
- if (menuVisible) {
69
- activity?.hideKeyboard()
70
- }
71
- }
72
-
73
72
private fun migrateCheckListOnFailure (note : Note ) {
74
73
items.clear()
75
74
76
- val notes = note.value.split(" \n " ).map { it.trim() }.filter { it.isNotBlank() }
77
-
78
- notes.forEachIndexed { index, value ->
75
+ note.value.split(" \n " ).map { it.trim() }.filter { it.isNotBlank() }.forEachIndexed { index, value ->
79
76
items.add(ChecklistItem (
80
77
id = index,
81
78
title = value,
@@ -88,53 +85,63 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
88
85
89
86
private fun setupFragment () {
90
87
val plusIcon = resources.getColoredDrawableWithColor(R .drawable.ic_plus_vector, if (requiredActivity.isBlackAndWhiteTheme()) Color .BLACK else Color .WHITE )
88
+
91
89
view.apply {
92
- checklist_fab. apply {
90
+ with ( checklist_fab) {
93
91
setImageDrawable(plusIcon)
94
92
background.applyColorFilter(requiredActivity.getAdjustedPrimaryColor())
95
93
setOnClickListener {
96
94
showNewItemDialog()
97
95
}
98
96
}
99
97
100
- fragment_placeholder_2. apply {
98
+ with ( fragment_placeholder_2) {
101
99
setTextColor(requiredActivity.getAdjustedPrimaryColor())
102
100
underlineText()
103
101
setOnClickListener {
104
102
showNewItemDialog()
105
103
}
106
104
}
107
105
}
106
+
108
107
setupAdapter()
109
108
}
110
109
111
110
private fun showNewItemDialog () {
112
- NewChecklistItemDialog (activity as SimpleActivity ) {
113
- var currentMaxId = items.maxBy { it.id }?.id ? : 0
114
- it.forEach {
115
- val checklistItem = ChecklistItem (currentMaxId + 1 , it, false )
116
- items.add(checklistItem)
117
- currentMaxId++
111
+ NewChecklistItemDialog (activity as SimpleActivity ) { titles ->
112
+ var currentMaxId = items.maxBy { item -> item.id }?.id ? : 0
113
+
114
+ titles.forEach { title ->
115
+ title.split(" \n " ).map { it.trim() }.filter { it.isNotBlank() }.forEach { row ->
116
+ items.add(ChecklistItem (currentMaxId + 1 , row, false ))
117
+ currentMaxId++
118
+ }
118
119
}
120
+
119
121
saveNote()
120
- if (items.size == it.size) {
121
- setupAdapter()
122
- } else {
123
- (view.checklist_list.adapter as ? ChecklistAdapter )?.notifyDataSetChanged()
124
- }
122
+ setupAdapter()
123
+
124
+ (view.checklist_list.adapter as ? ChecklistAdapter )?.notifyDataSetChanged()
125
125
}
126
126
}
127
127
128
128
private fun setupAdapter () {
129
- view. apply {
129
+ with ( view) {
130
130
fragment_placeholder.beVisibleIf(items.isEmpty())
131
131
fragment_placeholder_2.beVisibleIf(items.isEmpty())
132
132
checklist_list.beVisibleIf(items.isNotEmpty())
133
133
}
134
134
135
- ChecklistAdapter (activity as SimpleActivity , items, this , view.checklist_list, true ) {
136
- val clickedNote = it as ChecklistItem
135
+ ChecklistAdapter (
136
+ activity = activity as SimpleActivity ,
137
+ items = items,
138
+ listener = this ,
139
+ recyclerView = view.checklist_list,
140
+ showIcons = true
141
+ ) { item ->
142
+ val clickedNote = item as ChecklistItem
137
143
clickedNote.isDone = ! clickedNote.isDone
144
+
138
145
saveNote(items.indexOfFirst { it.id == clickedNote.id })
139
146
context?.updateWidgets()
140
147
}.apply {
@@ -144,27 +151,22 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
144
151
145
152
private fun saveNote (refreshIndex : Int = -1) {
146
153
ensureBackgroundThread {
147
- if (note != null && context != null ) {
148
- if (refreshIndex != - 1 ) {
149
- view.checklist_list.post {
150
- view.checklist_list.adapter?.notifyItemChanged(refreshIndex)
154
+ context?.let { ctx ->
155
+ note?.let { currentNote ->
156
+ if (refreshIndex != - 1 ) {
157
+ view.checklist_list.post {
158
+ view.checklist_list.adapter?.notifyItemChanged(refreshIndex)
159
+ }
151
160
}
152
- }
153
161
154
- note!! .value = getChecklistItems()
155
- context?.notesDB?.insertOrUpdate(note!! )
156
- context?.updateWidgets()
162
+ currentNote.value = checklistItems
163
+ ctx.notesDB.insertOrUpdate(currentNote)
164
+ ctx.updateWidgets()
165
+ }
157
166
}
158
167
}
159
168
}
160
169
161
- fun getChecklistItems () = Gson ().toJson(items)
162
-
163
- override fun saveChecklist () {
164
- saveNote()
165
- }
166
-
167
- override fun refreshItems () {
168
- setupAdapter()
169
- }
170
+ override fun saveChecklist () { saveNote() }
171
+ override fun refreshItems () { setupAdapter() }
170
172
}
0 commit comments