Skip to content

Commit

Permalink
[fix] checklist items now consistently contain their state.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Oct 17, 2024
1 parent b22491e commit adaadec
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

<application
android:name=".PFNotesApplication"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.secuso.privacyfriendlynotes.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Paint
import android.text.Editable
import android.text.TextWatcher
Expand All @@ -25,21 +26,28 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.checkbox.MaterialCheckBox
import org.secuso.privacyfriendlynotes.R
import org.secuso.privacyfriendlynotes.ui.SettingsActivity
import org.secuso.privacyfriendlynotes.ui.util.ChecklistItem
import java.util.Collections

/**
* Provides bindings to show a Checklist-Item in a RecyclerView.
* @author Patrick Schneider
*/
class ChecklistAdapter(
private var items: MutableList<Pair<Boolean, String>>,
private val startDrag: (ItemHolder) -> Unit,
) : RecyclerView.Adapter<ChecklistAdapter.ItemHolder>() {

private var items: MutableList<ChecklistItem> = mutableListOf()
var hasChanged = false
private set

fun getItems(): List<Pair<Boolean, String>> {
@SuppressLint("NotifyDataSetChanged")
fun setItems(items: List<ChecklistItem>) {
this.items = items.toMutableList()
notifyDataSetChanged()
}

fun getItems(): List<ChecklistItem> {
return items
}

Expand All @@ -48,16 +56,6 @@ class ChecklistAdapter(
hasChanged = true
}

fun setAll(items: Collection<Pair<Boolean, String>>) {
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)
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Boolean, Int?> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,34 @@ 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
*/
class ChecklistUtil {

companion object {
fun parse(checklist: String): List<Pair<Boolean, String>> {
fun parse(checklist: String): List<ChecklistItem> {
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<Pair<Boolean, String>>): JSONArray {
fun json(checklist: List<ChecklistItem>): 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) {
Expand All @@ -52,15 +54,15 @@ class ChecklistUtil {
return jsonArray
}

fun textToItem(text: String): Pair<Boolean, String> {
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)
}
}
}

0 comments on commit adaadec

Please sign in to comment.