Skip to content

Commit

Permalink
[feat] Finishes conversion text note <-> checklist note
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Feb 7, 2024
1 parent 15e1c28 commit bc36a28
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ package org.secuso.privacyfriendlynotes.ui.notes

import android.content.Intent
import android.os.Bundle
import android.text.Html
import android.text.Spanned
import android.text.SpannedString
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.ItemTouchHelper
Expand Down Expand Up @@ -85,6 +90,26 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI
adaptFontSize(etNewItem)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.activity_checklist, menu)
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId) {
R.id.action_convert_to_note -> {
super.convertNote(Html.toHtml(SpannedString(getContentString())), DbContract.NoteEntry.TYPE_TEXT) {
val i = Intent(application, TextNoteActivity::class.java)
i.putExtra(BaseNoteActivity.EXTRA_ID, it)
startActivity(i)
finish()
}
}
else -> {}
}
return super.onOptionsItemSelected(item)
}

override fun onNewNote() {

}
Expand Down Expand Up @@ -134,7 +159,7 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI
}

private fun getContentString(): String {
return adapter.getItems().joinToString(separator = "\n") { (checked, name) -> "- $name [${if (checked) "" else " "}]" }
return adapter.getItems().joinToString(System.lineSeparator()) { (checked, name) -> "- [${if (checked) "" else " "}] $name" }
}

private fun addItem() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class TextNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_TEXT) {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId) {
R.id.action_convert_to_checklist -> {
val json = ChecklistUtil.json(etContent.text.lines().filter { it.isNotBlank() }.map { line -> Pair(false, line) })
val json = ChecklistUtil.json(etContent.text.lines().filter { it.isNotBlank() }.map(ChecklistUtil::textToItem))
super.convertNote(json.toString(), DbContract.NoteEntry.TYPE_CHECKLIST) {
val i = Intent(application, ChecklistNoteActivity::class.java)
i.putExtra(BaseNoteActivity.EXTRA_ID, it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.secuso.privacyfriendlynotes.ui.util
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.regex.Pattern

class ChecklistUtil {

Expand Down Expand Up @@ -33,5 +34,20 @@ class ChecklistUtil {
}
return jsonArray
}

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

// fun contentString(checklist: List<Pair<Boolean, String>>): String {
// checklist.map { (checked, text) -> }
// }
}
}

0 comments on commit bc36a28

Please sign in to comment.