Some cleaning to handle actions on connection restore.
This commit is contained in:
parent
fb572dbb27
commit
75b566a38d
@ -69,7 +69,14 @@ class MyApp : MultiDexApplication(), DIAware {
|
|||||||
ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifeCycleObserver(connectivityStatus, repository))
|
ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifeCycleObserver(connectivityStatus, repository))
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
viewModel.toastMessageProvider.collect { toastMessage ->
|
viewModel.networkAvailableProvider.collect { networkAvailable ->
|
||||||
|
val toastMessage = if (networkAvailable) {
|
||||||
|
repository.handleDBActions()
|
||||||
|
R.string.network_connectivity_retrieved
|
||||||
|
} else {
|
||||||
|
R.string.network_connectivity_lost
|
||||||
|
}
|
||||||
|
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
applicationContext,
|
applicationContext,
|
||||||
toastMessage,
|
toastMessage,
|
||||||
|
@ -9,19 +9,21 @@ import kotlinx.coroutines.flow.asSharedFlow
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class AppViewModel(private val repository: Repository) : ViewModel() {
|
class AppViewModel(private val repository: Repository) : ViewModel() {
|
||||||
private val _toastMessageProvider = MutableSharedFlow<Int>()
|
private val _networkAvailableProvider = MutableSharedFlow<Boolean>()
|
||||||
val toastMessageProvider = _toastMessageProvider.asSharedFlow()
|
val networkAvailableProvider = _networkAvailableProvider.asSharedFlow()
|
||||||
private var wasConnected = true
|
private var wasConnected = true
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
repository.isConnectionAvailable.collect { isConnected ->
|
repository.isConnectionAvailable.collect { isConnected ->
|
||||||
if (isConnected && !wasConnected && repository.connectionMonitored) {
|
if (repository.connectionMonitored) {
|
||||||
_toastMessageProvider.emit(R.string.network_connectivity_retrieved)
|
if (isConnected && !wasConnected && repository.connectionMonitored) {
|
||||||
wasConnected = true
|
_networkAvailableProvider.emit(true)
|
||||||
} else if (!isConnected && wasConnected && repository.connectionMonitored){
|
wasConnected = true
|
||||||
_toastMessageProvider.emit(R.string.network_connectivity_lost)
|
} else if (!isConnected && wasConnected && repository.connectionMonitored){
|
||||||
wasConnected = false
|
_networkAvailableProvider.emit(false)
|
||||||
|
wasConnected = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user