Skip to content
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

Feat/week compose07 #20

Open
wants to merge 2 commits into
base: develop-compose
Choose a base branch
from
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
9 changes: 3 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}
20 changes: 20 additions & 0 deletions app/src/main/java/com/sopt/now/compose/data/AuthRepositoryImpl.kt
Original file line number Diff line number Diff line change
@@ -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<BaseResponse<Unit>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override suspend fun loginUser(data: RequestLoginDto): Response<BaseResponse<Unit>> {
override suspend fun loginUser(data: RequestLoginDto): Response<BaseResponse<Unit>> =runCatching {
authService.login(data)
}

이처럼 ν•˜λ©΄ runCatch에 ν•΄λ‹Ήν•˜λŠ” λ‘œμ§μ„ 여기에 넣을 수 도 μžˆμ–΄μš”!

return authService.login(data)
}

override suspend fun signUp(data: RequestSignUpDto): Response<BaseResponse<Unit>> {
return authService.signUp(data)
}
}
Original file line number Diff line number Diff line change
@@ -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 <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) {
val repository = AuthRepositoryImpl(ServicePool.loginService)
return LoginViewModel(repository) as T
} else {
throw IllegalArgumentException("fail ${modelClass.name}")
}
}
}
Comment on lines +8 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 viewdmodel λ³„λ‘œ factoryλ₯Ό λ”°λ‘œ λ§Œλ“€ ν•„μš”λŠ” μ—†μ–΄ λ³΄μž…λ‹ˆλ‹€! λΆˆν•„μš”ν•œ μ½”λ“œκ°€ μ¦κ°€ν•˜λŠ” 것 κ°™μ•„μš”
또 ν•˜λ‚˜μ˜ 일반적인 factory클래슀λ₯Ό 톡해 νŒ©ν† λ¦¬ λ‘œμ§μ„ 쀑앙집쀑화 ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ν™•μž₯성도 더 μ’‹κ΅¬μš”
제 컴포즈 prμ—μ„œ viewmodel factoryλ₯Ό ν•˜λ‚˜λ‘œ μ‚¬μš©ν•˜λŠ” μ½”λ“œ μ°Έκ³ ν•΄λ³΄μ‹œλ©΄ 쒋을것 κ°™μ•„μš”~!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else ifλ‚˜ when절둜 λΆ„κΈ°μ²˜λ¦¬λ§Œ ν•˜λ©΄ λ©λ‹ˆλ‹€

Original file line number Diff line number Diff line change
@@ -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 <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(SignUpViewModel::class.java)) {
val repository = AuthRepositoryImpl(ServicePool.authService)
return SignUpViewModel(repository) as T
} else {
throw IllegalArgumentException("fail ${modelClass.name}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -46,7 +46,7 @@ import com.sopt.now.compose.utils.showToast


class LoginActivity : ComponentActivity() {
private val loginViewModel by viewModels<LoginViewModel>()
private val loginViewModel: LoginViewModel by viewModels { LoginViewModelFactory() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>(UiState.LOADING)
class LoginViewModel(
private val authRepository: AuthRepository
) : ViewModel() {
private val _state = MutableStateFlow<UiState<Unit>>(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"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<SignUpViewModel>()
private val signUpViewModel: SignUpViewModel by viewModels { SignUpViewModelFactory() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>(UiState.LOADING)
class SignUpViewModel(
private val authRepository: AuthRepository
) : ViewModel() {
private val _state = MutableStateFlow<UiState<Unit>>(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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCatch ꡬ문을 Repositoryimpl λ‚΄μ—μ„œ μ²˜λ¦¬ν•  수 μžˆμ–΄μš”!
근데 μ—¬κΈ°μ„œ μ²˜λ¦¬ν•˜λŠ”κ²Œ 더 μ˜³μ€ 길인지 λͺ°λΌμ„œ
rucCatchλ₯Ό Repositoryimplμ—μ„œ μ‚¬μš©ν•  수 μžˆλ‹€ μ •λ„λ§Œ λ‚¨κ²¨λ‘˜κ²Œμš”

}.onSuccess {
if (it.isSuccessful) _state.value = UiState.SUCCESS<Unit>()
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -10,7 +11,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class MyPageViewModel : ViewModel() {
private val _state = MutableStateFlow<UiState>(UiState.LOADING)
private val _state = MutableStateFlow<UiState<UserInfo>>(UiState.LOADING)
val state = _state.asStateFlow()

fun getUserInfo() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν•΄λ‹Ή repsitoryλŠ” dataλ ˆμ΄μ–΄λ‘œ κ°€λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€!
λ˜ν•œ returnνƒ€μž…μœΌλ‘œ Responseνƒ€μž…μ΄ μ•„λ‹Œ Resultνƒ€μž…μ„ μ‚¬μš©ν•΄μ£Όμ„Έμš”!

Response νƒ€μž…μ„ μ‚¬μš©ν•˜λ©΄ λ„€νŠΈμ›Œν¬ 응닡을 직접 μ²˜λ¦¬ν•΄μ•Ό ν•˜λ©°, 성곡과 μ‹€νŒ¨μ— λŒ€ν•œ λ‘œμ§μ„ λͺ…μ‹œμ μœΌλ‘œ μž‘μ„±ν•΄μ•Ό ν•©λ‹ˆλ‹€. 또 retroiftμ΄λΌλŠ” λΌμ΄λΈŒλŸ¬λ¦¬μ— 의쑴적이 단점이 μžˆμ–΄μš©

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κΈ°νšκ²½μ„ λ–„ μ•Œλ €μ£Όμ…¨λ˜ λΆ€λΆ„μ΄λ„€μš” λ‹€μ‹œν•œλ²ˆ λ¦¬λ§ˆμΈλ“œλ„ 되고 많이 λ°°μ›Œκ°€μš”


interface AuthRepository {
suspend fun loginUser(data: RequestLoginDto): Response<BaseResponse<Unit>>

suspend fun signUp(data: RequestSignUpDto): Response<BaseResponse<Unit>>
}
8 changes: 4 additions & 4 deletions app/src/main/java/com/sopt/now/compose/utils/UiState.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.sopt.now.compose.utils

sealed class UiState {
data object LOADING : UiState()
sealed class UiState<out T> {
data object LOADING : UiState<Nothing>()
data class SUCCESS<T>(
val data: T? = null
) : UiState()
) : UiState<T>()

data class FAILURE(
val errorMessage: String
) : UiState()
) : UiState<Nothing>()
}
Comment on lines +3 to 12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goood!!
seale interface도 ν•œλ²ˆ μ‚¬μš©ν•΄λ³΄μ„Έμš”γ…Žγ…Ž