Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
break into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kglazko committed Nov 12, 2019
1 parent d85dfe1 commit 3591a9a
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions app/src/main/java/org/mozilla/fenix/utils/Undo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import org.mozilla.fenix.components.FenixSnackbar
import android.app.AlertDialog
import org.mozilla.fenix.R
import android.content.Context
import android.content.Context.ACCESSIBILITY_SERVICE
import android.view.accessibility.AccessibilityManager
import androidx.core.content.ContextCompat.getSystemService
import java.util.concurrent.atomic.AtomicBoolean

internal const val UNDO_DELAY = 3000L
Expand Down Expand Up @@ -47,30 +45,24 @@ fun CoroutineScope.allowUndo(
// writing a volatile variable.
val requestedUndo = AtomicBoolean(false)

// Check if accessibility is enabled and if so show a confirmation dialog instead
val am = view.context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
val isAccessibilityEnabled = am.isEnabled

if (isAccessibilityEnabled) {
// Launch an alert dialog for undo- for accessibility users
fun showUndoDialog() {
val dialogBuilder = AlertDialog.Builder(view.context)
dialogBuilder.setMessage(message).setCancelable(false).setPositiveButton(R.string.a11y_dialog_deleted_confirm) { _, _ ->
launch {
operation.invoke()
}
}.setNegativeButton(R.string.a11y_dialog_deleted_undo){_,_ ->
launch {
onCancel.invoke()
onCancel.invoke()
}
}

// Create dialog box
val alert = dialogBuilder.create()
alert.show()
}

else {

// Launch an indefinite snackbar
// Launch an indefinite snackbar
fun showUndoSnackbar() {
val snackbar = FenixSnackbar
.make(view, FenixSnackbar.LENGTH_INDEFINITE)
.setText(message)
Expand All @@ -96,4 +88,17 @@ fun CoroutineScope.allowUndo(
}
}
}

// Check if accessibility is enabled
val isAccessibilityEnabled = accessibilityEnabled(view)

// Show either Snackbar or dialog based on result
if (isAccessibilityEnabled) showUndoDialog() else showUndoSnackbar()
}



fun accessibilityEnabled(view:View): Boolean {
val am = view.context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
return am.isEnabled
}

0 comments on commit 3591a9a

Please sign in to comment.