From 48ac015fb0a0bf2e834ae3d83bfa638a95abdbb7 Mon Sep 17 00:00:00 2001 From: tgyuu-An Date: Tue, 25 Jul 2023 23:42:48 +0900 Subject: [PATCH] =?UTF-8?q?#187=20refactor=20viewModel=EC=97=90=EC=84=9C?= =?UTF-8?q?=20context=20=EC=B0=B8=EC=A1=B0=ED=95=98=EA=B3=A0=20=EC=9E=88?= =?UTF-8?q?=EB=8D=98=20=EA=B1=B0=20Mapper=EB=A1=9C=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=98=AE=EA=B2=A8=EC=84=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/common/mapper/MedicineInfoMapper.kt | 23 +++++- core/common/src/main/res/values/color.xml | 4 + core/common/src/main/res/values/strings.xml | 3 + .../RecentPenaltyListFragment.kt | 76 +++++++++++-------- .../RecentPenaltyListViewModel.kt | 26 +------ .../layout/fragment_recent_penalty_list.xml | 3 +- 6 files changed, 75 insertions(+), 60 deletions(-) create mode 100644 core/common/src/main/res/values/color.xml diff --git a/core/common/src/main/java/com/android/mediproject/core/common/mapper/MedicineInfoMapper.kt b/core/common/src/main/java/com/android/mediproject/core/common/mapper/MedicineInfoMapper.kt index 69ccb9cde..3ce061ef3 100644 --- a/core/common/src/main/java/com/android/mediproject/core/common/mapper/MedicineInfoMapper.kt +++ b/core/common/src/main/java/com/android/mediproject/core/common/mapper/MedicineInfoMapper.kt @@ -3,11 +3,14 @@ package com.android.mediproject.core.common.mapper import android.content.Context import android.graphics.Typeface import android.text.Html +import android.text.Spannable import android.text.SpannableStringBuilder import android.text.Spanned +import android.text.style.ForegroundColorSpan import android.text.style.RelativeSizeSpan import android.text.style.StyleSpan import android.text.style.UnderlineSpan +import androidx.core.content.ContextCompat import androidx.core.text.toSpanned import com.android.mediproject.core.common.R import com.android.mediproject.core.model.remote.granule.GranuleIdentificationInfoDto @@ -188,17 +191,33 @@ class MedicineInfoMapper @Inject constructor() { fun initHeaderSpan(context: Context, text: String): SpannableStringBuilder { return SpannableStringBuilder(text).apply { - val underline1Idx = text.indexOf(context.getString(R.string.highlightWord1)) to text.indexOf(context.getString(R.string.highlightWord1)) + 2 + val underline1Idx = + text.indexOf(context.getString(R.string.highlightWord1)) to text.indexOf(context.getString(R.string.highlightWord1)) + 2 setSpan(UnderlineSpan(), underline1Idx.first, underline1Idx.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE) setSpan(StyleSpan(Typeface.BOLD), underline1Idx.first, underline1Idx.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE) setSpan(RelativeSizeSpan(TEXT_SIZE_PERCENT), underline1Idx.first, underline1Idx.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE) - val underline2Idx = text.indexOf(context.getString(R.string.highlightWord2)) to text.indexOf(context.getString(R.string.highlightWord2)) + 2 + val underline2Idx = + text.indexOf(context.getString(R.string.highlightWord2)) to text.indexOf(context.getString(R.string.highlightWord2)) + 2 setSpan(UnderlineSpan(), underline2Idx.first, underline2Idx.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE) setSpan(StyleSpan(Typeface.BOLD), underline2Idx.first, underline2Idx.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE) setSpan(RelativeSizeSpan(TEXT_SIZE_PERCENT), underline2Idx.first, underline2Idx.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE) } } + + fun getNoHistorySpan(context: Context): SpannableStringBuilder { + val text = context.getString(R.string.failedLoading) + val highLightIndex = text.indexOf(context.getString(R.string.highlightWord3)) + return SpannableStringBuilder(text).apply { + setSpan( + ForegroundColorSpan(ContextCompat.getColor(context, R.color.main)), + highLightIndex, + highLightIndex + 3, + Spannable.SPAN_INCLUSIVE_INCLUSIVE, + ) + setSpan(UnderlineSpan(), highLightIndex, highLightIndex + 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE) + } + } } diff --git a/core/common/src/main/res/values/color.xml b/core/common/src/main/res/values/color.xml new file mode 100644 index 000000000..3f3a75ba4 --- /dev/null +++ b/core/common/src/main/res/values/color.xml @@ -0,0 +1,4 @@ + + + #22A7CC + diff --git a/core/common/src/main/res/values/strings.xml b/core/common/src/main/res/values/strings.xml index 1c35abe12..4f1e71478 100644 --- a/core/common/src/main/res/values/strings.xml +++ b/core/common/src/main/res/values/strings.xml @@ -76,4 +76,7 @@ 찾고 소통 + + 식약처로 부터 데이터를 불러오는데 실패했습니다. + 데이터 diff --git a/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListFragment.kt b/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListFragment.kt index a596baee8..93c1f3a87 100644 --- a/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListFragment.kt +++ b/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListFragment.kt @@ -4,15 +4,18 @@ import android.os.Bundle import android.view.View import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController +import com.android.mediproject.core.common.mapper.MedicineInfoMapper import com.android.mediproject.core.common.util.deepNavigate import com.android.mediproject.core.common.util.navigateByDeepLink import com.android.mediproject.core.common.viewmodel.UiState import com.android.mediproject.core.model.local.navargs.RecallDisposalArgs +import com.android.mediproject.core.model.remote.recall.RecallSuspensionListItemDto import com.android.mediproject.core.ui.base.BaseFragment import com.android.mediproject.core.ui.base.view.stateAsCollect import com.android.mediproject.feature.penalties.databinding.FragmentRecentPenaltyListBinding import dagger.hilt.android.AndroidEntryPoint import repeatOnStarted +import javax.inject.Inject /** @@ -29,46 +32,55 @@ class RecentPenaltyListFragment : } override val fragmentViewModel: RecentPenaltyListViewModel by viewModels() + lateinit var penaltyListAdapter: PenaltyListAdapter - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) + @Inject + lateinit var medicineInfoMapper: MedicineInfoMapper - initHeader() - fragmentViewModel.createNoHistoryText(requireContext()) - binding.apply { - penaltyList.setHasFixedSize(true) + private fun setRecyclerView() { + penaltyListAdapter = PenaltyListAdapter() + binding.penaltyList.apply { + setHasFixedSize(true) + adapter = penaltyListAdapter + } + } - val penaltyListAdapter = PenaltyListAdapter() - penaltyList.adapter = penaltyListAdapter + private fun handleUiState(uiState: UiState>) { + when (uiState) { + is UiState.Error -> {} + is UiState.Initial -> {} + is UiState.Loading -> {} + is UiState.Success -> { + uiState.data.forEach { itemDto -> + itemDto.onClick = { + findNavController().deepNavigate( + "medilens://main/news_nav", RecallDisposalArgs(it.product), + ) + } + } + penaltyListAdapter.submitList(uiState.data) + } + } + } + private fun setBinding() { + binding.apply { viewLifecycleOwner.repeatOnStarted { - fragmentViewModel.recallDisposalList.stateAsCollect(headerView, noHistoryTextView).collect { uiState -> - when (uiState) { - is UiState.Error -> { - - } - - is UiState.Initial -> {} - - is UiState.Loading -> {} - - is UiState.Success -> { - - uiState.data.forEach { itemDto -> - itemDto.onClick = { - findNavController().deepNavigate( - "medilens://main/news_nav", RecallDisposalArgs(it.product), - ) - } - } - - penaltyListAdapter.submitList(uiState.data) - } + fragmentViewModel.apply { + recallDisposalList.stateAsCollect(headerView, noHistoryTextView).collect { uiState -> + handleUiState(uiState) } } + noHistoryTextView.text = medicineInfoMapper.getNoHistorySpan(requireContext()) } - } + setRecyclerView() + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + initHeader() + setBinding() } @@ -86,4 +98,4 @@ class RecentPenaltyListFragment : ) } } -} \ No newline at end of file +} diff --git a/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListViewModel.kt b/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListViewModel.kt index 9a311430a..b80ed4542 100644 --- a/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListViewModel.kt +++ b/feature/penalties/src/main/java/com/android/mediproject/feature/penalties/recentpenaltylist/RecentPenaltyListViewModel.kt @@ -24,33 +24,11 @@ import javax.inject.Inject @HiltViewModel class RecentPenaltyListViewModel @Inject constructor( - private val getRecallSuspensionInfoUseCase: GetRecallSuspensionInfoUseCase, - @Dispatcher(MediDispatchers.Default) private val defaultDispatcher: CoroutineDispatcher) : BaseViewModel() { + private val getRecallSuspensionInfoUseCase: GetRecallSuspensionInfoUseCase) : BaseViewModel() { private val _recallDisposalList = MutableStateFlow>>(UiState.Initial) val recallDisposalList get() = _recallDisposalList.asStateFlow() - private val _noHistoryText = MutableStateFlow(SpannableStringBuilder()) - val noHistoryText get() = _noHistoryText.asStateFlow() - - fun createNoHistoryText(context: Context) { - viewModelScope.launch(defaultDispatcher) { - val text = context.getString(com.android.mediproject.feature.penalties.R.string.failedLoading) - val firstIdx = text.indexOf("데이터") - val span = SpannableStringBuilder(text).apply { - setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.main)), - firstIdx, - firstIdx + 3, - Spannable.SPAN_INCLUSIVE_INCLUSIVE) - setSpan(UnderlineSpan(), firstIdx, firstIdx + 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE) - } - _noHistoryText.value = span - } - } - - /** - * 회수 폐기 공고 목록을 로드 - */ init { viewModelScope.launch { getRecallSuspensionInfoUseCase.getRecentRecallDisposalList(numOfRows = 5).fold(onSuccess = { @@ -61,4 +39,4 @@ class RecentPenaltyListViewModel @Inject constructor( } } -} \ No newline at end of file +} diff --git a/feature/penalties/src/main/res/layout/fragment_recent_penalty_list.xml b/feature/penalties/src/main/res/layout/fragment_recent_penalty_list.xml index 039b85d66..d101c66fc 100644 --- a/feature/penalties/src/main/res/layout/fragment_recent_penalty_list.xml +++ b/feature/penalties/src/main/res/layout/fragment_recent_penalty_list.xml @@ -41,11 +41,10 @@ android:layout_marginBottom="40dp" android:bufferType="spannable" android:gravity="center" - android:text="@{viewModel.noHistoryText}" android:textColor="@color/gray2" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@id/headerView" /> - \ No newline at end of file +