Skip to content

Commit

Permalink
#11 [Mod] MyPageViewModel,ProfileScreen 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoeunjoo committed Dec 17, 2024
1 parent 8d11065 commit a147228
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
38 changes: 23 additions & 15 deletions app/src/main/java/org/sopt/and/feature/mypage/MyPageViewModel.kt
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("데이터를 불러오는데 실패했습니다.") }
}
}
}
}
47 changes: 33 additions & 14 deletions app/src/main/java/org/sopt/and/feature/mypage/ProfileScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.sopt.and.feature.mypage

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -29,26 +30,39 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import kotlinx.coroutines.flow.collectLatest
import org.sopt.and.R
import org.sopt.and.feature.mypage.model.MyPageContract
import org.sopt.and.feature.mypage.viewmodel.MyPageViewModel

@Composable
fun ProfileScreen() {
val viewModel: MyPageViewModel = hiltViewModel()
val hobby by viewModel.hobby.collectAsState()
fun ProfileScreen(viewModel: MyPageViewModel = hiltViewModel()) {
val uiState by viewModel.uiState.collectAsState()
val context = LocalContext.current

LaunchedEffect(Unit) {
val token = getAuthToken(context)
if (token != null) {
viewModel.loadHobby(token)
viewModel.setEvent(MyPageContract.MyPageEvent.LoadHobby(token))
} else {
Log.e("MyPageScreen", "토근 못 찾음")
Log.e("ProfileScreen", "토큰 못 찾음")
}
}

LaunchedEffect(Unit) {
viewModel.sideEffect.collectLatest { sideEffect ->
when (sideEffect) {
is MyPageContract.MyPageSideEffect.ShowErrorToast -> {
Toast.makeText(context, sideEffect.message, Toast.LENGTH_SHORT).show()
}
}
}
}

Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()),
.verticalScroll(rememberScrollState())
) {
Row(
modifier = Modifier
Expand All @@ -63,30 +77,35 @@ fun ProfileScreen() {
alignment = Alignment.CenterStart
)
Text(
hobby ?: "찾을 수 없습니다",
text = when {
uiState.isLoading -> "로딩 중..."
uiState.hobby.isNotEmpty() -> uiState.hobby
else -> "찾을 수 없습니다"
},
color = Color.White,
fontSize = 20.sp,
modifier = Modifier
.weight(1f)
.padding(end = 8.dp),
fontSize = 20.sp
.padding(end = 8.dp)
)
Image(
painter = painterResource(id = R.drawable.ic_alarm),
contentDescription = "alarm",
contentDescription = "Alarm Icon",
modifier = Modifier.size(80.dp),
alignment = Alignment.CenterEnd
)
}

MyPagePurchase(stringResource(R.string.profile_first_purchase_description))
Spacer(modifier = Modifier.padding(top = 4.dp))
MyPagePurchase(stringResource(R.string.profile_no_ticket))
VideoList(
stringResource(R.string.profile_total_view_history),
stringResource(R.string.profile_no_view_history)
videoDescription = stringResource(R.string.profile_total_view_history),
emptyDescription = stringResource(R.string.profile_no_view_history)
)
VideoList(
stringResource(R.string.profile_interest_program),
stringResource(R.string.profile_no_interest_program)
videoDescription = stringResource(R.string.profile_interest_program),
emptyDescription = stringResource(R.string.profile_no_interest_program)
)
}
}
Expand Down

0 comments on commit a147228

Please sign in to comment.