Skip to content

Commit

Permalink
Reworks checklist item functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Nov 5, 2023
1 parent 1c3dcab commit 5fbdc98
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 165 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'com.getbase:floatingactionbutton:1.10.1'
implementation 'com.simplify:ink:1.0.0'
implementation 'io.github.eltos:simpledialogfragments:3.7'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
This file is part of the application Privacy Friendly Notes.
Privacy Friendly Notes is free software:
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or any later version.
Privacy Friendly Notes is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Privacy Friendly Notes. If not, see <http://www.gnu.org/licenses/>.
*/
package org.secuso.privacyfriendlynotes.ui.adapter

import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.checkbox.MaterialCheckBox
import org.secuso.privacyfriendlynotes.R


class ChecklistAdapter(var items: MutableList<Pair<Boolean, String>>) : RecyclerView.Adapter<ChecklistAdapter.ItemHolder>() {

private var onLongClickListener: ((View) -> Unit)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.item_checklist, parent, false)
return ItemHolder(itemView)
}

override fun onBindViewHolder(holder: ItemHolder, position: Int) {
val (checked, item) = items[position]
holder.textView.text = item
holder.checkbox.isChecked = checked

holder.textView.apply {
paintFlags = if (checked) {
paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
} else {
paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
}
}

override fun getItemCount(): Int {
return items.size
}

fun addItem(item: String) {
this.items.add(Pair(false,item))
notifyItemInserted(items.size - 1)
}

fun removeItem(position: Int) {
this.items.removeAt(position);
notifyItemRemoved(position)
}

fun setOnLongClickListener(listener: (View) -> Unit) {
this.onLongClickListener = listener
}

inner class ItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.item_name)
val checkbox: MaterialCheckBox = itemView.findViewById(R.id.item_checkbox)

init {
checkbox.setOnClickListener { _ ->
items[adapterPosition] = Pair(checkbox.isChecked, items[adapterPosition].second)
notifyItemChanged(adapterPosition)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.secuso.privacyfriendlynotes.room.DbContract
import org.secuso.privacyfriendlynotes.room.NoteDatabase
import org.secuso.privacyfriendlynotes.room.model.Category
import org.secuso.privacyfriendlynotes.room.model.Note
import org.secuso.privacyfriendlynotes.ui.util.ChecklistUtil
import java.io.File

/**
Expand Down Expand Up @@ -203,14 +204,8 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
if (note.type != DbContract.NoteEntry.TYPE_CHECKLIST) {
throw IllegalArgumentException("Only checklist notes allowed")
}
try {
val content = JSONArray(note.content)
return (0 until content.length()).map {
val obj = content.getJSONObject(it)
return@map Pair(obj.getBoolean("checked"), String.format("[%s] ${obj.getString("name")}", if (obj.getBoolean("checked")) "x" else " "))
}.toList()
} catch (ex: JSONException) {
return ArrayList()
return ChecklistUtil.parse(note.content).map {(checked, name) ->
return@map Pair(checked, String.format("[%s] $name", if (checked) "x" else " "))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli
private var id = -1

private var notification: Notification? = null
var allCategories: List<Category>? = null
var adapter: ArrayAdapter<CharSequence>? = null
private var allCategories: List<Category>? = null
private var adapter: ArrayAdapter<CharSequence>? = null
private lateinit var createEditNoteViewModel: CreateEditNoteViewModel

private val noteType by lazy { noteType }
Expand Down
Loading

0 comments on commit 5fbdc98

Please sign in to comment.