diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ec19294..4946b99 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -78,4 +78,5 @@ dependencies { implementation(libs.retrofit) implementation(libs.retrofit.kotlin.serialization.converter) implementation(libs.kotlinx.serialization.json) + implementation(libs.converter.gson) } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/network/ApiFactory.kt b/app/src/main/java/org/sopt/and/data/network/ApiFactory.kt index 5efe441..b70d0e1 100644 --- a/app/src/main/java/org/sopt/and/data/network/ApiFactory.kt +++ b/app/src/main/java/org/sopt/and/data/network/ApiFactory.kt @@ -27,10 +27,4 @@ object ApiFactory { } inline fun create(): T = retrofit.create(T::class.java) -} - -//이 userService를, 서버를 붙이는 부분에서 사용해야 함. -//Viewmodel에서 이 userService에 접근하면 됨. -object ServicePool { - val userService = ApiFactory.create() } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/network/RetrofitInstance.kt b/app/src/main/java/org/sopt/and/data/network/RetrofitInstance.kt new file mode 100644 index 0000000..afb88c0 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/network/RetrofitInstance.kt @@ -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) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/presentation/loginScreen/LoginViewModel.kt b/app/src/main/java/org/sopt/and/presentation/loginScreen/LoginViewModel.kt index 9de63ba..e92b326 100644 --- a/app/src/main/java/org/sopt/and/presentation/loginScreen/LoginViewModel.kt +++ b/app/src/main/java/org/sopt/and/presentation/loginScreen/LoginViewModel.kt @@ -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 diff --git a/app/src/main/java/org/sopt/and/presentation/mypageScreen/MypageViewModel.kt b/app/src/main/java/org/sopt/and/presentation/mypageScreen/MypageViewModel.kt index 8f9154d..b3cc267 100644 --- a/app/src/main/java/org/sopt/and/presentation/mypageScreen/MypageViewModel.kt +++ b/app/src/main/java/org/sopt/and/presentation/mypageScreen/MypageViewModel.kt @@ -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 @@ -51,37 +48,4 @@ class MypageViewModel( return "취미 가져오기 에러" } } - - - -// fun getUserHobby(request: RequestGetUserHobbyDto, userViewModel: UserViewModel) { -// val TAG = "UserService" -// -// userService.getMyHobby(request).enqueue(object : Callback { -// override fun onResponse( -// call: Call, -// response: Response -// ) { -// 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, t: Throwable) { -// // 네트워크 오류 처리 -// Log.e("UserService", "API 호출 실패: ${t.message}") -// } -// }) -// -// } } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/presentation/signupScreen/SignUpViewModel.kt b/app/src/main/java/org/sopt/and/presentation/signupScreen/SignUpViewModel.kt index f757962..b0adaa5 100644 --- a/app/src/main/java/org/sopt/and/presentation/signupScreen/SignUpViewModel.kt +++ b/app/src/main/java/org/sopt/and/presentation/signupScreen/SignUpViewModel.kt @@ -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 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b33b409..64bd24c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" @@ -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" }