Skip to content

Commit

Permalink
Scroll statistics to the end initially and show progress indicator wh…
Browse files Browse the repository at this point in the history
…ile statistics are loading
  • Loading branch information
mjaakko committed Feb 9, 2024
1 parent 2684915 commit 68a8cde
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package xyz.malkki.neostumbler.ui.screens

import android.text.format.DateFormat
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
Expand All @@ -18,7 +23,9 @@ import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis
import com.patrykandpatrick.vico.compose.chart.Chart
import com.patrykandpatrick.vico.compose.chart.line.lineChart
import com.patrykandpatrick.vico.compose.chart.scroll.rememberChartScrollSpec
import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer
import com.patrykandpatrick.vico.core.scroll.InitialScroll
import xyz.malkki.neostumbler.R
import xyz.malkki.neostumbler.extensions.defaultLocale
import xyz.malkki.neostumbler.ui.viewmodel.StatisticsViewModel
Expand Down Expand Up @@ -48,17 +55,29 @@ private fun StationsByDayChart(entryModel: ChartEntryModelProducer, title: Strin
},
itemPlacer = remember { TextLabelItemPlacer() }
),
chartScrollSpec = rememberChartScrollSpec(initialScroll = InitialScroll.End),
modifier = Modifier.height(320.dp)
)
}
@Composable
fun StatisticsScreen(statisticsViewModel: StatisticsViewModel = viewModel()) {
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
StationsByDayChart(entryModel = statisticsViewModel.wifiEntryModel, title = stringResource(id = R.string.wifis))
Spacer(modifier = Modifier.height(16.dp))
StationsByDayChart(entryModel = statisticsViewModel.cellEntryModel, title = stringResource(id = R.string.cells))
Spacer(modifier = Modifier.height(16.dp))
StationsByDayChart(entryModel = statisticsViewModel.beaconEntryModel, title = stringResource(id = R.string.beacons))
Spacer(modifier = Modifier.height(16.dp))
val dataLoaded = statisticsViewModel.dataLoaded.observeAsState(initial = false)

if (!dataLoaded.value) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
} else {
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
StationsByDayChart(entryModel = statisticsViewModel.wifiEntryModel, title = stringResource(id = R.string.wifis))
Spacer(modifier = Modifier.height(16.dp))
StationsByDayChart(entryModel = statisticsViewModel.cellEntryModel, title = stringResource(id = R.string.cells))
Spacer(modifier = Modifier.height(16.dp))
StationsByDayChart(entryModel = statisticsViewModel.beaconEntryModel, title = stringResource(id = R.string.beacons))
Spacer(modifier = Modifier.height(16.dp))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package xyz.malkki.neostumbler.ui.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.viewModelScope
import com.patrykandpatrick.vico.core.entry.ChartEntry
import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer
import com.patrykandpatrick.vico.core.entry.entryOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import xyz.malkki.neostumbler.StumblerApplication
import java.util.SortedMap
Expand All @@ -22,8 +27,24 @@ class StatisticsViewModel(application: Application) : AndroidViewModel(applicati

val beaconEntryModel = ChartEntryModelProducer(emptyList<ChartEntry>())

private val wifisLoaded = MutableLiveData(false)
private val cellsLoaded = MutableLiveData(false)
private val beaconsLoaded = MutableLiveData(false)

val dataLoaded = MediatorLiveData<Boolean>()
.apply {
val setDataLoaded = { _: Boolean ->
value = wifisLoaded.value == true && cellsLoaded.value == true && beaconsLoaded.value == true
}

addSource(wifisLoaded, setDataLoaded)
addSource(cellsLoaded, setDataLoaded)
addSource(beaconsLoaded, setDataLoaded)
}
.distinctUntilChanged()

init {
viewModelScope.launch {
viewModelScope.launch(Dispatchers.Default) {
statisticsDao.newWifisPerDay()
.distinctUntilChanged()
.map { cumulativeSum(it.toSortedMap()) }
Expand All @@ -32,10 +53,11 @@ class StatisticsViewModel(application: Application) : AndroidViewModel(applicati
entryOf(date.toEpochDay(), count)
}
}
.onEach { wifisLoaded.postValue(true) }
.collectLatest { entries -> wifiEntryModel.setEntries(entries) }
}

viewModelScope.launch {
viewModelScope.launch(Dispatchers.Default) {
statisticsDao.newCellsPerDay()
.distinctUntilChanged()
.map { cumulativeSum(it.toSortedMap()) }
Expand All @@ -44,10 +66,11 @@ class StatisticsViewModel(application: Application) : AndroidViewModel(applicati
entryOf(date.toEpochDay(), count)
}
}
.onEach { cellsLoaded.postValue(true) }
.collectLatest { entries -> cellEntryModel.setEntries(entries) }
}

viewModelScope.launch {
viewModelScope.launch(Dispatchers.Default) {
statisticsDao.newBeaconsPerDay()
.distinctUntilChanged()
.map { cumulativeSum(it.toSortedMap()) }
Expand All @@ -56,6 +79,7 @@ class StatisticsViewModel(application: Application) : AndroidViewModel(applicati
entryOf(date.toEpochDay(), count)
}
}
.onEach { beaconsLoaded.postValue(true) }
.collectLatest { entries -> beaconEntryModel.setEntries(entries) }
}
}
Expand Down

0 comments on commit 68a8cde

Please sign in to comment.