2020-08-18 06:31:15 -07:00
|
|
|
package com.topjohnwu.magisk.arch
|
2020-01-13 03:56:03 +08:00
|
|
|
|
2022-08-23 03:59:09 -07:00
|
|
|
import android.Manifest.permission.*
|
2022-02-14 02:15:50 -08:00
|
|
|
import android.annotation.SuppressLint
|
2022-05-08 14:29:59 -07:00
|
|
|
import android.os.Bundle
|
2020-07-12 14:37:07 -07:00
|
|
|
import androidx.databinding.PropertyChangeRegistry
|
2020-01-13 03:56:03 +08:00
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
2020-03-17 17:28:09 +01:00
|
|
|
import androidx.navigation.NavDirections
|
2020-07-17 01:27:35 -07:00
|
|
|
import com.topjohnwu.magisk.R
|
2021-09-01 01:11:57 -07:00
|
|
|
import com.topjohnwu.magisk.databinding.ObservableHost
|
2021-05-09 20:45:53 -07:00
|
|
|
import com.topjohnwu.magisk.events.BackPressEvent
|
2023-04-03 17:46:49 -07:00
|
|
|
import com.topjohnwu.magisk.events.DialogBuilder
|
|
|
|
import com.topjohnwu.magisk.events.DialogEvent
|
2021-05-09 20:45:53 -07:00
|
|
|
import com.topjohnwu.magisk.events.NavigationEvent
|
|
|
|
import com.topjohnwu.magisk.events.PermissionEvent
|
|
|
|
import com.topjohnwu.magisk.events.SnackbarEvent
|
2020-01-13 03:56:03 +08:00
|
|
|
|
2022-06-09 23:28:46 -07:00
|
|
|
abstract class BaseViewModel : ViewModel(), ObservableHost {
|
2020-07-12 14:37:07 -07:00
|
|
|
|
|
|
|
override var callbacks: PropertyChangeRegistry? = null
|
2020-01-13 03:56:03 +08:00
|
|
|
|
|
|
|
private val _viewEvents = MutableLiveData<ViewEvent>()
|
2022-06-10 04:12:31 -07:00
|
|
|
val viewEvents: LiveData<ViewEvent> get() = _viewEvents
|
2020-01-13 03:56:03 +08:00
|
|
|
|
2022-05-08 14:29:59 -07:00
|
|
|
open fun onSaveState(state: Bundle) {}
|
|
|
|
open fun onRestoreState(state: Bundle) {}
|
2022-06-09 23:28:46 -07:00
|
|
|
open fun onNetworkChanged(network: Boolean) {}
|
|
|
|
|
2020-08-22 05:27:30 -07:00
|
|
|
fun withPermission(permission: String, callback: (Boolean) -> Unit) {
|
|
|
|
PermissionEvent(permission, callback).publish()
|
2020-07-08 01:26:45 -07:00
|
|
|
}
|
|
|
|
|
2021-11-05 15:53:34 -07:00
|
|
|
inline fun withExternalRW(crossinline callback: () -> Unit) {
|
2022-02-14 02:15:50 -08:00
|
|
|
withPermission(WRITE_EXTERNAL_STORAGE) {
|
2020-07-17 01:27:35 -07:00
|
|
|
if (!it) {
|
|
|
|
SnackbarEvent(R.string.external_rw_permission_denied).publish()
|
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 03:56:03 +08:00
|
|
|
}
|
|
|
|
|
2022-02-14 02:15:50 -08:00
|
|
|
@SuppressLint("InlinedApi")
|
|
|
|
inline fun withInstallPermission(crossinline callback: () -> Unit) {
|
|
|
|
withPermission(REQUEST_INSTALL_PACKAGES) {
|
|
|
|
if (!it) {
|
|
|
|
SnackbarEvent(R.string.install_unknown_denied).publish()
|
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 03:59:09 -07:00
|
|
|
@SuppressLint("InlinedApi")
|
|
|
|
inline fun withPostNotificationPermission(crossinline callback: () -> Unit) {
|
|
|
|
withPermission(POST_NOTIFICATIONS) {
|
|
|
|
if (!it) {
|
|
|
|
SnackbarEvent(R.string.post_notifications_denied).publish()
|
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 03:56:03 +08:00
|
|
|
fun back() = BackPressEvent().publish()
|
|
|
|
|
2023-04-03 17:46:49 -07:00
|
|
|
fun ViewEvent.publish() {
|
2020-01-13 03:56:03 +08:00
|
|
|
_viewEvents.postValue(this)
|
|
|
|
}
|
|
|
|
|
2023-04-03 17:46:49 -07:00
|
|
|
fun DialogBuilder.show() {
|
|
|
|
DialogEvent(this).publish()
|
2020-07-10 04:19:18 -07:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:13:16 +08:00
|
|
|
fun NavDirections.navigate(pop: Boolean = false) {
|
|
|
|
_viewEvents.postValue(NavigationEvent(this, pop))
|
2020-03-17 17:28:09 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 03:56:03 +08:00
|
|
|
}
|