All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.plusmobileapps.firebase.auth.Cache.kt Maven / Gradle / Ivy

Go to download

A kotlin multiplatform mobile library for authenticating with Firebase for Android, iOS, and JVM

The newest version!
package com.plusmobileapps.firebase.auth

import com.plusmobileapps.firebase.auth.model.FirebaseUserEntity
import com.russhwolf.settings.Settings
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

internal interface Cache {
    val user: StateFlow

    fun saveUser(user: FirebaseUserEntity)

    fun clear()
}

internal class CacheImpl(
    private val settings: Settings,
) : Cache {
    private val _user = MutableStateFlow(
        settings.getStringOrNull(FIREBASE_USER_KEY)?.let { firebaseUserJson ->
            Json.decodeFromString(firebaseUserJson)
        }
    )

    override val user: StateFlow = _user.asStateFlow()

    override fun saveUser(user: FirebaseUserEntity) {
        settings.putString(FIREBASE_USER_KEY, Json.encodeToString(user))
        _user.value = user
    }

    override fun clear() {
        settings.remove(FIREBASE_USER_KEY)
        _user.value = null
    }

    companion object {
        const val FIREBASE_USER_KEY = "firebase-user-key"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy