diff --git a/app/build.gradle b/app/build.gradle index e5c99b6..2638b1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,16 +87,13 @@ dependencies { implementation 'com.squareup.okhttp3:okhttp' implementation 'com.squareup.okhttp3:logging-interceptor' - // dagger hilt - implementation "com.google.dagger:hilt-android:2.51" - kapt "com.google.dagger:hilt-compiler:2.48.1" - // viewmodel_ktx - implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0' + implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.8.1' // coroutine implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3' - implementation 'androidx.lifecycle:lifecycle-runtime-compose:2.8.0' + implementation 'androidx.lifecycle:lifecycle-runtime-compose:2.8.1' implementation 'androidx.compose.runtime:runtime-livedata:1.6.7' + } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/data/AuthRepositoryImpl.kt b/app/src/main/java/com/sopt/now/compose/data/AuthRepositoryImpl.kt new file mode 100644 index 0000000..40339fd --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/data/AuthRepositoryImpl.kt @@ -0,0 +1,20 @@ +package com.sopt.now.compose.data + +import com.sopt.now.compose.datasource.AuthService +import com.sopt.now.compose.model.login.RequestLoginDto +import com.sopt.now.compose.model.signup.RequestSignUpDto +import com.sopt.now.compose.repository.AuthRepository +import com.sopt.now.compose.utils.BaseResponse +import retrofit2.Response + +class AuthRepositoryImpl( + private val authService: AuthService +) : AuthRepository { + override suspend fun loginUser(data: RequestLoginDto): Response> { + return authService.login(data) + } + + override suspend fun signUp(data: RequestSignUpDto): Response> { + return authService.signUp(data) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/data/LoginViewModelFactory.kt b/app/src/main/java/com/sopt/now/compose/data/LoginViewModelFactory.kt new file mode 100644 index 0000000..36820fc --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/data/LoginViewModelFactory.kt @@ -0,0 +1,17 @@ +package com.sopt.now.compose.data + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import com.sopt.now.compose.presentation.auth.login.LoginViewModel +import com.sopt.now.compose.utils.ServicePool + +class LoginViewModelFactory: ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + if (modelClass.isAssignableFrom(LoginViewModel::class.java)) { + val repository = AuthRepositoryImpl(ServicePool.loginService) + return LoginViewModel(repository) as T + } else { + throw IllegalArgumentException("fail ${modelClass.name}") + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/data/SignUpViewModelFactory.kt b/app/src/main/java/com/sopt/now/compose/data/SignUpViewModelFactory.kt new file mode 100644 index 0000000..bdf28a5 --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/data/SignUpViewModelFactory.kt @@ -0,0 +1,17 @@ +package com.sopt.now.compose.data + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import com.sopt.now.compose.presentation.auth.signup.SignUpViewModel +import com.sopt.now.compose.utils.ServicePool + +class SignUpViewModelFactory : ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + if (modelClass.isAssignableFrom(SignUpViewModel::class.java)) { + val repository = AuthRepositoryImpl(ServicePool.authService) + return SignUpViewModel(repository) as T + } else { + throw IllegalArgumentException("fail ${modelClass.name}") + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginActivity.kt b/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginActivity.kt index 86d0f25..6d6ecf0 100644 --- a/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginActivity.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginActivity.kt @@ -19,7 +19,6 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -36,6 +35,7 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.sopt.now.compose.ApplicationClass.SharedPreferences.sSharedPreferences import com.sopt.now.compose.R +import com.sopt.now.compose.data.LoginViewModelFactory import com.sopt.now.compose.model.login.RequestLoginDto import com.sopt.now.compose.presentation.auth.signup.SignUpActivity import com.sopt.now.compose.presentation.main.MainActivity @@ -46,7 +46,7 @@ import com.sopt.now.compose.utils.showToast class LoginActivity : ComponentActivity() { - private val loginViewModel by viewModels() + private val loginViewModel: LoginViewModel by viewModels { LoginViewModelFactory() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -79,7 +79,7 @@ class LoginActivity : ComponentActivity() { when(state) { is UiState.FAILURE -> showToast((state as UiState.FAILURE).errorMessage) UiState.LOADING -> { } - is UiState.SUCCESS<*> -> { + is UiState.SUCCESS -> { showToast(sSharedPreferences.getString(MEMBER_ID, null) ?: "") navigateToMain() } diff --git a/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginViewModel.kt b/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginViewModel.kt index 9fbedd2..e795dca 100644 --- a/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginViewModel.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/auth/login/LoginViewModel.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.sopt.now.compose.ApplicationClass.SharedPreferences.editor import com.sopt.now.compose.model.login.RequestLoginDto +import com.sopt.now.compose.repository.AuthRepository import com.sopt.now.compose.utils.Constants.Companion.MEMBER_ID import com.sopt.now.compose.utils.NetworkUtil import com.sopt.now.compose.utils.ServicePool.loginService @@ -13,15 +14,17 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch -class LoginViewModel : ViewModel() { - private val _state = MutableStateFlow(UiState.LOADING) +class LoginViewModel( + private val authRepository: AuthRepository +) : ViewModel() { + private val _state = MutableStateFlow>(UiState.LOADING) val state = _state.asStateFlow() fun postLogin(data: RequestLoginDto) { viewModelScope.launch(Dispatchers.IO) { _state.value = UiState.LOADING runCatching { - loginService.login(data) + authRepository.loginUser(data) }.onSuccess { if (it.isSuccessful) { editor.putString(MEMBER_ID, it.headers()["location"]) diff --git a/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpActivity.kt b/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpActivity.kt index fe7f75c..74e17ef 100644 --- a/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpActivity.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpActivity.kt @@ -35,6 +35,7 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.sopt.now.compose.ApplicationClass import com.sopt.now.compose.R +import com.sopt.now.compose.data.SignUpViewModelFactory import com.sopt.now.compose.model.signup.RequestSignUpDto import com.sopt.now.compose.presentation.auth.login.LoginActivity import com.sopt.now.compose.ui.theme.NOWSOPTAndroidTheme @@ -43,7 +44,7 @@ import com.sopt.now.compose.utils.UiState import com.sopt.now.compose.utils.showToast class SignUpActivity : ComponentActivity() { - private val signUpViewModel by viewModels() + private val signUpViewModel: SignUpViewModel by viewModels { SignUpViewModelFactory() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -67,10 +68,10 @@ class SignUpActivity : ComponentActivity() { fun SignUpPage() { val state by signUpViewModel.state.collectAsState() - when(state) { + when (state) { is UiState.FAILURE -> showToast((state as UiState.FAILURE).errorMessage) - UiState.LOADING -> { } - is UiState.SUCCESS<*> -> { + UiState.LOADING -> {} + is UiState.SUCCESS -> { showToast(getString(R.string.success_signup)) navigateToLogin() } diff --git a/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpViewModel.kt b/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpViewModel.kt index dba4851..4442754 100644 --- a/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpViewModel.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/auth/signup/SignUpViewModel.kt @@ -3,6 +3,7 @@ package com.sopt.now.compose.presentation.auth.signup import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.sopt.now.compose.model.signup.RequestSignUpDto +import com.sopt.now.compose.repository.AuthRepository import com.sopt.now.compose.utils.NetworkUtil import com.sopt.now.compose.utils.ServicePool.authService import com.sopt.now.compose.utils.UiState @@ -11,15 +12,17 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch -class SignUpViewModel : ViewModel() { - private val _state = MutableStateFlow(UiState.LOADING) +class SignUpViewModel( + private val authRepository: AuthRepository +) : ViewModel() { + private val _state = MutableStateFlow>(UiState.LOADING) val state = _state.asStateFlow() fun signUp(data: RequestSignUpDto) { viewModelScope.launch(Dispatchers.IO) { _state.value = UiState.LOADING runCatching { - authService.signUp(data) + authRepository.signUp(data) }.onSuccess { if (it.isSuccessful) _state.value = UiState.SUCCESS() else { diff --git a/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageScreen.kt b/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageScreen.kt index 99bdc6d..8287012 100644 --- a/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageScreen.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageScreen.kt @@ -39,7 +39,7 @@ fun MyPageScreen( Toast.makeText(context, (state as UiState.FAILURE).errorMessage, Toast.LENGTH_SHORT).show() } UiState.LOADING -> { } - is UiState.SUCCESS<*> -> { + is UiState.SUCCESS -> { MyPage((state as UiState.SUCCESS<*>).data as UserInfo) } } diff --git a/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageViewModel.kt b/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageViewModel.kt index 3e7be4a..6addd8c 100644 --- a/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageViewModel.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/main/mypage/MyPageViewModel.kt @@ -2,6 +2,7 @@ package com.sopt.now.compose.presentation.main.mypage import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.sopt.now.compose.model.userinfo.UserInfo import com.sopt.now.compose.utils.NetworkUtil import com.sopt.now.compose.utils.ServicePool.infoService import com.sopt.now.compose.utils.UiState @@ -10,7 +11,7 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch class MyPageViewModel : ViewModel() { - private val _state = MutableStateFlow(UiState.LOADING) + private val _state = MutableStateFlow>(UiState.LOADING) val state = _state.asStateFlow() fun getUserInfo() { diff --git a/app/src/main/java/com/sopt/now/compose/repository/AuthRepository.kt b/app/src/main/java/com/sopt/now/compose/repository/AuthRepository.kt new file mode 100644 index 0000000..b2e49e9 --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/repository/AuthRepository.kt @@ -0,0 +1,12 @@ +package com.sopt.now.compose.repository + +import com.sopt.now.compose.model.login.RequestLoginDto +import com.sopt.now.compose.model.signup.RequestSignUpDto +import com.sopt.now.compose.utils.BaseResponse +import retrofit2.Response + +interface AuthRepository { + suspend fun loginUser(data: RequestLoginDto): Response> + + suspend fun signUp(data: RequestSignUpDto): Response> +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/utils/UiState.kt b/app/src/main/java/com/sopt/now/compose/utils/UiState.kt index 889dd88..2abf987 100644 --- a/app/src/main/java/com/sopt/now/compose/utils/UiState.kt +++ b/app/src/main/java/com/sopt/now/compose/utils/UiState.kt @@ -1,12 +1,12 @@ package com.sopt.now.compose.utils -sealed class UiState { - data object LOADING : UiState() +sealed class UiState { + data object LOADING : UiState() data class SUCCESS( val data: T? = null - ) : UiState() + ) : UiState() data class FAILURE( val errorMessage: String - ) : UiState() + ) : UiState() } \ No newline at end of file