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
@@ -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()
}
@@ -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)
}
/**