Created
August 29, 2022 12:42
-
-
Save cp-hardik-p/e5bbdd69aa06a39008e4ade26b2be948 to your computer and use it in GitHub Desktop.
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
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