Add app/src/main/java/space/bloggin/scribe/MainActivity.kt
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
package space.bloggin.scribe
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.app.AlertDialog
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Color
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.BaseAdapter
|
||||
import android.widget.Button
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
|
||||
class MainActivity : Activity() {
|
||||
private lateinit var stateView: TextView
|
||||
private lateinit var hintView: TextView
|
||||
private lateinit var partialView: TextView
|
||||
private lateinit var toggle: Button
|
||||
private lateinit var list: ListView
|
||||
private lateinit var empty: TextView
|
||||
private var items: List<Note> = emptyList()
|
||||
private val listener = { refresh() }
|
||||
|
||||
private val adapter = object : BaseAdapter() {
|
||||
override fun getCount() = items.size
|
||||
override fun getItem(i: Int) = items[i]
|
||||
override fun getItemId(i: Int) = i.toLong()
|
||||
override fun getView(i: Int, v: View?, p: ViewGroup?): View {
|
||||
val n = items[i]
|
||||
val row = LinearLayout(this@MainActivity)
|
||||
row.orientation = LinearLayout.VERTICAL
|
||||
val pad = Ui.dp(this@MainActivity, 16)
|
||||
row.setPadding(pad, Ui.dp(this@MainActivity, 12), pad, Ui.dp(this@MainActivity, 12))
|
||||
val body = n.text.trim().ifEmpty { "(empty note)" }
|
||||
val t = Ui.text(this@MainActivity, body, 16f, Ui.INK, true)
|
||||
t.maxLines = 2
|
||||
t.ellipsize = android.text.TextUtils.TruncateAt.END
|
||||
row.addView(t)
|
||||
val live = Bus.activeNoteId == n.id
|
||||
row.addView(Ui.text(this@MainActivity, if (live) "Being dictated now" else Ui.fmtDate(n.updated), 13f, if (live) Ui.LIVE else Ui.MUTED))
|
||||
return row
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(b: Bundle?) {
|
||||
super.onCreate(b)
|
||||
Store.init(this)
|
||||
val root = LinearLayout(this)
|
||||
root.orientation = LinearLayout.VERTICAL
|
||||
|
||||
val settings = Ui.barButton(this, "Settings")
|
||||
settings.setOnClickListener { startActivity(Intent(this, SettingsActivity::class.java)) }
|
||||
root.addView(Ui.topBar(this, "Hey Dumby", settings))
|
||||
|
||||
val card = LinearLayout(this)
|
||||
card.orientation = LinearLayout.VERTICAL
|
||||
val pad = Ui.dp(this, 16)
|
||||
card.setPadding(pad, pad, pad, pad)
|
||||
stateView = Ui.text(this, "", 20f, Ui.INK, true)
|
||||
hintView = Ui.text(this, "", 14f, Ui.MUTED)
|
||||
partialView = Ui.text(this, "", 16f, Ui.LIVE)
|
||||
partialView.minLines = 2
|
||||
card.addView(stateView)
|
||||
card.addView(hintView)
|
||||
card.addView(partialView)
|
||||
|
||||
val row = LinearLayout(this)
|
||||
row.orientation = LinearLayout.HORIZONTAL
|
||||
toggle = Ui.button(this, "Start listening", Ui.ACCENT)
|
||||
toggle.setOnClickListener { onToggle() }
|
||||
val newNote = Ui.ghostButton(this, "New note")
|
||||
newNote.setOnClickListener {
|
||||
val n = Store.create()
|
||||
startActivity(Intent(this, NoteActivity::class.java).putExtra("id", n.id))
|
||||
}
|
||||
val lp = { LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f).also { it.setMargins(0, Ui.dp(this, 12), Ui.dp(this, 6), 0) } }
|
||||
row.addView(toggle, lp())
|
||||
row.addView(newNote, lp().also { it.setMargins(Ui.dp(this, 6), Ui.dp(this, 12), 0, 0) })
|
||||
card.addView(row)
|
||||
root.addView(card)
|
||||
|
||||
val sep = View(this)
|
||||
sep.setBackgroundColor(Ui.LINE)
|
||||
root.addView(sep, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Ui.dp(this, 1)))
|
||||
|
||||
list = ListView(this)
|
||||
list.divider = android.graphics.drawable.ColorDrawable(Ui.LINE)
|
||||
list.dividerHeight = Ui.dp(this, 1)
|
||||
list.adapter = adapter
|
||||
list.setOnItemClickListener { _, _, i, _ ->
|
||||
startActivity(Intent(this, NoteActivity::class.java).putExtra("id", items[i].id))
|
||||
}
|
||||
list.setOnItemLongClickListener { _, _, i, _ ->
|
||||
val n = items[i]
|
||||
AlertDialog.Builder(this).setTitle("Delete this note?")
|
||||
.setPositiveButton("Delete") { _, _ -> Store.delete(n.id); refresh() }
|
||||
.setNegativeButton("Keep", null).show()
|
||||
true
|
||||
}
|
||||
empty = Ui.text(this, "No notes yet. Start listening and say your wake phrase, or tap New note.", 15f, Ui.MUTED)
|
||||
empty.setPadding(pad, pad, pad, pad)
|
||||
root.addView(empty)
|
||||
root.addView(list, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f))
|
||||
setContentView(root)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
Bus.listeners.add(listener)
|
||||
if (Prefs.wanted(this) && !Bus.running && hasMic()) ListenService.start(this)
|
||||
refresh()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
Bus.listeners.remove(listener)
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
private fun hasMic() = checkSelfPermission(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
|
||||
|
||||
private fun onToggle() {
|
||||
if (Bus.running) {
|
||||
Prefs.setWanted(this, false)
|
||||
ListenService.stop(this)
|
||||
return
|
||||
}
|
||||
val need = ArrayList<String>()
|
||||
if (!hasMic()) need.add(Manifest.permission.RECORD_AUDIO)
|
||||
if (Build.VERSION.SDK_INT >= 33 && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED)
|
||||
need.add(Manifest.permission.POST_NOTIFICATIONS)
|
||||
if (need.isNotEmpty()) { requestPermissions(need.toTypedArray(), 1); return }
|
||||
ListenService.start(this)
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(code: Int, perms: Array<out String>, res: IntArray) {
|
||||
if (hasMic()) ListenService.start(this)
|
||||
else stateView.text = "Hey Dumby needs the microphone permission to listen."
|
||||
}
|
||||
|
||||
private fun refresh() {
|
||||
items = Store.all()
|
||||
adapter.notifyDataSetChanged()
|
||||
empty.visibility = if (items.isEmpty()) View.VISIBLE else View.GONE
|
||||
stateView.text = Bus.state
|
||||
partialView.text = Bus.partial
|
||||
hintView.text = if (Bus.running) "Notes are saved as you speak. Say “scratch that” to remove the last bit."
|
||||
else "Tap Start listening. The phone then waits for “${Prefs.wakeShown(this)}”."
|
||||
toggle.text = if (Bus.running) "Stop listening" else "Start listening"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user