-
Notifications
You must be signed in to change notification settings - Fork 0
Week4 사람 리스트 불러오는 api 붙이기 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package org.sopt.do_sopt_compose.data | ||
|
|
||
| import android.util.Log | ||
| import okhttp3.Interceptor | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.logging.HttpLoggingInterceptor | ||
| import org.sopt.do_sopt_compose.BuildConfig.BASE_URL | ||
| import org.sopt.do_sopt_compose.data.service.PeopleApiService | ||
| import retrofit2.Retrofit | ||
| import retrofit2.converter.gson.GsonConverterFactory | ||
|
|
||
| object ApiFactory { | ||
|
|
||
| private fun getLogOkHttpClient(): Interceptor { | ||
| val loggingInterceptor = HttpLoggingInterceptor { message -> | ||
| Log.d("Retrofit2", "CONNECTION INFO -> $message") | ||
| } | ||
| loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | ||
| return loggingInterceptor | ||
| } | ||
|
|
||
| private val okHttpClient = OkHttpClient.Builder() | ||
| .addInterceptor(getLogOkHttpClient()) | ||
| .build() | ||
|
|
||
| fun retrofit(url: String): Retrofit = | ||
| Retrofit.Builder() | ||
| .baseUrl(url) | ||
| .client(okHttpClient) | ||
| //.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
| .addConverterFactory(GsonConverterFactory.create()) | ||
| .build() | ||
|
|
||
| inline fun <reified T, B> create(url: B): T = | ||
| retrofit(url.toString()).create(T::class.java) | ||
| } | ||
|
|
||
| object ServicePool { | ||
| val apiService = ApiFactory.create<PeopleApiService, String>(BASE_URL) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.sopt.do_sopt_compose.data.dto | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
|
|
||
| @Serializable | ||
| data class ResponsePeopleDto( | ||
| @SerialName("page") | ||
| val page: Int, | ||
| @SerialName("per_page") | ||
| val perPage: Int, | ||
| @SerialName("total") | ||
| val total: Int, | ||
| @SerialName("total_pages") | ||
| val totalPages: Int, | ||
| @SerialName("data") | ||
| val data: List<User>, | ||
| @SerialName("support") | ||
| val support: Support, | ||
| ) | ||
|
|
||
| @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, | ||
| ) | ||
|
|
||
| @Serializable | ||
| data class Support( | ||
| @SerialName("url") | ||
| val url: String, | ||
| @SerialName("text") | ||
| val text: String, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.sopt.do_sopt_compose.data.repository | ||
|
|
||
| import org.sopt.do_sopt_compose.data.ServicePool | ||
|
|
||
| class PeopleRepository { | ||
|
|
||
| private val apiService = ServicePool.apiService | ||
| suspend fun getUsers(page: Int) = apiService.getUsers(page, 10) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package org.sopt.do_sopt_compose.data.service | ||
|
|
||
| import org.sopt.do_sopt_compose.data.dto.ResponsePeopleDto | ||
| import retrofit2.http.GET | ||
| import retrofit2.http.Query | ||
|
|
||
| interface PeopleApiService { | ||
| @GET("users") | ||
| suspend fun getUsers( | ||
| @Query("page") page: Int, | ||
| @Query("per_page") perPage: Int, | ||
| ): ResponsePeopleDto | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package org.sopt.do_sopt_compose.ui.pages.doandroid | ||
|
|
||
| import androidx.compose.foundation.Image | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.foundation.lazy.LazyColumn | ||
| import androidx.compose.foundation.lazy.items | ||
| import androidx.compose.material3.Card | ||
| import androidx.compose.material3.Scaffold | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||
| import androidx.navigation.NavController | ||
| import coil.compose.rememberAsyncImagePainter | ||
| import coil.request.ImageRequest | ||
| import coil.size.Size | ||
| import org.sopt.do_sopt_compose.R | ||
| import org.sopt.do_sopt_compose.data.dto.User | ||
| import org.sopt.do_sopt_compose.navigation.BottomNavigation | ||
|
|
||
| @Composable | ||
| fun DoAndroidScreen( | ||
| navController: NavController, | ||
| ) { | ||
| val viewModel: DoAndroidViewModel = viewModel() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개인적으로 뷰모델은 파라미터로 두는 것이 여러가지로 이점이 있다고 생각합니다이 :) |
||
| val usersState = viewModel.users.collectAsState() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lifecycle-runtime 의존성을 추가해서 dependencies {
implementation "androidx.lifecycle:lifecycle-runtime-compose:[version]"
}https://developer.android.com/jetpack/androidx/releases/lifecycle |
||
|
|
||
| Scaffold( | ||
| bottomBar = { BottomNavigation(navController = navController) }, | ||
| content = { | ||
| Box( | ||
| modifier = Modifier | ||
| .fillMaxSize() | ||
| .padding(it), | ||
| ) { | ||
| UserList(users = usersState.value) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| fun UserList(users: List<User>) { | ||
| LazyColumn { | ||
| items(users) { user -> | ||
| UserItem(user) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| fun UserItem(user: User) { | ||
| Card( | ||
| modifier = Modifier | ||
| .fillMaxWidth() | ||
| .padding(8.dp) | ||
| ) { | ||
| Row( | ||
| modifier = Modifier.padding(8.dp) | ||
| ) { | ||
| // 프로필 이미지 표시 | ||
| Image( | ||
| painter = | ||
| rememberAsyncImagePainter( | ||
| ImageRequest.Builder(LocalContext.current).data(data = user.avatar) | ||
| .size(Size.ORIGINAL) | ||
| .apply(block = fun ImageRequest.Builder.() { | ||
| placeholder(R.drawable.profile_background) | ||
| }).build() | ||
| ), | ||
| contentDescription = "Profile Picture", | ||
| modifier = Modifier | ||
| .size(64.dp) | ||
| .padding(end = 16.dp) | ||
| ) | ||
| Column { | ||
| Text( | ||
| text = "ID: ${user.id}", | ||
| modifier = Modifier.padding(bottom = 4.dp) | ||
| ) | ||
| Text( | ||
| text = "Name: ${user.firstName} ${user.lastName}", | ||
| modifier = Modifier.padding(bottom = 4.dp) | ||
| ) | ||
| Text( | ||
| text = "Email: ${user.email}", | ||
| modifier = Modifier.padding(bottom = 4.dp) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package org.sopt.do_sopt_compose.ui.pages.doandroid | ||
|
|
||
| sealed class DoAndroidSideEffect { | ||
| data class ToastMessage(val message: String) : DoAndroidSideEffect() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 데이터 클래스가 프로퍼티로 message를 가지고 있어서 ToastMessage라는 클래스 네이밍은 함수형 프로그래밍에 있어서 조금 어색하다고 생각해요!! 예를 들어서,
그래서 다른 클래스 네이밍을 고민해보는 건 어떠신가요!? 👻👻👻 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.sopt.do_sopt_compose.ui.pages.doandroid | ||
|
|
||
| import org.sopt.do_sopt_compose.ui.UiStatus | ||
|
|
||
| data class DoAndroidState( | ||
| val uiStatus: UiStatus = UiStatus.Loading, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converter를 Gson으로 변경하신 이유가 있을까요!!?