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