From adaadecafd7a15eb0230cd0d887e70f76ee9636a Mon Sep 17 00:00:00 2001 From: Patrick Schneider Date: Thu, 17 Oct 2024 11:20:39 +0200 Subject: [PATCH] [fix] checklist items now consistently contain their state. --- app/build.gradle | 4 +-- app/src/main/AndroidManifest.xml | 1 - .../ui/adapter/ChecklistAdapter.kt | 28 +++++++++---------- .../ui/notes/ChecklistNoteActivity.kt | 6 ++-- .../ui/util/ChecklistUtil.kt | 18 ++++++------ 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ae8e3c3f..3b3b6cce 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,8 +24,8 @@ android { minSdkVersion 21 compileSdk 34 targetSdkVersion 34 - versionCode 20 - versionName "2.0.1" + versionCode 21 + versionName "2.0.2" } applicationVariants.configureEach { variant -> diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d626dddf..06f5205c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,7 +6,6 @@ - >, private val startDrag: (ItemHolder) -> Unit, ) : RecyclerView.Adapter() { + private var items: MutableList = mutableListOf() var hasChanged = false private set - fun getItems(): List> { + @SuppressLint("NotifyDataSetChanged") + fun setItems(items: List) { + this.items = items.toMutableList() + notifyDataSetChanged() + } + + fun getItems(): List { return items } @@ -48,16 +56,6 @@ class ChecklistAdapter( hasChanged = true } - fun setAll(items: Collection>) { - if (this.items.isNotEmpty()) { - this.items.clear() - } else { - hasChanged = false - } - this.items.addAll(items) - notifyItemRangeChanged(0, this.items.size) - } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder { val itemView = LayoutInflater.from(parent.context) .inflate(R.layout.item_checklist, parent, false) @@ -73,7 +71,7 @@ class ChecklistAdapter( v.performClick() } holder.checkbox.setOnClickListener { _ -> - items[holder.bindingAdapterPosition] = Pair(holder.checkbox.isChecked, holder.textView.text.toString()) + items[holder.bindingAdapterPosition].state = holder.checkbox.isChecked holder.textView.apply { paintFlags = if (holder.checkbox.isChecked) { paintFlags or Paint.STRIKE_THRU_TEXT_FLAG @@ -93,7 +91,7 @@ class ChecklistAdapter( } override fun afterTextChanged(text: Editable?) { - items[holder.bindingAdapterPosition] = Pair(holder.checkbox.isChecked, (text ?: "").toString()) + items[holder.bindingAdapterPosition].name = (text ?: "").toString() hasChanged = true } @@ -114,7 +112,7 @@ class ChecklistAdapter( } fun addItem(item: String) { - this.items.add(Pair(false, item)) + this.items.add(ChecklistItem(false, item)) notifyItemInserted(items.size - 1) hasChanged = true } diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt index 5b7470c8..f8366580 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt @@ -31,9 +31,11 @@ import org.secuso.privacyfriendlynotes.R import org.secuso.privacyfriendlynotes.room.DbContract import org.secuso.privacyfriendlynotes.room.model.Note import org.secuso.privacyfriendlynotes.ui.adapter.ChecklistAdapter +import org.secuso.privacyfriendlynotes.ui.util.ChecklistItem import org.secuso.privacyfriendlynotes.ui.util.ChecklistUtil import java.io.OutputStream import java.io.PrintWriter +import java.util.Collections /** * Activity that allows to add, edit and delete checklist notes. @@ -79,7 +81,7 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI } } val ith = ItemTouchHelper(itemTouchCallback) - adapter = ChecklistAdapter(mutableListOf()) { holder -> ith.startDrag(holder) } + adapter = ChecklistAdapter(startDrag = { ith.startDrag(it) }) checklist.adapter = adapter checklist.layoutManager = LinearLayoutManager(this) btnAdd.setOnClickListener { @@ -125,7 +127,7 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI } override fun onNoteLoadedFromDB(note: Note) { - adapter.setAll(ChecklistUtil.parse(note.content)) + adapter.setItems(ChecklistUtil.parse(note.content)) } override fun hasNoteChanged(title: String, category: Int): Pair { diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/util/ChecklistUtil.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/util/ChecklistUtil.kt index 1e496cd9..8d11460a 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/util/ChecklistUtil.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/util/ChecklistUtil.kt @@ -18,6 +18,8 @@ import org.json.JSONException import org.json.JSONObject import java.util.regex.Pattern +data class ChecklistItem(var state: Boolean, var name: String) + /** * Provides common utilities to interact with a checklist. * @author Patrick Schneider @@ -25,25 +27,25 @@ import java.util.regex.Pattern class ChecklistUtil { companion object { - fun parse(checklist: String): List> { + fun parse(checklist: String): List { try { val content = JSONArray(checklist) return (0 until content.length()).map { val obj = content.getJSONObject(it) - return@map Pair(obj.getBoolean("checked"), obj.getString("name")) + return@map ChecklistItem(obj.getBoolean("checked"), obj.getString("name")) } } catch (ex: JSONException) { return ArrayList() } } - fun json(checklist: List>): JSONArray { + fun json(checklist: List): JSONArray { val jsonArray = JSONArray() try { - for ((checked, name) in checklist) { + for ((state, name) in checklist) { val jsonObject = JSONObject() jsonObject.put("name", name) - jsonObject.put("checked", checked) + jsonObject.put("checked", state) jsonArray.put(jsonObject) } } catch (e: JSONException) { @@ -52,15 +54,15 @@ class ChecklistUtil { return jsonArray } - fun textToItem(text: String): Pair { + fun textToItem(text: String): ChecklistItem { Pattern.compile("-\\s*\\[(.*)]\\s*(.*)").matcher(text).apply { if (matches()) { val checked = group(1) val name = group(2) - return Pair(checked !== null && checked.isNotEmpty() && checked.isNotBlank(), name!!) + return ChecklistItem(checked !== null && checked.isNotEmpty() && checked.isNotBlank(), name!!) } } - return Pair(false, text) + return ChecklistItem(false, text) } } } \ No newline at end of file