diff options
author | Isidro Henoch <imhenoch@protonmail.com> | 2022-01-12 18:10:01 -0600 |
---|---|---|
committer | Isidro Henoch <imhenoch@protonmail.com> | 2022-01-12 18:10:01 -0600 |
commit | 9a125dd2ca5144ae49761b73564871bb0b38c0b7 (patch) | |
tree | 2f64529e1be83c896a47774876ea1bd514f1c719 /androidApp/src/main/java/mx/trackermap/TrackerMap/android/session | |
parent | b9a9eeeb9f104b6fe11a5bc523cad189b293160c (diff) | |
parent | 270e54f2d83a962a33646c47917d8ea00a282bd9 (diff) | |
download | etbsa-trackermap-mobile-9a125dd2ca5144ae49761b73564871bb0b38c0b7.tar.gz etbsa-trackermap-mobile-9a125dd2ca5144ae49761b73564871bb0b38c0b7.tar.bz2 etbsa-trackermap-mobile-9a125dd2ca5144ae49761b73564871bb0b38c0b7.zip |
Merge branch 'user_information'
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android/session')
2 files changed, 122 insertions, 0 deletions
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 |