Repo
This commit is contained in:
@@ -0,0 +1,83 @@
|
|||||||
|
package `in`.sminnovations.attendanceapp.data.dataStore
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.datastore.preferences.core.edit
|
||||||
|
import androidx.datastore.preferences.core.emptyPreferences
|
||||||
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import `in`.sminnovations.attendanceapp.screens.settings.data.EmployeeDataSettings
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import java.io.IOException
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
|
||||||
|
val Context.employeeDataStore by preferencesDataStore("employee_data")
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class EmployeeRepository @Inject constructor(
|
||||||
|
@ApplicationContext private val context: Context
|
||||||
|
) {
|
||||||
|
suspend fun saveEmployeeData(employee: Employee) {
|
||||||
|
context.employeeDataStore.edit { preferences ->
|
||||||
|
preferences[EmployeePreferencesKeys.ANDROID_ID] = employee.androidID
|
||||||
|
preferences[EmployeePreferencesKeys.DATE_OF_JOINING] = employee.dateOfJoining
|
||||||
|
preferences[EmployeePreferencesKeys.DATE_OF_LEAVING] = employee.dateOfLeaving
|
||||||
|
preferences[EmployeePreferencesKeys.DESIGNATION] = employee.designation
|
||||||
|
preferences[EmployeePreferencesKeys.EMPLOYEE_STATUS] = employee.employeeStatus
|
||||||
|
preferences[EmployeePreferencesKeys.ID] = employee.id
|
||||||
|
preferences[EmployeePreferencesKeys.NAME] = employee.name
|
||||||
|
preferences[EmployeePreferencesKeys.PASSWORD] = employee.password
|
||||||
|
preferences[EmployeePreferencesKeys.PHOTO] = employee.photo
|
||||||
|
preferences[EmployeePreferencesKeys.DATE_OF_BIRTH] = employee.dateOfBirth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun getEmployeeData(): Flow<Employee> {
|
||||||
|
return context.employeeDataStore.data
|
||||||
|
.catch { exception ->
|
||||||
|
// If an error occurs, emit an empty preferences object
|
||||||
|
if (exception is IOException) {
|
||||||
|
emit(emptyPreferences())
|
||||||
|
} else {
|
||||||
|
throw exception
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.map { preferences ->
|
||||||
|
Employee(
|
||||||
|
androidID = preferences[EmployeePreferencesKeys.ANDROID_ID] ?: "",
|
||||||
|
dateOfJoining = preferences[EmployeePreferencesKeys.DATE_OF_JOINING] ?: "",
|
||||||
|
dateOfLeaving = preferences[EmployeePreferencesKeys.DATE_OF_LEAVING] ?: "",
|
||||||
|
designation = preferences[EmployeePreferencesKeys.DESIGNATION] ?: "Android Developer",
|
||||||
|
employeeStatus = preferences[EmployeePreferencesKeys.EMPLOYEE_STATUS] ?: true,
|
||||||
|
id = preferences[EmployeePreferencesKeys.ID] ?: "",
|
||||||
|
name = preferences[EmployeePreferencesKeys.NAME] ?: "",
|
||||||
|
password = preferences[EmployeePreferencesKeys.PASSWORD] ?: "",
|
||||||
|
photo = preferences[EmployeePreferencesKeys.PHOTO] ?: "",
|
||||||
|
dateOfBirth = preferences[EmployeePreferencesKeys.DATE_OF_BIRTH] ?: ""
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getEmployeeDataSettings(): Flow<EmployeeDataSettings> {
|
||||||
|
return context.employeeDataStore.data
|
||||||
|
.catch { exception ->
|
||||||
|
if (exception is IOException) {
|
||||||
|
emit(emptyPreferences())
|
||||||
|
} else {
|
||||||
|
throw exception
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.map { preferences ->
|
||||||
|
EmployeeDataSettings(
|
||||||
|
name = preferences[EmployeePreferencesKeys.NAME] ?: "",
|
||||||
|
id = preferences[EmployeePreferencesKeys.ID] ?: "",
|
||||||
|
designation = preferences[EmployeePreferencesKeys.DESIGNATION] ?: ""
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user