diff --git a/app/src/main/java/com/dzeio/openhealth/di/DatabaseModule.kt b/app/src/main/java/com/dzeio/openhealth/di/DatabaseModule.kt index c7bb1e5..e79b83f 100644 --- a/app/src/main/java/com/dzeio/openhealth/di/DatabaseModule.kt +++ b/app/src/main/java/com/dzeio/openhealth/di/DatabaseModule.kt @@ -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 { diff --git a/app/src/main/java/com/dzeio/openhealth/di/SystemModule.kt b/app/src/main/java/com/dzeio/openhealth/di/SystemModule.kt index 2315607..5a5e697 100644 --- a/app/src/main/java/com/dzeio/openhealth/di/SystemModule.kt +++ b/app/src/main/java/com/dzeio/openhealth/di/SystemModule.kt @@ -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 { diff --git a/app/src/main/java/com/dzeio/openhealth/graphs/WeightChart.kt b/app/src/main/java/com/dzeio/openhealth/graphs/WeightChart.kt index 70fd8e8..6f239fd 100644 --- a/app/src/main/java/com/dzeio/openhealth/graphs/WeightChart.kt +++ b/app/src/main/java/com/dzeio/openhealth/graphs/WeightChart.kt @@ -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, diff --git a/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationChannels.kt b/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationChannels.kt index 1ed7fb1..a0f4344 100644 --- a/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationChannels.kt +++ b/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationChannels.kt @@ -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, diff --git a/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationIds.kt b/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationIds.kt index 8668f81..d1e72b1 100644 --- a/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationIds.kt +++ b/app/src/main/java/com/dzeio/openhealth/interfaces/NotificationIds.kt @@ -1,5 +1,8 @@ package com.dzeio.openhealth.interfaces +/** + * The different notifications the application can send to the user + */ enum class NotificationIds { WaterIntake, @@ -7,4 +10,4 @@ enum class NotificationIds { * Open Health Main Service Notification ID */ Service -} \ No newline at end of file +} diff --git a/app/src/main/java/com/dzeio/openhealth/services/OpenHealthService.kt b/app/src/main/java/com/dzeio/openhealth/services/OpenHealthService.kt index 461da37..6f5b765 100644 --- a/app/src/main/java/com/dzeio/openhealth/services/OpenHealthService.kt +++ b/app/src/main/java/com/dzeio/openhealth/services/OpenHealthService.kt @@ -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