Send toast messages to the home activity on connectivity changes
This commit is contained in:
parent
9d0bb452e3
commit
fbfb775f7f
@ -181,8 +181,9 @@ dependencies {
|
||||
implementation("androidx.viewpager2:viewpager2:1.1.0-beta01")
|
||||
|
||||
//Dependency Injection
|
||||
implementation("org.kodein.di:kodein-di:7.12.0")
|
||||
implementation("org.kodein.di:kodein-di-framework-android-x:7.12.0")
|
||||
implementation("org.kodein.di:kodein-di:7.14.0")
|
||||
implementation("org.kodein.di:kodein-di-framework-android-x:7.14.0")
|
||||
implementation("org.kodein.di:kodein-di-framework-android-x-viewmodel:7.14.0")
|
||||
|
||||
//Settings
|
||||
implementation("com.russhwolf:multiplatform-settings-no-arg:0.9")
|
||||
|
@ -17,6 +17,7 @@ import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.view.doOnNextLayout
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.*
|
||||
import androidx.room.Room
|
||||
import androidx.work.Constraints
|
||||
@ -46,6 +47,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.bottombar.removeBadge
|
||||
import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper
|
||||
import bou.amine.apps.readerforselfossv2.android.utils.persistence.toEntity
|
||||
import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView
|
||||
import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel
|
||||
import bou.amine.apps.readerforselfossv2.repository.Repository
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
import bou.amine.apps.readerforselfossv2.utils.ItemType
|
||||
@ -134,6 +136,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
||||
|
||||
override val di by closestDI()
|
||||
private val repository : Repository by instance()
|
||||
private val viewModel : AppViewModel by instance()
|
||||
|
||||
data class DrawerData(val tags: List<SelfossModel.Tag>?, val sources: List<SelfossModel.Source>?)
|
||||
|
||||
@ -173,6 +176,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
||||
AppDatabase::class.java, "selfoss-database"
|
||||
).addMigrations(MIGRATION_1_2).addMigrations(MIGRATION_2_3).addMigrations(MIGRATION_3_4).build()
|
||||
|
||||
lifecycleScope.launch {
|
||||
viewModel.toastMessageProvider.collect { toastMessage ->
|
||||
Toast.makeText(
|
||||
this@HomeActivity,
|
||||
toastMessage,
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
|
||||
customTabActivityHelper = CustomTabActivityHelper()
|
||||
|
||||
|
@ -15,6 +15,7 @@ import androidx.preference.PreferenceManager
|
||||
import bou.amine.apps.readerforselfossv2.DI.networkModule
|
||||
import bou.amine.apps.readerforselfossv2.android.utils.Config
|
||||
import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth
|
||||
import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel
|
||||
import bou.amine.apps.readerforselfossv2.repository.Repository
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
@ -30,6 +31,7 @@ class MyApp : MultiDexApplication(), DIAware {
|
||||
override val di by DI.lazy {
|
||||
import(networkModule)
|
||||
bind<Repository>() with singleton { Repository(instance(), instance(), ConnectivityStatus(applicationContext)) }
|
||||
bindProvider { AppViewModel(repository = instance()) }
|
||||
}
|
||||
|
||||
private val repository: Repository by instance()
|
||||
|
@ -0,0 +1,25 @@
|
||||
package bou.amine.apps.readerforselfossv2.android.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import bou.amine.apps.readerforselfossv2.repository.Repository
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class AppViewModel(private val repository: Repository) : ViewModel() {
|
||||
private val _toastMessageProvider = MutableSharedFlow<String>()
|
||||
val toastMessageProvider = _toastMessageProvider.asSharedFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
repository.isConnectionAvailable.collect { isConnected ->
|
||||
if (isConnected && repository.connectionMonitored) {
|
||||
_toastMessageProvider.emit("Network connection is now available")
|
||||
} else if (repository.connectionMonitored){
|
||||
_toastMessageProvider.emit("Network connection lost")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -16,7 +16,8 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
val settings = Settings()
|
||||
|
||||
var items = ArrayList<SelfossModel.Item>()
|
||||
private val isConnectionAvailable = connectivityStatus.isNetworkConnected
|
||||
val isConnectionAvailable = connectivityStatus.isNetworkConnected
|
||||
var connectionMonitored = false
|
||||
|
||||
var baseUrl = apiDetails.getBaseUrl()
|
||||
lateinit var dateUtils: DateUtils
|
||||
@ -348,9 +349,11 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
// https://github.com/ln-12/multiplatform-connectivity-status/issues/2
|
||||
fun startNetwork() {
|
||||
connectivityStatus.start()
|
||||
connectionMonitored = true
|
||||
}
|
||||
|
||||
fun stopNetwork() {
|
||||
connectionMonitored = false
|
||||
connectivityStatus.stop()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user