Skip to content

Commit

Permalink
Load disease information whether is visible while sliding up the bott…
Browse files Browse the repository at this point in the history
…om sheet
  • Loading branch information
Javinator9889 committed Jun 30, 2020
1 parent 35604f9 commit 61a50ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Expand Up @@ -87,18 +87,16 @@ class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
if (it.isEmpty())
return@observe
parsedHTMLTexts = it
upperAdsAdapter.add(Ads())
lowerAdsAdapter.add(Ads())
it.forEachIndexed { i, parsedText ->
val animation =
if (i % 2 == 0) R.raw.virus_red
else R.raw.virus_loader
val layoutId =
if (i % 2 == 0) R.layout.disease_card_layout
else R.layout.disease_card_alt_layout
diseasesAdapter.add(
Disease(animation, parsedText, layoutId, i)
)
val disease = Disease(animation, parsedText, layoutId, i)
if (diseasesAdapter.getAdapterPosition(disease) == -1)
diseasesAdapter.add(disease)
}
loading.visibility = View.INVISIBLE
container.visibility = View.VISIBLE
Expand Down Expand Up @@ -151,6 +149,20 @@ class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
fastAdapter.addEventHook(DiseaseClickEventHook())
fastAdapter.withSavedInstanceState(savedInstanceState)
behavior = BottomSheetBehavior.from(view.contentLayout)
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == STATE_EXPANDED) {
if (upperAdsAdapter.adapterItemCount == 0)
upperAdsAdapter.add(Ads())
if (lowerAdsAdapter.adapterItemCount == 0)
lowerAdsAdapter.add(Ads())
informationViewModel.parseHtml()
}
}

override fun onSlide(bottomSheet: View, slideOffset: Float) {}

})
view.countChart.setDrawGridBackground(false)
view.countChart.axisLeft.setDrawGridLines(false)
view.countChart.axisRight.setDrawGridLines(false)
Expand Down Expand Up @@ -226,8 +238,8 @@ class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
}

override fun onVisibilityChanged(visibility: Int) {
if (visibility == View.VISIBLE)
lifecycleScope.launchWhenCreated { informationViewModel.parseHtml() }
/*if (visibility == View.VISIBLE)
lifecycleScope.launchWhenCreated { informationViewModel.parseHtml() }*/
}

private suspend fun setCountText(
Expand Down
Expand Up @@ -36,9 +36,13 @@ data class Disease(
override val type: Int
) : AbstractItem<Disease.ViewHolder>() {

init {
identifier = type.toLong()
}

override fun getViewHolder(v: View): ViewHolder = ViewHolder(v)

class ViewHolder(private val view: View) :
class ViewHolder(view: View) :
FastAdapter.ViewHolder<Disease>(view) {
val cardContainer: MaterialCardView =
view.findViewById(R.id.cardDiseaseContainer)
Expand Down
Expand Up @@ -35,6 +35,7 @@ import kotlinx.coroutines.*
import org.sufficientlysecure.htmltextview.HtmlFormatter
import org.sufficientlysecure.htmltextview.HtmlFormatterBuilder
import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean

private const val DATA_KEY = "text:html:text"
private const val PARSED_JSON_KEY = "text:json:parsed"
Expand All @@ -43,6 +44,7 @@ class DiseaseInformationViewModel(
private val state: SavedStateHandle
) : ViewModel() {
private val informationList: DiseasesList = loadHtmlData()
private val isHTMLParsed = AtomicBoolean(false)
val parsedHTMLText: MutableLiveData<List<ParsedHTMLText>> =
state.getLiveData(DATA_KEY, emptyList())

Expand All @@ -60,7 +62,9 @@ class DiseaseInformationViewModel(

fun parseHtml() = viewModelScope.launch {
Timber.d("Parsing HTML")
if (!state.get<List<ParsedHTMLText>>(DATA_KEY).isNullOrEmpty())
if (!state.get<List<ParsedHTMLText>>(DATA_KEY)
.isNullOrEmpty() || isHTMLParsed.get()
)
return@launch
val parsedItemsList =
ArrayList<ParsedHTMLText>(informationList.diseases.size)
Expand Down Expand Up @@ -104,6 +108,7 @@ class DiseaseInformationViewModel(
}
}
}
isHTMLParsed.set(true)
}

private fun createHTML(htmlText: String): Spanned =
Expand Down

0 comments on commit 61a50ce

Please sign in to comment.