package `in`.caresoft.carepulse.connect.ui import android.graphics.Typeface import android.os.Bundle import android.view.Gravity import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.widget.LinearLayout import android.widget.ScrollView import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import com.google.android.material.button.MaterialButton import `in`.caresoft.carepulse.connect.Prefs import `in`.caresoft.carepulse.connect.net.Api import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.json.JSONObject /** * Today's medicines (Phase 5). Patients without a smart dispenser confirm each dose here; rescue-inhaler users log puffs. * Doses from a smart pill dispenser or inhaler sensor arrive from the device's own cloud and show here as already taken. */ class MedicinesActivity : AppCompatActivity() { private lateinit var list: LinearLayout private lateinit var status: TextView private val api by lazy { val p = Prefs.get(this); Api(p.server!!, p.token) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) title = "My medicines" val pad = (20 * resources.displayMetrics.density).toInt() list = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL; setPadding(pad, pad, pad, pad) } status = TextView(this).apply { textSize = 15f } list.addView(status) setContentView(ScrollView(this).apply { addView(list) }) load() } private fun load() = lifecycleScope.launch { status.text = "Loading…" val res = runCatching { withContext(Dispatchers.IO) { api.medications() } } res.onSuccess { render(it) }.onFailure { status.text = "Could not load: ${it.message}" } } private fun render(j: JSONObject) { list.removeViews(1, list.childCount - 1) val doses = j.optJSONArray("doses") status.text = if (doses == null || doses.length() == 0) "No medicines scheduled today." else "Tap after you take each dose." for (i in 0 until (doses?.length() ?: 0)) { val d = doses!!.getJSONObject(i) val rescue = d.optString("kind") == "inhaler_rescue" val row = LinearLayout(this).apply { orientation = LinearLayout.HORIZONTAL; gravity = Gravity.CENTER_VERTICAL; setPadding(0, 24, 0, 24) } val text = TextView(this).apply { textSize = 17f text = (if (d.isNull("slot")) "As needed" else d.getString("slot")) + " · " + d.getString("name") + (if (d.isNull("dose")) "" else "\n" + d.getString("dose")) layoutParams = LinearLayout.LayoutParams(0, WRAP_CONTENT, 1f) } row.addView(text) val st = if (d.isNull("status")) null else d.getString("status") if (st != null && st != "missed" && !rescue) { row.addView(TextView(this).apply { textSize = 16f; setTypeface(null, Typeface.BOLD); this.text = when (st) { "taken" -> "✓ Taken"; "late" -> "✓ Late"; "skipped" -> "Skipped"; else -> st } }) } else { row.addView(MaterialButton(this).apply { this.text = if (rescue) "Used (2 puffs)" else if (st == "missed") "Taken now" else "Taken" setOnClickListener { record(d.getInt("medication_id"), if (rescue) "puff" else "taken", if (rescue) 2 else null) } }) } list.addView(row, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)) } } private fun record(id: Int, type: String, puffs: Int?) = lifecycleScope.launch { status.text = "Saving…" runCatching { withContext(Dispatchers.IO) { api.dose(id, type, puffs) } } .onSuccess { load() }.onFailure { status.text = "Not saved – check internet and try again (${it.message})" } } }