Skip to content

Commit

Permalink
rename : UiState관련 파일 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonhh committed Nov 24, 2024
1 parent 48d2221 commit 935fa9b
Show file tree
Hide file tree
Showing 30 changed files with 98 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.com_us.data.model
package com.example.com_us.base.data

import com.example.com_us.data.default_repository.NetworkError
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -20,8 +19,8 @@ fun <T> BaseResponse<T>.toResult(): Result<T> {
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))
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))
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
package com.example.com_us.data.default_repository

import com.example.com_us.base.data.NetworkError
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 com.example.com_us.base.data.toResult
import javax.inject.Inject



sealed class NetworkError : Exception(){
data class ApiError(
val statusCode : Int,
override val message : String
) : NetworkError()

data class NetworkException(
override val cause : Throwable,
) : NetworkError()

data class NullDataError(
override val message : String = "Data is null"
) : NetworkError()

}


class DefaultHomeRepository @Inject constructor(
private val defaultHomeDataSource: DefaultHomeDataSource
) : HomeRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.example.com_us.data.default_repository

import com.example.com_us.base.data.NetworkError
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 com.example.com_us.base.data.toResult
import javax.inject.Inject

class DefaultProfileRepository @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.com_us.data.default_repository

import com.example.com_us.base.data.NetworkError
import com.example.com_us.data.model.question.request.RequestAnswerRequest
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailDto
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailWithDateDto
Expand All @@ -8,7 +9,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 com.example.com_us.base.data.toResult
import javax.inject.Inject

class DefaultQuestionRepository @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.com_us.data.default_source

import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.home.ResponseHomeDataDto
import com.example.com_us.data.service.HomeService
import com.example.com_us.data.source.HomeDataSource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.com_us.data.default_source

import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.question.response.question.ResponseProfileDto
import com.example.com_us.data.service.ProfileService
import com.example.com_us.data.source.ProfileDataSource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.com_us.data.default_source

import com.example.com_us.data.model.question.request.RequestAnswerRequest
import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailDto
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailWithDateDto
import com.example.com_us.data.model.question.response.question.ResponsePreviousAnswerDto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.com_us.data.service

import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.home.ResponseHomeDataDto
import retrofit2.http.GET

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.com_us.data.service

import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.question.response.question.ResponseProfileDto
import retrofit2.http.GET

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.com_us.data.service

import com.example.com_us.data.model.question.request.RequestAnswerRequest
import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailDto
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailWithDateDto
import com.example.com_us.data.model.question.response.question.ResponsePreviousAnswerDto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.com_us.data.source

import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.home.ResponseHomeDataDto

interface HomeDataSource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.com_us.data.source

import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.question.response.question.ResponseProfileDto

interface ProfileDataSource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.com_us.data.source

import com.example.com_us.data.model.question.request.RequestAnswerRequest
import com.example.com_us.data.model.BaseResponse
import com.example.com_us.base.data.BaseResponse
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailDto
import com.example.com_us.data.model.question.response.question.ResponseAnswerDetailWithDateDto
import com.example.com_us.data.model.question.response.question.ResponsePreviousAnswerDto
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/com/example/com_us/ui/ApiResult.kt

This file was deleted.

9 changes: 9 additions & 0 deletions app/src/main/java/com/example/com_us/ui/base/UiState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.com_us.ui.base


