One Firebase Firestore

Firestore, also known as Firebase Firestore, is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. It's designed to store and sync data for client- and server-side development. Firestore can be integrated into Android applications to provide real-time data synchronization, offline support, and efficient querying capabilities.

Checkout the full features here

Before you can use OneFirebaseFirestore you must add dependencies from firebase first here.

Implement One Firebase Firestore

Create a class and implement the built in OneFirebaseFirestore. For example

class OneFirebaseDataSource : FirebaseRepository, OneFirebaseFirestore() {
    override val databaseRef: FirebaseFirestore = FirebaseFirestore.getInstance()
}

Then, you can use all fungsion of OneFirebaseFirestore. For Example:

class OneFirebaseDataSource : FirebaseRepository, OneFirebaseFirestore() {

    override val databaseRef: FirebaseFirestore = FirebaseFirestore.getInstance()
    
    override suspend fun addData(request: FirestoreRequest): Flow<Resource<Boolean>> =
        callbackFlow {
            setValue(
                value = request.toMap(),
                collection = "users",
                eventListener = { trySend(it) }
            )
            awaitClose { this.close() }
        }

    override suspend fun update(request: FirestoreRequest): Flow<Resource<Boolean>> = callbackFlow {
        val collection = "users"

        updateValue(
            id = request.id.toString(),
            collection = collection,
            value = request.toMap(),
            eventListener = { trySend(it) }
        )
        awaitClose { this.close() }
    }

    override suspend fun getList(): Flow<Resource<List<FirestoreResponse>>> = callbackFlow {
        val collection = "users"

        getListValue(
            collection = collection,
            clazz = FirestoreResponse::class.java,
            eventListener = { trySend(it) }
        )

        awaitClose { this.close() }
    }

    override suspend fun getSingle(id: String): Flow<Resource<FirestoreResponse>> = callbackFlow {
        val document = "users"

        getSingleValue(
            id = id,
            collection = document,
            clazz = FirestoreResponse::class.java,
            eventListener = { trySend(it) },
        )

        awaitClose { this.close() }
    }

    override suspend fun remove(id: String): Flow<Resource<Boolean>> = callbackFlow {
        deleteValue(
            id = id,
            collection = "users",
            eventListener = { trySend(it) }
        )
        awaitClose { this.close() }
    }
}

This is content of RequestModel and ResponseModel:

data class FirestoreRequest(
    var id: String? = emptyString(),
    var image: String? = emptyString(),
    val name: String? = emptyString(),
    val age: Int? = null,
    val address: String? = emptyString()
){
    @Exclude
    fun toMap() = hashMapOf<String, Any?>(
        "id" to id,
        "name" to name,
        "age" to age,
        "address" to address,
        "image" to image
   )
}

The FirestoreRequest class is a data model for holding information typically stored in a Firestore document. The toMap function provides a convenient way to convert the object's properties into a format suitable for Firestore operations. The @Exclude annotation ensures that the toMap function itself is not serialized when the object is converted to a Firestore document.

data class FirestoreResponse(
    val id: String? = null,
    val name: String? = null,
    val age: Int? = null,
    val address: String? = null,
    val image: String? = null
)

The FirestoreResponse data class is a Kotlin model representing a response from a Firestore database. It contains five nullable properties: id (a string for the document ID), name (a string for the name of the person or item), age (an integer for the age), address (a string for the address), and image (a string for the image URL or path). Each property is initialized to null by default, making it optional when creating instances of this class. This model can be used to map Firestore documents to Kotlin objects, facilitating data retrieval and manipulation within an application.

And this is the content of FirebaseRepository:

interface FirebaseRepository {
    suspend fun addData(request: FirestoreRequest): Flow<Resource<Boolean>>
    suspend fun update(request: FirestoreRequest): Flow<Resource<Boolean>>
    suspend fun getList(): Flow<Resource<List<FirestoreResponse>>>
    suspend fun getSingle(id: String): Flow<Resource<FirestoreResponse>>
    suspend fun remove(id: String): Flow<Resource<Boolean>>
}

Last updated