Skip to content

Commit

Permalink
Merge pull request #231 from ooni/notification-stop-and-dismiss
Browse files Browse the repository at this point in the history
Notification: Handle stopping state and disable dismiss
  • Loading branch information
sdsantos authored Oct 30, 2024
2 parents 88dd4b0 + d2ba8e1 commit 1ca3624
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import co.touchlab.kermit.Logger
import kotlinx.coroutines.flow.collectLatest
import kotlinx.serialization.encodeToString
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Running
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Stopping_Notice
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Stopping_Title
import ooniprobe.composeapp.generated.resources.Modal_ResultsNotUploaded_Uploading
import ooniprobe.composeapp.generated.resources.Notification_StopTest
import ooniprobe.composeapp.generated.resources.Res
Expand Down Expand Up @@ -91,6 +93,9 @@ class RunWorker(

is RunBackgroundTask.State.RunningTests ->
buildNotification(state.state)

is RunBackgroundTask.State.StoppingTests ->
buildStoppingNotification()
},
)
}
Expand Down Expand Up @@ -132,21 +137,18 @@ class RunWorker(

private suspend fun buildNotification(state: UploadMissingMeasurements.State) =
buildNotification {
setColor(primaryLight.toArgb())
.run {
if (state is UploadMissingMeasurements.State.Uploading) {
val progress = state.uploaded + state.failedToUpload + 1
setContentText(
getString(
Res.string.Modal_ResultsNotUploaded_Uploading,
"$progress/${state.total}",
),
)
.setProgress(state.total, progress, false)
} else {
setProgress(1, 0, true)
}
}
if (state is UploadMissingMeasurements.State.Uploading) {
val progress = state.uploaded + state.failedToUpload + 1
setContentText(
getString(
Res.string.Modal_ResultsNotUploaded_Uploading,
"$progress/${state.total}",
),
)
.setProgress(state.total, progress, false)
} else {
setProgress(1, 0, true)
}
}

private suspend fun buildNotification(state: TestRunState.Running) =
Expand All @@ -163,16 +165,25 @@ class RunWorker(
)
}

private suspend fun buildStoppingNotification() =
buildNotification {
setContentTitle(getString(Res.string.Dashboard_Running_Stopping_Title))
.setContentText(getString(Res.string.Dashboard_Running_Stopping_Notice))
.setProgress(1, 0, true)
}

private suspend fun buildNotification(build: suspend NotificationCompat.Builder.() -> NotificationCompat.Builder): Notification {
return build(
NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(getString(Res.string.Dashboard_Running_Running))
.setAutoCancel(false)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setSound(null)
.setVibrate(null)
.setLights(0, 0, 0)
.setColor(primaryLight.toArgb())
.setContentIntent(openAppIntent),
).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,20 @@ class RunBackgroundTask(
var testStarted = false
getCurrentTestState()
.takeWhile { state ->
state is TestRunState.Running || (state is TestRunState.Idle && !testStarted)
state is TestRunState.Running ||
state is TestRunState.Stopping ||
(state is TestRunState.Idle && !testStarted)
}
.onEach { state ->
if (state !is TestRunState.Running) return@onEach
if (state is TestRunState.Idle) return@onEach
testStarted = true
send(State.RunningTests(state))
send(
if (state is TestRunState.Running) {
State.RunningTests(state)
} else {
State.StoppingTests
},
)
}
.collect()

Expand All @@ -71,5 +79,7 @@ class RunBackgroundTask(
data class UploadingMissingResults(val state: UploadMissingMeasurements.State) : State

data class RunningTests(val state: TestRunState.Running) : State

data object StoppingTests : State
}
}

0 comments on commit 1ca3624

Please sign in to comment.