-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...page/src/main/java/org/sopt/official/feature/mypage/mypage/state/rememberMyPageUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.sopt.official.feature.mypage.mypage.state | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import io.github.takahirom.rin.rememberRetained | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.tasks.await | ||
import org.sopt.official.auth.model.UserActiveState | ||
import org.sopt.official.auth.repository.AuthRepository | ||
import org.sopt.official.domain.soptamp.repository.StampRepository | ||
import timber.log.Timber | ||
|
||
@Composable | ||
fun rememberMyPageUiState( | ||
userActiveState: UserActiveState, | ||
authRepository: AuthRepository, | ||
stampRepository: StampRepository, | ||
onRestartApp: () -> Unit | ||
): MyPageUiState { | ||
var userActiveState by rememberRetained { mutableStateOf(userActiveState) } | ||
var dialogState by rememberRetained { mutableStateOf(MyPageDialogState.CLEAR) } | ||
|
||
val scope = rememberCoroutineScope() | ||
|
||
return MyPageUiState( | ||
user = userActiveState, | ||
dialogState = dialogState, | ||
onEventSink = { action -> | ||
when (action) { | ||
is ResetSoptamp -> { | ||
scope.launch { | ||
stampRepository.deleteAllStamps() | ||
.onSuccess { dialogState = MyPageDialogState.CLEAR } | ||
.onFailure { Timber.e(it) } | ||
} | ||
} | ||
|
||
is ClearSoptamp -> { | ||
dialogState = MyPageDialogState.CLEAR_SOPTAMP | ||
} | ||
|
||
is Logout -> { | ||
scope.launch { | ||
runCatching { | ||
FirebaseMessaging.getInstance().token.await() | ||
}.onSuccess { | ||
authRepository.logout(it) | ||
.onSuccess { | ||
authRepository.clearLocalData() | ||
onRestartApp() | ||
}.onFailure { error -> | ||
Timber.e(error) | ||
} | ||
}.onFailure { | ||
Timber.e(it) | ||
} | ||
} | ||
} | ||
|
||
is CloseDialog -> { | ||
dialogState = MyPageDialogState.CLEAR | ||
} | ||
} | ||
} | ||
) | ||
} |