@@ -23,150 +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) 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
56
-
57
- try {
58
- val checklistItemType = object : TypeToken <List <ChecklistItem >>(){}.type
59
- items = Gson ().fromJson<ArrayList <ChecklistItem >>(storedNote.value, checklistItemType) ? : ArrayList (1 )
60
- } catch (e: Exception ) {
61
- migrateCheckListOnFailure(storedNote)
62
- if (isDebug) e.printStackTrace()
63
- }
64
-
65
- if (config?.moveUndoneChecklistItems == true ) items.sortBy { it.isDone }
66
- requiredActivity.updateTextColors(view.checklist_holder)
67
- setupFragment()
68
- }
69
- }
70
- }
71
-
72
- private fun migrateCheckListOnFailure (note : Note ) {
73
- items.clear()
74
-
75
- note.value.split(" \n " ).map { it.trim() }.filter { it.isNotBlank() }.forEachIndexed { index, value ->
76
- items.add(ChecklistItem (
77
- id = index,
78
- title = value,
79
- isDone = false
80
- ))
81
- }
82
-
83
- saveChecklist()
84
- }
85
-
86
- private fun setupFragment () {
87
- val plusIcon = resources.getColoredDrawableWithColor(R .drawable.ic_plus_vector, if (requiredActivity.isBlackAndWhiteTheme()) Color .BLACK else Color .WHITE )
88
-
89
- view.apply {
90
- with (checklist_fab) {
91
- setImageDrawable(plusIcon)
92
- background.applyColorFilter(requiredActivity.getAdjustedPrimaryColor())
93
- setOnClickListener {
94
- showNewItemDialog()
95
- }
96
- }
97
-
98
- with (fragment_placeholder_2) {
99
- setTextColor(requiredActivity.getAdjustedPrimaryColor())
100
- underlineText()
101
- setOnClickListener {
102
- showNewItemDialog()
103
- }
104
- }
49
+ if (menuVisible) {
50
+ activity?.hideKeyboard()
105
51
}
106
-
107
- setupAdapter()
108
- }
109
-
110
- private fun showNewItemDialog () {
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++
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
+ items.sortBy { it.isDone }
118
69
}
119
- }
120
-
121
- saveNote()
122
- setupAdapter()
123
-
124
- (view.checklist_list.adapter as ? ChecklistAdapter )?.notifyDataSetChanged()
125
- }
126
- }
127
-
128
- private fun setupAdapter () {
129
- with (view) {
130
- fragment_placeholder.beVisibleIf(items.isEmpty())
131
- fragment_placeholder_2.beVisibleIf(items.isEmpty())
132
- checklist_list.beVisibleIf(items.isNotEmpty())
133
- }
134
-
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
143
- clickedNote.isDone = ! clickedNote.isDone
144
-
145
- saveNote(items.indexOfFirst { it.id == clickedNote.id })
146
- context?.updateWidgets()
147
- }.apply {
148
- view.checklist_list.adapter = this
149
- }
150
- }
151
-
152
- private fun saveNote (refreshIndex : Int = -1) {
153
- ensureBackgroundThread {
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
- }
160
- }
161
-
162
- currentNote.value = checklistItems
163
- ctx.notesDB.insertOrUpdate(currentNote)
164
- ctx.updateWidgets()
165
- }
166
- }
167
- }
168
- }
169
70
170
- override fun saveChecklist () { saveNote() }
171
- override fun refreshItems () { setupAdapter() }
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
+ }
172
182
}
0 commit comments