Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/com/example/techexactly/model/dataclass/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.techexactly.model.dataclass

data class User(
val id: Long,
val name: String,
val username: String,
val email: String,
val address: Address,
val phone: String,
val website: String,
val company: Company,
)

data class Address(
val street: String,
val suite: String,
val city: String,
val zipcode: String,
val geo: Geo,
)

data class Geo(
val lat: String,
val lng: String,
)

data class Company(
val name: String,
val catchPhrase: String,
val bs: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.techexactly.model.network

import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

private const val BASE_URL = "https://jsonplaceholder.typicode.com/"

fun provideRetrofit(): Retrofit {
val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
val okHttpClient = OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()
val gson = GsonBuilder().setLenient().create()

return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}

fun provideUserApi(retrofit: Retrofit): UserApi {
return retrofit.create(UserApi::class.java)
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/example/techexactly/model/network/UserApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.techexactly.model.network

import com.example.techexactly.model.dataclass.User
import retrofit2.Response
import retrofit2.http.GET

interface UserApi {
@GET("/users")
suspend fun getUsers(): Response<List<User>>
}
Original file line number Diff line number Diff line change
@@ -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<Result<List<User>>> = 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
}