Skip to content

Commit

Permalink
Fix alert checking logic (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
minizzang authored Jan 8, 2024
1 parent ade76a4 commit 06c452b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
5 changes: 3 additions & 2 deletions wear/src/main/java/com/kaist/k_dual/model/UseBloodGlucose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object UseBloodGlucose {

private var updateJob: Job? = null

fun updateBloodGlucose(context: Context) {
fun updateBloodGlucose(context: Context): Job? {
val latestGlucoseProps = LatestGlucoseProps(180, 36)

val sharedPref = context.getSharedPreferences(
Expand All @@ -69,7 +69,7 @@ object UseBloodGlucose {
}
} else {
null
} ?: return
} ?: return updateJob

val glucoseUnit = settings.glucoseUnits

Expand Down Expand Up @@ -149,6 +149,7 @@ object UseBloodGlucose {
}
}
}
return updateJob
}

private suspend fun getNightScoutData(url: String, glucoseUnit: GlucoseUnits): List<Any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kaist.k_dual.service
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import com.kaist.k_dual.model.UseBloodGlucose
import kotlinx.coroutines.*

Expand All @@ -14,7 +15,19 @@ class GlucoseUpdateService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
serviceScope.launch {
while (true) {
UseBloodGlucose.updateBloodGlucose(applicationContext)
val firstPrev = UseBloodGlucose.firstUser
val secondPrev = UseBloodGlucose.secondUser

UseBloodGlucose.updateBloodGlucose(applicationContext)?.join()
val firstNew = UseBloodGlucose.firstUser
val secondNew = UseBloodGlucose.secondUser

if (firstPrev != firstNew || secondPrev != secondNew) {
Intent().also { intent ->
intent.action = "com.kaist.k_dual.ACTION_BG_UPDATED"
sendBroadcast(intent)
}
}
delay(30 * 1000L)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ import androidx.core.app.ActivityCompat
import com.google.gson.Gson
import com.google.gson.JsonParseException
import com.google.gson.JsonSyntaxException
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import com.kaist.k_dual.model.UseBloodGlucose
import com.kaist.k_dual.model.UseSetting
import com.kaist.k_dual.service.GlucoseUpdateService

class KDualCanvasRenderer(
Expand Down Expand Up @@ -111,16 +108,6 @@ class KDualCanvasRenderer(
}
}

private val handler = Handler(Looper.getMainLooper())
private val updateInterval = 5 * 60 * 1000L

private val checkAlertConditionTask = object : Runnable {
override fun run() {
checkAlertCondition()
handler.postDelayed(this, updateInterval)
}
}

private fun checkAlertCondition() {
settings?.let {
val firstUserBGValue = UseBloodGlucose.firstUser.toIntOrNull()
Expand Down Expand Up @@ -150,19 +137,26 @@ class KDualCanvasRenderer(
}
}

private val bgUpdateReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == "com.kaist.k_dual.ACTION_BG_UPDATED") {
checkAlertCondition()
}
}
}

init {
context.registerReceiver(batteryReceiver, intentFilter)
sharedPref.registerOnSharedPreferenceChangeListener(sharedPrefChangeListener)
updateSettings()
checkAlertConditionTask.run()
val intent = Intent(context, GlucoseUpdateService::class.java)
context.startService(intent)
context.registerReceiver(bgUpdateReceiver, IntentFilter("com.kaist.k_dual.ACTION_BG_UPDATED"), Context.RECEIVER_NOT_EXPORTED)
}

override fun onDestroy() {
super.onDestroy()
context.unregisterReceiver(batteryReceiver)
handler.removeCallbacks(checkAlertConditionTask)
val intent = Intent(context, GlucoseUpdateService::class.java)
context.stopService(intent)
}
Expand Down

0 comments on commit 06c452b

Please sign in to comment.