Use AtomicReference for SmartSnackbar
This commit is contained in:
@@ -12,15 +12,15 @@ import be.mygod.vpnhotspot.App.Companion.app
|
|||||||
import be.mygod.vpnhotspot.util.readableMessage
|
import be.mygod.vpnhotspot.util.readableMessage
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
import com.topjohnwu.superuser.NoShellException
|
import com.topjohnwu.superuser.NoShellException
|
||||||
|
import java.util.concurrent.atomic.AtomicReference
|
||||||
|
|
||||||
sealed class SmartSnackbar {
|
sealed class SmartSnackbar {
|
||||||
companion object {
|
companion object {
|
||||||
@SuppressLint("StaticFieldLeak")
|
private val holder = AtomicReference<View?>()
|
||||||
private var holder: View? = null
|
|
||||||
|
|
||||||
fun make(@StringRes text: Int): SmartSnackbar = make(app.getText(text))
|
fun make(@StringRes text: Int): SmartSnackbar = make(app.getText(text))
|
||||||
fun make(text: CharSequence = ""): SmartSnackbar {
|
fun make(text: CharSequence = ""): SmartSnackbar {
|
||||||
val holder = holder
|
val holder = holder.get()
|
||||||
return if (holder == null) @SuppressLint("ShowToast") {
|
return if (holder == null) @SuppressLint("ShowToast") {
|
||||||
if (Looper.myLooper() == null) Looper.prepare()
|
if (Looper.myLooper() == null) Looper.prepare()
|
||||||
ToastWrapper(Toast.makeText(app, text, Toast.LENGTH_LONG))
|
ToastWrapper(Toast.makeText(app, text, Toast.LENGTH_LONG))
|
||||||
@@ -37,11 +37,9 @@ sealed class SmartSnackbar {
|
|||||||
view.findViewTreeLifecycleOwner()!!.lifecycle.addObserver(this)
|
view.findViewTreeLifecycleOwner()!!.lifecycle.addObserver(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume(owner: LifecycleOwner) {
|
override fun onResume(owner: LifecycleOwner) = holder.set(view)
|
||||||
holder = view
|
|
||||||
}
|
|
||||||
override fun onPause(owner: LifecycleOwner) {
|
override fun onPause(owner: LifecycleOwner) {
|
||||||
if (holder === view) holder = null
|
holder.compareAndSet(view, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user