Search in sources :

Example 26 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project quickstart-android by firebase.

the class PostDetailActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    // Add value event listener to the post
    // [START post_value_event_listener]
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            Post post = dataSnapshot.getValue(Post.class);
            // [START_EXCLUDE]
            mAuthorView.setText(post.author);
            mTitleView.setText(post.title);
            mBodyView.setText(post.body);
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
        // [END_EXCLUDE]
        }
    };
    mPostReference.addValueEventListener(postListener);
    // [END post_value_event_listener]
    // Keep copy of post listener so we can remove it when app stops
    mPostListener = postListener;
    // Listen for comments
    mAdapter = new CommentAdapter(this, mCommentsReference);
    mCommentsRecycler.setAdapter(mAdapter);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Post(com.google.firebase.quickstart.database.models.Post) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 27 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project priend by TakoJ.

the class NewPostActivity method submitPost.

private void submitPost() {
    final String title = mTitleField.getText().toString();
    final String body = mBodyField.getText().toString();
    // Title is required
    if (TextUtils.isEmpty(title)) {
        mTitleField.setError(REQUIRED);
        return;
    }
    // Body is required
    if (TextUtils.isEmpty(body)) {
        mBodyField.setError(REQUIRED);
        return;
    }
    // Disable button so there are no multi-posts
    setEditingEnabled(false);
    Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
    // [START single_value_read]
    final String userId = mAuth.getCurrentUser().getUid();
    mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user value
            User user = dataSnapshot.getValue(User.class);
            // [START_EXCLUDE]
            if (user == null) {
                // User is null, error out
                Log.e(TAG, "User " + userId + " is unexpectedly null");
                Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
            } else {
                // Write new post
                writeNewPost(userId, user.username, title, body);
            }
            // Finish this Activity, back to the stream
            setEditingEnabled(true);
            finish();
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            setEditingEnabled(true);
        // [END_EXCLUDE]
        }
    });
// [END single_value_read]
}
Also used : User(com.example.management.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 28 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project priend by TakoJ.

the class PostDetailActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    // Add value event listener to the post
    // [START post_value_event_listener]
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            Post post = dataSnapshot.getValue(Post.class);
            // [START_EXCLUDE]
            mAuthorView.setText(post.author);
            mTitleView.setText(post.title);
            mBodyView.setText(post.body);
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
        // [END_EXCLUDE]
        }
    };
    mPostReference.addValueEventListener(postListener);
    // [END post_value_event_listener]
    // Keep copy of post listener so we can remove it when app stops
    mPostListener = postListener;
    // Listen for comments
    mAdapter = new CommentAdapter(this, mCommentsReference);
    mCommentsRecycler.setAdapter(mAdapter);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Post(com.example.management.models.Post) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 29 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project priend by TakoJ.

the class PostDetailActivity method postComment.