// 성공 / 실패에 따른 UI 처리
sealed class UiState<out T> {
data object Initial : UiState<Nothing>()
data class Success<T>(val data : T) : UiState<T>()
data class Error(val message : String) : UiState<Nothing>()
}
7 changes: 3 additions & 4 deletions app/src/main/java/com/example/com_us/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import com.example.com_us.R
import com.example.com_us.data.model.home.Block
import com.example.com_us.data.model.home.Category
import com.example.com_us.databinding.FragmentHomeBinding
import com.example.com_us.ui.ApiResult
import com.example.com_us.ui.base.UiState
import com.example.com_us.ui.question.theme.ThemeQuestionListActivity
import com.example.com_us.util.ColorMatch
import com.example.com_us.util.ServerResponseHandler
import com.example.com_us.util.ThemeType
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -98,7 +97,7 @@ class HomeFragment : Fragment(), View.OnClickListener {
repeatOnLifecycle(Lifecycle.State.STARTED) {
homeViewModel.homeUiState.collect {
when (it) {
is ApiResult.Success -> {
is UiState.Success -> {
binding.constraintHome.visibility = View.VISIBLE
val chatMinute = it.data.user.todayChatTime.substring(3, 5).toInt()
binding.textviewHomeGreeting.text = String.format(
Expand All @@ -120,7 +119,7 @@ class HomeFragment : Fragment(), View.OnClickListener {
isReload.value = true

}
is ApiResult.Error -> {
is UiState.Error -> {
Toast.makeText(context,"잠시 후에 다시 시도해주세요!",Toast.LENGTH_SHORT).show()
}
else -> {}
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/example/com_us/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package com.example.com_us.ui.home

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.base.data.NetworkError
import com.example.com_us.data.repository.HomeRepository
import com.example.com_us.data.model.home.ResponseHomeDataDto
import com.example.com_us.ui.ApiResult
import com.example.com_us.ui.base.UiState
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 okhttp3.Response
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(private val homeRepository: HomeRepository) : ViewModel() {
private val _homeUiState= MutableStateFlow<ApiResult<ResponseHomeDataDto>>(ApiResult.Initial)
private val _homeUiState= MutableStateFlow<UiState<ResponseHomeDataDto>>(UiState.Initial)
val homeUiState = _homeUiState.asStateFlow()

var serverResponseHandler: ServerResponseHandler? = null
Expand All @@ -31,16 +29,19 @@ class HomeViewModel @Inject constructor(private val homeRepository: HomeReposito
viewModelScope.launch {
homeRepository.getHomeData()
.onSuccess {
_homeUiState.value = ApiResult.Success(it)
_homeUiState.value = UiState.Success(it)
}
.onFailure {
val errorMessage = when(it) {
is NetworkError.NetworkException -> { it.message }
is NetworkError.NullDataError -> { "데이터가 준비중이에요!" }
else -> "알 수 없는 에러가 발생했습니다. 다시 시도해주세요!"
}
if (errorMessage!= null ) _homeUiState.value = ApiResult.Error(errorMessage)
if (errorMessage!= null ) _homeUiState.value = UiState.Error(errorMessage)
}
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ 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.ui.base.UiState
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
Expand Down Expand Up @@ -61,7 +58,7 @@ class ProfileFragment : Fragment() ,ServerResponseHandler{
lifecycleScope.launch {
profileViewModel.profileUiState.collect {
when (it) {
is ApiResult.Success -> {
is UiState.Success -> {
binding.constraintProfile.visibility = View.VISIBLE
val themeGraphRatioList: List<Float> = listOf(
it.data.answerStatistic.dailyQuestionRatio.toFloat(),
Expand All @@ -80,7 +77,7 @@ class ProfileFragment : Fragment() ,ServerResponseHandler{
changeThemeGraph(themeGraphRatioList)
}

is ApiResult.Error -> {
is UiState.Error -> {
Toast.makeText(context,"잠시 후에 다시 시도해주세요!",Toast.LENGTH_SHORT).show()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package com.example.com_us.ui.profile

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.base.data.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 com.example.com_us.ui.base.UiState
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() {

private val _profileUiState = MutableStateFlow<ApiResult<ResponseProfileDto>>(ApiResult.Initial)
private val _profileUiState = MutableStateFlow<UiState<ResponseProfileDto>>(UiState.Initial)
val profileUiState = _profileUiState.asStateFlow()


Expand All @@ -32,7 +25,7 @@ class ProfileViewModel @Inject constructor(private val profileRepository: Profil
viewModelScope.launch {
profileRepository.getProfileData()
.onSuccess {
_profileUiState.value = ApiResult.Success(it)
_profileUiState.value = UiState.Success(it)
}
.onFailure {
val errorMessage = when (it) {
Expand All @@ -41,7 +34,7 @@ class ProfileViewModel @Inject constructor(private val profileRepository: Profil
else -> "알 수 없는 에러가 발생했습니다. 다시 시도해주세요!"
}
if (errorMessage != null) {
_profileUiState.value = ApiResult.Error(errorMessage)
_profileUiState.value = UiState.Error(errorMessage)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.ApiResult
import com.example.com_us.ui.base.UiState
import com.example.com_us.ui.compose.QuestionListItem
import com.example.com_us.ui.question.select.SelectAnswerActivity
import com.example.com_us.util.ThemeType
Expand Down Expand Up @@ -75,7 +75,7 @@ class AllQuestionListFragment : Fragment(), View.OnClickListener {
repeatOnLifecycle(Lifecycle.State.STARTED){
viewModel.apiResult.collect {
when(it) {
is ApiResult.Success -> {
is UiState.Success -> {
if (it.data.isNotEmpty()) {
setThemeSelected()
binding.constraintQuestion.visibility = View.VISIBLE
Expand All @@ -97,7 +97,7 @@ class AllQuestionListFragment : Fragment(), View.OnClickListener {
}
}
}
is ApiResult.Error -> {
is UiState.Error -> {
binding.constraintQuestion.visibility = View.GONE
Toast.makeText(context, it.toString(), Toast.LENGTH_SHORT).show()
}
Expand Down
Loading

0 comments on commit 935fa9b

Please sign in to comment.