-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate DialogUtils to ProgressBarDialogIndeterminate
- Loading branch information
1 parent
86ad046
commit abfc41a
Showing
4 changed files
with
79 additions
and
168 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/osfans/trime/ui/components/ProgressBarDialogIndeterminate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
package com.osfans.trime.ui.components | ||
|
||
import android.content.Context | ||
import androidx.annotation.StringRes | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.lifecycle.LifecycleCoroutineScope | ||
import com.osfans.trime.R | ||
import kotlinx.coroutines.cancelAndJoin | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import splitties.dimensions.dp | ||
import splitties.views.dsl.core.add | ||
import splitties.views.dsl.core.horizontalMargin | ||
import splitties.views.dsl.core.lParams | ||
import splitties.views.dsl.core.matchParent | ||
import splitties.views.dsl.core.styles.AndroidStyles | ||
import splitties.views.dsl.core.verticalLayout | ||
import splitties.views.dsl.core.verticalMargin | ||
|
||
@Suppress("FunctionName") | ||
fun Context.ProgressBarDialogIndeterminate( | ||
@StringRes title: Int, | ||
): AlertDialog.Builder { | ||
val androidStyles = AndroidStyles(this) | ||
return AlertDialog | ||
.Builder(this) | ||
.setTitle(title) | ||
.setView( | ||
verticalLayout { | ||
add( | ||
androidStyles.progressBar.horizontal { | ||
isIndeterminate = true | ||
}, | ||
lParams { | ||
width = matchParent | ||
verticalMargin = dp(20) | ||
horizontalMargin = dp(26) | ||
}, | ||
) | ||
}, | ||
).setCancelable(false) | ||
} | ||
|
||
fun LifecycleCoroutineScope.withLoadingDialog( | ||
context: Context, | ||
@StringRes title: Int = R.string.loading, | ||
threshold: Long = 200L, | ||
action: suspend () -> Unit, | ||
) { | ||
var loadingDialog: AlertDialog? = null | ||
val loadingJob = | ||
launch { | ||
delay(threshold) | ||
loadingDialog = context.ProgressBarDialogIndeterminate(title).show() | ||
} | ||
launch { | ||
action() | ||
loadingJob.cancelAndJoin() | ||
loadingDialog?.dismiss() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.