generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mod/#12: my 마이 화면 baseviewmodel, uiEvent, uiSideEffect, uiState 사용으로 수정
- Loading branch information
1 parent
4753dd3
commit 7d6bff0
Showing
5 changed files
with
46 additions
and
33 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
8 changes: 8 additions & 0 deletions
8
presentation/src/main/java/org/sopt/and/presentation/my/contract/MyEvent.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,8 @@ | ||
package org.sopt.and.presentation.my.contract | ||
|
||
import org.sopt.and.presentation.util.base.UiEvent | ||
|
||
sealed class MyEvent: UiEvent { | ||
data class GetMyHobby(val token: String): MyEvent() | ||
data object OnLogoutButtonClick: MyEvent() | ||
} |
3 changes: 2 additions & 1 deletion
3
presentation/src/main/java/org/sopt/and/presentation/my/model/MyState.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package org.sopt.and.presentation.my.model | ||
|
||
import org.sopt.and.presentation.util.KeyUtil.DEFAULT_STRING | ||
import org.sopt.and.presentation.util.base.UiState | ||
|
||
data class MyState( | ||
val hobby: String = DEFAULT_STRING | ||
) | ||
): UiState |
5 changes: 3 additions & 2 deletions
5
presentation/src/main/java/org/sopt/and/presentation/my/sideeffect/MySideEffect.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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package org.sopt.and.presentation.my.sideeffect | ||
|
||
import androidx.annotation.StringRes | ||
import org.sopt.and.presentation.util.base.UiSideEffect | ||
|
||
sealed class MySideEffect { | ||
sealed class MySideEffect: UiSideEffect { | ||
data class SnackBar(@StringRes val message: Int): MySideEffect() | ||
data class SnackBarText(val message: String): MySideEffect() | ||
data object Logout: MySideEffect() | ||
data object NavigateToSignIn: MySideEffect() | ||
} |
40 changes: 21 additions & 19 deletions
40
presentation/src/main/java/org/sopt/and/presentation/my/viewmodel/MyViewModel.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 |
---|---|---|
@@ -1,59 +1,61 @@ | ||
package org.sopt.and.presentation.my.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import org.sopt.and.domain.exception.onError | ||
import org.sopt.and.domain.exception.onSuccess | ||
import org.sopt.and.domain.repository.UserRepository | ||
import org.sopt.and.presentation.delegate.NetworkDelegate | ||
import org.sopt.and.presentation.my.contract.MyEvent | ||
import org.sopt.and.presentation.my.model.MyState | ||
import org.sopt.and.presentation.my.sideeffect.MySideEffect | ||
import org.sopt.and.presentation.sign.signin.sideeffect.SignInSideEffect | ||
import org.sopt.and.presentation.util.base.BaseViewModel | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class MyViewModel @Inject constructor( | ||
private val userRepository: UserRepository, | ||
private val networkDelegate: NetworkDelegate | ||
): ViewModel() { | ||
): BaseViewModel<MyState, MyEvent, MySideEffect>() { | ||
|
||
private val _state = MutableStateFlow(MyState()) | ||
val state = _state.asStateFlow() | ||
override fun createInitialState(): MyState = MyState() | ||
|
||
private val _intent = MutableSharedFlow<MySideEffect>() | ||
val intent = _intent.asSharedFlow() | ||
override suspend fun handleEvent(event: MyEvent) { | ||
when(event) { | ||
MyEvent.OnLogoutButtonClick -> { | ||
navigateToSignIn() | ||
} | ||
is MyEvent.GetMyHobby -> { | ||
getMyHobby() | ||
} | ||
} | ||
} | ||
|
||
val networkState get() = networkDelegate.networkState | ||
|
||
fun getMyHobby() = viewModelScope.launch { | ||
val token = userRepository.getToken() | ||
userRepository.getMyHobby(token).onSuccess { result -> | ||
_state.update { | ||
it.copy(hobby = result.hobby) | ||
} | ||
setState { copy(hobby = result.hobby) } | ||
networkDelegate.handleNetworkSuccess() | ||
}.onError { | ||
networkDelegate.handleGetMyHobbyError(it) | ||
} | ||
} | ||
|
||
fun onLogOutButtonClick() = viewModelScope.launch { | ||
_intent.emit(MySideEffect.Logout) | ||
private fun navigateToSignIn() = viewModelScope.launch { | ||
delay(100) | ||
setSideEffect(MySideEffect.NavigateToSignIn) | ||
} | ||
|
||
fun clearUserPreference() = viewModelScope.launch { | ||
userRepository.clearUserPreference() | ||
} | ||
|
||
suspend fun handleMyIntentError(message: String) { | ||
_intent.emit(MySideEffect.SnackBarText(message)) | ||
fun handleMyIntentError(message: String) = viewModelScope.launch { | ||
setSideEffect(MySideEffect.SnackBarText(message)) | ||
} | ||
|
||
} |