Code Monkey home page Code Monkey logo

firebase-coroutines-android's Introduction

Firebase-Coroutines-Android

Kotlin Coroutines helpers for Firebase Library

Extension functions:

Google Play API

Name Description
[Task.await] Awaits for completion of the Task and return the result
[Task.asDeferred] Converts the Task in a the instance of Deferred
[Deferred.asTask] Converts the Deferred in a the instance of Task

Firebase Firestore

Name Description
[CollectionReference.getDocument] Awaits for the document and returns it casted to the desired type. Throws exception over null values
[CollectionReference.getDocumentOrNull] Awaits for the document and returns it casted to the desired type
[CollectionReference.addDocumentBlocking] Blocking call over the add document method
[Query.getCollection] Awaits for the collection and returns it casted to a list of the desired type
[DocumentReference.getCollection] Awaits for the collection and returns it casted to a list of the desired type
[DocumentReference.setValueBlocking] Awaits for the given value to be set on the current reference
[DocumentReference.updateFieldBlocking] Awaits for the given field over the current reference to be updated
[DocumentReference.updateBlocking] Awaits for the given fields over the current reference to be updated
[DocumentReference.deleteBlocking] Awaits for the current reference to be deleted
[DocumentReference.snapshotChangesChannel] Returns a Channel with unlimited capacity that will send a new message every time that there is a change on the current reference

Flow Utilities

This library includes extensions to provide compatibility with flow while using Firestore snapshot listeners. The extensions are ready to take into account the possibility of offline and cached data, together with the casting from Firebase model to application models. Example:

scope.launch {
             firestore.collection("example")
                 .snapshotChangesFlow(
                     skipFirstCacheHit = ConnectivityUtils.hasInternetConnection(context),
                     documentMapping = { documentSnapshot -> documentSnapshot.toObject(FirebaseModel::class.java) },
                     mappingFn = { firebaseModel, id -> Model(id= id, data = firebaseModel) })
                 .catch { manageError(it) }
                 .collect { event ->
                     when (event) {
                         is SnapshotListenerEvent.FirestoreDocumentChanges -> {
                             //manage event based on type(modified, deleted, added)
                         }
                         is SnapshotListenerEvent.IsEmpty -> {
                             //Empty result
                         }
                         is SnapshotListenerEvent.HasNotPendingWrites -> {
                             //Data updated from offline mode and after connect to internet there is no needed changes
                         }
                     }
                 }
         }

Firebase Authentication

Name Description
[FirebaseAuth.authStateChannel] Returns a Channel with conflated capacity that will send a new message every time that there is a change on the authentication user over the current FirebaseAuth instance
[FirebaseAuth.idTokenChannel] Returns a Channel with conflated capacity that will send a new message every time that there is a change on the authentication id token over the current FirebaseAuth instance
[FirebaseAuth.updateCurrentUserProfile] Updates the current FirebaseUser inside the current FirebaseAuth instance
[FirebaseUser.updateProfile] Helper method to simplify working with FirebaseUser.updateProfile during a FirebaseUser update

Firebase Storage

Name Description
[StorageReference.updateMetadata] Helper method to simplify working with StorageMetadata.Builder during a StorageMetadata update
[StorageReference.deleteBlocking] Awaits for the current reference to be deleted

Working with Firebase and Kotlin coroutines

The following code shows some examples of the use of Firebase with coroutines instead of the common Play Services API.

The standard approach for send an email verification would be:

    private fun sendVerificationEmailToUser(user: FirebaseUser) {
        user.reload().addOnCompleteListener { reloadResult ->
            if (reloadResult.isSuccessful) {
                user.sendEmailVerification().addOnCompleteListener { emailResult ->
                    if (reloadResult.isSuccessful) {
                        //manage success
                    } else {
                        //Manage error
                    }
                }
            } else {
                //Manage error
            }
        }
    }

The coroutines approach:

 private suspend fun sendVerificationEmailToUser(user: FirebaseUser) {
        try {
            user.reload().await()
            user.sendEmailVerification().await()
            //Manage success
        } catch (e: Exception) {
            //Manage exception
        }
    }

firebase-coroutines-android's People

Contributors

frangsierra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.