EasyFirebase

More: Author   ReportBugs   
Tags:
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.
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea