@@ -9,6 +9,8 @@ import com.google.android.gms.auth.api.signin.GoogleSignInOptions
99import com.kakao.sdk.user.UserApiClient
1010import com.texthip.thip.data.manager.TokenManager
1111import com.texthip.thip.data.repository.UserRepository
12+ import com.texthip.thip.data.repository.NotificationRepository
13+ import com.texthip.thip.utils.auth.clearAppScopeDeviceData
1214import dagger.hilt.android.lifecycle.HiltViewModel
1315import kotlinx.coroutines.flow.MutableStateFlow
1416import kotlinx.coroutines.flow.asStateFlow
@@ -25,7 +27,8 @@ data class DeleteAccountUiState(
2527@HiltViewModel
2628class DeleteAccountViewModel @Inject constructor(
2729 private val userRepository : UserRepository ,
28- private val tokenManager : TokenManager
30+ private val tokenManager : TokenManager ,
31+ private val notificationRepository : NotificationRepository
2932) : ViewModel() {
3033
3134 private val _uiState = MutableStateFlow (DeleteAccountUiState ())
@@ -35,36 +38,52 @@ class DeleteAccountViewModel @Inject constructor(
3538 viewModelScope.launch {
3639 _uiState .update { it.copy(isLoading = true , errorMessage = null ) }
3740
38- // 1. 서버에 회원탈퇴 요청
39- userRepository.deleteAccount()
40- .onSuccess {
41- // 2. 서버 요청 성공 후 토큰 삭제
42- tokenManager.clearTokens()
43-
44- // 3. 카카오 SDK에서 연결 끊기
45- UserApiClient .instance.unlink { error ->
46- if (error != null ) {
47- Log .e(" DeleteAccountViewModel" , " 카카오 연결 끊기 실패" , error)
48- } else {
49- Log .d(" DeleteAccountViewModel" , " 카카오 연결 끊기 성공" )
50- }
41+ try {
42+ notificationRepository.deleteFcmToken()
43+ .onFailure { exception ->
44+ Log .w(" DeleteAccountViewModel" , " FCM 토큰 삭제 실패 (계속 진행)" , exception)
5145 }
52-
53- // 4. 구글 SDK에서 로그아웃
54- val gso = GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN ).build()
55- GoogleSignIn .getClient(context, gso).signOut()
56-
57- _uiState .update { it.copy(isLoading = false , isDeleteCompleted = true ) }
58- }
59- .onFailure { exception ->
60- Log .e(" DeleteAccountViewModel" , " 회원탈퇴 실패" , exception)
61- _uiState .update {
62- it.copy(
63- isLoading = false ,
64- errorMessage = exception.message ? : " 회원탈퇴에 실패했습니다."
65- )
46+
47+ userRepository.deleteAccount()
48+ .onSuccess {
49+ performLocalDataCleanup(context)
50+
51+ _uiState .update { it.copy(isLoading = false , isDeleteCompleted = true ) }
52+ }
53+ .onFailure { exception ->
54+ throw exception
6655 }
56+ } catch (exception: Exception ) {
57+ Log .e(" DeleteAccountViewModel" , " 회원탈퇴 실패" , exception)
58+ _uiState .update {
59+ it.copy(
60+ isLoading = false ,
61+ errorMessage = exception.message ? : " 회원탈퇴에 실패했습니다."
62+ )
6763 }
64+ }
65+ }
66+ }
67+
68+ private suspend fun performLocalDataCleanup (context : Context ) {
69+ try {
70+ tokenManager.clearTokens()
71+ context.clearAppScopeDeviceData()
72+
73+ UserApiClient .instance.unlink { error ->
74+ if (error != null ) {
75+ Log .e(" DeleteAccountViewModel" , " 카카오 연결 끊기 실패" , error)
76+ } else {
77+ Log .d(" DeleteAccountViewModel" , " 카카오 연결 끊기 성공" )
78+ }
79+ }
80+
81+ val gso = GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN ).build()
82+ GoogleSignIn .getClient(context, gso).signOut()
83+
84+ Log .i(" DeleteAccountViewModel" , " 로컬 데이터 정리 완료" )
85+ } catch (e: Exception ) {
86+ Log .e(" DeleteAccountViewModel" , " 로컬 데이터 정리 중 오류" , e)
6887 }
6988 }
7089}
0 commit comments