Skip to content

Commit

Permalink
#187 refactor viewModel에서 context 참조하고 있던 거 Mapper로 로직 옮겨서 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Jul 25, 2023
1 parent cf727bf commit 48ac015
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
4 changes: 4 additions & 0 deletions core/common/src/main/res/values/color.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main">#22A7CC</color>
</resources>
3 changes: 3 additions & 0 deletions core/common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@

<string name="highlightWord1">찾고</string>
<string name="highlightWord2">소통</string>

<string name="failedLoading">식약처로 부터 데이터를 불러오는데 실패했습니다.</string>
<string name="highlightWord3">데이터</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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


/**
Expand All @@ -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<List<RecallSuspensionListItemDto>>) {
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()
}


Expand All @@ -86,4 +98,4 @@ class RecentPenaltyListFragment :
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<RecallSuspensionListItemDto>>>(UiState.Initial)
val recallDisposalList get() = _recallDisposalList.asStateFlow()

private val _noHistoryText = MutableStateFlow<Spanned>(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 = {
Expand All @@ -61,4 +39,4 @@ class RecentPenaltyListViewModel @Inject constructor(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
</layout>

0 comments on commit 48ac015

Please sign in to comment.