Skip to content

Commit

Permalink
fix #week4 : userService Retrofit으로 빼기
Browse files Browse the repository at this point in the history
  • Loading branch information
tunaunnie committed Dec 3, 2024
1 parent 26c22c1 commit 9de52cb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 62 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ dependencies {
implementation(libs.retrofit)
implementation(libs.retrofit.kotlin.serialization.converter)
implementation(libs.kotlinx.serialization.json)
implementation(libs.converter.gson)
}
6 changes: 0 additions & 6 deletions app/src/main/java/org/sopt/and/data/network/ApiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,4 @@ object ApiFactory {
}

inline fun <reified T> create(): T = retrofit.create(T::class.java)
}

//이 userService를, 서버를 붙이는 부분에서 사용해야 함.
//Viewmodel에서 이 userService에 접근하면 됨.
object ServicePool {
val userService = ApiFactory.create<UserService>()
}
20 changes: 20 additions & 0 deletions app/src/main/java/org/sopt/and/data/network/RetrofitInstance.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.sopt.and.data.network

import org.sopt.and.BuildConfig
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitInstance {
private val BASE_URL = BuildConfig.BASE_URL

private val retrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

val userService: UserService by lazy {
retrofit.create(UserService::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@ import org.sopt.and.data.datalocal.datasource.UserInfoLocalDataSource
import org.sopt.and.domain.User
import org.sopt.and.data.dto.login.RequestGetUserDto
import org.sopt.and.data.dto.login.ResponseGetUserFailedDto
import org.sopt.and.data.dto.signup.RequestCreateUserDto
import org.sopt.and.data.dto.signup.ResponseCreateUserFailedDto
import org.sopt.and.data.network.ServicePool
import org.sopt.and.presentation.main.UserViewModel
import org.sopt.and.presentation.mypageScreen.MypageViewModel
import org.sopt.and.data.network.RetrofitInstance.userService
import org.sopt.and.presentation.signupScreen.StringInputValidCheck
import org.sopt.and.presentation.signupScreen.PasswordValidCheck
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LoginViewModel(
private val userInfoLocalDataSource: UserInfoLocalDataSource
) : ViewModel() {

//로그인 요청을 서버로 보내기 위함
private val userService by lazy { ServicePool.userService }

private val _user = MutableStateFlow(User())
val user: StateFlow<User> = _user

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.sopt.and.data.datalocal.datasource.UserInfoLocalDataSource
import org.sopt.and.data.network.ServicePool
import org.sopt.and.data.network.RetrofitInstance.userService
import org.sopt.and.domain.User
import org.sopt.and.presentation.main.UserViewModel


class MypageViewModel(
private val userInfoLocalDataSource: UserInfoLocalDataSource
) : ViewModel() {

private val userService by lazy { ServicePool.userService }

private val _user = MutableStateFlow(User())
val user: StateFlow<User> = _user

Expand Down Expand Up @@ -51,37 +48,4 @@ class MypageViewModel(
return "취미 가져오기 에러"
}
}



// fun getUserHobby(request: RequestGetUserHobbyDto, userViewModel: UserViewModel) {
// val TAG = "UserService"
//
// userService.getMyHobby(request).enqueue(object : Callback<ResponseGetUserHobbyWrapperDto> {
// override fun onResponse(
// call: Call<ResponseGetUserHobbyWrapperDto>,
// response: Response<ResponseGetUserHobbyWrapperDto>
// ) {
// if (response.isSuccessful) {
// // 성공 응답 처리
// val body = response.body()
// if (body != null) {
// val userHobby = body.success.result?.userHobby ?: "default hobby"
// Log.d("UserService", "유저 취미: $userHobby")
// userViewModel.setHobby(userHobby)
// } else {
// Log.e("UserService", "응답 본문 없음")
// }
// } else {
// // 실패 응답 처리
// }
// }
//
// override fun onFailure(call: Call<ResponseGetUserHobbyWrapperDto>, t: Throwable) {
// // 네트워크 오류 처리
// Log.e("UserService", "API 호출 실패: ${t.message}")
// }
// })
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ import kotlinx.serialization.json.Json
import org.sopt.and.domain.User
import org.sopt.and.data.dto.signup.RequestCreateUserDto
import org.sopt.and.data.dto.signup.ResponseCreateUserFailedDto
import org.sopt.and.data.dto.signup.ResponseCreateUserSuccessDto
import org.sopt.and.data.network.ServicePool
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import org.sopt.and.data.network.RetrofitInstance.userService

class SignUpViewModel : ViewModel() {

//회원가입 성공 시 서버로 create 요청 보내기 위함
private val userService by lazy { ServicePool.userService }

private val _user = MutableStateFlow(User())
val user: StateFlow<User> = _user

Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
agp = "8.7.0"
kotlin = "2.0.0"
coreKtx = "1.10.1"
converterGson = "2.9.0"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
Expand Down Expand Up @@ -36,6 +37,7 @@ okhttp-logging-interceptor = { group = "com.squareup.okhttp3", name = "logging-i
retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
retrofit-kotlin-serialization-converter = { group = "com.jakewharton.retrofit", name = "retrofit2-kotlinx-serialization-converter", version.ref = "retrofitKotlinSerializationConverter" }
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
converter-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "converterGson" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down

0 comments on commit 9de52cb

Please sign in to comment.