From 90531d15440a4653b717a390e53e42776a8b0c58 Mon Sep 17 00:00:00 2001 From: Sunghyeon Date: Sat, 23 Nov 2024 01:28:41 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A7=88=EC=9D=B4=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EB=B3=B4=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - visibility로 인한 문제였음 --- .../DefaultHomeRepository.kt | 9 +-- .../DefaultProfileRepository.kt | 1 + .../DefaultQuestionRepository.kt | 1 + .../example/com_us/data/model/BaseResponse.kt | 11 +++ .../java/com/example/com_us/ui/ApiResult.kt | 9 +++ .../java/com/example/com_us/ui/UiState.kt | 9 --- .../com_us/ui/profile/ProfileFragment.kt | 72 ++++++++++++++----- .../com_us/ui/profile/ProfileViewModel.kt | 29 +++++--- .../question/list/AllQuestionListFragment.kt | 6 +- .../question/list/AllQuestionListViewModel.kt | 13 ++-- .../previous/PreviousAnswerActivity.kt | 4 +- .../previous/PreviousAnswerViewModel.kt | 15 ++-- .../question/select/SelectAnswerActivity.kt | 8 +-- .../question/select/SelectAnswerViewModel.kt | 10 +-- .../ui/question/sign/SignAnswerViewModel.kt | 12 ++-- .../theme/ThemeQuestionListActivity.kt | 7 +- .../theme/ThemeQuestionListViewModel.kt | 12 ++-- 17 files changed, 137 insertions(+), 91 deletions(-) create mode 100644 app/src/main/java/com/example/com_us/ui/ApiResult.kt delete mode 100644 app/src/main/java/com/example/com_us/ui/UiState.kt diff --git a/app/src/main/java/com/example/com_us/data/default_repository/DefaultHomeRepository.kt b/app/src/main/java/com/example/com_us/data/default_repository/DefaultHomeRepository.kt index 025b154..eb3a2b2 100644 --- a/app/src/main/java/com/example/com_us/data/default_repository/DefaultHomeRepository.kt +++ b/app/src/main/java/com/example/com_us/data/default_repository/DefaultHomeRepository.kt @@ -4,15 +4,10 @@ import com.example.com_us.data.model.home.ResponseHomeDataDto import com.example.com_us.data.repository.HomeRepository import com.example.com_us.data.default_source.DefaultHomeDataSource import com.example.com_us.data.model.BaseResponse +import com.example.com_us.data.model.toResult import javax.inject.Inject -fun BaseResponse.toResult(): Result { - return when { - status in 200..299 && data != null -> Result.success(data) - status in 200..299 && data == null -> Result.failure(NetworkError.NullDataError()) - else -> Result.failure(NetworkError.ApiError(status, message)) - } -} + sealed class NetworkError : Exception(){ data class ApiError( diff --git a/app/src/main/java/com/example/com_us/data/default_repository/DefaultProfileRepository.kt b/app/src/main/java/com/example/com_us/data/default_repository/DefaultProfileRepository.kt index 36c4a02..44aeb4f 100644 --- a/app/src/main/java/com/example/com_us/data/default_repository/DefaultProfileRepository.kt +++ b/app/src/main/java/com/example/com_us/data/default_repository/DefaultProfileRepository.kt @@ -3,6 +3,7 @@ package com.example.com_us.data.default_repository import com.example.com_us.data.model.question.response.question.ResponseProfileDto import com.example.com_us.data.repository.ProfileRepository import com.example.com_us.data.default_source.DefaultProfileDataSource +import com.example.com_us.data.model.toResult import javax.inject.Inject class DefaultProfileRepository @Inject constructor( diff --git a/app/src/main/java/com/example/com_us/data/default_repository/DefaultQuestionRepository.kt b/app/src/main/java/com/example/com_us/data/default_repository/DefaultQuestionRepository.kt index 59de12c..df25509 100644 --- a/app/src/main/java/com/example/com_us/data/default_repository/DefaultQuestionRepository.kt +++ b/app/src/main/java/com/example/com_us/data/default_repository/DefaultQuestionRepository.kt @@ -8,6 +8,7 @@ import com.example.com_us.data.model.question.response.question.ResponseQuestion import com.example.com_us.data.model.question.response.question.ResponseQuestionDto import com.example.com_us.data.repository.QuestionRepository import com.example.com_us.data.default_source.DefaultQuestionDataSource +import com.example.com_us.data.model.toResult import javax.inject.Inject class DefaultQuestionRepository @Inject constructor( diff --git a/app/src/main/java/com/example/com_us/data/model/BaseResponse.kt b/app/src/main/java/com/example/com_us/data/model/BaseResponse.kt index ec3c668..9178821 100644 --- a/app/src/main/java/com/example/com_us/data/model/BaseResponse.kt +++ b/app/src/main/java/com/example/com_us/data/model/BaseResponse.kt @@ -1,5 +1,6 @@ package com.example.com_us.data.model +import com.example.com_us.data.default_repository.NetworkError import kotlinx.serialization.Serializable @Serializable @@ -14,3 +15,13 @@ data class BaseResponseNoData( val status: Int, val message: String, ) + +fun BaseResponse.toResult(): Result { + return when{ + status == 200 && data != null -> Result.success(data) + status == 200 && data == null -> Result.failure(NetworkError.NullDataError()) + status == 400 or 401 -> Result.failure(NetworkError.ApiError(status,message)) + status == 500 -> Result.failure(NetworkError.ApiError(status,message)) + else -> Result.failure(NetworkError.ApiError(status,message)) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/com_us/ui/ApiResult.kt b/app/src/main/java/com/example/com_us/ui/ApiResult.kt new file mode 100644 index 0000000..ffc0bbc --- /dev/null +++ b/app/src/main/java/com/example/com_us/ui/ApiResult.kt @@ -0,0 +1,9 @@ +package com.example.com_us.ui + + +// 성공 / 실패에 따른 UI 처리 +sealed class ApiResult { + data object Initial : ApiResult() + data class Success(val data : T) : ApiResult() + data class Error(val message : String) : ApiResult() +} diff --git a/app/src/main/java/com/example/com_us/ui/UiState.kt b/app/src/main/java/com/example/com_us/ui/UiState.kt deleted file mode 100644 index d02272d..0000000 --- a/app/src/main/java/com/example/com_us/ui/UiState.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.com_us.ui - - -// 성공 / 실패에 따른 UI 처리 -sealed class UiState { - data object Initial : UiState() - data class Success(val data : T) : UiState() - data class Error(val message : String) : UiState() -} diff --git a/app/src/main/java/com/example/com_us/ui/profile/ProfileFragment.kt b/app/src/main/java/com/example/com_us/ui/profile/ProfileFragment.kt index e50706b..641b364 100644 --- a/app/src/main/java/com/example/com_us/ui/profile/ProfileFragment.kt +++ b/app/src/main/java/com/example/com_us/ui/profile/ProfileFragment.kt @@ -3,27 +3,35 @@ package com.example.com_us.ui.profile import android.graphics.drawable.GradientDrawable import android.os.Build import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout.LayoutParams +import android.widget.Toast import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.bitmap.RoundedCorners import com.bumptech.glide.request.RequestOptions import com.example.com_us.R import com.example.com_us.databinding.FragmentProfileBinding +import com.example.com_us.ui.ApiResult import com.example.com_us.util.ServerResponseHandler +import com.google.android.material.datepicker.OnSelectionChangedListener import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import java.time.LocalTime import java.time.format.DateTimeFormatter @AndroidEntryPoint -class ProfileFragment : Fragment(), ServerResponseHandler { +class ProfileFragment : Fragment() ,ServerResponseHandler{ private var _binding: FragmentProfileBinding? = null private val profileViewModel: ProfileViewModel by viewModels() @@ -38,31 +46,53 @@ class ProfileFragment : Fragment(), ServerResponseHandler { _binding = FragmentProfileBinding.inflate(inflater, container, false) profileViewModel.loadProfileData() - profileViewModel.serverResponseHandler = this - - setProfile() return binding.root } + @RequiresApi(Build.VERSION_CODES.O) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + setProfile() + super.onViewCreated(view, savedInstanceState) + } + @RequiresApi(Build.VERSION_CODES.O) private fun setProfile() { - profileViewModel.profileData.observe(viewLifecycleOwner) { - val themeGraphRatioList: List = listOf( - it.answerStatistic.dailyQuestionRatio.toFloat(), - it.answerStatistic.schoolQuestionRatio.toFloat(), - it.answerStatistic.friendQuestionRatio.toFloat(), - it.answerStatistic.familyQuestionRatio.toFloat(), - it.answerStatistic.hobbyQuestionRatio.toFloat(), - ) - setProfile(it.userInfo.name, it.userInfo.imageUrl) - setStatic(it.userInfo.totalChatTime, it.userInfo.totalChatCount) - changeTypeGraph(it.answerStatistic.multipleChoiceRatio.toFloat(), it.answerStatistic.sentenceRatio.toFloat()) - changeThemeGraph(themeGraphRatioList) + lifecycleScope.launch { + profileViewModel.profileUiState.collect { + when (it) { + is ApiResult.Success -> { + binding.constraintProfile.visibility = View.VISIBLE + val themeGraphRatioList: List = listOf( + it.data.answerStatistic.dailyQuestionRatio.toFloat(), + it.data.answerStatistic.schoolQuestionRatio.toFloat(), + it.data.answerStatistic.friendQuestionRatio.toFloat(), + it.data.answerStatistic.familyQuestionRatio.toFloat(), + it.data.answerStatistic.hobbyQuestionRatio.toFloat(), + ) + + setProfile(it.data.userInfo.name, it.data.userInfo.imageUrl) + setStatic(it.data.userInfo.totalChatTime, it.data.userInfo.totalChatCount) + changeTypeGraph( + it.data.answerStatistic.multipleChoiceRatio.toFloat(), + it.data.answerStatistic.sentenceRatio.toFloat() + ) + changeThemeGraph(themeGraphRatioList) + } + + is ApiResult.Error -> { + Toast.makeText(context,"잠시 후에 다시 시도해주세요!",Toast.LENGTH_SHORT).show() + } + + else -> {} + } + + } } } private fun setProfile(username: String, profileImgUrl: String) { + Log.d("profile","setProfile") binding.textviewProfileNickname.text = String.format(resources.getString(R.string.profile_username), username) Glide.with(this) .load(profileImgUrl) @@ -72,12 +102,14 @@ class ProfileFragment : Fragment(), ServerResponseHandler { @RequiresApi(Build.VERSION_CODES.O) private fun setStatic(totalChatTime: String, totalChatCount: Int) { + Log.d("profile","setStatic") val totalChatTime = stringToLocalTime(totalChatTime) binding.textviewTimeTaken.text = String.format(resources.getString(R.string.profile_time_taken_minute), totalChatTime.toSecondOfDay() / 60) binding.textviewConvCount.text = String.format(resources.getString(R.string.profile_conv_count_count), totalChatCount) } private fun changeTypeGraph(choiceRatio: Float, interactionRatio: Float) { + Log.d("profile","changeTypeGraph") changeGraphShape(R.drawable.shape_type_graph_fill_stroke_rect34_blue, choiceRatio, GraphPosition.LEFT) changeGraphShape(R.drawable.shape_type_graph_fill_stroke_rect34_pink, interactionRatio, GraphPosition.RIGHT) @@ -89,6 +121,7 @@ class ProfileFragment : Fragment(), ServerResponseHandler { } private fun changeThemeGraph(themeGraphRatioList: List) { + Log.d("profile","changeThemeGraph") val themeGraphBg: List = listOf( R.drawable.shape_theme_graph_fill_rect_orange700, R.drawable.shape_theme_graph_fill_rect_blue700, @@ -123,6 +156,7 @@ class ProfileFragment : Fragment(), ServerResponseHandler { changeGraphLayoutWeight(binding.viewProfileThemeGraphFamily, themeGraphRatioList[3]) changeGraphLayoutWeight(binding.viewProfileThemeGraphInterest, themeGraphRatioList[4]) + print(binding) binding.includeProfileGraphThemeDaily.textviewGraphPercent.text = String.format(resources.getString(R.string.percent), themeGraphRatioList[0].toInt()) binding.includeProfileGraphThemeSchool.textviewGraphPercent.text = String.format(resources.getString(R.string.percent), themeGraphRatioList[1].toInt()) binding.includeProfileGraphThemeFriend.textviewGraphPercent.text = String.format(resources.getString(R.string.percent), themeGraphRatioList[2].toInt()) @@ -131,6 +165,7 @@ class ProfileFragment : Fragment(), ServerResponseHandler { } private fun changeGraphLayoutWeight(view: View, ratio: Float) { + Log.d("profile","changeGraphLayoutWeight") val marginInDp = resources.getDimensionPixelSize(R.dimen.profile_graph_margin) view.layoutParams = LayoutParams( LayoutParams.WRAP_CONTENT, @@ -142,6 +177,7 @@ class ProfileFragment : Fragment(), ServerResponseHandler { } private fun changeGraphShape(graphDrawableId: Int, ratio: Float, position: GraphPosition) { + Log.d("profile","changeGraphShape") val drawable = context?.let { ContextCompat.getDrawable(it, graphDrawableId) } as? GradientDrawable if(ratio >= 100.0) { drawable?.cornerRadius = 40f @@ -160,6 +196,7 @@ class ProfileFragment : Fragment(), ServerResponseHandler { @RequiresApi(Build.VERSION_CODES.O) fun stringToLocalTime(timeString: String): LocalTime { + Log.d("profile","stringToLocalTime") val formatter = DateTimeFormatter.ofPattern("HH:mm:ss") return LocalTime.parse(timeString, formatter) @@ -174,10 +211,11 @@ class ProfileFragment : Fragment(), ServerResponseHandler { RIGHT, MIDDLE, LEFT } + @RequiresApi(Build.VERSION_CODES.O) override fun onServerSuccess() { - binding.constraintProfile.visibility = View.VISIBLE } override fun onServerFailure() { + TODO("Not yet implemented") } } \ No newline at end of file diff --git a/app/src/main/java/com/example/com_us/ui/profile/ProfileViewModel.kt b/app/src/main/java/com/example/com_us/ui/profile/ProfileViewModel.kt index 2b5afac..58f3c2e 100644 --- a/app/src/main/java/com/example/com_us/ui/profile/ProfileViewModel.kt +++ b/app/src/main/java/com/example/com_us/ui/profile/ProfileViewModel.kt @@ -4,32 +4,45 @@ import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.asFlow import androidx.lifecycle.viewModelScope +import com.example.com_us.data.default_repository.NetworkError import com.example.com_us.data.repository.ProfileRepository import com.example.com_us.data.model.question.response.question.ResponseProfileDto +import com.example.com_us.ui.ApiResult import com.example.com_us.util.ServerResponseHandler import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch +import kotlinx.serialization.builtins.UIntArraySerializer +import okhttp3.Response import javax.inject.Inject @HiltViewModel class ProfileViewModel @Inject constructor(private val profileRepository: ProfileRepository) : ViewModel() { - var serverResponseHandler: ServerResponseHandler? = null + private val _profileUiState = MutableStateFlow>(ApiResult.Initial) + val profileUiState = _profileUiState.asStateFlow() - private val _profileData = MutableLiveData() - val profileData: LiveData = _profileData - fun loadProfileData(){ + + + fun loadProfileData() { viewModelScope.launch { profileRepository.getProfileData() .onSuccess { - _profileData.value = it - serverResponseHandler?.onServerSuccess() + _profileUiState.value = ApiResult.Success(it) } .onFailure { - Log.d("GET: [PROFILE DATA]", it.toString()) - serverResponseHandler?.onServerFailure() + val errorMessage = when (it) { + is NetworkError.NetworkException -> { it.message } + is NetworkError.NullDataError -> { "데이터가 준비중이에요!" } + else -> "알 수 없는 에러가 발생했습니다. 다시 시도해주세요!" + } + if (errorMessage != null) { + _profileUiState.value = ApiResult.Error(errorMessage) + } } } } diff --git a/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListFragment.kt b/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListFragment.kt index 0a8a7de..f3cbe63 100644 --- a/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListFragment.kt +++ b/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListFragment.kt @@ -19,7 +19,7 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import com.example.com_us.R import com.example.com_us.databinding.FragmentQuestionBinding -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult import com.example.com_us.ui.compose.QuestionListItem import com.example.com_us.ui.question.select.SelectAnswerActivity import com.example.com_us.util.ThemeType @@ -73,9 +73,9 @@ class AllQuestionListFragment : Fragment(), View.OnClickListener { // 데이터 리스트 lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED){ - viewModel.uiState.collect { + viewModel.apiResult.collect { when(it) { - is UiState.Success -> { + is ApiResult.Success -> { if (it.data.isNotEmpty()) { setThemeSelected() binding.constraintQuestion.visibility = View.VISIBLE diff --git a/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListViewModel.kt b/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListViewModel.kt index e2b71e8..86a3755 100644 --- a/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListViewModel.kt +++ b/app/src/main/java/com/example/com_us/ui/question/list/AllQuestionListViewModel.kt @@ -6,14 +6,11 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.com_us.R import com.example.com_us.data.default_repository.NetworkError -import com.example.com_us.data.model.home.ResponseHomeDataDto -import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailWithDateDto import com.example.com_us.data.model.question.response.question.ResponseQuestionDto import com.example.com_us.data.repository.QuestionRepository -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult 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 javax.inject.Inject @@ -34,8 +31,8 @@ class AllQuestionListViewModel @Inject constructor( // ui 상태 변수 - private val _uiState = MutableStateFlow>>(UiState.Initial) - val uiState = _uiState.asStateFlow() + private val _apiResult = MutableStateFlow>>(ApiResult.Initial) + val apiResult = _apiResult.asStateFlow() // 선택한 테마의 id @@ -56,7 +53,7 @@ class AllQuestionListViewModel @Inject constructor( viewModelScope.launch { questionRepository.getQuestionListByCate(category) .onSuccess { - _uiState.value = UiState.Success(it) + _apiResult.value = ApiResult.Success(it) } .onFailure { val errorMessage = when(it){ @@ -65,7 +62,7 @@ class AllQuestionListViewModel @Inject constructor( else -> "알 수 없는 에러가 발생했습니다. 다시 시도해주세요!" } if (errorMessage != null) { - _uiState.value = UiState.Error(errorMessage) + _apiResult.value = ApiResult.Error(errorMessage) } } } } diff --git a/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerActivity.kt b/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerActivity.kt index b95bacd..cb1cda2 100644 --- a/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerActivity.kt +++ b/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerActivity.kt @@ -15,7 +15,7 @@ import androidx.lifecycle.repeatOnLifecycle import com.example.com_us.R import com.example.com_us.data.model.question.response.question.Answer import com.example.com_us.databinding.ActivityQuestionPreviousAnswerBinding -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult import com.example.com_us.ui.compose.AnswerHistoryItem import com.example.com_us.ui.compose.AnswerTypeTag import com.example.com_us.util.ColorMatch @@ -48,7 +48,7 @@ class PreviousAnswerActivity : AppCompatActivity() { repeatOnLifecycle(Lifecycle.State.STARTED){ viewModel.uiState.collect{ when(it){ - is UiState.Success -> { + is ApiResult.Success -> { setQuestion(it.data.question.questionCount, it.data.question.questionContent) setQuestionTypeCompose(it.data.question.category, it.data.question.answerType) setComposeList(it.data.answerList) diff --git a/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerViewModel.kt b/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerViewModel.kt index a9fd0f2..d11db2b 100644 --- a/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerViewModel.kt +++ b/app/src/main/java/com/example/com_us/ui/question/previous/PreviousAnswerViewModel.kt @@ -1,22 +1,17 @@ package com.example.com_us.ui.question.previous -import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.com_us.data.default_repository.NetworkError -import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailDto import com.example.com_us.data.model.question.response.question.ResponsePreviousAnswerDto -import com.example.com_us.data.model.question.response.question.ResponseQuestionDto import com.example.com_us.data.repository.QuestionRepository -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult 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 okhttp3.Response import javax.inject.Inject @@ -27,8 +22,8 @@ class PreviousAnswerViewModel @Inject constructor( ) : ViewModel(){ // ui 상태 변수 - private val _uiState = MutableStateFlow>(UiState.Initial) - val uiState= _uiState.asStateFlow() + private val _apiResult = MutableStateFlow>(ApiResult.Initial) + val uiState= _apiResult.asStateFlow() private val _answerPrevious = MutableLiveData() val answerPrevious: LiveData = _answerPrevious @@ -37,7 +32,7 @@ class PreviousAnswerViewModel @Inject constructor( viewModelScope.launch { questionRepository.getPreviousAnswer(questionId) .onSuccess { - _uiState.value = UiState.Success(it) + _apiResult.value = ApiResult.Success(it) } .onFailure { val errorMessage = when(it){ @@ -46,7 +41,7 @@ class PreviousAnswerViewModel @Inject constructor( else -> "알 수 없는 에러가 발생했어요. 다시 시도해주세요!" } if (errorMessage != null) { - _uiState.value = UiState.Error(errorMessage) + _apiResult.value = ApiResult.Error(errorMessage) } } } diff --git a/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerActivity.kt b/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerActivity.kt index 752c247..3ef79b6 100644 --- a/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerActivity.kt +++ b/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerActivity.kt @@ -12,7 +12,7 @@ import androidx.core.content.ContextCompat import androidx.lifecycle.lifecycleScope import com.example.com_us.R import com.example.com_us.databinding.ActivityQuestionDetailBinding -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult import com.example.com_us.util.ColorMatch import com.example.com_us.ui.compose.AnswerOptionList import com.example.com_us.ui.compose.AnswerTypeTag @@ -61,8 +61,8 @@ class SelectAnswerActivity : AppCompatActivity() { private fun setQuestionDetail() { lifecycleScope.launch { - viewModel.uiState.collect { - if (it is UiState.Success) { + viewModel.apiResult.collect { + if (it is ApiResult.Success) { binding.constraintQuestionDetail.visibility = View.VISIBLE binding.textviewDetailQuestion.text = it.data.question.questionContent question = it.data.question.questionContent @@ -71,7 +71,7 @@ class SelectAnswerActivity : AppCompatActivity() { setQuestionAnswerOptionCompose(it.data.answerList) } - if (it is UiState.Error) { + if (it is ApiResult.Error) { Toast.makeText( this@SelectAnswerActivity, it.message, diff --git a/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerViewModel.kt b/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerViewModel.kt index 7a4d179..62815ee 100644 --- a/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerViewModel.kt +++ b/app/src/main/java/com/example/com_us/ui/question/select/SelectAnswerViewModel.kt @@ -7,7 +7,7 @@ import androidx.lifecycle.viewModelScope import com.example.com_us.data.default_repository.NetworkError import com.example.com_us.data.model.question.response.question.ResponseQuestionDetailDto import com.example.com_us.data.repository.QuestionRepository -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -20,8 +20,8 @@ class SelectAnswerViewModel @Inject constructor( ): ViewModel() { // ui 상태 변수 - private val _uiState = MutableStateFlow>(UiState.Initial) - val uiState: StateFlow> = _uiState + private val _apiResult = MutableStateFlow>(ApiResult.Initial) + val apiResult: StateFlow> = _apiResult private val _questionDetail = MutableLiveData() val questionDetail: LiveData = _questionDetail @@ -38,7 +38,7 @@ class SelectAnswerViewModel @Inject constructor( viewModelScope.launch { questionRepository.getQuestionDetail(questionId) .onSuccess { - _uiState.value = UiState.Success(it) + _apiResult.value = ApiResult.Success(it) } .onFailure { println(it) @@ -48,7 +48,7 @@ class SelectAnswerViewModel @Inject constructor( is NetworkError.NetworkException -> { "잠시 후에 다시 시도해주세요" } else -> "알 수 없는 에러가 발생했어요. 다시 시도해주세요!" } - _uiState.value = UiState.Error(errorMessage) + _apiResult.value = ApiResult.Error(errorMessage) } } } diff --git a/app/src/main/java/com/example/com_us/ui/question/sign/SignAnswerViewModel.kt b/app/src/main/java/com/example/com_us/ui/question/sign/SignAnswerViewModel.kt index a5097b0..61b9fc0 100644 --- a/app/src/main/java/com/example/com_us/ui/question/sign/SignAnswerViewModel.kt +++ b/app/src/main/java/com/example/com_us/ui/question/sign/SignAnswerViewModel.kt @@ -8,7 +8,7 @@ import com.example.com_us.data.default_repository.NetworkError import com.example.com_us.data.repository.QuestionRepository import com.example.com_us.data.model.question.request.RequestAnswerRequest import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailWithDateDto -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow @@ -25,21 +25,21 @@ class SignAnswerViewModel @Inject constructor( private val _signIndex = MutableStateFlow(0) val signIndex = _signIndex.asStateFlow() - private val _uiState = MutableStateFlow>(UiState.Initial) - val uiState= _uiState.asStateFlow() + private val _apiResult = MutableStateFlow>(ApiResult.Initial) + val uiState= _apiResult.asStateFlow() private val _resultData = MutableLiveData() val resultData: LiveData = _resultData fun setSignIndex(index : Int ) { _signIndex.value = index } -// todo : +// todo : 이 함수의 역할은 뭐야? fun postAnswer(questionId: Long, answerContent: String){ val body = RequestAnswerRequest(questionId, answerContent) viewModelScope.launch { questionRepository.postAnswer(body) .onSuccess { - _uiState.value = UiState.Success(it) + _apiResult.value = ApiResult.Success(it) } .onFailure { val errorMessage = when(it) { @@ -48,7 +48,7 @@ class SignAnswerViewModel @Inject constructor( is NetworkError.NullDataError -> {"데이터를 준비하고 있어요!"} else -> { "잠시 후에 다시 시도해주세요"} } - _uiState.value = UiState.Error(errorMessage) + _apiResult.value = ApiResult.Error(errorMessage) } } } diff --git a/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListActivity.kt b/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListActivity.kt index 6814927..ae75a59 100644 --- a/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListActivity.kt +++ b/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListActivity.kt @@ -8,15 +8,12 @@ import android.widget.Toast import androidx.activity.viewModels import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.ui.platform.ViewCompositionStrategy -import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle import com.example.com_us.R import com.example.com_us.databinding.ActivityThemeQuestionListBinding -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult import com.example.com_us.ui.question.select.SelectAnswerActivity import com.example.com_us.ui.compose.QuestionListItem -import com.example.com_us.ui.question.list.AllQuestionListViewModel import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch @@ -48,7 +45,7 @@ class ThemeQuestionListActivity : AppCompatActivity() { lifecycleScope.launch { viewModel.uiState.collect { when (it) { - is UiState.Success -> { + is ApiResult.Success -> { binding.composeviewTheme.apply { setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) setContent { diff --git a/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListViewModel.kt b/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListViewModel.kt index 53d642f..7db27e4 100644 --- a/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListViewModel.kt +++ b/app/src/main/java/com/example/com_us/ui/question/theme/ThemeQuestionListViewModel.kt @@ -6,13 +6,11 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.com_us.R import com.example.com_us.data.default_repository.NetworkError -import com.example.com_us.data.model.question.response.question.ResponsePreviousAnswerDto import com.example.com_us.data.model.question.response.question.ResponseQuestionDto import com.example.com_us.data.repository.QuestionRepository -import com.example.com_us.ui.UiState +import com.example.com_us.ui.ApiResult 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 javax.inject.Inject @@ -29,8 +27,8 @@ class ThemeQuestionListViewModel @Inject constructor( // ui 상태 변수 - private val _uiState = MutableStateFlow>>(UiState.Initial) - val uiState = _uiState.asStateFlow() + private val _apiResult = MutableStateFlow>>(ApiResult.Initial) + val uiState = _apiResult.asStateFlow() // 선택한 테마의 id private val _selectedThemeId = MutableLiveData().apply { @@ -45,7 +43,7 @@ class ThemeQuestionListViewModel @Inject constructor( viewModelScope.launch { questionRepository.getQuestionListByCate(category) .onSuccess { - _uiState.value = UiState.Success(it) + _apiResult.value = ApiResult.Success(it) } .onFailure { val errorMessage = when(it){ @@ -54,7 +52,7 @@ class ThemeQuestionListViewModel @Inject constructor( else -> "알 수 없는 에러가 발생했습니다. 다시 시도해주세요!" } if (errorMessage != null) { - _uiState.value = UiState.Error(errorMessage) + _apiResult.value = ApiResult.Error(errorMessage) } } }