private void postComment() {
    final String uid = mAuth.getCurrentUser().getUid();
    FirebaseDatabase.getInstance().getReference().child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user information
            User user = dataSnapshot.getValue(User.class);
            String authorName = user.username;
            // Create new comment object
            String commentText = mCommentField.getText().toString();
            Comment comment = new Comment(uid, authorName, commentText);
            // Push the comment, it will appear in the list
            mCommentsReference.push().setValue(comment);
            // Clear the field
            mCommentField.setText(null);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : Comment(com.example.management.models.Comment) User(com.example.management.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 30 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project Robot-Scouter by SUPERCILEX.

the class Scouts method build.

public Task<Map<TeamHelper, List<Scout>>> build() {
    List<Task<Pair<TeamHelper, List<String>>>> scoutIndicesTasks = new ArrayList<>();
    for (TeamHelper helper : mTeamHelpers) {
        TaskCompletionSource<Pair<TeamHelper, List<String>>> scoutIndicesTask = new TaskCompletionSource<>();
        scoutIndicesTasks.add(scoutIndicesTask.getTask());
        getScoutIndicesRef(helper.getTeam().getKey()).addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot snapshot) {
                AsyncTaskExecutor.Companion.execute(() -> {
                    List<String> scoutKeys = new ArrayList<>();
                    for (DataSnapshot scoutKeyTemplate : snapshot.getChildren()) {
                        scoutKeys.add(scoutKeyTemplate.getKey());
                    }
                    return scoutKeys;
                }).addOnSuccessListener(scoutKeys -> scoutIndicesTask.setResult(Pair.create(helper, scoutKeys)));
            }

            @Override
            public void onCancelled(DatabaseError error) {
                scoutIndicesTask.setException(error.toException());
                FirebaseCrash.report(error.toException());
            }
        });
    }
    for (Task<Pair<TeamHelper, List<String>>> scoutKeysTask : scoutIndicesTasks) {
        scoutKeysTask.addOnSuccessListener(this).addOnFailureListener(this);
    }
    Tasks.whenAll(scoutIndicesTasks).addOnSuccessListener(aVoid -> Tasks.whenAll(mScoutMetricsTasks).addOnSuccessListener(aVoid1 -> mScoutsTask.setResult(mScouts)).addOnFailureListener(this)).addOnFailureListener(this);
    return mScoutsTask.getTask();
}
Also used : Context(android.content.Context) Query(com.google.firebase.database.Query) Metric(com.supercilex.robotscouter.data.model.Metric) DataSnapshot(com.google.firebase.database.DataSnapshot) Pair(android.util.Pair) DatabaseReference(com.google.firebase.database.DatabaseReference) Timer(java.util.Timer) FIREBASE_NAME(com.supercilex.robotscouter.util.ConstantsKt.FIREBASE_NAME) NonNull(android.support.annotation.NonNull) Task(com.google.android.gms.tasks.Task) ChildEventListener(com.google.firebase.database.ChildEventListener) ArrayList(java.util.ArrayList) ValueEventListener(com.google.firebase.database.ValueEventListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Map(java.util.Map) TimerTask(java.util.TimerTask) ConnectivityUtilsKt.isOffline(com.supercilex.robotscouter.util.ConnectivityUtilsKt.isOffline) AsyncTaskExecutor(com.supercilex.robotscouter.util.AsyncTaskExecutor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) METRIC_PARSER(com.supercilex.robotscouter.data.util.ScoutUtilsKt.METRIC_PARSER) FIREBASE_METRICS(com.supercilex.robotscouter.util.ConstantsKt.FIREBASE_METRICS) FirebaseCrash(com.google.firebase.crash.FirebaseCrash) FIREBASE_SCOUTS(com.supercilex.robotscouter.util.ConstantsKt.FIREBASE_SCOUTS) Size(android.support.annotation.Size) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Tasks(com.google.android.gms.tasks.Tasks) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) Scout(com.supercilex.robotscouter.data.model.Scout) ScoutUtilsKt.getScoutIndicesRef(com.supercilex.robotscouter.data.util.ScoutUtilsKt.getScoutIndicesRef) DatabaseError(com.google.firebase.database.DatabaseError) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Task(com.google.android.gms.tasks.Task) TimerTask(java.util.TimerTask) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) DatabaseError(com.google.firebase.database.DatabaseError) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ValueEventListener(com.google.firebase.database.ValueEventListener) Pair(android.util.Pair)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)44 DatabaseError (com.google.firebase.database.DatabaseError)44 ValueEventListener (com.google.firebase.database.ValueEventListener)44 View (android.view.View)19 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)12 RecyclerView (android.support.v7.widget.RecyclerView)12 User (com.polito.mad17.madmax.entities.User)12 Intent (android.content.Intent)11 Bundle (android.os.Bundle)10 DatabaseReference (com.google.firebase.database.DatabaseReference)9 TextView (android.widget.TextView)7 ImageView (android.widget.ImageView)6 Event (com.polito.mad17.madmax.entities.Event)5 SimpleDateFormat (java.text.SimpleDateFormat)5 HashMap (java.util.HashMap)4 TabLayout (android.support.design.widget.TabLayout)3 Query (com.google.firebase.database.Query)3 ArrayList (java.util.ArrayList)3 TargetApi (android.annotation.TargetApi)2 SharedPreferences (android.content.SharedPreferences)2