Coder Social home page Coder Social logo

easyfirebase's Introduction

EasyFirebase

compile 'com.github.florent37:easyfirebase:1.0.0'
Android app on Google Play

Google Login

Initialize

private EasyFirebaseAuth easyFirebaseAuth;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        GoogleApiClient googleApiClient= ...

        this.easyFirebaseAuth = new EasyFirebaseAuth(googleApiClient);
}

@Override
public void onStart() {
    super.onStart();
    easyFirebaseAuth.onStart();
}

@Override
public void onStop() {
    easyFirebaseAuth.onStop();
    super.onStop();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    easyFirebaseAuth.onActivityResult(requestCode, data);
}

Start Login

easyFirebaseAuth.signInWithGoogle((Activity)this)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(Pair<googleSignInAccount, firebaseUser> -> {
                    //user is connected
                    final String id = firebaseUser.getUid();
                    final String name = firebaseUser.getDisplayName();
                    final String email = firebaseUser.getEmail();
                    final String photoUrl = firebaseUser.getPhotoUrl().toString();
                    //update your UI
                });

Database

Initialize

DatabaseReference database = FirebaseDatabase.getInstance().getReference();

EasyFirebaseDb userDb = new EasyFirebaseDb(database.child("users"), converter);

Create

userDb.addNew(name)
      .withValue("registerDate", System.currentTimeMillis())
      .withValue("id", id)
      .withValue("name", name)
      .withValue("email", email)
      .withValue("photoUrl", photoUrl)
      .push()
      .subscribe(v -> {
            //the user has been created
      })

Query

Observable<User> = userDb.getObject(userName, User.class);

Observable<List<User>> = userDb.getChildsObjects(User.class).toList();

Join

Observable<List<User>> = Observable.zip(
        userDb.getChildsObjects(User.class).toList(),
        followersDb.getKeys(username), //only retrieve the keys
        (users, followers) -> {
             for (User user : users) {
                 final String userName = user.getName();
                 if (followers.contains(userName)) {
                     user.setFollower(true);
                 }
             }
             return users;
});

Converter

public class MyConverterImpl implements RxFirebaseConverter {

    @Override
    public <T> void convert(DataSnapshot dataSnapshot, Class<? super T> theClass, Subscriber<? super T> subscriber){
            if(User.class.equals(theClass)){
                userFromDataSnapshot(dataSnapshot, subscriber);
            } else {
                throw new UnsupportedOperationException("unknown :" + theClass.getCanonicalName().toString());
            }
    }

    public <T> void userFromDataSnapshot(DataSnapshot dataSnapshot, Subscriber<? super T> subscriber) {
            final String userName = dataSnapshot.getKey();
            final String email = dataSnapshot.child("email").getValue(String.class);
            final String id = dataSnapshot.child("id").getValue(String.class);
            final String photoUrl = dataSnapshot.child("photoUrl").getValue(String.class);

            final User user = new User(userName, email, id, photoUrl);

            subscriber.onNext((T)user);
    }

#Credits

Author: Florent Champigny http://www.florentchampigny.com/

Android app on Google Play Follow me on Google+ Follow me on Twitter Follow me on LinkedIn

License

Copyright 2016 florent37, Inc.

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.

easyfirebase's People

Contributors

florent37 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.