aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2023-09-17 21:56:55 -0600
committerIván Ávalos <avalos@disroot.org>2023-09-17 23:51:33 -0600
commitedbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23 (patch)
tree885ca095c993c7a661303d215d9be0a6271ba3ea /androidApp/src/main/java/mx/trackermap/TrackerMap/android
parent7aec305729b872d668df45eae4821b106c1a20cb (diff)
downloadetbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.tar.gz
etbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.tar.bz2
etbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.zip
- [shared] Implement network state monitoring
- [android] UI reacts to network state - [ios] UI reacts to network state
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android')
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt4
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt23
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt20
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt5
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt14
5 files changed, 59 insertions, 7 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt
index aa92c91..1bb921d 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt
@@ -28,6 +28,7 @@ import mx.trackermap.TrackerMap.android.units.UnitsViewModel
import mx.trackermap.TrackerMap.client.apis.*
import mx.trackermap.TrackerMap.client.infrastructure.SessionManager
import mx.trackermap.TrackerMap.controllers.*
+import mx.trackermap.TrackerMap.controllers.NetworkController
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.androidx.viewmodel.dsl.viewModel
@@ -56,7 +57,8 @@ open class TrackerApp : Application() {
factory { ReportsApi(get()) }
factory { GeofencesApi(get()) }
- factory { SessionController(get(), get()) }
+ factory { NetworkController(applicationContext) }
+ factory { SessionController(get(), get(), get()) }
factory { UnitsController(get(), get()) }
factory { GeofencesController(get()) }
factory { ReportController(get(), get()) }
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt
index 6a30789..8879349 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt
@@ -67,7 +67,6 @@ class LoginFragment : Fragment() {
getString(R.string.default_server_url)
) ?: getString(R.string.default_server_url)
)
- loginViewModel.restoreSession()
}
override fun onStart() {
@@ -100,6 +99,26 @@ class LoginFragment : Fragment() {
}
private fun setupObservers() {
+ loginViewModel.networkAvailable.observe(viewLifecycleOwner) { available ->
+ Log.d("LoginFragment", "available = $available, session = ${loginViewModel.hasSession}")
+
+ binding.offlineBanner.root.visibility =
+ if (available == true) View.GONE else View.VISIBLE
+
+ binding.infoLoading.root.visibility = when (available) {
+ null -> View.VISIBLE
+ true -> if (loginViewModel.hasSession) {
+ loginViewModel.restoreSession()
+ View.VISIBLE
+ } else View.GONE
+ false -> if (loginViewModel.hasSession) {
+ View.VISIBLE
+ } else {
+ View.GONE
+ }
+ }
+ }
+
loginViewModel.loginState.observe(viewLifecycleOwner) { result ->
Log.d("LoginFragment", result.toString())
when (result) {
@@ -164,4 +183,4 @@ class LoginFragment : Fragment() {
const val PREFERENCE_SERVER_URL = "server_url"
const val PREFERENCE_TOKEN = "token"
}
-} \ No newline at end of file
+}
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt
index bcee2ec..434ac44 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt
@@ -17,32 +17,46 @@
*/
package mx.trackermap.TrackerMap.android.session
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zhuinden.eventemitter.EventEmitter
import com.zhuinden.eventemitter.EventSource
import kotlinx.coroutines.DelicateCoroutinesApi
-import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import mx.trackermap.TrackerMap.client.models.SessionBody
+import mx.trackermap.TrackerMap.controllers.NetworkController
import mx.trackermap.TrackerMap.controllers.SessionController
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
@DelicateCoroutinesApi
class LoginViewModel : ViewModel(), KoinComponent {
-
+ private val networkController: NetworkController by inject()
private val sessionController: SessionController by inject()
+ private val _networkAvailable = MutableLiveData<Boolean?>(null)
+ val networkAvailable: LiveData<Boolean?> = _networkAvailable
private val loginStateEmitter = EventEmitter<SessionController.LoginState>()
val loginState: EventSource<SessionController.LoginState> = loginStateEmitter
+ val hasSession: Boolean get() = sessionController.hasSession
init {
viewModelScope.launch {
+ setupNetworkObserver()
+ }
+ viewModelScope.launch {
setupLoginStateObserver()
}
}
+ private suspend fun setupNetworkObserver() {
+ networkController.networkAvailable.collect {
+ _networkAvailable.postValue(it)
+ }
+ }
+
private suspend fun setupLoginStateObserver() {
sessionController.loginStateFlow.collect {
it?.let {
@@ -52,7 +66,7 @@ class LoginViewModel : ViewModel(), KoinComponent {
}
fun restoreSession() {
- sessionController.restoreSession()
+ sessionController.getSession()
}
fun login(email: String, password: String, url: String, token: String?) {
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt
index 8a161c3..443a205 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt
@@ -28,7 +28,6 @@ import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.TooltipCompat
-import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.commit
import kotlinx.coroutines.DelicateCoroutinesApi
import mx.trackermap.TrackerMap.android.R
@@ -150,6 +149,10 @@ class UnitsActivity : AppCompatActivity() {
}
private fun setupObservers() {
+ unitsViewModel.networkAvailable.observe(this) { available ->
+ binding.offlineBanner.root.visibility =
+ if (available == true) View.GONE else View.VISIBLE
+ }
unitsViewModel.unitsDisplayMode.observe(this) { displayMode ->
binding.displayModeToggle.setImageResource(
when (displayMode) {
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt
index b5ed78d..05962e9 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt
@@ -27,6 +27,7 @@ import mx.trackermap.TrackerMap.client.models.Geofence
import mx.trackermap.TrackerMap.client.models.MapLayer
import mx.trackermap.TrackerMap.client.models.UnitInformation
import mx.trackermap.TrackerMap.controllers.GeofencesController
+import mx.trackermap.TrackerMap.controllers.NetworkController
import mx.trackermap.TrackerMap.controllers.UnitsController
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
@@ -48,9 +49,11 @@ class UnitsViewModel(
val animated: Boolean,
)
+ private val networkController: NetworkController by inject()
private val unitsController: UnitsController by inject()
private val geofenceController: GeofencesController by inject()
+ private var _networkAvailable = MutableLiveData<Boolean?>()
private var _searchQuery = savedStateHandle.getLiveData("searchQuery", "")
private var _unitsDisplayMode = MutableLiveData(UnitsDisplayMode.MAP)
private var _units = MutableLiveData<List<UnitInformation>>()
@@ -61,6 +64,7 @@ class UnitsViewModel(
private var _geofences = MutableLiveData<Map<Int, Geofence>>()
private val _camera = MutableLiveData<Camera?>()
+ val networkAvailable: LiveData<Boolean?> get() = _networkAvailable
val searchQuery: LiveData<String> get() = _searchQuery
val unitsDisplayMode: LiveData<UnitsDisplayMode> get() = _unitsDisplayMode
val units: LiveData<List<UnitInformation>> get() = _units
@@ -75,6 +79,9 @@ class UnitsViewModel(
Log.d("UnitsViewModel", "Initializing Units View Model")
unitsController.fetchUnits(viewModelScope)
viewModelScope.launch {
+ setupNetworkObserver()
+ }
+ viewModelScope.launch {
setupUnitsObserver()
}
viewModelScope.launch {
@@ -86,6 +93,13 @@ class UnitsViewModel(
}
}
+ private suspend fun setupNetworkObserver() {
+ networkController.networkAvailable.collect { available ->
+ Log.d("UnitsViewModel", "Collecting network state")
+ _networkAvailable.value = available
+ }
+ }
+
private suspend fun setupUnitsObserver() {
(unitsController.displayedUnitsFlow as StateFlow<List<UnitInformation>>).collect { units ->
Log.d("UnitsViewModel", "Collecting units")