network #28

Merged
AmineB merged 28 commits from davidoskky/ReaderForSelfoss-multiplatform:network into master 2022-08-22 19:01:16 +00:00
5 changed files with 46 additions and 3 deletions
Showing only changes of commit 2a44162c5a - Show all commits

View File

@ -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")

View File

@ -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,
AmineB marked this conversation as resolved Outdated

This shouldn't be only on the home activity.

This shouldn't be only on the home activity.

Yes, however it might actually be better to remove this logic from the repository and implement a viewModel to send these messages.
Localization in the common source set appears to be complicated.

Yes, however it might actually be better to remove this logic from the repository and implement a viewModel to send these messages. Localization in the common source set appears to be complicated.
toastMessage,
Toast.LENGTH_SHORT
).show()
}
}
customTabActivityHelper = CustomTabActivityHelper()

View File

@ -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
@ -32,6 +33,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()

View File

@ -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){
AmineB marked this conversation as resolved Outdated

It would be better to emit an enum. We'll handle the translation on the android/ios side.

It would be better to emit an enum. We'll handle the translation on the android/ios side.

What do you mean? This view model is already in the android side; these strings will have to be localized. I can do it now

What do you mean? This view model is already in the android side; these strings will have to be localized. I can do it now

I didn´t see that it was in the android side.

Yes, please use localized stringd.

I didn´t see that it was in the android side. Yes, please use localized stringd.
_toastMessageProvider.emit("Network connection lost")
}
}
}
}
}

View File

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