Skip to content
Open
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
16 changes: 16 additions & 0 deletions week7/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import java.util.Properties
import kotlin.apply

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.compose)
alias(libs.plugins.kotlin.serialization)
}

val properties = Properties().apply{
load(project.rootProject.file("local.properties").inputStream())
}

android {
namespace = "com.example.nike"
compileSdk {
Expand All @@ -20,6 +27,8 @@ android {
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "BASE_URL", properties["base.url"].toString())
buildConfigField("String", "API_KEY", "\"${properties["api.key"]}\"")
}

buildTypes {
Expand All @@ -37,6 +46,7 @@ android {
}
buildFeatures {
compose = true
buildConfig = true
}
}

Expand All @@ -59,4 +69,10 @@ dependencies {
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.ui.test.manifest)
implementation("io.coil-kt:coil-compose:2.6.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
implementation("com.squareup.retrofit2:converter-kotlinx-serialization:2.11.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0")
}
2 changes: 2 additions & 0 deletions week7/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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.nike.data.remote

import com.example.nike.BuildConfig
import com.example.nike.data.remote.api.ProfileApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.kotlinx.serialization.asConverterFactory

object RetrofitClient {
private const val BASE_URL = BuildConfig.BASE_URL
private val json = Json { ignoreUnknownKeys = true }

private val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}

private val okHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("x-api-key", BuildConfig.API_KEY)
.build()
chain.proceed(request)
}
.build()

private val instance: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(json.asConverterFactory("application/json; charset=UTF-8".toMediaType()))
.build()

fun <T> create(service: Class<T>): T = instance.create(service)

val profileApi: ProfileApi by lazy {
create(ProfileApi::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.nike.data.remote.api

import com.example.nike.data.remote.dto.response.UserResponse
import retrofit2.http.GET
import retrofit2.http.Query

interface ProfileApi {
@GET("users")
suspend fun getUsers(
@Query("page") page: Int = 1,
): UserResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.nike.data.remote.dto.response

import com.example.nike.domain.model.profile.User
import kotlinx.serialization.Serializable

@Serializable
data class UserResponse(
val data: List<User>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.nike.data.remote.repository

import com.example.nike.data.remote.RetrofitClient
import com.example.nike.domain.model.profile.User

class ProfileRepository {
private val profileApi = RetrofitClient.profileApi

suspend fun getUsers(): List<User> {
return profileApi.getUsers().data
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.nike.domain.model.profile

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class User(
@SerialName("id")
val id: Int,
@SerialName("email")
val email: String,
@SerialName("first_name")
val firstName: String,
@SerialName("last_name")
val lastName: String,
@SerialName("avatar")
val avatar: String,
)
Loading