From b7c2bd81c92fadd654fcd7db999d2be891458ea6 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Fri, 14 Jan 2022 22:35:23 -0600 Subject: Deleted unused controllers and refactored session management code into SessionController --- .../mx/trackermap/TrackerMap/android/TrackerApp.kt | 4 +- .../TrackerMap/android/session/LoginFragment.kt | 11 ++-- .../TrackerMap/android/session/LoginViewModel.kt | 70 +++++++++------------- .../TrackerMap/client/apis/SessionApi.kt | 6 +- .../TrackerMap/client/models/Session_body.kt | 6 +- .../TrackerMap/controllers/AttributesController.kt | 52 ---------------- .../TrackerMap/controllers/DeviceController.kt | 34 ----------- .../TrackerMap/controllers/SessionController.kt | 63 ++++++++++++++++--- .../TrackerMap/controllers/UserController.kt | 10 ---- 9 files changed, 95 insertions(+), 161 deletions(-) delete mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt delete mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt delete mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt 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 = savedStateHandle.getLiveData("user", "") val password: MutableLiveData = savedStateHandle.getLiveData("password", "") - private val loginStateEmitter = EventEmitter() - val loginState: EventSource = loginStateEmitter + private val loginStateEmitter = EventEmitter() + val loginState: EventSource = 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 diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt index 417a818..2fba8a8 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/SessionApi.kt @@ -15,16 +15,16 @@ import com.russhwolf.settings.Settings import mx.trackermap.TrackerMap.client.models.User import mx.trackermap.TrackerMap.client.infrastructure.* -val ACCESS_TOKEN_KEY = "access_token" +const val ACCESS_TOKEN_KEY = "access_token" -class SessionApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiClient(basePath) { +class SessionApi(basePath: String = "https://demo.traccar.org/api") : ApiClient(basePath) { /** * Close the Session * * @return void */ - suspend fun sessionDelete(): Unit { + suspend fun sessionDelete() { val localVariableConfig = RequestConfig( RequestMethod.DELETE, "/session" diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Session_body.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Session_body.kt index 7958097..bda2fa4 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Session_body.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Session_body.kt @@ -11,15 +11,13 @@ */ package mx.trackermap.TrackerMap.client.models - /** * * @param email * @param password */ data class SessionBody ( - - val email: kotlin.String, - val password: kotlin.String + val email: String, + val password: String ) { } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt deleted file mode 100644 index 6262ec8..0000000 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt +++ /dev/null @@ -1,52 +0,0 @@ -package mx.trackermap.TrackerMap.controllers - -import mx.trackermap.TrackerMap.client.apis.AttributesApi -import mx.trackermap.TrackerMap.client.models.Attribute -import mx.trackermap.TrackerMap.client.models.Device - -class AttributesController( - private val attributesApi: AttributesApi, - private val sessionController: SessionController) -{ - var userAttributes = emptyArray() - val deviceAttributes = mutableMapOf>() - - /** userAttributes **/ - - suspend fun getUserAttributes() { - userAttributes = attributesApi.attributesComputedGet(userId = sessionController.user?.id) - } - - fun getUserAttribute(attribute: String) = userAttributes.find { it.attribute == attribute } - - suspend fun createUserAttribute(attribute: Attribute) { - userAttributes += attributesApi.attributesComputedPost(attribute) - } - - suspend fun updateUserAttribute(attribute: Attribute) { - userAttributes = userAttributes.map { - if (attribute.id != null && it.id == attribute.id) { - attributesApi.attributesComputedIdPut(attribute, attribute.id) - } else it - }.toTypedArray() - } - - suspend fun deleteUserAttribute(attribute: String) { - userAttributes.find { it.attribute == attribute }?.id?.let { id -> - attributesApi.attributesComputedIdDelete(id) - userAttributes = userAttributes.filter { it.id != id }.toTypedArray() - } - } - - /** deviceAttributes **/ - - suspend fun getDeviceAttributes(device: Device) { - device.id?.let { - deviceAttributes[it] = attributesApi.attributesComputedGet(deviceId = device.id) - } - } - - fun getDeviceAttribute(device: Device, attribute: String) = - deviceAttributes[device.id]?.find { it.attribute == attribute } - -} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt deleted file mode 100644 index 2e0d924..0000000 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt +++ /dev/null @@ -1,34 +0,0 @@ -package mx.trackermap.TrackerMap.controllers - -import mx.trackermap.TrackerMap.client.apis.DevicesApi -import mx.trackermap.TrackerMap.client.models.Device - -class DeviceController( - private val devicesApi: DevicesApi, - private val sessionController: SessionController) -{ - var devices = emptyArray() - - suspend fun getDevices() { - devices = devicesApi.devicesGet(userId = sessionController.user?.id) - } - - fun getDevice(id: Int) = devices.find { it.id == id } - - suspend fun createDevice(device: Device) { - devices += devicesApi.devicesPost(device) - } - - suspend fun updateDevice(id: Int, device: Device) { - devices = devices.map { - if (it.id == id) - devicesApi.devicesIdPut(id = id, body = device) - else it - }.toTypedArray() - } - - suspend fun deleteDevice(id: Int) { - devicesApi.devicesIdDelete(id) - devices = devices.filter { it.id != id }.toTypedArray() - } -} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt index ba892f6..8f5ac56 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt @@ -1,20 +1,65 @@ package mx.trackermap.TrackerMap.controllers +import android.util.Log +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.launch import mx.trackermap.TrackerMap.client.apis.SessionApi +import mx.trackermap.TrackerMap.client.models.SessionBody import mx.trackermap.TrackerMap.client.models.User -class SessionController(private val sessionApi: SessionApi) { - var user: User? = null - - suspend fun createSession (email: String, password: String) { - user = sessionApi.sessionPost(email, password) +@DelicateCoroutinesApi +class SessionController( + private val sessionApi: SessionApi +) { + sealed class LoginState { + object Loading: LoginState() + object EmailMissing : LoginState() + object PasswordMissing : LoginState() + object Failure : LoginState() + object Success : LoginState() } - suspend fun getSession () { - user = sessionApi.sessionGet() + val loginStateFlow = MutableStateFlow(null) + val userFlow = MutableStateFlow(null) + + fun restoreSession() { + loginStateFlow.value = LoginState.Loading + GlobalScope.launch { + try { + userFlow.value = sessionApi.sessionGet() + Log.d("LoginViewModel", userFlow.value.toString()) + loginStateFlow.value = LoginState.Success + } catch (e: Exception) { + Log.d("LoginViewModel", "No session") + } + } } - suspend fun deleteSession () { - sessionApi.sessionDelete() + fun login(body: SessionBody) { + val email = body.email.trim() + val password = body.password.trim() + + if (email.isEmpty()) { + loginStateFlow.value = LoginState.EmailMissing + return + } + + if (password.isEmpty()) { + loginStateFlow.value = LoginState.PasswordMissing + return + } + + loginStateFlow.value = LoginState.Loading + GlobalScope.launch { + try { + userFlow.value = sessionApi.sessionPost(email, password) + Log.d("LoginViewModel", userFlow.value.toString()) + loginStateFlow.value = LoginState.Success + } catch (e: Exception) { + loginStateFlow.value = LoginState.Failure + } + } } } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt deleted file mode 100644 index a398185..0000000 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt +++ /dev/null @@ -1,10 +0,0 @@ -package mx.trackermap.TrackerMap.controllers - -import mx.trackermap.TrackerMap.client.apis.UsersApi -import mx.trackermap.TrackerMap.client.models.User - -class UserController(private val usersApi: UsersApi) { - suspend fun updateUser(user: User) { - usersApi.usersPost(user) - } -} \ No newline at end of file -- cgit v1.2.3