Skip to content

Commit

Permalink
Use domain model for battery repo
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Sep 8, 2023
1 parent 71cb17d commit 906cc51
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ class BatteryReadingEntity(
return BatteryReading(time, percent, capacity, isCharging)
}

companion object {
fun from(reading: BatteryReading): BatteryReadingEntity {
return BatteryReadingEntity(
reading.percent,
reading.capacity,
reading.isCharging,
reading.time
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import com.kylecorry.andromeda.battery.Battery
import com.kylecorry.andromeda.battery.BatteryChargingStatus
import com.kylecorry.trail_sense.shared.commands.CoroutineCommand
import com.kylecorry.trail_sense.tools.battery.domain.BatteryReading
import com.kylecorry.trail_sense.tools.battery.domain.BatteryReadingEntity
import com.kylecorry.trail_sense.tools.battery.infrastructure.persistence.BatteryRepo
import kotlinx.coroutines.Dispatchers
Expand All @@ -29,7 +30,7 @@ class BatteryLogCommand(private val context: Context) : CoroutineCommand {
val charging = battery.chargingStatus == BatteryChargingStatus.Charging
val time = Instant.now()
val capacity = battery.capacity
val reading = BatteryReadingEntity(pct, capacity, charging, time)
val reading = BatteryReading(time, pct, capacity, charging)
if (battery.hasValidReading) {
batteryRepo.add(reading)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.kylecorry.trail_sense.tools.battery.infrastructure.persistence

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.map
import com.kylecorry.andromeda.preferences.FloatPreference
import com.kylecorry.sol.units.Reading
import com.kylecorry.trail_sense.shared.database.AppDatabase
import com.kylecorry.trail_sense.shared.extensions.onIO
import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem
import com.kylecorry.trail_sense.tools.battery.domain.BatteryReading
import com.kylecorry.trail_sense.tools.battery.domain.BatteryReadingEntity
import java.time.Instant

Expand All @@ -15,13 +18,13 @@ class BatteryRepo private constructor(context: Context) : IBatteryRepo {
private val prefs = PreferencesSubsystem.getInstance(context).preferences
private var maxCapacityPref by FloatPreference(prefs, "pref_max_battery_capacity", 0f)

override fun get(): LiveData<List<BatteryReadingEntity>> {
return batteryDao.get()
override fun get(): LiveData<List<BatteryReading>> {
return batteryDao.get().map { it.map { reading -> reading.toBatteryReading() } }
}

override suspend fun add(reading: BatteryReadingEntity) {
override suspend fun add(reading: BatteryReading) {
onIO {
batteryDao.insert(reading)
batteryDao.insert(BatteryReadingEntity.from(reading))

// Record the maximum capacity the battery has reached
if (getMaxCapacity() < reading.capacity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.kylecorry.trail_sense.tools.battery.infrastructure.persistence

import androidx.lifecycle.LiveData
import com.kylecorry.trail_sense.tools.battery.domain.BatteryReadingEntity
import com.kylecorry.trail_sense.tools.battery.domain.BatteryReading
import java.time.Instant

interface IBatteryRepo {
fun get(): LiveData<List<BatteryReadingEntity>>
suspend fun add(reading: BatteryReadingEntity)
fun get(): LiveData<List<BatteryReading>>
suspend fun add(reading: BatteryReading)
suspend fun deleteBefore(time: Instant)
fun getMaxCapacity(): Float
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class FragmentToolBattery : BoundFragment<FragmentToolBatteryBinding>() {

observe(batteryRepo.get()) { allReadings ->
readings =
allReadings.sortedBy { it.time }.map { it.toBatteryReading() } + listOfNotNull(
allReadings.sortedBy { it.time } + listOfNotNull(
if (battery.hasValidReading)
BatteryReading(
Instant.now(),
Expand Down

0 comments on commit 906cc51

Please sign in to comment.