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

[6차 세미나/xml] : 필수 과제 + 심화 과제 #18

Open
wants to merge 2 commits into
base: develop-xml
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.sopt.now.presentation.home

import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.sopt.now.R
import com.sopt.now.core.base.BindingFragment
import com.sopt.now.core.util.fragment.snackBar
import com.sopt.now.core.util.fragment.viewLifeCycle
import com.sopt.now.core.util.fragment.viewLifeCycleScope
import com.sopt.now.core.util.view.UiState
import com.sopt.now.databinding.FragmentHomeBinding
import com.sopt.now.presentation.home.friend.Friend
import com.sopt.now.presentation.home.friend.FriendAdapter
import com.sopt.now.presentation.home.friend.FriendItemDecorator
import com.sopt.now.presentation.home.user.UserAdapter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber

class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home) {
Expand All @@ -22,13 +27,13 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
}

private fun initObserverUserList() {
homeViewModel.getUserList.observe(this) {
homeViewModel.getUserList.flowWithLifecycle(viewLifeCycle).onEach {
when (it) {
is UiState.Success -> initHomeAdapter(it.data)
is UiState.Failure -> initErrorMessage(it.errorMessage)
is UiState.Loading -> Timber.d("로딩중")
}
}
}.launchIn(viewLifeCycleScope)
}

