GithubHelp home page GithubHelp logo

ariskou / rxfirebase Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nmoskalenko/rxfirebase

1.0 1.0 0.0 204 KB

RxJava 2 wrapper on Google's Firebase for Android library

License: Apache License 2.0

Java 100.00%
rxjava2 firebase android android-library

rxfirebase's Introduction

Hex.pm Platform

RxFirebase 2

RxJava 2 wrapper on Google's Firebase for Android library.

Usage

Library provides set of static methods of classes:

  • RxFirebaseAuth
  • RxFirebaseUser
  • RxFirebaseDatabase
  • RxFirebaseStorage

Authentication:

Sign in anonymously and get token:

RxFirebaseAuth.signInAnonymously(FirebaseAuth.getInstance())
                .flatMap(x -> RxFirebaseUser.getToken(FirebaseAuth.getInstance().getCurrentUser(), false))
                .subscribe(token -> {
                    Log.i("RxFirebaseSample", "user token: " + token.getToken());
                }, throwable -> {
                    Log.e("RxFirebaseSample", throwable.toString());
                });

Database:

Many thanks to @renanferrari for making it much more flexible.

You can simply observe values providing the Class of expected data like:

RxFirebaseDatabase.observeSingleValue(reference.child("users").child("nick"), User.class)
            .subscribe(user -> {
                // process user value(nullable)
            });

or providing your own mapper between DataSnapshot and your data type:

RxFirebaseDatabase.observeSingleValueEvent(reference.child("posts"),
                dataSnapshot -> {
                    // do your own mapping here
                    return new User();
                })
                .subscribe(user -> {
                    // process user value
                });

There are some pre-defined mappers to make things easier:

Observing list values
RxFirebaseDatabase.observeSingleValueEvent(reference.child("posts"), DataSnapshotMapper.listOf(BlogPost.class))
                .subscribe(blogPost -> {
                    // process blogPost list item
                });
Observing map values
        RxFirebaseDatabase.observeSingleValueEvent(reference.child("posts"), DataSnapshotMapper.mapOf(BlogPost.class))
                .subscribe(blogPostAsMapItem -> {
                    // process blogPost as key-value pair
                });

Storage

Download file from Firebase storage

StorageReference storageRef = FirebaseStorage.getInstance().getReferenceFromUrl("gs://project-1125675579821020265.appspot.com");
RxFirebaseStorage.getFile(storageRef.child("README.md"), targetFile)
        .subscribe(snapshot -> {
            Log.i("RxFirebaseSample", "transferred: " + snapshot.getBytesTransferred() + " bytes");
        }, throwable -> {
            Log.e("RxFirebaseSample", throwable.toString());
        });

or download file as bytes array

RxFirebaseStorage.getBytes(storageRef.child("README.md"), 1024 * 100)
                .subscribe(bytes -> {
                    Log.i("RxFirebaseSample", "downloaded: " + new String(bytes));
                }, throwable -> {
                    Log.e("RxFirebaseSample", throwable.toString());
                });

Download

Gradle:

Step 1. Add the JitPack repository to your build file (root build.gradle file)

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Step 2. Add the RxFirebase dependency along with the needed Firebase dependencies

dependencies {
  compile 'com.google.firebase:firebase-auth:10.0.1'
  compile 'com.google.firebase:firebase-database:10.0.1'
  compile 'com.google.firebase:firebase-storage:10.0.1'
  compile 'com.github.ariskou:rxfirebase:master-SNAPSHOT'
}
Maven:

Step 1. Add the JitPack repository to your pom file

<repositories>
  <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
  </repository>
</repositories>

Step 2. Add the RxFirebase dependency

<dependency>
  <groupId>com.github.ariskou</groupId>
  <artifactId>rxfirebase</artifactId>
  <version>master-SNAPSHOT</version>
  <type>pom</type>
</dependency>

License

Copyright 2016 Nickolay Moskalenko
Adaptation to RxJava 2 - Copyright 2017 Remous-Aris Koutsiamanis

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

rxfirebase's People

Contributors

nmoskalenko avatar ordonteam avatar renanferrari avatar ariskou avatar ahaverty avatar

Stargazers

Alex avatar

Watchers

 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.