Add app/src/main/java/space/bloggin/scribe/NoteActivity.kt

This commit is contained in:
2026-11-11 17:59:34 +00:00
parent 40139a6ed1
commit 398acf890d
@@ -0,0 +1,115 @@
package space.bloggin.scribe
import android.app.Activity
import android.app.AlertDialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.InputType
import android.view.Gravity
import android.view.ViewGroup
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
class NoteActivity : Activity() {
private lateinit var id: String
private lateinit var edit: EditText
private var lastSaved = ""
private val listener = { pullFromStore() }
override fun onCreate(b: Bundle?) {
super.onCreate(b)
Store.init(this)
id = intent.getStringExtra("id") ?: run { finish(); return }
val note = Store.get(id) ?: run { finish(); return }
val root = LinearLayout(this)
root.orientation = LinearLayout.VERTICAL
val back = Ui.barButton(this, "Back")
back.setOnClickListener { finish() }
root.addView(Ui.topBar(this, "Note", back))
edit = EditText(this)
edit.setText(note.text)
lastSaved = note.text
edit.gravity = Gravity.TOP or Gravity.START
edit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
edit.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 18f)
edit.setTextColor(Ui.INK)
edit.background = null
val pad = Ui.dp(this, 16)
edit.setPadding(pad, pad, pad, pad)
root.addView(edit, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f))
val row = LinearLayout(this)
row.orientation = LinearLayout.HORIZONTAL
row.setPadding(Ui.dp(this, 10), Ui.dp(this, 8), Ui.dp(this, 10), Ui.dp(this, 10))
fun add(label: String, fn: () -> Unit) {
val btn = Ui.ghostButton(this, label)
btn.setOnClickListener { fn() }
val lp = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
lp.setMargins(Ui.dp(this, 3), 0, Ui.dp(this, 3), 0)
row.addView(btn, lp)
}
add("Copy") {
(getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(ClipData.newPlainText("note", edit.text.toString()))
Toast.makeText(this, "Copied", Toast.LENGTH_SHORT).show()
}
add("Share") {
val i = Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, edit.text.toString())
startActivity(Intent.createChooser(i, "Share note"))
}
add("Delete") {
AlertDialog.Builder(this).setTitle("Delete this note?")
.setPositiveButton("Delete") { _, _ -> Store.delete(id); lastSaved = edit.text.toString(); Bus.changed(); finish() }
.setNegativeButton("Keep", null).show()
}
root.addView(row)
val row2 = LinearLayout(this)
row2.orientation = LinearLayout.HORIZONTAL
row2.setPadding(Ui.dp(this, 10), 0, Ui.dp(this, 10), Ui.dp(this, 10))
fun add2(label: String, docx: Boolean) {
val btn = Ui.ghostButton(this, label)
btn.setOnClickListener {
save()
val n = Store.get(id) ?: return@setOnClickListener
Export.share(this, listOf(n), Export.fileName(listOf(n)), docx)
}
val lp = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
lp.setMargins(Ui.dp(this, 3), 0, Ui.dp(this, 3), 0)
row2.addView(btn, lp)
}
add2("Export PDF", false)
add2("Export Word", true)
root.addView(row2)
setContentView(root)
}
/** While this note is being dictated, show new words as they arrive. */
private fun pullFromStore() {
if (Bus.activeNoteId != id) return
val n = Store.get(id) ?: return
if (n.text != edit.text.toString()) {
edit.setText(n.text)
edit.setSelection(edit.text.length)
lastSaved = n.text
}
}
override fun onResume() { super.onResume(); Bus.listeners.add(listener) }
override fun onPause() {
Bus.listeners.remove(listener)
save()
super.onPause()
}
private fun save() {
if (!::edit.isInitialized || Store.get(id) == null) return
val t = edit.text.toString()
if (t != lastSaved) { Store.setText(id, t); lastSaved = t }
}
}