-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Siddharth Agarwal
committed
Dec 9, 2024
1 parent
1bfe2a8
commit 3d1b8ac
Showing
9 changed files
with
288 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...roidTest/java/org/simple/clinic/patientattribute/PatientAttributeRepositoryAndroidTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.simple.clinic.patientattribute | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.RuleChain | ||
import org.simple.clinic.TestClinicApp | ||
import org.simple.clinic.rules.LocalAuthenticationRule | ||
import org.simple.clinic.rules.SaveDatabaseRule | ||
import org.simple.sharedTestCode.TestData | ||
import org.simple.sharedTestCode.util.Rules | ||
import javax.inject.Inject | ||
|
||
class PatientAttributeRepositoryAndroidTest { | ||
|
||
@Inject | ||
lateinit var repository: PatientAttributeRepository | ||
|
||
@Inject | ||
lateinit var testData: TestData | ||
|
||
@get:Rule | ||
val rules: RuleChain = Rules | ||
.global() | ||
.around(LocalAuthenticationRule()) | ||
.around(SaveDatabaseRule()) | ||
|
||
@Before | ||
fun setup() { | ||
TestClinicApp.appComponent().inject(this) | ||
} | ||
|
||
@Test | ||
fun saving_a_patient_attribute_should_work_correctly() { | ||
//given | ||
val bmiReading = BMIReading(height = "177", weight = "64") | ||
val patientAttribute = testData.patientAttribute(reading = bmiReading) | ||
|
||
//when | ||
repository.save(listOf(patientAttribute)) | ||
|
||
//then | ||
val savedPatientAttribute = repository.getPatientAttribute(patientAttribute.patientUuid) | ||
assertThat(savedPatientAttribute).isEqualTo(patientAttribute) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/org/simple/clinic/patientattribute/PatientAttributeModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.simple.clinic.patientattribute | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import org.simple.clinic.AppDatabase | ||
|
||
@Module | ||
class PatientAttributeModule { | ||
@Provides | ||
fun dao(appDatabase: AppDatabase): PatientAttribute.RoomDao { | ||
return appDatabase.patientAttributeDao() | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
app/src/main/java/org/simple/clinic/patientattribute/PatientAttributeRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.simple.clinic.patientattribute | ||
|
||
import io.reactivex.Completable | ||
import io.reactivex.Observable | ||
import org.simple.clinic.di.AppScope | ||
import org.simple.clinic.patient.SyncStatus | ||
import org.simple.clinic.patient.SyncStatus.PENDING | ||
import org.simple.clinic.patientattribute.sync.PatientAttributePayload | ||
import org.simple.clinic.storage.Timestamps | ||
import org.simple.clinic.sync.SynceableRepository | ||
import org.simple.clinic.user.User | ||
import org.simple.clinic.util.UtcClock | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
|
||
@AppScope | ||
class PatientAttributeRepository @Inject constructor( | ||
val dao: PatientAttribute.RoomDao, | ||
private val utcClock: UtcClock | ||
) : SynceableRepository<PatientAttribute, PatientAttributePayload> { | ||
fun save( | ||
reading: BMIReading, | ||
patientUuid: UUID, | ||
loggedInUser: User, | ||
uuid: UUID, | ||
): Completable { | ||
val patientAttribute = PatientAttribute( | ||
uuid = uuid, | ||
patientUuid = patientUuid, | ||
userUuid = loggedInUser.uuid, | ||
reading = reading, | ||
timestamps = Timestamps.create(utcClock), | ||
syncStatus = PENDING | ||
) | ||
return Completable.fromAction { save(listOf(patientAttribute)) } | ||
} | ||
|
||
override fun save(records: List<PatientAttribute>) { | ||
dao.save(records) | ||
} | ||
|
||
override fun setSyncStatus(from: SyncStatus, to: SyncStatus) { | ||
dao.updateSyncStatus(from, to) | ||
} | ||
|
||
override fun setSyncStatus(ids: List<UUID>, to: SyncStatus) { | ||
if (ids.isEmpty()) { | ||
throw AssertionError() | ||
} | ||
|
||
dao.updateSyncStatusForIds(ids, to) | ||
} | ||
|
||
override fun recordCount(): Observable<Int> = dao.count().toObservable() | ||
|
||
override fun pendingSyncRecordCount(): Observable<Int> = | ||
dao.countWithStatus(PENDING).toObservable() | ||
|
||
override fun pendingSyncRecords(limit: Int, offset: Int): List<PatientAttribute> { | ||
return dao | ||
.recordsWithSyncStatusBatched( | ||
syncStatus = PENDING, | ||
limit = limit, | ||
offset = offset | ||
) | ||
} | ||
|
||
override fun mergeWithLocalData(payloads: List<PatientAttributePayload>) { | ||
val dirtyRecords = dao.recordIdsWithSyncStatus(PENDING) | ||
|
||
val payloadsToSave = payloads | ||
.filterNot { it.uuid in dirtyRecords } | ||
.map { it.toDatabaseModel(SyncStatus.DONE) } | ||
|
||
dao.save(payloadsToSave) | ||
} | ||
|
||
fun getPatientAttribute(patientUuid: UUID): PatientAttribute? { | ||
return dao.patientImmediate(patientUuid) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/org/simple/clinic/patientattribute/sync/PatientAttributePayload.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.simple.clinic.patientattribute.sync | ||
|
||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.JsonClass | ||
import org.simple.clinic.patient.SyncStatus | ||
import org.simple.clinic.patientattribute.BMIReading | ||
import org.simple.clinic.patientattribute.PatientAttribute | ||
import org.simple.clinic.storage.Timestamps | ||
import java.time.Instant | ||
import java.util.UUID | ||
|
||
@JsonClass(generateAdapter = false) | ||
data class PatientAttributePayload( | ||
@Json(name = "id") | ||
val uuid: UUID, | ||
|
||
@Json(name = "height") | ||
val height: Float, | ||
|
||
@Json(name = "weight") | ||
val weight: Float, | ||
|
||
@Json(name = "patient_id") | ||
val patientUuid: UUID, | ||
|
||
@Json(name = "user_id") | ||
val userUuid: UUID, | ||
|
||
@Json(name = "created_at") | ||
val createdAt: Instant, | ||
|
||
@Json(name = "updated_at") | ||
val updatedAt: Instant, | ||
|
||
@Json(name = "deleted_at") | ||
val deletedAt: Instant?, | ||
) { | ||
|
||
fun toDatabaseModel(syncStatus: SyncStatus) = PatientAttribute( | ||
uuid = uuid, | ||
patientUuid = patientUuid, | ||
userUuid = userUuid, | ||
reading = BMIReading( | ||
height = height.toString(), | ||
weight = weight.toString() | ||
), | ||
timestamps = Timestamps( | ||
createdAt = createdAt, | ||
updatedAt = updatedAt, | ||
deletedAt = deletedAt | ||
), | ||
syncStatus = syncStatus, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters