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.
#11 [Mod] MyPageViewModel,ProfileScreen 변경
- Loading branch information
Showing
2 changed files
with
56 additions
and
29 deletions.
There are no files selected for viewing
38 changes: 23 additions & 15 deletions
38
app/src/main/java/org/sopt/and/feature/mypage/MyPageViewModel.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,31 +1,39 @@ | ||
package org.sopt.and.feature.mypage | ||
package org.sopt.and.feature.mypage.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import org.sopt.and.core.component.BaseViewModel | ||
import org.sopt.and.domain.repository.MyPageRepository | ||
import org.sopt.and.feature.mypage.model.MyPageContract.MyPageEvent | ||
import org.sopt.and.feature.mypage.model.MyPageContract.MyPageSideEffect | ||
import org.sopt.and.feature.mypage.model.MyPageContract.MyPageState | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class MyPageViewModel @Inject constructor( | ||
private val myPageRepository: MyPageRepository | ||
) : ViewModel() { | ||
) : BaseViewModel<MyPageState, MyPageSideEffect, MyPageEvent>() { | ||
|
||
private val _hobby = MutableStateFlow<String?>(null) | ||
val hobby: StateFlow<String?> = _hobby.asStateFlow() | ||
override fun createInitialState() = MyPageState() | ||
|
||
fun loadHobby(token: String) { | ||
override suspend fun handleEvent(event: MyPageEvent) { | ||
when (event) { | ||
is MyPageEvent.LoadHobby -> loadHobby(event.token) | ||
} | ||
} | ||
|
||
private fun loadHobby(token: String) { | ||
setState { copy(isLoading = true) } | ||
viewModelScope.launch { | ||
val result = myPageRepository.getMyHobby(token) | ||
result.onSuccess { response -> | ||
_hobby.value = response.hobby | ||
}.onFailure { | ||
_hobby.value = "데이터를 불러오는데 실패했습니다." | ||
} | ||
myPageRepository.getMyHobby(token) | ||
.onSuccess { response -> | ||
setState { copy(hobby = response.hobby, isLoading = false) } | ||
} | ||
.onFailure { | ||
setState { copy(hobby = "", isLoading = false, error = "데이터를 불러오는데 실패했습니다.") } | ||
setSideEffect { MyPageSideEffect.ShowErrorToast("데이터를 불러오는데 실패했습니다.") } | ||
} | ||
} | ||
} | ||
} |
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