diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-14 22:35:23 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-14 22:35:23 -0600 |
commit | a22a13de9b4d1e46bb64371242fa3528bfac7675 (patch) | |
tree | c51c043f9c05e0cf5b80d8d6f3a09d9bdbd0a87c /androidApp | |
parent | 7c3e13114fd189c49e86b8d6c200ffb3c27bb498 (diff) | |
download | etbsa-trackermap-mobile-a22a13de9b4d1e46bb64371242fa3528bfac7675.tar.gz etbsa-trackermap-mobile-a22a13de9b4d1e46bb64371242fa3528bfac7675.tar.bz2 etbsa-trackermap-mobile-a22a13de9b4d1e46bb64371242fa3528bfac7675.zip |
Deleted unused controllers and refactored session management code into SessionController
Diffstat (limited to 'androidApp')
3 files changed, 36 insertions, 49 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 d591b0b..a9d4a8b 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt @@ -11,6 +11,7 @@ import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.apis.* import mx.trackermap.TrackerMap.controllers.GeofencesController import mx.trackermap.TrackerMap.controllers.ReportController +import mx.trackermap.TrackerMap.controllers.SessionController import mx.trackermap.TrackerMap.controllers.UnitsController import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidLogger @@ -34,11 +35,12 @@ open class TrackerApp : Application() { factory { ReportsApi(get()) } factory { GeofencesApi(get()) } + factory { SessionController(get()) } factory { UnitsController(get(), get()) } factory { GeofencesController(get()) } factory { ReportController(get(), get()) } - viewModel { LoginViewModel(get(), get()) } + viewModel { LoginViewModel(get()) } viewModel { UnitInformationViewModel(get()) } viewModel { UnitCommandsViewModel(get()) } viewModel { UnitsViewModel(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 185a444..fa61d53 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 @@ -15,6 +15,7 @@ import com.zhuinden.liveevent.observe import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.databinding.LoginBinding import mx.trackermap.TrackerMap.android.units.UnitsActivity +import mx.trackermap.TrackerMap.controllers.SessionController import org.koin.androidx.viewmodel.ext.android.viewModel @DelicateCoroutinesApi @@ -74,19 +75,19 @@ class LoginFragment : Fragment() { loginViewModel.loginState.observe(viewLifecycleOwner) { result -> Log.d("LoginFragment", result.toString()) when (result) { - LoginViewModel.LoginState.Loading -> { + SessionController.LoginState.Loading -> { Toast.makeText(context, "Loading...", Toast.LENGTH_SHORT).show() } - LoginViewModel.LoginState.EmailMissing -> { + SessionController.LoginState.EmailMissing -> { Toast.makeText(context, "Email is missing", Toast.LENGTH_SHORT).show() } - LoginViewModel.LoginState.PasswordMissing -> { + SessionController.LoginState.PasswordMissing -> { Toast.makeText(context, "Password is missing", Toast.LENGTH_SHORT).show() } - LoginViewModel.LoginState.Failure -> { + SessionController.LoginState.Failure -> { Toast.makeText(context, "Failed login", Toast.LENGTH_SHORT).show() } - LoginViewModel.LoginState.Success -> { + SessionController.LoginState.Success -> { broadcastManager.sendBroadcast(Intent(EVENT_LOGIN)) val activity = requireActivity() val intent = Intent(activity.applicationContext, UnitsActivity::class.java) 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 8f53b7e..2590504 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 @@ -1,70 +1,54 @@ package mx.trackermap.TrackerMap.android.session -import android.util.Log import androidx.lifecycle.MutableLiveData import androidx.lifecycle.SavedStateHandle 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.apis.SessionApi +import mx.trackermap.TrackerMap.client.models.SessionBody +import mx.trackermap.TrackerMap.controllers.SessionController +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject +@DelicateCoroutinesApi class LoginViewModel( - private val sessionApi: SessionApi, savedStateHandle: SavedStateHandle -) : ViewModel() { +) : ViewModel(), KoinComponent { - sealed class LoginState { - object Loading: LoginState() - object EmailMissing : LoginState() - object PasswordMissing : LoginState() - object Failure : LoginState() - object Success : LoginState() - } + private val sessionController: SessionController by inject() val email: MutableLiveData<String> = savedStateHandle.getLiveData("user", "") val password: MutableLiveData<String> = savedStateHandle.getLiveData("password", "") - private val loginStateEmitter = EventEmitter<LoginState>() - val loginState: EventSource<LoginState> = loginStateEmitter + private val loginStateEmitter = EventEmitter<SessionController.LoginState>() + val loginState: EventSource<SessionController.LoginState> = loginStateEmitter - fun restoreSession() { - loginStateEmitter.emit(LoginState.Loading); + init { viewModelScope.launch { - try { - val user = sessionApi.sessionGet() - Log.d("LoginViewModel", user.toString()) - loginStateEmitter.emit(LoginState.Success) - } catch (e: Exception) { - Log.d("LoginViewModel", "No session") - } + setupLoginStateObserver() } } - fun login() { - val email = email.value!!.toString().trim() - val password = password.value!!.toString().trim() - - if (email.isEmpty()) { - loginStateEmitter.emit(LoginState.EmailMissing) - return + private suspend fun setupLoginStateObserver() { + sessionController.loginStateFlow.collect { + it?.let { + loginStateEmitter.emit(it) + } } + } - if (password.isEmpty()) { - loginStateEmitter.emit(LoginState.PasswordMissing) - return - } + fun restoreSession() { + sessionController.restoreSession() + } - loginStateEmitter.emit(LoginState.Loading) - viewModelScope.launch { - try { - val user = sessionApi.sessionPost(email, password) - Log.d("LoginViewModel", user.toString()) - loginStateEmitter.emit(LoginState.Success) - } catch (e: Exception) { - loginStateEmitter.emit(LoginState.Failure) - } - } + fun login() { + sessionController.login(SessionBody( + email = email.value!!, + password = password.value!! + )) } }
\ No newline at end of file |