package io.github.fbmediahack.quiethome.db;

import android.support.annotation.NonNull;

import com.google.android.gms.tasks.Task;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.Query;

import io.github.fbmediahack.quiethome.model.User;
import rx.Observable;
import rx.Subscriber;

public class UserTable {
    private final DatabaseReference ref;
    private static final String PATH = "users";

    public UserTable(@NonNull final DatabaseReference reference) {
        this.ref = reference;
    }

    @NonNull
    public Task<Void> insert(@NonNull final User user) {
        return ref.child(PATH).child(user.userId).setValue(user);
    }

    public Observable<User> getAllUsers() {
        return Observable.create(new Observable.OnSubscribe<User>() {
            @Override
            public void call(final Subscriber<? super User> subscriber) {
                ref.child(PATH).addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        subscriber.onNext(dataSnapshot.getValue(User.class));
                    }

                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });
    }

    public Query getAllUsersQuery() {
        return ref.child(PATH);
    }
}

@@ -128,165 +128,8 @@
 sk;%0A
-import com.google.firebase.database.ChildEventListener;%0Aimport com.google.firebase.database.DataSnapshot;%0Aimport com.google.firebase.database.DatabaseError;%0A
 impo
@@ -179,16 +179,16 @@
 erence;%0A
+
 import c
@@ -277,52 +277,8 @@
 ser;
-%0Aimport rx.Observable;%0Aimport rx.Subscriber;
 %0A%0Apu
@@ -560,24 +560,24 @@
 ser user) %7B%0A
+
         retu
@@ -641,1103 +641,8 @@
  %7D%0A%0A
-    public Observable%3CUser%3E getAllUsers() %7B%0A        return Observable.create(new Observable.OnSubscribe%3CUser%3E() %7B%0A            @Override%0A            public void call(final Subscriber%3C? super User%3E subscriber) %7B%0A                ref.child(PATH).addChildEventListener(new ChildEventListener() %7B%0A                    @Override%0A                    public void onChildAdded(DataSnapshot dataSnapshot, String s) %7B%0A                        subscriber.onNext(dataSnapshot.getValue(User.class));%0A                    %7D%0A%0A                    @Override%0A                    public void onChildChanged(DataSnapshot dataSnapshot, String s) %7B%0A%0A                    %7D%0A%0A                    @Override%0A                    public void onChildRemoved(DataSnapshot dataSnapshot) %7B%0A%0A                    %7D%0A%0A                    @Override%0A                    public void onChildMoved(DataSnapshot dataSnapshot, String s) %7B%0A%0A                    %7D%0A%0A                    @Override%0A                    public void onCancelled(DatabaseError databaseError) %7B%0A%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     

Delete user observable