diff options
author | Isidro Henoch <imhenoch@protonmail.com> | 2022-01-12 15:09:52 -0600 |
---|---|---|
committer | Isidro Henoch <imhenoch@protonmail.com> | 2022-01-12 15:09:52 -0600 |
commit | cc7cfcb2d65a1164f5b75ae4292ea9a099746038 (patch) | |
tree | e3c4c78016fe2bac20bef92098d0d02b3c6f6ed2 /androidApp/src/main/java | |
parent | 9b5850870bef2770f281d51b5c96ac9103ab3667 (diff) | |
download | etbsa-trackermap-mobile-cc7cfcb2d65a1164f5b75ae4292ea9a099746038.tar.gz etbsa-trackermap-mobile-cc7cfcb2d65a1164f5b75ae4292ea9a099746038.tar.bz2 etbsa-trackermap-mobile-cc7cfcb2d65a1164f5b75ae4292ea9a099746038.zip |
Adds the user information functionality
- Displays the user information
- Allows the user to sign out
Diffstat (limited to 'androidApp/src/main/java')
4 files changed, 130 insertions, 22 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 1563534..808808a 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt @@ -6,6 +6,7 @@ import mx.trackermap.TrackerMap.android.details.commands.UnitCommandsViewModel import mx.trackermap.TrackerMap.android.details.information.UnitInformationViewModel import mx.trackermap.TrackerMap.android.details.reports.UnitReportsViewModel import mx.trackermap.TrackerMap.android.session.LoginViewModel +import mx.trackermap.TrackerMap.android.session.UserInformationViewModel import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.apis.* import mx.trackermap.TrackerMap.controllers.GeofencesController @@ -40,6 +41,7 @@ class TrackerApp : Application() { viewModel { UnitCommandsViewModel(get()) } single { UnitsViewModel(get()) } viewModel { UnitReportsViewModel(get(), get()) } + viewModel { UserInformationViewModel(get()) } } startKoin { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt new file mode 100644 index 0000000..da1c0d5 --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt @@ -0,0 +1,78 @@ +package mx.trackermap.TrackerMap.android.session + +import android.content.Intent +import android.os.Bundle +import android.view.View +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import mx.trackermap.TrackerMap.android.databinding.UserInformationActivityBinding +import mx.trackermap.TrackerMap.client.models.User +import org.koin.androidx.viewmodel.ext.android.viewModel + +class UserInformationActivity : AppCompatActivity() { + + private var _binding: UserInformationActivityBinding? = null + private val binding get() = _binding!! + private val userInformationViewModel: UserInformationViewModel by viewModel() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + _binding = UserInformationActivityBinding.inflate(layoutInflater) + setContentView(binding.root) + + initialize() + } + + private fun initialize() { + setupObservers() + setupEvents() + + userInformationViewModel.fetchUserInfo() + } + + private fun setupObservers() { + userInformationViewModel.userInformation.observe(this) { userInformation -> + when (userInformation) { + UserInformationViewModel.InformationState.Loading -> setLoading(true) + UserInformationViewModel.InformationState.Failure -> failure() + is UserInformationViewModel.InformationState.Success -> display(userInformation.user) + UserInformationViewModel.InformationState.Signout -> signout() + } + } + } + + private fun setupEvents() { + binding.signoutButton.setOnClickListener { userInformationViewModel.signout() } + } + + private fun setLoading(isLoading: Boolean) { + binding.infoLoading.visibility = if (isLoading) View.VISIBLE else View.GONE + binding.userInfoCard.visibility = if (isLoading) View.GONE else View.VISIBLE + } + + private fun failure() { + setLoading(false) + + Toast.makeText(this, "Something went wrong...", Toast.LENGTH_LONG).show() + } + + private fun display(user: User) { + setLoading(false) + + binding.apply { + usernameInfo.text = user.name ?: "" + emailInfo.text = user.email ?: "" + idInfo.text = "${user.id ?: "--"}" + deviceLimitInfo.text = "${user.deviceLimit ?: "--"}" + adminInfo.text = "${user.administrator}" + } + } + + private fun signout() { + val intent = Intent(applicationContext, LoginActivity::class.java) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + startActivity(intent) + finish() + } + +}
\ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationViewModel.kt new file mode 100644 index 0000000..b2379fc --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationViewModel.kt @@ -0,0 +1,44 @@ +package mx.trackermap.TrackerMap.android.session + +import android.util.Log +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.launch +import mx.trackermap.TrackerMap.client.apis.SessionApi +import mx.trackermap.TrackerMap.client.models.User + +class UserInformationViewModel(private val sessionApi: SessionApi) : ViewModel() { + + sealed class InformationState { + object Loading: InformationState() + object Failure: InformationState() + class Success(val user: User) : InformationState() + object Signout: InformationState() + } + + var userInformation = MutableLiveData<InformationState>(InformationState.Loading) + + init { + Log.d("UserInformationVM", "Initializing User Information View Model") + } + + fun fetchUserInfo() { + viewModelScope.launch { + userInformation.postValue(InformationState.Loading) + try { + userInformation.postValue(InformationState.Success(sessionApi.sessionGet())) + } catch (e: Exception) { + userInformation.postValue(InformationState.Failure) + } + } + } + + fun signout() { + viewModelScope.launch { + userInformation.postValue(InformationState.Loading) + sessionApi.sessionDelete() + userInformation.postValue(InformationState.Signout) + } + } +}
\ No newline at end of file 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 409919a..7e5f162 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 @@ -1,5 +1,6 @@ package mx.trackermap.TrackerMap.android.units +import android.content.Intent import android.os.Bundle import android.util.Log import android.view.Gravity @@ -12,6 +13,8 @@ import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UnitsActivityBinding import mx.trackermap.TrackerMap.android.devices.DevicesFragment import mx.trackermap.TrackerMap.android.map.UnitMapFragment +import mx.trackermap.TrackerMap.android.session.LoginActivity +import mx.trackermap.TrackerMap.android.session.UserInformationActivity import org.koin.androidx.viewmodel.ext.android.viewModel @DelicateCoroutinesApi @@ -38,28 +41,9 @@ class UnitsActivity : AppCompatActivity() { } private fun setupEvents() { - binding.drawerButton.setOnClickListener { - binding.drawerLayout.openDrawer(binding.navigationView) - } - binding.navigationView.setNavigationItemSelectedListener { - when (it.itemId) { - R.id.nav_account -> { - Toast.makeText(this, "Account button tapped!", Toast.LENGTH_SHORT).show() - binding.drawerLayout.closeDrawers() - false - } - R.id.nav_about -> { - Toast.makeText(this, "About button tapped!", Toast.LENGTH_SHORT).show() - binding.drawerLayout.closeDrawers() - false - } - R.id.nav_logout -> { - Toast.makeText(this, "Logout button tapped!", Toast.LENGTH_SHORT).show() - binding.drawerLayout.closeDrawers() - false - } - else -> false - } + binding.userButton.setOnClickListener { + val intent = Intent(applicationContext, UserInformationActivity::class.java) + startActivity(intent) } binding.displayModeToggle.setOnClickListener { unitsViewModel.toggleDisplayMode() |