1
0
mirror of https://github.com/dzeiocom/OpenHealth.git synced 2025-04-23 19:32:11 +00:00

misc: Cleanup DI/Graphs/Interface and Services

This commit is contained in:
Florian Bouillon 2023-01-07 23:42:07 +01:00
parent a9da9198be
commit eb2c6f7c4c
Signed by: Florian Bouillon
GPG Key ID: BEEAF3722D0EBF64
6 changed files with 44 additions and 7 deletions

View File

@ -14,6 +14,9 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
/**
* Provide to the application the Database/Daos and external services
*/
@InstallIn(SingletonComponent::class)
@Module
class DatabaseModule {

View File

@ -11,6 +11,9 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
/**
* Provide to the application System elements
*/
@InstallIn(SingletonComponent::class)
@Module
class SystemModule {

View File

@ -15,6 +15,11 @@ import com.google.android.material.color.MaterialColors
import kotlin.math.max
import kotlin.math.min
/**
* TODO: Migrate to DzeioCharts when it is ready
*
* Setup the mikephil Chart for the Weight
*/
object WeightChart {
fun setup(
chart: LineChart,

View File

@ -2,6 +2,9 @@ package com.dzeio.openhealth.interfaces
import android.app.NotificationManager
/**
* The different notification channels the applicaiton is using
*/
enum class NotificationChannels(
val id: String,
val channelName: String,

View File

@ -1,5 +1,8 @@
package com.dzeio.openhealth.interfaces
/**
* The different notifications the application can send to the user
*/
enum class NotificationIds {
WaterIntake,

View File

@ -29,12 +29,18 @@ import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
/**
* The Service that allow the application to run in the background
*/
class OpenHealthService : Service() {
companion object {
private const val TAG = "${Application.TAG}/Service"
}
/**
* Get the StepRepository without DI because it is unavailable here
*/
private val stepRepository: StepRepository
get() = StepRepository_Factory.newInstance(
AppDatabase.getInstance(applicationContext).stepDao()
@ -64,35 +70,49 @@ class OpenHealthService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
scope.launch {
// start the StepSource
val source = StepSource(this@OpenHealthService)
source.events.receiveAsFlow().collectLatest {
Log.d(TAG, "Received value: $it")
// receive each updates as to the number of steps taken
source.events.receiveAsFlow().collectLatest {
// Log.d(TAG, "Received value: $it")
// handle case where no new steps were taken
if (it <= 0f) {
Log.d(TAG, "No new steps registered ($it)")
return@collectLatest
}
Log.d(TAG, "New steps registered: $it")
// update internal variables to keep track of the number of steps taken
// Log.d(TAG, "New steps registered: $it")
stepsTaken += it.toUInt()
stepsBuffer += it.toInt()
// show the notification
showNotification()
// try to get the current number of steps for the hour from the DB
val step = withTimeoutOrNull(1000) {
return@withTimeoutOrNull stepRepository.currentStep().firstOrNull()
}
Log.d(TAG, "stepRepository: $step")
// Log.d(TAG, "stepRepository: $step")
// if steps registered, add them and send them back
if (step != null) {
step.value += stepsBuffer
stepRepository.updateStep(step)
// create a new steps object and send it
} else {
stepRepository.addStep(Step(value = stepsBuffer))
}
// reset the internal buffer
stepsBuffer = 0
Log.d(TAG, "Added step!")
// Log.d(TAG, "Added step!")
}
}
// Display a notification about us starting. We put an icon in the status bar.
startForeground(NotificationIds.Service.ordinal, showNotification())
return START_STICKY