Skip to content

Commit

Permalink
LoginViewModel migration
Browse files Browse the repository at this point in the history
- Add new keys

- Migrate WritePractitionerDetails
  • Loading branch information
kelvin-ngure committed Dec 21, 2023
1 parent 0200cb1 commit 4c84ee1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = DAT

@Singleton
class PreferencesDataStore @Inject constructor(@ApplicationContext val context: Context) {
fun <T> read(key: Preferences.Key<T>) =
fun <T> read(key: Preferences.Key<T>) =
context.dataStore.data
.catch { exception ->
if (exception is IOException) {
Expand All @@ -46,25 +46,34 @@ class PreferencesDataStore @Inject constructor(@ApplicationContext val context:
}
.map { preferences -> preferences[key] as T }

// expose flows to be used all over the engine and view models
val appId by lazy { read(APP_ID) }
val lang by lazy { read(LANG) }
val careTeam by lazy { read(CARE_TEAM) }
val organization by lazy { read(ORGANIZATION) }
val practitionerLocation by lazy { read(PRACTITIONER_LOCATION) }
val practitionerId by lazy { read(PRACTITIONER_ID) }

// expose flows to be used all over the engine and view models
val appId by lazy { read(APP_ID) }
val lang by lazy { read(LANG) }
val careTeamIds by lazy { read(CARE_TEAM_IDS) }
val careTeamNames by lazy { read(CARE_TEAM_NAMES) }
val locationIds by lazy { read(LOCATION_IDS) }
val locationNames by lazy { read(LOCATION_NAMES) }
val organizationIds by lazy { read(ORGANIZATION_IDS) }
val organizationNames by lazy { read(ORGANIZATION_NAMES) }
val practitionerId by lazy { read(PRACTITIONER_ID) }
val practitionerLocation by lazy { read(PRACTITIONER_LOCATION) }
val practitionerLocationHierarchies by lazy { read(PRACTITIONER_LOCATION_HIERARCHIES) }

suspend fun <T> write(key: Preferences.Key<T>, data: T) {
context.dataStore.edit { preferences -> preferences[key] = data }
}

companion object Keys {
val APP_ID by lazy { stringPreferencesKey("APP_ID") }
val LANG by lazy { stringPreferencesKey("LANG") }
val CARE_TEAM by lazy { stringPreferencesKey("CARE_TEAM") }
val ORGANIZATION by lazy { stringPreferencesKey("ORGANIZATION") }
val PRACTITIONER_ID by lazy { stringPreferencesKey("PRACTITIONER_ID") }
val PRACTITIONER_LOCATION by lazy { stringPreferencesKey("PRACTITIONER_LOCATION ") }
val APP_ID by lazy { stringPreferencesKey("APP_ID") }
val LANG by lazy { stringPreferencesKey("LANG") }
val CARE_TEAM_IDS by lazy { stringPreferencesKey("CARE_TEAM_IDS") }
val CARE_TEAM_NAMES by lazy { stringPreferencesKey("CARE_TEAM_NAMES") }
val LOCATION_IDS by lazy { stringPreferencesKey("LOCATION_IDS") }
val LOCATION_NAMES by lazy { stringPreferencesKey("LOCATION_NAMES") }
val ORGANIZATION_IDS by lazy { stringPreferencesKey("ORGANIZATION_IDS") }
val ORGANIZATION_NAMES by lazy { stringPreferencesKey("ORGANIZATION_NAMES") }
val PRACTITIONER_ID by lazy { stringPreferencesKey("PRACTITIONER_ID") }
val PRACTITIONER_LOCATION by lazy { stringPreferencesKey("PRACTITIONER_LOCATION ") }
val PRACTITIONER_LOCATION_HIERARCHIES by lazy { stringPreferencesKey("LOCATION_HIERARCHIES") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import org.smartregister.fhircore.engine.data.remote.auth.KeycloakService
import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceService
import org.smartregister.fhircore.engine.data.remote.model.response.UserInfo
import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator
import org.smartregister.fhircore.engine.datastore.PreferencesDataStore
import org.smartregister.fhircore.engine.datastore.ProtoDataStore
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.SecureSharedPreference
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
Expand All @@ -67,6 +69,8 @@ class LoginViewModel
constructor(
val configurationRegistry: ConfigurationRegistry,
val accountAuthenticator: AccountAuthenticator,
val preferencesDataStore: PreferencesDataStore,
val protoDataStore: ProtoDataStore,
val sharedPreferences: SharedPreferencesHelper,
val secureSharedPreference: SecureSharedPreference,
val defaultRepository: DefaultRepository,
Expand Down Expand Up @@ -335,20 +339,20 @@ constructor(
}
}

val location =
val locationNames =
withContext(dispatcherProvider.io()) {
defaultRepository.createRemote(false, *locations.toTypedArray()).run {
locations.map { it.name }
}
}

val careTeam =
val careTeamNames =
withContext(dispatcherProvider.io()) {
defaultRepository.createRemote(false, *careTeams.toTypedArray()).run {
careTeams.map { it.name }
}
}
val organization =
val organizationNames =
withContext(dispatcherProvider.io()) {
defaultRepository.createRemote(false, *organizations.toTypedArray()).run {
organizations.map { it.name }
Expand All @@ -368,14 +372,14 @@ constructor(

if (practitionerId.isNotEmpty()) {
writePractitionerDetailsToShredPref(
careTeam = careTeam,
organization = organization,
location = location,
careTeamNames = careTeamNames,
organizationNames = organizationNames,
locationNames = locationNames,
fhirPractitionerDetails = practitionerDetails,
careTeams = careTeamIds,
organizations = organizationIds,
locations = locationIds,
locationHierarchies = locationHierarchies,
careTeamIds = careTeamIds,
organizationIds = organizationIds,
locationIds = locationIds,
locationHierarchies = locationHierarchies
)
} else {
// The assumption here is that only 1 practitioner is returned from the server in the
Expand All @@ -388,14 +392,14 @@ constructor(
identifier.value == userInfo!!.keycloakUuid
) {
writePractitionerDetailsToShredPref(
careTeam = careTeam,
organization = organization,
location = location,
careTeamNames = careTeamNames,
organizationNames = organizationNames,
locationNames = locationNames,
fhirPractitionerDetails = practitionerDetails,
careTeams = careTeamIds,
organizations = organizationIds,
locations = locationIds,
locationHierarchies = locationHierarchies,
careTeamIds = careTeamIds,
organizationIds = organizationIds,
locationIds = locationIds,
locationHierarchies = locationHierarchies
)
}
}
Expand All @@ -408,49 +412,41 @@ constructor(
private fun writeUserInfo(
userInfo: UserInfo?,
) {
sharedPreferences.write(
key = SharedPreferenceKey.USER_INFO.name,
value = userInfo,
)
viewModelScope.launch {
if (userInfo != null) {
protoDataStore.writeUserInfo(userInfo)
}
}
}

private fun writePractitionerDetailsToShredPref(
careTeam: List<String>,
organization: List<String>,
location: List<String>,
fhirPractitionerDetails: PractitionerDetails,
careTeams: List<String>,
organizations: List<String>,
locations: List<String>,
careTeamIds: List<String>,
careTeamNames: List<String>,
locationIds: List<String>,
locationNames: List<String>,
locationHierarchies: List<LocationHierarchy>,
organizationIds: List<String>,
organizationNames: List<String>
) {
sharedPreferences.write(
key = SharedPreferenceKey.PRACTITIONER_ID.name,
value = fhirPractitionerDetails.fhirPractitionerDetails?.id,
)

// TODO: Store the whole object in proto datastore instead of sharedPreferences
sharedPreferences.write(
SharedPreferenceKey.PRACTITIONER_DETAILS.name,
fhirPractitionerDetails,
)
sharedPreferences.write(ResourceType.CareTeam.name, careTeams)
sharedPreferences.write(ResourceType.Organization.name, organizations)
sharedPreferences.write(ResourceType.Location.name, locations)
sharedPreferences.write(
SharedPreferenceKey.PRACTITIONER_LOCATION_HIERARCHIES.name,
locationHierarchies,
)
sharedPreferences.write(
key = SharedPreferenceKey.PRACTITIONER_LOCATION.name,
value = location.joinToString(separator = ""),
)
sharedPreferences.write(
key = SharedPreferenceKey.CARE_TEAM.name,
value = careTeam.joinToString(separator = ""),
)
sharedPreferences.write(
key = SharedPreferenceKey.ORGANIZATION.name,
value = organization.joinToString(separator = ""),
)

// Store the practitioner details components in the preferences datastore
viewModelScope.launch {
preferencesDataStore.write(PreferencesDataStore.PRACTITIONER_ID, fhirPractitionerDetails.fhirPractitionerDetails?.id as String)
preferencesDataStore.write(PreferencesDataStore.CARE_TEAM_IDS, careTeamIds as List<String>)
preferencesDataStore.write(PreferencesDataStore.CARE_TEAM_NAMES, careTeamNames)
preferencesDataStore.write(PreferencesDataStore.LOCATION_IDS, locationIds)
preferencesDataStore.write(PreferencesDataStore.LOCATION_NAMES, locationNames)
preferencesDataStore.write(PreferencesDataStore.ORGANIZATION_IDS, organizationIds)
preferencesDataStore.write(PreferencesDataStore.ORGANIZATION_NAMES, organizationNames)
preferencesDataStore.write(PreferencesDataStore.PRACTITIONER_LOCATION_HIERARCHIES, locationHierarchies)
}
}

fun downloadNowWorkflowConfigs() {
Expand Down

0 comments on commit 4c84ee1

Please sign in to comment.