Skip to content

Instantly share code, notes, and snippets.

@cp-hardik-p
Created August 29, 2022 12:42
Show Gist options
  • Save cp-hardik-p/e5bbdd69aa06a39008e4ade26b2be948 to your computer and use it in GitHub Desktop.
Save cp-hardik-p/e5bbdd69aa06a39008e4ade26b2be948 to your computer and use it in GitHub Desktop.
private const val DATA_STORE_FILE_NAME = "emp_prefs.pb"
class EmployeeInfo(private val context: Context) {
private val Context.employeeProtoDataStore: DataStore<EmployeePreference> by dataStore(
fileName = DATA_STORE_FILE_NAME,
serializer = EmployeePreferencesSerializer
)
//Writing to a proto data store
suspend fun saveEmployeeInfo(eName: String, eDesignation: String) {
context.employeeProtoDataStore.updateData { employeeData ->
employeeData.toBuilder()
.setEmpName(eName)
.setEmpDesignation(eDesignation)
.build()
}
}
//Reading employee object from a proto data store
val employeeInfo: Flow<EmployeePreference> = context.employeeProtoDataStore.data
.map {
it
}
//Reading employee object property empName from a proto data store
val empName: Flow<String> = context.employeeProtoDataStore.data
.map {
it.empName
}
//Reading employee object property empDesignation from a proto data store
val empDesignation: Flow<String> = context.employeeProtoDataStore.data
.map {
it.empDesignation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment