diff options
3 files changed, 34 insertions, 8 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 index f384173..5ae8c75 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt @@ -7,12 +7,11 @@ import android.os.Bundle import android.view.View import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.preference.PreferenceManager import com.zhuinden.liveevent.observe import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UserInformationActivityBinding -import mx.trackermap.TrackerMap.client.models.User -import mx.trackermap.TrackerMap.controllers.ReportController import mx.trackermap.TrackerMap.controllers.SessionController import org.koin.androidx.viewmodel.ext.android.viewModel @@ -71,7 +70,11 @@ class UserInformationActivity : AppCompatActivity() { private fun setupEvents() { binding.backButton.setOnClickListener { onBackPressed() } - binding.signoutButton.setOnClickListener { userInformationViewModel.signOut() } + binding.signoutButton.setOnClickListener { + userInformationViewModel.signOut(PreferenceManager + .getDefaultSharedPreferences(this) + .getString(LoginFragment.PREFERENCE_TOKEN, null)) + } binding.sourceCodeButton.setOnClickListener { openURL(getString(R.string.app_source_code_url)) } 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 index 607f4e5..9bb5ac7 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationViewModel.kt @@ -10,7 +10,6 @@ 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.User import mx.trackermap.TrackerMap.controllers.SessionController import org.koin.core.component.KoinComponent @@ -68,9 +67,9 @@ class UserInformationViewModel : ViewModel(), KoinComponent { } } - fun signOut() { + fun signOut(token: String?) { viewModelScope.launch { - sessionController.logout() + sessionController.logout(token) } } }
\ 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 da9b115..3c213d5 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt @@ -85,7 +85,7 @@ class SessionController( } private suspend fun registerFcmToken(token: String) { - print("FCM token: $token\n") + print("Register FCM token: $token\n") try { userFlow.value?.let { user -> val attributes = user.attributes.toMutableMap() @@ -109,9 +109,33 @@ class SessionController( } } - fun logout() { + private suspend fun unregisterFcmToken(token: String) { + print("Unregister FCM token: $token\n") + try { + userFlow.value?.let { user -> + val attributes = user.attributes.toMutableMap() + attributes["notificationTokens"].toString().let { tokens -> + if (tokens.contains("$token,")) { + attributes["notificationTokens"] = + JsonPrimitive(token.replace("$token,", "")) + } else if (tokens.contains(token)) { + attributes["notificationTokens"] = + JsonPrimitive(token.replace(token, "")) + } + usersApi.usersIdPut(user.copy( + attributes = attributes + ), user.id!!) + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + fun logout(token: String?) { GlobalScope.launch { loginStateFlow.value = LoginState.Loading + token?.let { unregisterFcmToken(it) } sessionApi.sessionDelete() loginStateFlow.value = LoginState.SignOut } |