|
| 1 | +package org.smartregister.fhircore.engine.datastore |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.datastore.core.DataStore |
| 5 | +import androidx.datastore.dataStore |
| 6 | +import dagger.hilt.android.qualifiers.ApplicationContext |
| 7 | +import kotlinx.coroutines.flow.catch |
| 8 | +import javax.inject.Inject |
| 9 | +import org.smartregister.fhircore.engine.datastore.mockdata.SerializablePractitionerDetails |
| 10 | +import org.smartregister.fhircore.engine.datastore.mockdata.SerializableUserInfo |
| 11 | +import org.smartregister.fhircore.engine.datastore.serializers.PractitionerDetailsDataStoreSerializer |
| 12 | +import org.smartregister.fhircore.engine.datastore.serializers.UserInfoDataStoreSerializer |
| 13 | +import timber.log.Timber |
| 14 | +import java.io.IOException |
| 15 | +import javax.inject.Singleton |
| 16 | + |
| 17 | +private const val PRACTITIONER_DETAILS_DATASTORE_JSON = "practitioner_details.json" |
| 18 | +private const val USER_INFO_DATASTORE_JSON = "user_info.json" |
| 19 | +private const val TAG = "Proto DataStore" |
| 20 | + |
| 21 | +val Context.practitionerProtoStore: DataStore<SerializablePractitionerDetails> by dataStore( |
| 22 | + fileName = PRACTITIONER_DETAILS_DATASTORE_JSON, |
| 23 | + serializer = PractitionerDetailsDataStoreSerializer |
| 24 | +) |
| 25 | + |
| 26 | +val Context.userInfoProtoStore: DataStore<SerializableUserInfo> by dataStore( |
| 27 | + fileName = USER_INFO_DATASTORE_JSON, |
| 28 | + serializer = UserInfoDataStoreSerializer |
| 29 | +) |
| 30 | +@Singleton |
| 31 | +class ProtoDataStore @Inject constructor(@ApplicationContext val context: Context) { |
| 32 | + |
| 33 | + val practitioner = context.practitionerProtoStore.data |
| 34 | + .catch { exception -> |
| 35 | + if (exception is IOException) { |
| 36 | + Timber.tag(TAG).e(exception, "Error reading practitioner details preferences.") |
| 37 | + emit(SerializablePractitionerDetails()) |
| 38 | + } else { |
| 39 | + throw exception |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + suspend fun writePractitioner(serializablePractitionerDetails: SerializablePractitionerDetails) { |
| 44 | + context.practitionerProtoStore.updateData { practitionerData -> |
| 45 | + practitionerData.copy( |
| 46 | + name = serializablePractitionerDetails.name, |
| 47 | + id = serializablePractitionerDetails.id |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + val userInfo = context.userInfoProtoStore.data |
| 53 | + .catch { exception -> |
| 54 | + if (exception is IOException) { |
| 55 | + Timber.tag(TAG).e(exception, "Error reading practitioner details preferences.") |
| 56 | + emit(SerializableUserInfo()) |
| 57 | + } else { |
| 58 | + throw exception |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + suspend fun writeUserInfo(serializableUserInfo: SerializableUserInfo) { |
| 63 | + context.userInfoProtoStore.updateData { userInfo -> |
| 64 | + userInfo.copy( |
| 65 | + name = serializableUserInfo.name |
| 66 | + ) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments