diff --git a/.idea/misc.xml b/.idea/misc.xml index 3aadec2..15f3e07 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -605,7 +605,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a51f988..2b2196f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + > +} \ No newline at end of file diff --git a/app/src/main/java/com/example/techexactly/model/repository/UserRepository.kt b/app/src/main/java/com/example/techexactly/model/repository/UserRepository.kt new file mode 100644 index 0000000..adf3ae1 --- /dev/null +++ b/app/src/main/java/com/example/techexactly/model/repository/UserRepository.kt @@ -0,0 +1,28 @@ +package com.example.techexactly.model.repository + +import com.example.techexactly.model.dataclass.User +import com.example.techexactly.model.network.UserApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.flowOn + +class UserRepository(private val userApi: UserApi) { + fun getUsers(): Flow>> = flow { + try { + // Switch to Dispatchers.IO for the network call. + val response = userApi.getUsers() + if (response.isSuccessful) { + // Handle success case. + val users = response.body() ?: emptyList() + emit(Result.success(users)) + } else { + // Handle HTTP error cases. + emit(Result.failure(Exception("Error: ${response.code()} ${response.message()}"))) + } + } catch (e: Exception) { + // Handle any exception during the network call. + emit(Result.failure(Exception("Error fetching users: ${e.message}"))) + } + }.flowOn(Dispatchers.IO) // Use flowOn to change the context of the flow to Dispatchers.IO +} \ No newline at end of file