Skip to content
Merged
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
Expand Up @@ -38,10 +38,9 @@ class UserRepositoryImpl @Inject constructor(private val userService: UserServic
emit(userService.getMyInfo())
}

override suspend fun signOut(): Flow<BaseResponse<Boolean>> =
flow {
emit(userService.signOut())
}
override suspend fun signOut(): Boolean {
return userService.signOut().result ?: false
}
Comment on lines +41 to +43
Copy link
Member Author

Choose a reason for hiding this comment

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

BaseResponse에 result가 T?라서 Boolean이 올 자리에 Boolean?이 와서 false로 기본값을 주었는데, 이렇게 해도 괜찮을까요?

Copy link
Member

Choose a reason for hiding this comment

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

signOut에 대한 result도 단순 Boolean값이라 null로 올 것 같지는 않지만
null인 경우 false로 두는 거 괜찮을거같은데요?!


override suspend fun getTotalColleges(): List<College> =
userService.getCollegeList().result?.map { it.toDomain() }.orEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface UserRepository {

suspend fun getUserReviews(): Flow<BaseResponse<MyReviewResponse>>
suspend fun getUserNickName(): Flow<BaseResponse<MyNickNameResponse>>
suspend fun signOut(): Flow<BaseResponse<Boolean>>
suspend fun signOut(): Boolean

// 모든 단과대 조회
suspend fun getTotalColleges(): List<College>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.eatssu.android.domain.usecase.auth

import com.eatssu.android.data.dto.response.BaseResponse
import com.eatssu.android.domain.repository.UserRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class SignOutUseCase @Inject constructor(
private val userRepository: UserRepository,
) {
suspend operator fun invoke(): Flow<BaseResponse<Boolean>> =
suspend operator fun invoke(): Boolean =
userRepository.signOut()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.eatssu.android.presentation.mypage

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.lifecycleScope
import com.eatssu.android.databinding.ActivitySignOutBinding
import com.eatssu.android.presentation.UiEvent
import com.eatssu.android.presentation.UiState
import com.eatssu.android.presentation.base.BaseActivity
import com.eatssu.android.presentation.login.LoginActivity
import com.eatssu.android.presentation.util.showToast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -15,7 +18,6 @@ import kotlinx.coroutines.launch
@AndroidEntryPoint
class SignOutActivity :
BaseActivity<ActivitySignOutBinding>(ActivitySignOutBinding::inflate) {
//TODO 현재 dev서버 탈퇴하기 500

private val signOutViewModel: SignOutViewModel by viewModels()

Expand All @@ -25,47 +27,64 @@ class SignOutActivity :
super.onCreate(savedInstanceState)
toolbarTitle.text = "탈퇴하기" // 툴바 제목 설정

val nickname = intent.getStringExtra("nickname")
val nickname = intent.getStringExtra("nickname")?.trim() ?: ""

binding.btnSignOut.isEnabled = false

// binding.etEnterNickname.hint =
binding.etEnterNickname.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
binding.etEnterNickname.hint = nickname
binding.etEnterNickname.doAfterTextChanged {
compareNickname(nickname)
}

setOnClickListener()

lifecycleScope.launch {
signOutViewModel.uiState.collectLatest {
when (it) {
is UiState.Init -> {}

is UiState.Loading -> {
//로딩중
}

is UiState.Success -> {
if (it.data?.isSignOuted == true) {
val intent = Intent(this@SignOutActivity, LoginActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
finish()
}
}

//값 변경 시 실행되는 함수
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
if (nickname != null) {
checkNickname(nickname)
is UiState.Error -> {
//에러
}
}
}
}

override fun afterTextChanged(p0: Editable?) {}
})

setOnClickListener()
lifecycleScope.launch {
signOutViewModel.uiEvent.collectLatest { event ->
when (event) {
is UiEvent.ShowToast -> {
showToast(event.message)
}
}
}
}
}


private fun setOnClickListener() {
binding.btnSignOut.setOnClickListener {
signOutViewModel.signOut()

lifecycleScope.launch {
signOutViewModel.uiState.collectLatest {
if (it.isSignOuted) {
showToast(it.toastMessage) //Todo 사용가능 토스트가 무슨 3번이나 나옴
} else {
showToast(it.toastMessage) //Todo 사용가능 토스트가 무슨 3번이나 나옴
}
}
}
}
}

fun checkNickname(nickname: String) {
private fun compareNickname(nickname: String) {
//입력값 담기
inputNickname = binding.etEnterNickname.text.trim().toString()
inputNickname = binding.etEnterNickname.text?.toString()?.trim() ?: ""
// 값 유무에 따른 활성화 여부
if (inputNickname == nickname) {
binding.btnSignOut.isEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.eatssu.android.domain.usecase.auth.LogoutUseCase
import com.eatssu.android.domain.usecase.auth.SignOutUseCase
import com.eatssu.android.presentation.UiEvent
import com.eatssu.android.presentation.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
Expand All @@ -23,45 +21,33 @@ class SignOutViewModel @Inject constructor(
private val signOutUseCase: SignOutUseCase,
) : ViewModel() {

private val _uiState: MutableStateFlow<SignOutState> = MutableStateFlow(SignOutState())
val uiState: StateFlow<SignOutState> = _uiState.asStateFlow()
private val _uiState: MutableStateFlow<UiState<SignOutState>> = MutableStateFlow(UiState.Init)
val uiState: StateFlow<UiState<SignOutState>> = _uiState.asStateFlow()

private val _uiEvent = MutableSharedFlow<UiEvent>()
val uiEvent: SharedFlow<UiEvent> = _uiEvent

fun signOut() {
viewModelScope.launch {
signOutUseCase().onStart {
_uiState.update { it.copy(loading = true) }
}.onCompletion {
_uiState.update { it.copy(loading = false, error = true) }
}.catch { e ->
_uiState.update { it.copy(error = true, toastMessage = "정보를 불러올 수 없습니다.") }
Timber.d(TAG, e.toString())
}.collectLatest { result ->
Timber.d(TAG, result.toString())
if (result.result == true) {
logoutUseCase()
_uiState.update {
it.copy(
isSignOuted = true,
toastMessage = "탈퇴가 완료되었습니다."
)
}
_uiState.value = UiState.Loading
try {
val isSignOut = signOutUseCase()
if (isSignOut) {
_uiState.value = UiState.Success(SignOutState(isSignOuted = true))
_uiEvent.emit(UiEvent.ShowToast("탈퇴가 완료되었습니다."))
logoutUseCase() // 자동 로그인 정보 삭제
} else {
_uiState.value = UiState.Error
_uiEvent.emit(UiEvent.ShowToast("탈퇴에 실패했습니다."))
}
} catch (e: Exception) {
_uiState.value = UiState.Error
_uiEvent.emit(UiEvent.ShowToast("오류가 발생했습니다. $e"))
}
}
}

companion object {
val TAG = "SignOutViewModel"
}
}

data class SignOutState(
var loading: Boolean = true,
var error: Boolean = false,
var toastMessage: String = "",
var nickname: String = "",
var platform: String = "",
var isNicknameNull: Boolean = false,
var isLoginOuted: Boolean = false,
var isSignOuted: Boolean = false,
val isSignOuted: Boolean = false,
)