diff --git a/app/src/main/java/in/sminnovations/attendanceapp/data/dataStore/EmployeeRepo.kt b/app/src/main/java/in/sminnovations/attendanceapp/data/dataStore/EmployeeRepo.kt new file mode 100644 index 0000000..d9afb3c --- /dev/null +++ b/app/src/main/java/in/sminnovations/attendanceapp/data/dataStore/EmployeeRepo.kt @@ -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 { + 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 { + 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] ?: "" + ) + } + } + +}