Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification: Handle stopping state and disable dismiss #231

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -48,12 +48,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 @@ -65,5 +73,7 @@ class RunBackgroundTask(
data class UploadingMissingResults(val state: UploadMissingMeasurements.State) : State

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

data object StoppingTests : State
}
}