Skip to content

Commit

Permalink
feat: (OONI Run v2) Add link cancellation toast ooni/run#151
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Mar 16, 2024
1 parent e9b68a0 commit b43bc31
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class OoniRunV2Activity : AbstractActivity() {
val executor = TaskExecutor()
binding.cancelButton.setOnClickListener {
executor.cancelTask()
finishWithError(message = getString(R.string.Modal_Cancel))
finishWithError(message = "Link installation cancelled")
}
executor.executeTask({
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ typealias OnTaskComplete<R> = (R) -> Unit
class TaskExecutor {
private val executor = Executors.newSingleThreadExecutor()
private val handler = Handler(Looper.getMainLooper())
private var future: Future<*>? = null

private lateinit var future: Future<*>
/**
* Executes a task in a separate thread and posts the result on the main thread.
* @param task The task to be executed.
Expand All @@ -54,8 +53,10 @@ class TaskExecutor {
fun <R> executeTask(task: Task<R>, onComplete: OnTaskComplete<R>) {
future = executor.submit {
val result = task.call()
handler.post {
onComplete(result)
if (!future.isCancelled) {
handler.post {
onComplete(result)
}
}
}
}
Expand Down Expand Up @@ -90,6 +91,6 @@ class TaskExecutor {
* Cancels the currently running task.
*/
fun cancelTask() {
future?.cancel(true)
this.future.cancel(true)
}
}

0 comments on commit b43bc31

Please sign in to comment.