|
| 1 | +/* |
| 2 | + * Copyright 2021-2023 Ona Systems, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.smartregister.fhircore.engine.datastore |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import androidx.datastore.core.DataStore |
| 21 | +import androidx.datastore.dataStore |
| 22 | +import dagger.hilt.android.qualifiers.ApplicationContext |
| 23 | +import java.io.IOException |
| 24 | +import javax.inject.Inject |
| 25 | +import javax.inject.Singleton |
| 26 | +import kotlinx.coroutines.flow.catch |
| 27 | +import org.smartregister.fhircore.engine.datastore.mockdata.PractitionerDetails |
| 28 | +import org.smartregister.fhircore.engine.datastore.mockdata.UserInfo |
| 29 | +import org.smartregister.fhircore.engine.datastore.serializers.PractitionerDetailsDataStoreSerializer |
| 30 | +import org.smartregister.fhircore.engine.datastore.serializers.UserInfoDataStoreSerializer |
| 31 | +import timber.log.Timber |
| 32 | + |
| 33 | +private const val PRACTITIONER_DETAILS_DATASTORE_JSON = "practitioner_details.json" |
| 34 | +private const val USER_INFO_DATASTORE_JSON = "user_info.json" |
| 35 | +private const val TAG = "Proto DataStore" |
| 36 | + |
| 37 | +val Context.practitionerProtoStore: DataStore<PractitionerDetails> by |
| 38 | + dataStore( |
| 39 | + fileName = PRACTITIONER_DETAILS_DATASTORE_JSON, |
| 40 | + serializer = PractitionerDetailsDataStoreSerializer, |
| 41 | + ) |
| 42 | + |
| 43 | +val Context.userInfoProtoStore: DataStore<UserInfo> by |
| 44 | + dataStore( |
| 45 | + fileName = USER_INFO_DATASTORE_JSON, |
| 46 | + serializer = UserInfoDataStoreSerializer, |
| 47 | + ) |
| 48 | + |
| 49 | +@Singleton |
| 50 | +class ProtoDataStore @Inject constructor(@ApplicationContext val context: Context) { |
| 51 | + |
| 52 | + val practitioner = |
| 53 | + context.practitionerProtoStore.data.catch { exception -> |
| 54 | + if (exception is IOException) { |
| 55 | + Timber.tag(TAG).e(exception, "Error reading practitioner details preferences.") |
| 56 | + emit(PractitionerDetails()) |
| 57 | + } else { |
| 58 | + throw exception |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + suspend fun writePractitioner(practitionerDetails: PractitionerDetails) { |
| 63 | + context.practitionerProtoStore.updateData { practitionerData -> |
| 64 | + practitionerData.copy( |
| 65 | + name = practitionerDetails.name, |
| 66 | + id = practitionerDetails.id, |
| 67 | + ) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + val userInfo = |
| 72 | + context.userInfoProtoStore.data.catch { exception -> |
| 73 | + if (exception is IOException) { |
| 74 | + Timber.tag(TAG).e(exception, "Error reading practitioner details preferences.") |
| 75 | + emit(UserInfo()) |
| 76 | + } else { |
| 77 | + throw exception |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + suspend fun writeUserInfo(userInfo: UserInfo) { |
| 82 | + context.userInfoProtoStore.updateData { userInfo -> |
| 83 | + userInfo.copy( |
| 84 | + name = userInfo.name, |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments