From cc7cfcb2d65a1164f5b75ae4292ea9a099746038 Mon Sep 17 00:00:00 2001 From: Isidro Henoch Date: Wed, 12 Jan 2022 15:09:52 -0600 Subject: Adds the user information functionality - Displays the user information - Allows the user to sign out --- .../mx/trackermap/TrackerMap/android/TrackerApp.kt | 2 + .../android/session/UserInformationActivity.kt | 78 ++++++++++++++++++++++ .../android/session/UserInformationViewModel.kt | 44 ++++++++++++ .../TrackerMap/android/units/UnitsActivity.kt | 28 ++------ 4 files changed, 130 insertions(+), 22 deletions(-) create mode 100644 androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt create mode 100644 androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationViewModel.kt (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap') 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.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() -- cgit v1.2.3 From 270e54f2d83a962a33646c47917d8ea00a282bd9 Mon Sep 17 00:00:00 2001 From: Isidro Henoch Date: Wed, 12 Jan 2022 18:05:18 -0600 Subject: Fixes issues - Fixes the logout problem that don't let users sign in again - Fixes the search bug --- .../mx/trackermap/TrackerMap/android/TrackerApp.kt | 18 +++++++------- .../TrackerMap/android/devices/DevicesFragment.kt | 21 +++++++++++++--- .../TrackerMap/android/map/UnitMapFragment.kt | 28 +++++++++++++++------- .../TrackerMap/android/units/UnitFragment.kt | 8 +++++++ .../TrackerMap/android/units/UnitsActivity.kt | 11 ++++++--- 5 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitFragment.kt (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap') 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 808808a..cdb7102 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt @@ -26,20 +26,20 @@ class TrackerApp : Application() { val appModule = module { single { "https://etbsa.net/api" } - single { SessionApi(get()) } - single { DevicesApi(get()) } - single { PositionsApi(get()) } - single { CommandsApi(get()) } - single { ReportsApi(get()) } - single { GeofencesApi(get()) } + factory { SessionApi(get()) } + factory { DevicesApi(get()) } + factory { PositionsApi(get()) } + factory { CommandsApi(get()) } + factory { ReportsApi(get()) } + factory { GeofencesApi(get()) } - single { UnitsController(get(), get()) } - single { GeofencesController(get()) } + factory { UnitsController(get(), get()) } + factory { GeofencesController(get()) } viewModel { LoginViewModel(get(), get()) } viewModel { UnitInformationViewModel(get()) } viewModel { UnitCommandsViewModel(get()) } - single { UnitsViewModel(get()) } + viewModel { UnitsViewModel(get()) } viewModel { UnitReportsViewModel(get(), get()) } viewModel { UserInformationViewModel(get()) } } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt index bd3ab67..6bee413 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt @@ -11,18 +11,17 @@ import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.databinding.DevicesFragmentBinding import mx.trackermap.TrackerMap.android.details.DetailsActivity +import mx.trackermap.TrackerMap.android.units.UnitFragment import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.models.UnitInformation import org.koin.androidx.viewmodel.ext.android.viewModel @DelicateCoroutinesApi -class DevicesFragment : Fragment() { +class DevicesFragment(private val unitsViewModel: UnitsViewModel) : UnitFragment(unitsViewModel) { private var _binding: DevicesFragmentBinding? = null private val binding get() = _binding!! - private val unitsViewModel: UnitsViewModel by viewModel() - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -42,9 +41,20 @@ class DevicesFragment : Fragment() { super.onViewCreated(view, savedInstanceState) setupList() + } + + override fun onResume() { + super.onResume() + setupObservers() } + override fun onPause() { + super.onPause() + + removeObservers() + } + private fun setupList() { binding.devicesList.layoutManager = LinearLayoutManager( context, LinearLayoutManager.VERTICAL, @@ -65,6 +75,11 @@ class DevicesFragment : Fragment() { } } + private fun removeObservers() { + Log.d("DevicesFragment", "removeObservers()") + unitsViewModel.units.removeObservers(viewLifecycleOwner) + } + private fun itemAction(unit: UnitInformation, action: Action) { when (action) { Action.DETAILS, Action.REPORTS, Action.COMMANDS -> { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt index fc417a0..30e3cea 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt @@ -1,29 +1,23 @@ package mx.trackermap.TrackerMap.android.map import android.content.Intent -import android.graphics.Color import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.fragment.app.Fragment import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UnitMapFragmentBinding import mx.trackermap.TrackerMap.android.details.DetailsActivity import mx.trackermap.TrackerMap.android.devices.Action import mx.trackermap.TrackerMap.android.devices.UnitRenderData +import mx.trackermap.TrackerMap.android.units.UnitFragment import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.models.UnitInformation -import mx.trackermap.TrackerMap.utils.Formatter -import mx.trackermap.TrackerMap.utils.SpeedUnit -import org.koin.androidx.viewmodel.ext.android.viewModel @DelicateCoroutinesApi -class UnitMapFragment : Fragment() { - - private val unitsViewModel: UnitsViewModel by viewModel() +class UnitMapFragment(private val unitsViewModel: UnitsViewModel) : UnitFragment(unitsViewModel) { private var _binding: UnitMapFragmentBinding? = null private val binding get() = _binding!! @@ -42,9 +36,20 @@ class UnitMapFragment : Fragment() { super.onViewCreated(view, savedInstanceState) initializeMap() + } + + override fun onResume() { + super.onResume() + setupObservers() } + override fun onPause() { + super.onPause() + + removeObservers() + } + private fun initializeMap() { unitsMapFragment = childFragmentManager.findFragmentById(R.id.unitsMap) as MapFragment unitsMapFragment.markerCallback = unitsViewModel::selectUnitWith @@ -93,6 +98,13 @@ class UnitMapFragment : Fragment() { } } + private fun removeObservers() { + Log.d("MapFragment", "removeObservers()") + unitsViewModel.units.removeObservers(viewLifecycleOwner) + unitsViewModel.selectedUnit.removeObservers(viewLifecycleOwner) + unitsViewModel.geofences.removeObservers(viewLifecycleOwner) + } + private fun itemAction(unit: UnitInformation, action: Action) { when (action) { Action.DETAILS, Action.REPORTS, Action.COMMANDS -> { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitFragment.kt new file mode 100644 index 0000000..ab3afc1 --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitFragment.kt @@ -0,0 +1,8 @@ +package mx.trackermap.TrackerMap.android.units + +import androidx.fragment.app.Fragment +import kotlinx.coroutines.DelicateCoroutinesApi + +open class UnitFragment @DelicateCoroutinesApi constructor( + private val unitsViewModel: UnitsViewModel +) : Fragment() \ 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 7e5f162..e198ce0 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 @@ -24,6 +24,12 @@ class UnitsActivity : AppCompatActivity() { private val binding get() = _binding!! private val unitsViewModel: UnitsViewModel by viewModel() + private val mapFragment: UnitMapFragment by lazy { + UnitMapFragment(unitsViewModel) + } + private val devicesFragment: DevicesFragment by lazy { + DevicesFragment(unitsViewModel) + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -73,9 +79,8 @@ class UnitsActivity : AppCompatActivity() { val newFragment = when (displayMode) { - UnitsViewModel.UnitsDisplayMode.LIST -> DevicesFragment() - UnitsViewModel.UnitsDisplayMode.MAP -> UnitMapFragment() - else -> DevicesFragment() + UnitsViewModel.UnitsDisplayMode.LIST -> devicesFragment + UnitsViewModel.UnitsDisplayMode.MAP -> mapFragment } supportFragmentManager.commit { replace(R.id.displayContainer, newFragment) -- cgit v1.2.3