private fun initErrorMessage(errorMessage: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.sopt.now.presentation.home

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.core.util.view.UiState
import com.sopt.now.data.ServicePool
import com.sopt.now.presentation.home.friend.Friend
import com.sopt.now.presentation.home.user.User
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class HomeViewModel : ViewModel() {
Expand All @@ -19,8 +20,8 @@ class HomeViewModel : ViewModel() {
)
)

private val _getUserList = MutableLiveData<UiState<List<Friend>?>>(UiState.Loading)
val getUserList: MutableLiveData<UiState<List<Friend>?>> = _getUserList
private val _getUserList = MutableStateFlow<UiState<List<Friend>>>(UiState.Loading)
val getUserList: StateFlow<UiState<List<Friend>>> = _getUserList

init {
getUserList(2)
Expand All @@ -30,9 +31,7 @@ class HomeViewModel : ViewModel() {
runCatching {
ServicePool.userServiceApi.getUserList(page).body()?.data?.map { it.toFriend() }
}.fold(
{
_getUserList.value = UiState.Success(it)
},
{ _getUserList.value = UiState.Success(it ?: emptyList()) },
{ _getUserList.value = UiState.Failure(it.message.toString()) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sopt.now.R
import com.sopt.now.core.base.BindingActivity
import com.sopt.now.core.util.context.snackBar
Expand All @@ -13,6 +15,8 @@ import com.sopt.now.databinding.ActivityLoginBinding
import com.sopt.now.presentation.home.HomeActivity
import com.sopt.now.presentation.sign.SignActivity
import com.sopt.now.presentation.util.KeyStorage.ERROR_LOGIN_ID_PW
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber

class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_login) {
Expand Down Expand Up @@ -45,13 +49,13 @@ class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_lo
}

private fun initObserveLogin() {
loginViewModel.postLogin.observe(this) {
loginViewModel.postLogin.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Success -> navigateToHome(it.data.toString())
is UiState.Failure -> initErrorMessage(it.errorMessage)
is UiState.Loading -> Timber.d("로딩중")
}
}
}.launchIn(lifecycleScope)
}

private fun initErrorMessage(errorMessage: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
package com.sopt.now.presentation.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.core.MainApplication
import com.sopt.now.core.util.view.UiState
import com.sopt.now.data.ServicePool
import com.sopt.now.data.dto.request.RequestLoginDto
import com.sopt.now.presentation.util.KeyStorage.ERROR_LOGIN_ID_PW
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch

class LoginViewModel : ViewModel() {
private val _postLogin = MutableLiveData<UiState<Any?>>(UiState.Loading)
val postLogin: MutableLiveData<UiState<Any?>> = _postLogin
private val _postLogin = MutableSharedFlow<UiState<Any>>()
val postLogin: SharedFlow<UiState<Any>> = _postLogin

fun postLogin(id: String, pw: String) = viewModelScope.launch {
_postLogin.emit(UiState.Loading)

if (checkLoginValid(id, pw)) {
runCatching {
ServicePool.authServiceApi.postLogin(RequestLoginDto(id, pw))
}.fold(
{
if (it.code() == 200) {
_postLogin.value =
UiState.Success("로그인 성공! 유저의 ID는 ${it.headers()["location"]} 입니둥")
.apply { setUserMemberId(it.headers()["location"]?.toInt() ?: -1) }
} else _postLogin.value =
_postLogin.emit(UiState.Success("로그인 성공! 유저의 ID는 ${it.headers()["location"]} 입니둥")
.apply { setUserMemberId(it.headers()["location"]?.toInt() ?: -1) })
} else _postLogin.emit(
UiState.Failure("${it.errorBody()?.string()?.split("\"")?.get(5)}")
)

},
{ _postLogin.value = UiState.Failure(it.message.toString()) }
{ _postLogin.emit(UiState.Failure(it.message.toString())) }
)
} else {
_postLogin.value = UiState.Failure(ERROR_LOGIN_ID_PW)
_postLogin.emit(UiState.Failure(ERROR_LOGIN_ID_PW))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.sopt.now.presentation.mypage

import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sopt.now.R
import com.sopt.now.core.base.BindingDialogFragment
import com.sopt.now.core.util.context.dialogFragmentResize
import com.sopt.now.core.util.fragment.snackBar
import com.sopt.now.core.util.fragment.viewLifeCycle
import com.sopt.now.core.util.fragment.viewLifeCycleScope
import com.sopt.now.core.util.view.UiState
import com.sopt.now.databinding.FragmentMyPageDialogBinding
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber

class MyPageDialogFragment() :
Expand All @@ -25,7 +31,7 @@ class MyPageDialogFragment() :
}

private fun initObserveUserPw() {
myPageViewModel.patchUserPw.observe(this) {
myPageViewModel.patchUserPw.flowWithLifecycle(viewLifeCycle).onEach {
when (it) {
is UiState.Success -> {
snackBar(binding.root) { "비밀번호 변경 완료 : ${it.data.message}" }
Expand All @@ -35,7 +41,7 @@ class MyPageDialogFragment() :
is UiState.Failure -> snackBar(binding.root) { it.errorMessage }
is UiState.Loading -> Timber.d("로딩중")
}
}
}.launchIn(viewLifeCycleScope)
}

private fun initCancelBtnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package com.sopt.now.presentation.mypage

import android.graphics.Paint
import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
import com.sopt.now.R
import com.sopt.now.core.base.BindingFragment
import com.sopt.now.core.util.fragment.snackBar
import com.sopt.now.core.util.fragment.viewLifeCycle
import com.sopt.now.core.util.fragment.viewLifeCycleScope
import com.sopt.now.core.util.view.UiState
import com.sopt.now.data.dto.response.ResponseUserInfoDataDto
import com.sopt.now.databinding.FragmentMyPageBinding
import com.sopt.now.presentation.util.DialogTag.CHANGE_PW
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber

class MyPageFragment : BindingFragment<FragmentMyPageBinding>(R.layout.fragment_my_page) {
Expand All @@ -25,13 +30,13 @@ class MyPageFragment : BindingFragment<FragmentMyPageBinding>(R.layout.fragment_
}

private fun initObserveUserInfo() {
myPageViewModel.getUserInfo.observe(this) {
myPageViewModel.getUserInfo.flowWithLifecycle(viewLifeCycle).onEach {
when (it) {
is UiState.Success -> initResultLoginUserInformation(it.data.data)
is UiState.Failure -> initErrorMessage(it.errorMessage)
is UiState.Loading -> Timber.d("로딩중")
}
}
}.launchIn(viewLifeCycleScope)
}

private fun initErrorMessage(errorMessage: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sopt.now.presentation.mypage

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.core.MainApplication
Expand All @@ -10,15 +9,19 @@ import com.sopt.now.data.dto.request.RequestUserPwDto
import com.sopt.now.data.dto.response.ResponseUserInfoDto
import com.sopt.now.data.dto.response.ResponseUserPwDto
import com.sopt.now.presentation.util.Regex.PW_REGEX
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import java.util.regex.Pattern

class MyPageViewModel : ViewModel() {
private val _getUserInfo = MutableLiveData<UiState<ResponseUserInfoDto>>(UiState.Loading)
val getUserInfo: MutableLiveData<UiState<ResponseUserInfoDto>> = _getUserInfo
private val _getUserInfo = MutableStateFlow<UiState<ResponseUserInfoDto>>(UiState.Loading)
val getUserInfo: StateFlow<UiState<ResponseUserInfoDto>> = _getUserInfo

private val _patchUserPw = MutableLiveData<UiState<ResponseUserPwDto>>(UiState.Loading)
val patchUserPw: MutableLiveData<UiState<ResponseUserPwDto>> = _patchUserPw
private val _patchUserPw = MutableSharedFlow<UiState<ResponseUserPwDto>>()
val patchUserPw: SharedFlow<UiState<ResponseUserPwDto>> = _patchUserPw

init {
getUserInfo()
Expand All @@ -32,19 +35,22 @@ class MyPageViewModel : ViewModel() {
}

fun patchUserPw(beforePw: String, afterPw: String, checkPw: String) = viewModelScope.launch {
_patchUserPw.emit(UiState.Loading)

if (checkPwValid(afterPw)) {
runCatching {
ServicePool.authServiceApi.patchUserPw(
getUserMemberId(),
RequestUserPwDto(beforePw, afterPw, checkPw)
)
}.fold(
{ _patchUserPw.value = UiState.Success(it) },
{ _patchUserPw.value = UiState.Failure(it.message.toString()) }
{ _patchUserPw.emit(UiState.Success(it)) },
{ _patchUserPw.emit(UiState.Failure(it.message.toString())) }
)
} else {
_patchUserPw.value =
_patchUserPw.emit(
UiState.Failure("비밀번호를 8글자 이상, 숫자, 문자(a-z, A-Z), 특수문자 포함하여 입력해주세요.")
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.sopt.now.presentation.sign

import android.content.Intent
import androidx.activity.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sopt.now.R
import com.sopt.now.core.base.BindingActivity
import com.sopt.now.core.util.context.snackBar
Expand All @@ -14,6 +16,8 @@ import com.sopt.now.presentation.util.KeyStorage.ERROR_SIGN_ID
import com.sopt.now.presentation.util.KeyStorage.ERROR_SIGN_NICKNAME
import com.sopt.now.presentation.util.KeyStorage.ERROR_SIGN_PW
import com.sopt.now.presentation.util.KeyStorage.ERROR_SIGN_TEL
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber

class SignActivity : BindingActivity<ActivitySignBinding>(R.layout.activity_sign) {
Expand All @@ -25,13 +29,13 @@ class SignActivity : BindingActivity<ActivitySignBinding>(R.layout.activity_sign
}

private fun initObserveSign() {
signViewModel.postSign.observe(this) {
signViewModel.postSign.flowWithLifecycle(lifecycle).onEach {
when (it) {
is UiState.Success -> navigateToLogin(it.data.toString())
is UiState.Failure -> initErrorMessage(it.errorMessage)
is UiState.Loading -> Timber.d("로딩중")
}
}
}.launchIn(lifecycleScope)
}

private fun initErrorMessage(errorMessage: String) {
Expand Down
19 changes: 11 additions & 8 deletions app/src/main/java/com/sopt/now/presentation/sign/SignViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sopt.now.presentation.sign

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.core.util.view.UiState
Expand All @@ -13,14 +12,18 @@ import com.sopt.now.presentation.util.KeyStorage.ERROR_SIGN_PW
import com.sopt.now.presentation.util.KeyStorage.ERROR_SIGN_TEL
import com.sopt.now.presentation.util.Regex.PW_REGEX
import com.sopt.now.presentation.util.Regex.TEL_REGEX
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import java.util.regex.Pattern

class SignViewModel : ViewModel() {
private val _postSign = MutableLiveData<UiState<Any?>>(UiState.Loading)
val postSign: MutableLiveData<UiState<Any?>> = _postSign
private val _postSign = MutableSharedFlow<UiState<Any>>()
val postSign: SharedFlow<UiState<Any>> = _postSign

fun postSign(user: User) = viewModelScope.launch {
_postSign.emit(UiState.Loading)

if (checkSignValid(user) == null) {
runCatching {
ServicePool.authServiceApi.postSign(
Expand All @@ -29,15 +32,15 @@ class SignViewModel : ViewModel() {
}.fold(
{
if (it.code() == 201) {
_postSign.value =
UiState.Success("회원가입 성공! 유저의 ID는 ${it.headers()["location"]} 입니둥")
} else _postSign.value =
_postSign.emit(UiState.Success("회원가입 성공! 유저의 ID는 ${it.headers()["location"]} 입니둥"))
} else _postSign.emit(
UiState.Failure("${it.errorBody()?.string()?.split("\"")?.get(5)}")
)
},
{ _postSign.value = UiState.Failure(it.message.toString()) }
{ _postSign.emit(UiState.Failure(it.message.toString())) }
)
} else {
_postSign.value = UiState.Failure(checkSignValid(user).toString())
_postSign.emit(UiState.Failure(checkSignValid(user).toString()))
}
}

Expand Down