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 96148794..617d4ffc 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 @@ -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 @@ -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() { } @@ -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() { diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt index 65c6aac7..18d664f6 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt @@ -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) 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 f23fcc1c..6d1e429a 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 @@ -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 { @@ -33,5 +34,20 @@ class ChecklistUtil { } return jsonArray } + + fun textToItem(text: String): Pair { + 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>): String { +// checklist.map { (checked, text) -> } +// } } } \ No newline at end of file