Hoofdscherm omgebouwd naar wekker-UI: klok, geplande wekkers, dienstinfo; testwekker verplaatst uit de UI naar launch-intent
This commit is contained in:
parent
29691e1425
commit
6c0fa97e50
5 changed files with 146 additions and 70 deletions
|
|
@ -5,6 +5,8 @@ import android.content.Intent
|
|||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
|
|
@ -15,14 +17,27 @@ import kotlinx.coroutines.launch
|
|||
import nl.eddydevink.buschauffeurswekker.core.AlarmScheduler
|
||||
import nl.eddydevink.buschauffeurswekker.core.Prefs
|
||||
import nl.eddydevink.buschauffeurswekker.core.ShiftCheck
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import nl.eddydevink.buschauffeurswekker.core.ShiftPlanner
|
||||
import java.time.LocalDateTime
|
||||
import java.time.LocalTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
/** Hoofdscherm: status + acties. Instellingen staan in [SettingsActivity]. */
|
||||
/**
|
||||
* Hoofdscherm: klok + de geplande wekkers van morgen. Instellingen zitten in
|
||||
* [SettingsActivity]; de testwekker is alleen bereikbaar via launch-intent
|
||||
* (--ez test true), niet via de productie-UI.
|
||||
*/
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var prefs: Prefs
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
private val clockTick = object : Runnable {
|
||||
override fun run() {
|
||||
updateClock()
|
||||
handler.postDelayed(this, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
|
@ -32,32 +47,37 @@ class MainActivity : AppCompatActivity() {
|
|||
requestNotificationPermission()
|
||||
|
||||
findViewById<MaterialButton>(R.id.btn_check).setOnClickListener { runCheck() }
|
||||
findViewById<MaterialButton>(R.id.btn_test).setOnClickListener {
|
||||
AlarmScheduler.scheduleTest(this)
|
||||
toast("Testwekker gaat over 10 seconden af")
|
||||
}
|
||||
findViewById<MaterialButton>(R.id.btn_settings).setOnClickListener {
|
||||
startActivity(Intent(this, SettingsActivity::class.java))
|
||||
}
|
||||
|
||||
AlarmScheduler.scheduleNoonCheck(this)
|
||||
|
||||
// Config via launch-intent (installatiehulp):
|
||||
// adb shell am start ... --es url "..." --es user "..." --es pass "..."
|
||||
// Installatiehulp/config zonder UI-interactie:
|
||||
// --es url "…" --es user "…" --es pass "…" → config opslaan + controleren
|
||||
// --ez test true → testwekker (debugging)
|
||||
handleSetupIntent(intent)
|
||||
if (intent?.getBooleanExtra(EXTRA_TEST, false) == true) {
|
||||
AlarmScheduler.scheduleTest(this)
|
||||
toast("Testwekker gaat over 10 seconden af")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
setIntent(intent)
|
||||
handleSetupIntent(intent)
|
||||
if (intent.getBooleanExtra(EXTRA_TEST, false)) {
|
||||
AlarmScheduler.scheduleTest(this)
|
||||
toast("Testwekker gaat over 10 seconden af")
|
||||
}
|
||||
}
|
||||
|
||||
/** Slaat CalDAV-config van intent-extra's op en draait direct een controle. */
|
||||
private fun handleSetupIntent(intent: Intent?) {
|
||||
val url = intent?.getStringExtra(EXTRA_SETUP_URL)
|
||||
val user = intent?.getStringExtra(EXTRA_SETUP_USER)
|
||||
val pass = intent?.getStringExtra(EXTRA_SETUP_PASS)
|
||||
private fun handleSetupIntent(intent: Intent) {
|
||||
val url = intent.getStringExtra(EXTRA_SETUP_URL)
|
||||
val user = intent.getStringExtra(EXTRA_SETUP_USER)
|
||||
val pass = intent.getStringExtra(EXTRA_SETUP_PASS)
|
||||
if (url == null && user == null && pass == null) return
|
||||
if (!url.isNullOrBlank()) prefs.calendarUrl = url.trim()
|
||||
if (!user.isNullOrBlank()) prefs.username = user.trim()
|
||||
|
|
@ -72,24 +92,40 @@ class MainActivity : AppCompatActivity() {
|
|||
override fun onResume() {
|
||||
super.onResume()
|
||||
renderStatus()
|
||||
updateClock()
|
||||
handler.removeCallbacks(clockTick)
|
||||
handler.post(clockTick)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
handler.removeCallbacks(clockTick)
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
private fun updateClock() {
|
||||
val now = LocalTime.now()
|
||||
findViewById<TextView>(R.id.tv_clock).text = HH_MM.format(now)
|
||||
findViewById<TextView>(R.id.tv_date).text = DATE.format(java.time.LocalDate.now())
|
||||
}
|
||||
|
||||
private fun renderStatus() {
|
||||
val p = prefs
|
||||
val last = p.lastCheckedAtMillis.takeIf { it > 0 }?.let {
|
||||
Instant.ofEpochMilli(it)
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"))
|
||||
} ?: "nooit"
|
||||
findViewById<TextView>(R.id.tv_status).text = buildString {
|
||||
append("Dagelijkse controle: elke dag om 12:00\n")
|
||||
append("Laatst gecontroleerd: $last\n\n")
|
||||
append(p.lastStatusTxt ?: "Nog niet gecontroleerd.")
|
||||
val (day, alarms) = p.loadPlan()
|
||||
if (day != null && alarms.isNotEmpty()) {
|
||||
append("\n\nGepland voor $day:")
|
||||
alarms.forEach { append("\n ${it.name} om ${it.time.toLocalTime()}") }
|
||||
val (_, alarms) = p.loadPlan()
|
||||
val next = findViewById<TextView>(R.id.tv_next_alarms)
|
||||
val info = findViewById<TextView>(R.id.tv_shift_info)
|
||||
|
||||
if (alarms.isNotEmpty()) {
|
||||
next.text = alarms.joinToString("\n") { "${it.name} ${HH_MM.format(it.time)}" }
|
||||
val shift = p.lastShiftStartIso?.let { runCatching { LocalDateTime.parse(it) }.getOrNull() }
|
||||
info.text = if (shift != null) {
|
||||
val rounded = ShiftPlanner.roundDownToQuarter(shift.toLocalTime())
|
||||
"Dienst om ${HH_MM.format(shift.toLocalTime())} · afgerond ${HH_MM.format(rounded)}"
|
||||
} else {
|
||||
p.lastStatusTxt.orEmpty()
|
||||
}
|
||||
} else {
|
||||
next.text = "Geen wekkers"
|
||||
info.text = p.lastStatusTxt ?: "Nog niet gecontroleerd"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -125,5 +161,8 @@ class MainActivity : AppCompatActivity() {
|
|||
const val EXTRA_SETUP_URL = "url"
|
||||
const val EXTRA_SETUP_USER = "user"
|
||||
const val EXTRA_SETUP_PASS = "pass"
|
||||
const val EXTRA_TEST = "test"
|
||||
val HH_MM = DateTimeFormatter.ofPattern("HH:mm")
|
||||
val DATE = DateTimeFormatter.ofPattern("EEEE d MMMM")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue