Files
AndroidIDE/common/src/main/java/com/itsaky/androidide/utils/FlashbarActivityUtils.kt
T
Akash Yadav 764a6fa8c2 fix(lsp): progress dialog for 'find references' action is shown indefinitely
This happens because the 'ReferenceProvider' in the Java LSP tries to start another compilation process while working with the current compilation task.
This results in a deadlock where the current task waits for the new compilation request to return and the new compilation request waits for the current task to finish. This commit fixes this issue.

Apart from fixing this issue, this commit also makes the 'Go to definition' and 'Find references' actions cancelable. Also, it eliminates the usage of the deprecated 'ProgressDialog' class for these two actions.
2023-07-09 12:56:28 +05:30

155 lines
4.5 KiB
Kotlin

/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.utils
import android.app.Activity
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.PorterDuff.Mode.SRC_ATOP
import android.os.Looper
import android.widget.ImageView.ScaleType
import android.widget.ImageView.ScaleType.FIT_CENTER
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.FloatRange
import androidx.annotation.StringRes
import com.blankj.utilcode.util.ThreadUtils
import com.itsaky.androidide.flashbar.Flashbar
import com.itsaky.androidide.flashbar.Flashbar.Gravity.TOP
import com.itsaky.androidide.resources.R
import com.itsaky.androidide.utils.FlashType.ERROR
import com.itsaky.androidide.utils.FlashType.INFO
import com.itsaky.androidide.utils.FlashType.SUCCESS
import java.util.function.Consumer
const val DURATION_SHORT = 2000L
const val DURATION_LONG = 3500L
const val DURATION_INDEFINITE = Flashbar.DURATION_INDEFINITE
val COLOR_SUCCESS = Color.parseColor("#4CAF50")
val COLOR_ERROR = Color.parseColor("#f44336")
const val COLOR_INFO = Color.DKGRAY
@JvmOverloads
fun Activity.flashbarBuilder(
gravity: Flashbar.Gravity = TOP,
duration: Long = DURATION_SHORT,
backgroundColor: Int = resolveAttr(R.attr.colorPrimaryContainer),
messageColor: Int = resolveAttr(R.attr.colorOnPrimaryContainer)
): Flashbar.Builder {
return Flashbar.Builder(this)
.gravity(gravity)
.duration(duration)
.backgroundColor(backgroundColor)
.messageColor(messageColor)
}
fun Activity.flashMessage(msg: String?, type: FlashType) {
msg ?: return
when (type) {
ERROR -> flashError(msg)
INFO -> flashInfo(msg)
SUCCESS -> flashSuccess(msg)
}
}
fun Activity.flashMessage(@StringRes msg: Int, type: FlashType) {
when (type) {
ERROR -> flashError(msg)
INFO -> flashInfo(msg)
SUCCESS -> flashSuccess(msg)
}
}
fun Activity.flashSuccess(msg: String?) {
msg ?: return
flashbarBuilder().successIcon().message(msg).showOnUiThread()
}
fun Activity.flashError(msg: String?) {
msg ?: return
flashbarBuilder().errorIcon().message(msg).showOnUiThread()
}
fun Activity.flashInfo(msg: String?) {
msg ?: return
flashbarBuilder().infoIcon().message(msg).showOnUiThread()
}
fun Activity.flashSuccess(@StringRes msg: Int) {
flashbarBuilder().successIcon().message(msg).showOnUiThread()
}
fun Activity.flashError(@StringRes msg: Int) {
flashbarBuilder().errorIcon().message(msg).showOnUiThread()
}
fun Activity.flashInfo(@StringRes msg: Int) {
flashbarBuilder().infoIcon().message(msg).showOnUiThread()
}
@JvmOverloads
fun Activity.flashProgress(configure: Consumer<Flashbar.Builder>? = null, action: Consumer<Flashbar>) {
val builder = flashbarBuilder(gravity = TOP, duration = DURATION_INDEFINITE)
.showProgress(Flashbar.ProgressPosition.LEFT)
configure?.accept(builder)
val flashbar = builder.build()
flashbar.show()
action.accept(flashbar)
}
fun Flashbar.Builder.showOnUiThread() {
build().showOnUiThread()
}
fun Flashbar.showOnUiThread() {
if (Looper.myLooper() == Looper.getMainLooper()) {
show()
} else {
ThreadUtils.runOnUiThread { show() }
}
}
fun Flashbar.Builder.successIcon(): Flashbar.Builder {
return withIcon(R.drawable.ic_ok, colorFilter = COLOR_SUCCESS)
}
fun Flashbar.Builder.errorIcon(): Flashbar.Builder {
return withIcon(R.drawable.ic_error, colorFilter = COLOR_ERROR)
}
fun Flashbar.Builder.infoIcon(): Flashbar.Builder {
return withIcon(R.drawable.ic_info, colorFilter = COLOR_INFO)
}
fun Flashbar.Builder.withIcon(
@DrawableRes icon: Int,
@FloatRange(from = 0.0, to = 1.0) scale: Float = 1.0f,
@ColorInt colorFilter: Int = -1,
colorFilterMode: PorterDuff.Mode = SRC_ATOP,
scaleType: ScaleType = FIT_CENTER
): Flashbar.Builder {
return showIcon(scale = scale, scaleType = scaleType).icon(icon).also {
if (colorFilter != -1) {
iconColorFilter(colorFilter, colorFilterMode)
}
}
}