Skip to content

Commit

Permalink
Pause the cellular signal chart from updating when the fragment is in…
Browse files Browse the repository at this point in the history
… the background
  • Loading branch information
christianrowlands committed Jan 14, 2024
1 parent 2bd9df1 commit 63d4c47
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
return binding.getRoot();
}

@Override
public void onPause()
{
chartViewModel.pauseChartUpdates();

super.onPause();
}

@Override
public void onResume()
{
Expand All @@ -139,6 +147,8 @@ public void onResume()
initializeLocationTextView();

startAndBindToService();

chartViewModel.resumeChartUpdates();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import timber.log.Timber
import kotlin.concurrent.Volatile
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -60,6 +61,9 @@ abstract class ASignalChartViewModel : ViewModel() {
private val xValueQueue: ArrayDeque<Int> by dequeLimiter(CHART_WIDTH)
private val rssiQueue: ArrayDeque<Float> by dequeLimiter(CHART_WIDTH)

@Volatile
private var isPaused = false

private val _latestChartRssi = MutableStateFlow(UNKNOWN_RSSI)
private val latestChartRssi: StateFlow<Float> = _latestChartRssi.asStateFlow()

Expand All @@ -72,7 +76,7 @@ abstract class ASignalChartViewModel : ViewModel() {
while (currentCoroutineContext().isActive) {
// This coroutine will make sure that the chart is updated every n seconds, even
// if there are not any new values coming in.
if (!rssiQueue.isEmpty()) {
if (!isPaused && !rssiQueue.isEmpty()) {
addRssiToChart(latestChartRssi.value)
}

Expand All @@ -81,6 +85,21 @@ abstract class ASignalChartViewModel : ViewModel() {
}
}

/**
* Pauses updates to the chart so that the chart does not keep getting values added when the
* fragment is in the background.
*/
fun pauseChartUpdates() {
isPaused = true;
}

/**
* Resumes updates to the chart.
*/
fun resumeChartUpdates() {
isPaused = false;
}

/**
* The label to use for the marker text on the chart. If this is an empty string, then the
* default marker text is displayed (the y-axis value). Override this method to provide a
Expand Down

0 comments on commit 63d4c47

Please sign in to comment.