Skip to content

Commit

Permalink
Delay pop-up of progress window
Browse files Browse the repository at this point in the history
Closes #12027
  • Loading branch information
dae authored and mikehardy committed Aug 16, 2022
1 parent 3e7ad6a commit 771056c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/anki/CoroutineHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package com.ichi2.anki

import android.content.Context
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.coroutineScope
import anki.collection.Progress
import com.ichi2.anki.UIUtils.showSimpleSnackbar
import com.ichi2.libanki.Collection
import com.ichi2.themes.StyledProgressDialog
import kotlinx.coroutines.*
import net.ankiweb.rsdroid.Backend
import net.ankiweb.rsdroid.BackendException
Expand Down Expand Up @@ -139,23 +139,31 @@ suspend fun <T> FragmentActivity.withProgress(
op()
}

@Suppress("Deprecation") // ProgressDialog deprecation
private suspend fun <T> withProgressDialog(
context: FragmentActivity,
onCancel: (() -> Unit)?,
@Suppress("Deprecation") // ProgressDialog deprecation
op: suspend (android.app.ProgressDialog) -> T
): T {
val dialog = StyledProgressDialog.show(
context, null,
null, onCancel != null
)
onCancel?.let {
dialog.setOnCancelListener { it() }
): T = coroutineScope {
val dialog = android.app.ProgressDialog(context).apply {
setCancelable(onCancel != null)
onCancel?.let {
setOnCancelListener { it() }
}
}
// disable taps immediately
context.window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
// reveal the dialog after 600ms
val dialogJob = launch {
delay(600)
dialog.show()
}
return try {
try {
op(dialog)
} finally {
dialogJob.cancel()
dialog.dismiss()
context.window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
}
}

Expand Down

0 comments on commit 771056c

Please sign in to comment.