Don't always refresh on network state change

This commit is contained in:
topjohnwu 2022-06-09 23:28:46 -07:00
parent 20124fe410
commit 482a5b991b
4 changed files with 19 additions and 17 deletions

View File

@ -21,9 +21,7 @@ import com.topjohnwu.magisk.events.PermissionEvent
import com.topjohnwu.magisk.events.SnackbarEvent
import kotlinx.coroutines.Job
abstract class BaseViewModel(
initialState: State = State.LOADING
) : ViewModel(), ObservableHost {
abstract class BaseViewModel : ViewModel(), ObservableHost {
override var callbacks: PropertyChangeRegistry? = null
@ -40,7 +38,7 @@ abstract class BaseViewModel(
val viewEvents: LiveData<ViewEvent> get() = _viewEvents
var state= initialState
var state = State.LOADING
set(value) = set(value, field, { field = it }, BR.loading, BR.loaded, BR.loadFailed)
private val _viewEvents = MutableLiveData<ViewEvent>()
@ -60,6 +58,8 @@ abstract class BaseViewModel(
protected open fun refresh(): Job? = null
open fun onNetworkChanged(network: Boolean) {}
fun withPermission(permission: String, callback: (Boolean) -> Unit) {
PermissionEvent(permission, callback).publish()
}

View File

@ -17,12 +17,8 @@ interface ViewModelHolder : LifecycleOwner, ViewModelStoreOwner {
val viewModel: BaseViewModel
fun startObserveLiveData() {
viewModel.viewEvents.observe(this) {
onEventDispatched(it)
}
Info.isConnected.observe(this) {
viewModel.requestRefresh()
}
viewModel.viewEvents.observe(this, this::onEventDispatched)
Info.isConnected.observe(this, viewModel::onNetworkChanged)
}
/**

View File

@ -104,13 +104,9 @@ class HomeViewModel(
ensureEnv()
}
val showTest = false
fun onTestPressed() = object : ViewEvent(), ActivityExecutor {
override fun invoke(activity: UIActivity<*>) {
/* Entry point to trigger test events within the app */
}
}.publish()
override fun onNetworkChanged(network: Boolean) {
requestRefresh()
}
fun onProgressUpdate(progress: Float, subject: Subject) {
if (subject is App)
@ -151,4 +147,10 @@ class HomeViewModel(
checkedEnv = true
}
val showTest = false
fun onTestPressed() = object : ViewEvent(), ActivityExecutor {
override fun invoke(activity: UIActivity<*>) {
/* Entry point to trigger test events within the app */
}
}.publish()
}

View File

@ -52,6 +52,10 @@ class ModuleViewModel : BaseViewModel() {
}
}
override fun onNetworkChanged(network: Boolean) {
requestRefresh()
}
private suspend fun loadInstalled() {
val installed = LocalModule.installed().map { LocalModuleRvItem(it) }
val diff = withContext(Dispatchers.Default) {