diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-15 00:42:53 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-15 00:42:53 -0600 |
commit | ff8074a32925dd51bcb29e630104ff031b29df75 (patch) | |
tree | 882fa93d0231059b052c7010f59dd2e0fe11173f /shared/src | |
parent | ff0930a91d62516ea278d462e647c14bd42989ca (diff) | |
download | etbsa-trackermap-mobile-ff8074a32925dd51bcb29e630104ff031b29df75.tar.gz etbsa-trackermap-mobile-ff8074a32925dd51bcb29e630104ff031b29df75.tar.bz2 etbsa-trackermap-mobile-ff8074a32925dd51bcb29e630104ff031b29df75.zip |
Added code for registering FCM token (HTTP 401 error)
Diffstat (limited to 'shared/src')
4 files changed, 45 insertions, 14 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt index 015b329..858cd10 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/UsersApi.kt @@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.User import mx.trackermap.TrackerMap.client.infrastructure.* -class UsersApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiClient(basePath) { +class UsersApi(basePath: String = "https://demo.traccar.org/api") : ApiClient(basePath) { /** * Fetch a list of Users @@ -24,18 +24,18 @@ class UsersApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiCl * @return kotlin.Array<User> */ @Suppress("UNCHECKED_CAST") - suspend fun usersGet(userId: kotlin.String? = null): kotlin.Array<User> { + suspend fun usersGet(userId: String? = null): Array<User> { val localVariableQuery: MultiValueMap = mapOf("userId" to listOf("$userId")) val localVariableConfig = RequestConfig( RequestMethod.GET, "/users", query = localVariableQuery ) - val response = request<kotlin.Array<User>>( + val response = request<Array<User>>( localVariableConfig ) return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array<User> + ResponseType.Success -> (response as Success<*>).data as Array<User> ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error") @@ -48,7 +48,7 @@ class UsersApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiCl * @param id * @return void */ - suspend fun usersIdDelete(id: kotlin.Int): Unit { + suspend fun usersIdDelete(id: Int) { val localVariableConfig = RequestConfig( RequestMethod.DELETE, @@ -74,9 +74,8 @@ class UsersApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiCl * @return User */ @Suppress("UNCHECKED_CAST") - suspend fun usersIdPut(body: User, id: kotlin.Int): User { - val localVariableBody: kotlin.Any? = body - + suspend fun usersIdPut(body: User, id: Int): User { + val localVariableBody: Any = body val localVariableConfig = RequestConfig( RequestMethod.PUT, "/users/{id}".replace("{" + "id" + "}", "$id") @@ -101,7 +100,7 @@ class UsersApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiCl */ @Suppress("UNCHECKED_CAST") suspend fun usersPost(body: User): User { - val localVariableBody: kotlin.Any? = body + val localVariableBody: Any = body val localVariableConfig = RequestConfig( RequestMethod.POST, diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt index 4ec7b46..4bda0ae 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt @@ -38,7 +38,7 @@ open class ApiClient(val baseUrl: String) { } install(Logging) { logger = Logger.DEFAULT - level = LogLevel.HEADERS + level = LogLevel.ALL } } 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 bda2fa4..e98ff34 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 @@ -18,6 +18,6 @@ package mx.trackermap.TrackerMap.client.models */ data class SessionBody ( val email: String, - val password: String -) { -}
\ No newline at end of file + val password: String, + val fcmToken: String? = null +)
\ 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 ed65824..b9cf335 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt @@ -3,15 +3,19 @@ package mx.trackermap.TrackerMap.controllers import android.util.Log import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch +import kotlinx.serialization.json.JsonPrimitive import mx.trackermap.TrackerMap.client.apis.SessionApi +import mx.trackermap.TrackerMap.client.apis.UsersApi import mx.trackermap.TrackerMap.client.models.SessionBody import mx.trackermap.TrackerMap.client.models.User @DelicateCoroutinesApi class SessionController( - private val sessionApi: SessionApi + private val sessionApi: SessionApi, + private val usersApi: UsersApi ) { sealed class LoginState { object Loading: LoginState() @@ -54,6 +58,7 @@ class SessionController( fun login(body: SessionBody) { val email = body.email.trim() val password = body.password.trim() + val token = body.fcmToken if (email.isEmpty()) { loginStateFlow.value = LoginState.EmailMissing @@ -69,7 +74,9 @@ class SessionController( GlobalScope.launch { try { userFlow.value = sessionApi.sessionPost(email, password) + delay(3000) Log.d("LoginViewModel", userFlow.value.toString()) + token?.let { registerFcmToken(it) } loginStateFlow.value = LoginState.Success } catch (e: Exception) { loginStateFlow.value = LoginState.Failure @@ -77,6 +84,31 @@ class SessionController( } } + private suspend fun registerFcmToken(token: String) { + print("FCM token: $token\n") + try { + userFlow.value?.let { user -> + val attributes = user.attributes.toMutableMap() + attributes["notificationTokens"].toString().let { tokens -> + if (tokens == "null" || !tokens.contains(token)) { + if (tokens == "null") { + attributes["notificationTokens"] = JsonPrimitive(token) + } else if (tokens.contains(token)) { + attributes["notificationTokens"] = JsonPrimitive("$tokens,$token") + } + + + } + } + usersApi.usersIdPut(user.copy( + attributes = attributes + ), user.id!!) + } + } catch (e: Exception) { + e.printStackTrace() + } + } + fun logout() { GlobalScope.launch { loginStateFlow.value = LoginState.Loading |