Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optimized main activity performance and fixed graph display
  • Loading branch information
Javinator9889 committed Jun 30, 2020
1 parent 61a50ce commit 3e70411
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 41 deletions.
Expand Up @@ -64,7 +64,7 @@ import kotlinx.coroutines.launch
import timber.log.Timber


class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange, View.OnClickListener {
override val layoutId: Int = R.layout.main_disease_view

private lateinit var parsedHTMLTexts: List<ParsedHTMLText>
Expand All @@ -84,23 +84,23 @@ class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
loading.visibility = View.VISIBLE
countLoader.visibility = View.VISIBLE
informationViewModel.parsedHTMLText.observe(viewLifecycleOwner) {
if (it.isEmpty())
return@observe
parsedHTMLTexts = it
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
val disease = Disease(animation, parsedText, layoutId, i)
if (diseasesAdapter.getAdapterPosition(disease) == -1)
diseasesAdapter.add(disease)
}
loading.visibility = View.INVISIBLE
container.visibility = View.VISIBLE
if (it.isEmpty())
return@observe
parsedHTMLTexts = it
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
val disease = Disease(animation, parsedText, layoutId, i)
if (diseasesAdapter.getAdapterPosition(disease) == -1)
diseasesAdapter.add(disease)
}
loading.visibility = View.INVISIBLE
container.visibility = View.VISIBLE
}
handwashingViewModel.allData.observe(viewLifecycleOwner) {
lifecycleScope.launch {
val dataSet = BarDataSet(it.toBarEntry(), "label")
Expand Down Expand Up @@ -149,26 +149,15 @@ 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)
view.countChart.xAxis.setDrawGridLines(false)
view.countChart.invalidate()
view.countUpButton.setOnClickListener {
behavior.addBottomSheetCallback(BottomSheetStateCallback())
// view.countChart.setDrawGridBackground(false)
// view.countChart.axisLeft.setDrawGridLines(false)
// view.countChart.axisRight.setDrawGridLines(false)
// view.countChart.xAxis.setDrawGridLines(false)
// view.countChart.invalidate()
view.countUpButton.setOnClickListener(this)
view.countDownButton.setOnClickListener(this)
/*view.countUpButton.setOnClickListener {
lifecycleScope.launch {
val createdItem =
handwashingViewModel.getAsync(CalendarUtils.today.time)
Expand Down Expand Up @@ -200,7 +189,7 @@ class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
)
handwashingViewModel.decrement(CalendarUtils.today.time)
}
}
}*/
lifecycleScope.launch {
val countUpText = getText(R.string.add_another)
val countDownText = getText(R.string.reduce_count)
Expand Down Expand Up @@ -242,6 +231,40 @@ class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
lifecycleScope.launchWhenCreated { informationViewModel.parseHtml() }*/
}

override fun onClick(v: View?) {
when (v) {
countUpButton -> lifecycleScope.launch {
val createdItem =
handwashingViewModel.getAsync(CalendarUtils.today.time)
.await()
if (createdItem == null)
handwashingViewModel.create(
Handwashing(
CalendarUtils.today.time,
0
)
)
handwashingViewModel.increment(CalendarUtils.today.time)
leaves.visibility = View.VISIBLE
if (!leaves.isAnimating)
leaves.playAnimation()
}
countDownButton -> lifecycleScope.launch {
val createdItem =
handwashingViewModel.getAsync(CalendarUtils.today.time)
.await()
if (createdItem == null)
handwashingViewModel.create(
Handwashing(
CalendarUtils.today.time,
0
)
)
handwashingViewModel.decrement(CalendarUtils.today.time)
}
}
}

private suspend fun setCountText(
view: MaterialTextView,
@StringRes id: Int,
Expand Down Expand Up @@ -285,4 +308,20 @@ class DiseasesFragment : BaseFragmentView(), LayoutVisibilityChange {
)
}
}

private inner class BottomSheetStateCallback :
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) {}
}
}
Expand Up @@ -18,6 +18,8 @@
*/
package com.javinator9889.handwashingreminder.utils

import android.util.SparseArray
import androidx.core.util.set
import com.github.mikephil.charting.data.BarEntry
import com.javinator9889.handwashingreminder.data.room.entities.Handwashing
import com.javinator9889.handwashingreminder.utils.calendar.CalendarUtils
Expand Down Expand Up @@ -53,14 +55,23 @@ fun List<Date>.closest(): Date {

fun List<Handwashing>.toBarEntry(): List<BarEntry> {
val entryBars = mutableListOf<BarEntry>()
val daysSorted = mutableListOf<Int>()
val daysEntriesArray = SparseArray<Float>(size)
for (entry in this) {
val daysBetween =
(CalendarUtils.today.time.time - entry.date.time).run {
TimeUnit.DAYS.convert(this, TimeUnit.MILLISECONDS).toFloat()
-TimeUnit.DAYS.convert(this, TimeUnit.MILLISECONDS).toInt()
}

daysEntriesArray[daysBetween] = entry.amount.toFloat()
daysSorted.add(daysBetween)
}
daysSorted.sort()
for (daysDifference in daysSorted) {
entryBars.add(
BarEntry(daysBetween, entry.amount.toFloat())
BarEntry(
daysDifference.toFloat(),
daysEntriesArray[daysDifference]
)
)
}
return entryBars
Expand Down

0 comments on commit 3e70411

Please sign in to comment.