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

View File

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

View File

@ -104,13 +104,9 @@ class HomeViewModel(
ensureEnv() ensureEnv()
} }
val showTest = false override fun onNetworkChanged(network: Boolean) {
requestRefresh()
fun onTestPressed() = object : ViewEvent(), ActivityExecutor {
override fun invoke(activity: UIActivity<*>) {
/* Entry point to trigger test events within the app */
} }
}.publish()
fun onProgressUpdate(progress: Float, subject: Subject) { fun onProgressUpdate(progress: Float, subject: Subject) {
if (subject is App) if (subject is App)
@ -151,4 +147,10 @@ class HomeViewModel(
checkedEnv = true 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() { private suspend fun loadInstalled() {
val installed = LocalModule.installed().map { LocalModuleRvItem(it) } val installed = LocalModule.installed().map { LocalModuleRvItem(it) }
val diff = withContext(Dispatchers.Default) { val diff = withContext(Dispatchers.Default) {