-
Notifications
You must be signed in to change notification settings - Fork 1
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 xml 06 #11
base: develop-xml
Are you sure you want to change the base?
Feat/week xml 06 #11
Conversation
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.
μΈμ μ΄λ κ² μ±μ₯νμ
¨μ£ ?
μ§μ§ μνμλ€μγ·γ·
class AuthRepoImpl( | ||
private val authService: AuthService, | ||
) : AuthRepository { | ||
override suspend fun logIn(authData: AuthData): Result<Response<Unit>> = runCatching { | ||
authService.logIn( | ||
request = RequestLogInDto( | ||
authenticationId = authData.id, | ||
password = authData.pw | ||
) | ||
) | ||
} | ||
override suspend fun signUp(authData: AuthData): Result<Response<Unit>> = runCatching { | ||
authService.signUp( | ||
RequestSignUpDto( | ||
authenticationId = authData.id, | ||
password = authData.pw, | ||
nickname = authData.name ?: "", | ||
phone = authData.phone ?: "" | ||
) | ||
) | ||
} | ||
|
||
|
||
} |
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.
μ μ§μ§ νΌ λ―Έμ³€λ€μγ·γ·
val loggingInterceptor = HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} |
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.
loggingInterceptor λ¬μλ³΄μ ¨μΌλ©΄ HeaderInterceptorλ ν΄λ³΄μλκ±° μΆμ²λ립λλ€γ γ
interface AuthRepository { | ||
suspend fun logIn(authData: AuthData): Result<Response<Unit>> | ||
suspend fun signUp(authData: AuthData): Result<Response<Unit>> | ||
} |
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.
μλ μ΄μ¬λ λμΌ?
|
||
import android.provider.ContactsContract.CommonDataKinds.Phone | ||
|
||
data class AuthData( |
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.
data class AuthData( | |
data class AuthEntity( |
domain μμμ data classλ λ³΄ν΅ Entityλ‘ λ€μ΄λ° μ§μ΄μ
κ° μμλ³ λ€μ΄λ°(λ³΄ν΅ μ΄λ κ² μ§μ΄μ)
- ui : model
- domain : entity
- data : dto
class LoginViewModel( | ||
private val authRepository: AuthRepository | ||
) : ViewModel() { | ||
val loginResult: MutableLiveData<Boolean> = MutableLiveData() |
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.
val loginResult: MutableLiveData<Boolean> = MutableLiveData() | |
private val _loginResult: MutableLiveData<Boolean> = MutableLiveData() | |
val loginResult: LiveData<Boolean> get() = _loginResult |
κ·Έλ°λ° μ¬κΈ°μ μμ ν΄μΌ ν λΆλΆμ΄ νλ λ μ‘΄μ¬ν©λλ€. λ°λ‘ MutableLiveDataλ₯Ό ꡬλ νλ λΆλΆμΈλ°μ, Kotlinμμλ κΈ°λ³Έμ μΌλ‘ λΆλ³μ±μ μ§ν₯ν©λλ€. ViewModel λ΄μμλ μλ²ν΅μ λ±μ λ°νκ°μ²΄μ λ΄λΆ μνλ₯Ό λ³κ²½ν΄μΌνλ μΌμ΄ μμ§λ§, μΈλΆ κ°μ²΄(Activity, Fragment)μμλ μ΄λ₯Ό λ³κ²½ν μ΄μ λ μκ³ μ€νλ € λ³κ²½νλ€λ©΄ μμμΉ λͺ»ν μΌλ€(SideEffect)μ΄ λ°μν μλ μμ΅λλ€.
λ°λΌμ μ΄λ° λ°νκ°μ²΄λ₯Ό μ¬μ©νλ κ²½μ° μνλ₯Ό λ³κ²½ν μ μλ κ°μ²΄λ privateμΌλ‘, μνλ₯Ό 곡κ°νλ κ°μ²΄λ λΆλ³κ°μ²΄λ‘ 곡κ°ν΄μΌν©λλ€. μ΄λ° κΈ°λ²μ **Backing Property
**λΌκ³ ν©λλ€.
μΆκ°λ‘ get() = λ₯Ό μ¬μ©νμ§ μκ³ = μΌλ‘ κ°μ λ°λ‘ ν λΉνλ λ°©λ²μ΄ μλλ° λ μ½λμ μ°¨μ΄μ μ΄ λ¬΄μμΈμ§ μ°Ύμλ³΄κ³ λκΈλ‘ λ¨κ²¨μ£ΌμΈμ!
val loginResult: LiveData<Boolean> get() = _loginResult
val loginResult: LiveData<Boolean> = _loginResult
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.
λ΄λΆμμ κ°μ λ³κ²½ν λλ _loginResult.value = κ°
μ΄λ°μμΌλ‘ μ¬μ©νμλ©΄ λ©λλ€!
μΈλΆμμλ loginResultλ₯Ό μ¬μ©νꡬμ!
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.
val loginResult: LiveData get() = _loginResult λ₯Ό μ¬μ©νλ©΄ _loginResultκ° λ³κ²½λ λλ§λ€ loginResultμ λ³κ²½λ κ°μ λ£μ΄μ€λλ€. λμ μΌλ‘ κ°μ κ°μ Έμ¬ μ μλ€λ μ₯μ μ΄ μμ΅λλ€.
val loginResult: LiveData = _loginResult
_loginResultμ κ°μ μ΄κΈ°ν μμ μ loginResultμ ν λΉν©λλ€.
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.
[MutableLiveData]
LiveDataλ Observer ν¨ν΄μ λ°λ₯΄λ©° λ°μ΄ν°μ λ³κ²½μ΄ μΌμ΄λ λλ§λ€ μ½λ°±μ λ°μ΅λλ€.
μ΄λ, LiveDataμ κ°μ λ³κ²½νκ² ν΄μ£Όλ ν¨μλ setValueμ postValueκ° μμ΅λλ€.
λ ν¨μλ protectedλ‘ λμ΄μκΈ°μ, μΈλΆμμ κ°μ λ³κ²½ν΄μ£ΌκΈ° μν΄μλ LiveDataλ₯Ό μμλ°λ MutableLiveDataλ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
[email protected] =it
loginResult.value = true
backing propertyλ₯Ό μ¬μ©νμ¬ loginResultλ₯Ό λ°κΎΈλ μμ λΆλΆμ λ¬Έμ κ° μκ²Όμ΅λλ€.
MutableLiveDataμ valueλ λ©μΈ μ€λ λμμλ§ μ¬μ©ν μ μμΌλ―λ‘ λ°±κ·ΈλΌμ΄λμμ μμ νκ² κ°μ λ³΄λΌ μ μλ postValueλ₯Ό μ¬μ©ν©λλ€.
private val authRepository: AuthRepository | ||
) : ViewModel() { | ||
val loginResult: MutableLiveData<Boolean> = MutableLiveData() | ||
val errorMessage: MutableLiveData<String> = MutableLiveData() |
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.
λ Uistateλ₯Ό μ¬μ©νκ² λλ©΄ loginResultμ errorMesageμ λ³μλ₯Ό ν©μ³μ μ¬μ© κ°λ₯ν©λλ€!
sealed class LoginState {
data object Loading : LoginState()
data class Success(val data: AuthData) : LoginState()
data class Failure(val errorMessage: String) : LoginState()
}
ν΄λΉ seale classλ₯Ό λ§λ€κ³ LiveDataμ νλΌλ―Έν°μ
private val _loginResult: MutableLiveData<LoginState<Boolean>> = MutableLiveData()
val loginResult: LiveData<LoginState<Boolean>> get() = _loginResult
μ‘ν°λΉν°μμ observeνκ³ κ° stateλ₯Ό whenμ λ‘ λΆκΈ°μ²λ¦¬ ν isλ₯Ό ν΅ν΄ smart castν λ€μ, κ° state(success, fail) λ±λ± νμν μμ μ νλ©΄ λ©λλ€
μ΄λ°μμΌλ‘ μ μΈν΄μ μ¬μ©νμλ©΄ λ©λλ€! μμΈν μ¬νλ€μ uistateμ λν΄μ μμ보면 μ’μκ² κ°μμ!
class LoginViewModelFactory : ViewModelProvider.Factory{ | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) { //modelClassκ° LoginViewModelμΈμ§ νμΈ | ||
val repository = | ||
AuthRepoImpl(ServicePool.authService)//authServiceλ₯Ό μΈμλ‘ λ°μμ RepositoryμΈμ€ν΄μ€ μμ± | ||
return LoginViewModel(repository) as T | ||
} else { | ||
throw IllegalArgumentException("Failed to create ViewModel: ${modelClass.name}") | ||
} | ||
} | ||
} |
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.
μλ μ΄κ±° μ΄λμ λ°°μ°μ ¨μ£ ?γ·γ·
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.
μ΄κ² μ κ° λ§μλλ Έλ μμ‘΄μ± μλ μ£Όμ μ λλ€!
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.
λ·°λͺ¨λΈ νν 리κΉμ§...γ·γ·
import androidx.lifecycle.Observer | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.sopt.now.R | ||
import com.sopt.now.databinding.ActivitySignupBinding | ||
import com.sopt.now.dto.SignUp.RequestSignUpDto | ||
import com.sopt.now.domain.model.AuthData | ||
import com.sopt.now.presentation.Login.LoginActivity | ||
import com.sopt.now.presentation.User.SignUpViewModelFactory | ||
|
||
|
||
class SignUpActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivitySignupBinding | ||
private lateinit var viewModel: SignUpViewModel | ||
private val viewModel: SignUpViewModel by viewModels { SignUpViewModelFactory() } |
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.
μ¬μ©λ μ ννλ€μ―
val error = response.errorBody()?.string() | ||
val jsonObject = error?.let { JSONObject(it) } | ||
|
||
val message = jsonObject?.getString("message") | ||
errorMessage.postValue(message.toString()) |
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.
json νμ± μ’μ΅λλ€! μ΄λ¬ν jsonνμ±λ dataμμμμ νλ©΄ μ’κ² μ£ ?
-tmi μλ΄λ λ¨-
λν μλ² μλ΅ μ€ν¨μ, λ΄ μμλλ‘ jsonννλ‘ μ€μ§ μλ κ²½μ°λ μμ΅λλ€! (ex. λ©ν°ννΈ-μ΄λ―Έμ§ μ μ‘)
μ΄λ° κ²½μ° μμ λ§μλλ¦° json νμ± μ€λ₯κ° λκ² μ£ ? μ΄λ¬ν μ€λ₯λ throwκ°λ₯ν©λλ€! κ°λ°μ μ
μ₯μμλ error handlingμ΄ μ©μ΄ν΄μ§μ£ (λ¬Όλ‘ μ΄λ°κ±΄ μ¬μ©μκ° νμΈν μλ¬λ©μΈμ§λ μλκ² μ§λ§μ)
class SignUpViewModelFactory :ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(SignUpViewModel::class.java)) { | ||
val repository = | ||
AuthRepoImpl(ServicePool.authService)//authServiceλ₯Ό μΈμλ‘ λ°μμ RepositoryμΈμ€ν΄μ€ μμ± | ||
return SignUpViewModel(repository) as T | ||
} else { | ||
throw IllegalArgumentException("Failed to create ViewModel: ${modelClass.name}") | ||
} | ||
} | ||
} |
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.
μ΄λ¬ν ViewModelProvider.Factoryλ₯Ό μμλ°λ Factoryλ€μ κ° λ·°λͺ¨λΈ λ³λ‘ λ§λ€ νμλ μμ΅λλ€!
νλμ ViewModel Classλ§μ λ§λ€κ³
if else νΉμ whenλ¬Έμ ν΅ν΄ κ° λ·°λͺ¨λΈκ³Ό repository λ³λ‘ λΆκΈ°μ²λ¦¬ νλ©΄ λ©λλ€!
κ·Έλ₯ else ifλ¬Έ νλ λ μΆκ°ν΄μ κ°μ λ°©μμΌλ‘ κ° λ·°λͺ¨λΈ νλ‘λ°μ΄λ λ³λ‘ λκ°μ΄ μ μΈν΄ μ£Όλ©΄ λ©λλ€.
νλ² ν΄λ³΄κ³ μλλ©΄ μ§λ¬Έμ£ΌμΈμ
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.
λ«μ μ΄μ λ²νΈ μμ± μνμ΄μ!
κ·Έλ¦¬κ³ μμ μ±μ₯νμ
¨λ€μ!!ππ
μμ μκ³ νμ
¨μ΅λλΉ!
import com.sopt.now.domain.model.AuthData | ||
import retrofit2.Response | ||
|
||
class AuthRepoImpl( |
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.
μ€νΈ λ ν¬μ§ν 리 ν¨ν΄ μ΄λ² μΈλ―Έλμμ λ°°μ λλ° λ²μ¨ μ μ©νμ ¨κ΅°μ₯!! μ λ μ μ©ν΄λ΄μΌκ² μ΄μ!
@@ -52,13 +53,14 @@ class LoginActivity : AppCompatActivity() { | |||
}) | |||
} | |||
|
|||
private fun getLogInRequestDto(): RequestLogInDto { | |||
private fun getLoginData(): AuthData { | |||
val id = binding.etId.text.toString() | |||
val password = binding.pw2.text.toString() |
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.
pw2λ₯Ό μ‘°κΈ λ μλ―Έμλ μμ΄λλͺ μΌλ‘ λ°κΎΈλ©΄ μ’μ κ² κ°μμ!
val userId = response.headers()["Location"] | ||
userId?.let { | ||
[email protected] =it | ||
loginResult.value = true | ||
} |
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.
μ€ν μ΄κ±°λ λͺ°λλ λ΄μ©μ΄μμ! μλ‘κ² μμκ°μ!
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.
μκ³ νμ ¨μ΄μ! μμ² μ΄μ¬ν νλκ² λκ»΄μ§λ€μ.. ν©ν 리μ λ ν¬μ§ν 리 ν¨ν΄κΉμ§.. λ©μμ΄μ!
|
||
|
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.
μ¬κΈ° μ€κ°κ²© νλ μ€μ¬μ£Όμλ©΄ μ’μκ±° κ°μμ
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.
λ ν¬μ§ν 리κΉμ§ μ°λ€λ.. νΌλ―Έμ³€λ€
val userId = response.headers()["Location"] | ||
userId?.let { | ||
[email protected] =it | ||
loginResult.value = true | ||
} |
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.
μ λ λͺ°λλ λ΄μ© μλ‘κ² μμκ°μ!
class LoginViewModelFactory : ViewModelProvider.Factory{ | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) { //modelClassκ° LoginViewModelμΈμ§ νμΈ | ||
val repository = | ||
AuthRepoImpl(ServicePool.authService)//authServiceλ₯Ό μΈμλ‘ λ°μμ RepositoryμΈμ€ν΄μ€ μμ± | ||
return LoginViewModel(repository) as T | ||
} else { | ||
throw IllegalArgumentException("Failed to create ViewModel: ${modelClass.name}") | ||
} | ||
} | ||
} |
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.
λ·°λͺ¨λΈ νν 리κΉμ§...γ·γ·
return AuthData( | ||
id = id, | ||
pw = password, | ||
name = nickname, | ||
phone = phoneNumber |
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.
return AuthData( | |
id = id, | |
pw = password, | |
name = nickname, | |
phone = phoneNumber | |
return AuthData( | |
id = binding.etIdSignup.text.toString(), | |
pw = binding.etPwSignup.text.toString(), | |
name = binding.etNicknameSignup.text.toString(), | |
phone = binding.etMbtiSignup.text.toString() |
μ΄λ κ² λΆνμν λ³μ μμ±μμ΄ μ¬μ©νλ©΄ μ½λ μ€μΌ μ μμκ±° κ°μμ
πππ΄π΄πΆπ¦π΄
πππ°π³π¬ ππ¦π΄π€π³πͺπ±π΅πͺπ°π―
π·ππ€π³π¦π¦π―π΄π©π°π΅
week06.mp4
π¬ππ° ππ¦π·πͺπ¦πΈπ¦π³π΄