com.google.firebase.firestore.DocumentChange Java Examples

The following examples show how to use com.google.firebase.firestore.DocumentChange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FirestoreArray.java    From FirebaseUI-Android with Apache License 2.0 6 votes vote down vote up
private void onDocumentModified(DocumentChange change) {
    QueryDocumentSnapshot snapshot = change.getDocument();
    if (change.getOldIndex() == change.getNewIndex()) {
        // Document modified only
        mSnapshots.set(change.getNewIndex(), snapshot);
        notifyOnChildChanged(ChangeEventType.CHANGED, snapshot,
                change.getNewIndex(), change.getNewIndex());
    } else {
        // Document moved and possibly also modified
        mSnapshots.remove(change.getOldIndex());
        mSnapshots.add(change.getNewIndex(), snapshot);

        notifyOnChildChanged(ChangeEventType.MOVED, snapshot,
                change.getNewIndex(), change.getOldIndex());
        notifyOnChildChanged(ChangeEventType.CHANGED, snapshot,
                change.getNewIndex(), change.getNewIndex());
    }
}
 
Example #2
Source File: QuerySnapshotConverter.java    From ground-android with Apache License 2.0 6 votes vote down vote up
private static <T> RemoteDataEvent<T> toEvent(
    DocumentChange dc, Function<DocumentSnapshot, T> converter) {
  try {
    Log.v(TAG, dc.getDocument().getReference().getPath() + " " + dc.getType());
    String id = dc.getDocument().getId();
    switch (dc.getType()) {
      case ADDED:
        return RemoteDataEvent.loaded(id, converter.apply(dc.getDocument()));
      case MODIFIED:
        return RemoteDataEvent.modified(id, converter.apply(dc.getDocument()));
      case REMOVED:
        return RemoteDataEvent.removed(id);
      default:
        return RemoteDataEvent.error(
            new DataStoreException("Unknown DocumentChange type: " + dc.getType()));
    }
  } catch (DataStoreException e) {
    return RemoteDataEvent.error(e);
  }
}
 
Example #3
Source File: DocSnippets.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void handleListenErrors() {
    // [START handle_listen_errors]
    db.collection("cities")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot snapshots,
                                    @Nullable FirebaseFirestoreException e) {
                    if (e != null) {
                        Log.w(TAG, "listen:error", e);
                        return;
                    }

                    for (DocumentChange dc : snapshots.getDocumentChanges()) {
                        if (dc.getType() == Type.ADDED) {
                            Log.d(TAG, "New city: " + dc.getDocument().getData());
                        }
                    }

                }
            });
    // [END handle_listen_errors]
}
 
Example #4
Source File: DocSnippets.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void offlineListen(FirebaseFirestore db) {
    // [START offline_listen]
    db.collection("cities").whereEqualTo("state", "CA")
            .addSnapshotListener(MetadataChanges.INCLUDE, new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot querySnapshot,
                                    @Nullable FirebaseFirestoreException e) {
                    if (e != null) {
                        Log.w(TAG, "Listen error", e);
                        return;
                    }

                    for (DocumentChange change : querySnapshot.getDocumentChanges()) {
                        if (change.getType() == Type.ADDED) {
                            Log.d(TAG, "New city:" + change.getDocument().getData());
                        }

                        String source = querySnapshot.getMetadata().isFromCache() ?
                                "local cache" : "server";
                        Log.d(TAG, "Data fetched from " + source);
                    }

                }
            });
    // [END offline_listen]
}
 
Example #5
Source File: FirestoreAdapter.java    From quickstart-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
    if (e != null) {
        Log.w(TAG, "onEvent:error", e);
        onError(e);
        return;
    }

    // Dispatch the event
    Log.d(TAG, "onEvent:numChanges:" + documentSnapshots.getDocumentChanges().size());
    for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
        switch (change.getType()) {
            case ADDED:
                onDocumentAdded(change);
                break;
            case MODIFIED:
                onDocumentModified(change);
                break;
            case REMOVED:
                onDocumentRemoved(change);
                break;
        }
    }

    onDataChanged();
}
 
Example #6
Source File: FirestoreArray.java    From FirebaseUI-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) {
    if (e != null) {
        notifyOnError(e);
        return;
    }

    // Break down each document event
    List<DocumentChange> changes = snapshots.getDocumentChanges(mMetadataChanges);
    for (DocumentChange change : changes) {
        switch (change.getType()) {
            case ADDED:
                onDocumentAdded(change);
                break;
            case REMOVED:
                onDocumentRemoved(change);
                break;
            case MODIFIED:
                onDocumentModified(change);
                break;
        }
    }

    notifyOnDataChanged();
}
 
Example #7
Source File: FirestoreAdapter.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
protected void onDocumentModified(DocumentChange change) {
    if (change.getOldIndex() == change.getNewIndex()) {
        // Item changed but remained in same position
        mSnapshots.set(change.getOldIndex(), change.getDocument());
        notifyItemChanged(change.getOldIndex());
    } else {
        // Item changed and changed position
        mSnapshots.remove(change.getOldIndex());
        mSnapshots.add(change.getNewIndex(), change.getDocument());
        notifyItemMoved(change.getOldIndex(), change.getNewIndex());
    }
}
 
Example #8
Source File: DocSnippets.java    From snippets-android with Apache License 2.0 5 votes vote down vote up
public void listenState() {
    // [START listen_state]
    db.collection("cities")
            .whereEqualTo("state", "CA")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot snapshots,
                                    @Nullable FirebaseFirestoreException e) {

                    if (e != null) {
                        Log.w(TAG, "listen:error", e);
                        return;
                    }

                    for (DocumentChange dc : snapshots.getDocumentChanges()) {
                        if (dc.getType() == Type.ADDED) {
                            Log.d(TAG, "New city: " + dc.getDocument().getData());
                        }
                    }

                    if (!snapshots.getMetadata().isFromCache()) {
                        Log.d(TAG, "Got initial state.");
                    }
                }
            });
    // [END listen_state]
}
 
Example #9
Source File: DocSnippets.java    From snippets-android with Apache License 2.0 5 votes vote down vote up
public void listenToDiffs() {
    // [START listen_diffs]
    db.collection("cities")
            .whereEqualTo("state", "CA")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot snapshots,
                                    @Nullable FirebaseFirestoreException e) {
                    if (e != null) {
                        Log.w(TAG, "listen:error", e);
                        return;
                    }

                    for (DocumentChange dc : snapshots.getDocumentChanges()) {
                        switch (dc.getType()) {
                            case ADDED:
                                Log.d(TAG, "New city: " + dc.getDocument().getData());
                                break;
                            case MODIFIED:
                                Log.d(TAG, "Modified city: " + dc.getDocument().getData());
                                break;
                            case REMOVED:
                                Log.d(TAG, "Removed city: " + dc.getDocument().getData());
                                break;
                        }
                    }

                }
            });
    // [END listen_diffs]
}
 
Example #10
Source File: FirestoreAdapter.java    From quickstart-android with Apache License 2.0 4 votes vote down vote up
protected void onDocumentRemoved(DocumentChange change) {
    mSnapshots.remove(change.getOldIndex());
    notifyItemRemoved(change.getOldIndex());
}
 
Example #11
Source File: FirestoreAdapter.java    From quickstart-android with Apache License 2.0 4 votes vote down vote up
protected void onDocumentAdded(DocumentChange change) {
    mSnapshots.add(change.getNewIndex(), change.getDocument());
    notifyItemInserted(change.getNewIndex());
}
 
Example #12
Source File: FirestoreArray.java    From FirebaseUI-Android with Apache License 2.0 4 votes vote down vote up
private void onDocumentAdded(DocumentChange change) {
    QueryDocumentSnapshot snapshot = change.getDocument();
    mSnapshots.add(change.getNewIndex(), snapshot);
    notifyOnChildChanged(ChangeEventType.ADDED, snapshot, change.getNewIndex(), -1);
}
 
Example #13
Source File: FirestoreArray.java    From FirebaseUI-Android with Apache License 2.0 4 votes vote down vote up
private void onDocumentRemoved(DocumentChange change) {
    mSnapshots.remove(change.getOldIndex());
    QueryDocumentSnapshot snapshot = change.getDocument();
    notifyOnChildChanged(ChangeEventType.REMOVED, snapshot, -1, change.getOldIndex());
}
 
Example #14
Source File: ConversationManager.java    From NaviBee with GNU General Public License v3.0 4 votes vote down vote up
private void listenGroupConv() {
    // private conversation (friend list)
    mListener2 = db.collection("conversations")
            .whereEqualTo("users."+uid, true)
            .whereEqualTo("isDeleted", false)
            .whereEqualTo("type", "group")
            .addSnapshotListener((snapshots, e) -> {
                if (e != null) {
                    Log.w(TAG, "listen:error", e);
                    return;
                }

                for (DocumentChange dc : snapshots.getDocumentChanges()) {
                    String convId = dc.getDocument().getId();
                    switch (dc.getType()) {
                        case ADDED:
                            // read message timestamp
                            Timestamp timestamp =  ((Map<String, Timestamp>) dc.getDocument().get("readTimestamps")).get(uid);

                            Timestamp createTimestamp = (Timestamp) dc.getDocument().get("createTimestamp");

                            // load new conversation
                            Conversation conv = new GroupConversation(convId, timestamp.toDate(), createTimestamp.toDate(),
                                    (String) dc.getDocument().get("name"), (String) dc.getDocument().get("icon"),
                                    (Map<String, Boolean>) dc.getDocument().get("users"), (String) dc.getDocument().get("creator"));
                            convIdMap.put(convId, conv);
                            break;

                        case MODIFIED:
                            break;

                        case REMOVED:
                            convIdMap.remove(dc.getDocument().getId()).stopListening();
                            break;
                    }
                }

                if (!waitingConvId.equals("")) {
                    if (openChatActivity(waitingConvId)) {
                        waitingConvId = "";
                    }
                }
                Intent intent = new Intent(BROADCAST_CONVERSATION_UPDATED);
                NaviBeeApplication.getInstance().sendBroadcast(intent);
            });
}
 
Example #15
Source File: ConversationManager.java    From NaviBee with GNU General Public License v3.0 4 votes vote down vote up
private void listenPrivateConv() {
    // private conversation (friend list)
    mListener1 = db.collection("conversations")
            .whereEqualTo("isDeleted", false)
            .whereEqualTo("users."+uid, true)
            .whereEqualTo("type", "private")
            .addSnapshotListener((snapshots, e) -> {
                if (e != null) {
                    Log.w(TAG, "listen:error", e);
                    return;
                }

                for (DocumentChange dc : snapshots.getDocumentChanges()) {
                    String friendUid = getOtherUserId(dc.getDocument().get("users"), uid);
                    String convId = dc.getDocument().getId();
                    switch (dc.getType()) {
                        case ADDED:
                            // read message timestamp
                            Timestamp timestamp =  ((Map<String, Timestamp>) dc.getDocument().get("readTimestamps")).get(uid);

                            Timestamp createTimestamp = (Timestamp) dc.getDocument().get("createTimestamp");

                            // load new conversation
                            Conversation conv = new PrivateConversation(convId, timestamp.toDate(), createTimestamp.toDate(), friendUid);

                            uidToConvId.put(friendUid, convId);
                            convIdMap.put(convId, conv);
                            friendList.add(friendUid);
                            break;

                        case MODIFIED:
                            break;

                        case REMOVED:
                            uidToConvId.remove(friendUid);
                            convIdMap.remove(dc.getDocument().getId()).stopListening();
                            friendList.remove(friendUid);
                            break;
                    }
                }

                if (!initialized) {
                    // initializing, warm user info cache
                    initialized = true;
                    UserInfoManager.getInstance().warmCache(friendList);
                }

                Intent intent = new Intent(BROADCAST_CONVERSATION_UPDATED);
                NaviBeeApplication.getInstance().sendBroadcast(intent);

                if (!waitingConvId.equals("")) {
                    if (openChatActivity(waitingConvId)) {
                        waitingConvId = "";
                    }
                }

            });
}
 
Example #16
Source File: AnswersActivity.java    From Hify with MIT License 4 votes vote down vote up
private void getAnswers() {

        answers.clear();
        refreshLayout.setRefreshing(true);
        findViewById(R.id.default_item).setVisibility(View.GONE);

        mFirestore.collection("Answers")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .whereEqualTo("question_id",doc_id)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                        if(e!=null){
                            refreshLayout.setRefreshing(false);
                            Log.e(TAG,e.getLocalizedMessage());
                            return;
                        }

                        if(queryDocumentSnapshots.isEmpty()){
                            refreshLayout.setRefreshing(false);
                            findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }else{
                            for(DocumentChange doc:queryDocumentSnapshots.getDocumentChanges()) {

                                if (doc.getType() == DocumentChange.Type.ADDED) {

                                    refreshLayout.setRefreshing(false);
                                    Answers answer = doc.getDocument().toObject(Answers.class).withId(doc.getDocument().getId());
                                    if (doc.getDocument().getString("is_answer").toLowerCase().equals("yes")) {
                                        answers.add(0, answer);
                                    } else {
                                        answers.add(answer);
                                    }
                                    adapter.notifyDataSetChanged();


                                }

                            }
                        }

                    }
                });

    }
 
Example #17
Source File: Notifications.java    From Hify with MIT License 4 votes vote down vote up
public void getNotifications() {

        findViewById(R.id.default_item).setVisibility(View.GONE);
        notificationsList.clear();
        refreshLayout.setRefreshing(true);
        FirebaseFirestore.getInstance().collection("Users")
                .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .collection("Info_Notifications")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener(queryDocumentSnapshots -> {

                    getSharedPreferences("Notifications",MODE_PRIVATE).edit().putInt("count",queryDocumentSnapshots.size()).apply();

                    if(!queryDocumentSnapshots.isEmpty()){

                        for(DocumentChange documentChange:queryDocumentSnapshots.getDocumentChanges()){

                            if(documentChange.getType()== DocumentChange.Type.ADDED){

                                refreshLayout.setRefreshing(false);
                                Notification notification=documentChange.getDocument().toObject(Notification.class).withId(documentChange.getDocument().getId());
                                notificationsList.add(notification);
                                notificationsAdapter.notifyDataSetChanged();

                            }

                        }

                        if(notificationsList.size()==0){
                            refreshLayout.setRefreshing(false);
                            findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }

                    }else{
                        refreshLayout.setRefreshing(false);
                        findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                    }

                })
                .addOnFailureListener(e -> {
                    refreshLayout.setRefreshing(false);
                    e.printStackTrace();
                });

    }
 
Example #18
Source File: CommentsActivity.java    From Hify with MIT License 4 votes vote down vote up
private void getComments(final ProgressBar mProgress) {
    mProgress.setVisibility(View.VISIBLE);
    mFirestore.collection("Posts")
            .document(post_id)
            .collection("Comments")
            .orderBy("timestamp", Query.Direction.ASCENDING)
            .addSnapshotListener(this, (querySnapshot, e) -> {

                if(e!=null){
                    mProgress.setVisibility(View.GONE);
                    e.printStackTrace();
                    return;
                }

                if(!querySnapshot.isEmpty()) {
                    for (DocumentChange doc : querySnapshot.getDocumentChanges()) {

                        if (doc.getDocument().exists()) {
                            if (doc.getType() == DocumentChange.Type.ADDED) {

                                mProgress.setVisibility(View.GONE);
                                Comment comment = doc.getDocument().toObject(Comment.class).withId(doc.getDocument().getId());
                                commentList.add(comment);
                                mAdapter.notifyDataSetChanged();

                            }

                        }
                    }

                    if (commentList.isEmpty()) {
                        mProgress.setVisibility(View.GONE);
                    }

                }else{
                    mProgress.setVisibility(View.GONE);
                }


            });
}
 
Example #19
Source File: FriendQuestions.java    From Hify with MIT License 4 votes vote down vote up
public void filterResult(String subject){
    if(subject.equals("All")){
        getQuestions();
    }else{
        getView().findViewById(R.id.default_item).setVisibility(View.GONE);
        refreshLayout.setRefreshing(true);

        mFirestore.collection("Questions")
                .whereEqualTo("subject",subject)
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot documentSnapshots) {

                        try {
                            if (!documentSnapshots.isEmpty()) {

                                for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                                    if (doc.getType() == DocumentChange.Type.ADDED) {

                                        if (doc.getDocument().getString("id").equals(userId)) {
                                            AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
                                            allQuestionsModelList.add(question);
                                            adapter.notifyDataSetChanged();
                                            refreshLayout.setRefreshing(false);
                                        }

                                    }


                                }

                                if(allQuestionsModelList.isEmpty()){
                                    getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                    refreshLayout.setRefreshing(false);
                                }

                            } else {
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                refreshLayout.setRefreshing(false);
                            }
                        }catch (NullPointerException eee){
                            if(allQuestionsModelList.isEmpty()){
                                refreshLayout.setRefreshing(false);
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            }
                            adapter.notifyDataSetChanged();
                        } catch (Exception ee){
                            Toast.makeText(context, "Some technical error occurred", Toast.LENGTH_SHORT).show();
                            if(allQuestionsModelList.isEmpty()){
                                refreshLayout.setRefreshing(false);
                            }
                            ee.printStackTrace();
                        }

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                        refreshLayout.setRefreshing(false);
                        Toast.makeText(context, "Some technical error occurred", Toast.LENGTH_SHORT).show();
                        Log.w("Error","listen:error",e);

                    }
                });


    }

}
 
Example #20
Source File: MessageHistory.java    From Hify with MIT License 4 votes vote down vote up
public void getTextMessage(){

        refreshLayout.setRefreshing(true);
        getView().findViewById(R.id.default_item).setVisibility(View.GONE);
        mFirestore.collection("Users")
                .document(mAuth.getCurrentUser().getUid())
                .collection("Notifications")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {

                        if(e!=null){
                            refreshLayout.setRefreshing(false);
                            Toasty.error(getView().getContext(), "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                            Log.w("error","listen",e);
                            return;
                        }

                        if(!queryDocumentSnapshots.isEmpty()){

                            for(DocumentChange doc:queryDocumentSnapshots.getDocumentChanges()) {

                                if (doc.getType() == DocumentChange.Type.ADDED) {

                                    Message message = doc.getDocument().toObject(Message.class).withId(doc.getDocument().getId());
                                    messages.add(message);
                                    messageTextAdapter.notifyDataSetChanged();
                                    refreshLayout.setRefreshing(false);

                                }

                            }

                            if(messages.isEmpty()){
                                refreshLayout.setRefreshing(false);
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            }

                        }else {
                            refreshLayout.setRefreshing(false);
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }

                    }
                });

    }
 
Example #21
Source File: MessageHistory.java    From Hify with MIT License 4 votes vote down vote up
public void getTextReplyMessage(){

        refreshLayout.setRefreshing(true);
        getView().findViewById(R.id.default_item).setVisibility(View.GONE);
        mFirestore.collection("Users")
                .document(mAuth.getCurrentUser().getUid())
                .collection("Notifications_reply")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                        if(!queryDocumentSnapshots.isEmpty()){

                            for(DocumentChange doc:queryDocumentSnapshots.getDocumentChanges()){

                                if(doc.getType()== DocumentChange.Type.ADDED){

                                    MessageReply messageReply=doc.getDocument().toObject(MessageReply.class).withId(doc.getDocument().getId());
                                    messageReplies.add(messageReply);
                                    messageTextReplyAdapter.notifyDataSetChanged();
                                    refreshLayout.setRefreshing(false);
                                }


                            }

                            if(messageReplies.isEmpty()){
                                refreshLayout.setRefreshing(false);
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            }

                        }else {
                            refreshLayout.setRefreshing(false);
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }


                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        refreshLayout.setRefreshing(false);
                        Toasty.error(getView().getContext(), "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                        Log.w("error","listen",e);
                    }
                });

    }
 
Example #22
Source File: MessageHistory.java    From Hify with MIT License 4 votes vote down vote up
public void getImageMessage(){

        refreshLayout.setRefreshing(true);
        getView().findViewById(R.id.default_item).setVisibility(View.GONE);
        mFirestore.collection("Users")
                .document(mAuth.getCurrentUser().getUid())
                .collection("Notifications_image")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                        if(!queryDocumentSnapshots.isEmpty()){

                            for(DocumentChange doc:queryDocumentSnapshots.getDocumentChanges()){

                                if(doc.getType()== DocumentChange.Type.ADDED) {

                                    Message message = doc.getDocument().toObject(Message.class).withId(doc.getDocument().getId());
                                    messages.add(message);
                                    messageImageAdapter.notifyDataSetChanged();
                                    refreshLayout.setRefreshing(false);
                                }

                            }
                            if(messages.isEmpty()){
                                refreshLayout.setRefreshing(false);
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            }

                        }else {
                            refreshLayout.setRefreshing(false);
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        refreshLayout.setRefreshing(false);
                        Toasty.error(getView().getContext(), "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                        Log.w("error","listen",e);
                    }
                });

    }
 
Example #23
Source File: MessageHistory.java    From Hify with MIT License 4 votes vote down vote up
public void getImageReplyMessage(){

        refreshLayout.setRefreshing(true);
        getView().findViewById(R.id.default_item).setVisibility(View.GONE);
        mFirestore.collection("Users")
                .document(mAuth.getCurrentUser().getUid())
                .collection("Notifications_reply_image")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                        if(!queryDocumentSnapshots.isEmpty()){

                            for(DocumentChange doc:queryDocumentSnapshots.getDocumentChanges()){

                                if(doc.getType()== DocumentChange.Type.ADDED){

                                    MessageReply messageReply=doc.getDocument().toObject(MessageReply.class).withId(doc.getDocument().getId());
                                    messageReplies.add(messageReply);
                                    messageImageReplyAdapter.notifyDataSetChanged();
                                    refreshLayout.setRefreshing(false);
                                }


                            }
                            if(messageReplies.isEmpty()){
                                refreshLayout.setRefreshing(false);
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            }

                        }else {
                            refreshLayout.setRefreshing(false);
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                        refreshLayout.setRefreshing(false);
                        Toasty.error(getView().getContext(), "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                        Log.w("error","listen",e);

                    }
                });

    }
 
Example #24
Source File: AllQuestions.java    From Hify with MIT License 4 votes vote down vote up
private void getQuestions() {

        getView().findViewById(R.id.default_item).setVisibility(View.GONE);
        refreshLayout.setRefreshing(true);
        Query firstQuery = mFirestore.collection("Questions")
                .orderBy("timestamp", Query.Direction.DESCENDING);
        firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                if (!documentSnapshots.isEmpty()) {

                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {

                            if (!doc.getDocument().getString("id").equals(mCurrentUser.getUid())) {
                                AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
                                allQuestionsModelList.add(question);
                                adapter.notifyDataSetChanged();
                                refreshLayout.setRefreshing(false);
                            }

                        }

                    }

                    if(allQuestionsModelList.isEmpty()){
                        getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        refreshLayout.setRefreshing(false);
                    }

                } else {
                    getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                    refreshLayout.setRefreshing(false);
                }

            }

        });


    }
 
Example #25
Source File: AllQuestions.java    From Hify with MIT License 4 votes vote down vote up
public void filterResult(String subject){

        if(subject.equals("All")){
            getQuestions();
        }else{

            refreshLayout.setRefreshing(true);
            getView().findViewById(R.id.default_item).setVisibility(View.GONE);

            Query firstQuery = mFirestore.collection("Questions")
                    .whereEqualTo("subject",subject)
                    .orderBy("timestamp", Query.Direction.DESCENDING);
            firstQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                    try {

                        if (!documentSnapshots.isEmpty()) {

                            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                                if (doc.getType() == DocumentChange.Type.ADDED) {

                                    if (!doc.getDocument().getString("id").equals(mCurrentUser.getUid())) {
                                        AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
                                        allQuestionsModelList.add(question);
                                        adapter.notifyDataSetChanged();
                                        refreshLayout.setRefreshing(false);
                                    }

                                }



                            }
                            if(allQuestionsModelList.isEmpty()){
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                refreshLayout.setRefreshing(false);
                            }

                        } else {
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            refreshLayout.setRefreshing(false);
                        }
                    }catch (NullPointerException eee){
                        Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT,true).show();
                        adapter.notifyDataSetChanged();
                        if(allQuestionsModelList.isEmpty()){
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            refreshLayout.setRefreshing(false);
                        }
                    } catch (Exception ee){
                        ee.printStackTrace();
                        Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                        if(allQuestionsModelList.isEmpty()){
                            refreshLayout.setRefreshing(false);
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }
                    }

                }

            });


        }

    }
 
Example #26
Source File: Home.java    From Hify with MIT License 4 votes vote down vote up
private void getCurrentUsersPosts() {

        mFirestore.collection("Posts")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                        if(queryDocumentSnapshots.isEmpty()){

                            refreshLayout.setRefreshing(false);
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);

                        }else{

                            for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
                                if (documentChange.getDocument().getString("userId").equals(mAuth.getCurrentUser().getUid())) {

                                    Post post = documentChange.getDocument().toObject(Post.class).withId(documentChange.getDocument().getId());
                                    mPostsList.add(post);
                                    refreshLayout.setRefreshing(false);
                                    mAdapter_v19.notifyDataSetChanged();

                                }
                            }

                            if (mPostsList.isEmpty()) {
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                refreshLayout.setRefreshing(false);
                            }

                        }

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        refreshLayout.setRefreshing(false);
                        Toasty.error(getView().getContext(), "Some technical error occurred", Toasty.LENGTH_SHORT,true).show();
                        Log.w("Error", "listen:error", e);
                    }
                });


    }
 
Example #27
Source File: FriendQuestions.java    From Hify with MIT License 4 votes vote down vote up
private void getQuestions() {
    refreshLayout.setRefreshing(true);
    getView().findViewById(R.id.default_item).setVisibility(View.GONE);
    mFirestore.collection("Questions")
            .orderBy("timestamp", Query.Direction.DESCENDING)
           .get()
           .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
               @Override
               public void onSuccess(QuerySnapshot documentSnapshots) {

                   if (!documentSnapshots.isEmpty()) {

                       for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                           if (doc.getType() == DocumentChange.Type.ADDED) {

                               if (doc.getDocument().getString("id").equals(userId)) {
                                   AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
                                   allQuestionsModelList.add(question);
                                   adapter.notifyDataSetChanged();
                                   refreshLayout.setRefreshing(false);
                               }

                           }

                       }

                       if(allQuestionsModelList.isEmpty()){
                           refreshLayout.setRefreshing(false);
                           getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                       }



                   } else {
                       refreshLayout.setRefreshing(false);
                       getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                   }

               }
           })
           .addOnFailureListener(new OnFailureListener() {
               @Override
               public void onFailure(@NonNull Exception e) {

                   refreshLayout.setRefreshing(false);
                   Toast.makeText(context, "Some technical error occurred", Toast.LENGTH_SHORT).show();
                   Log.w("Error","listen:error",e);

               }
           });

}
 
Example #28
Source File: FriendRequests.java    From Hify with MIT License 4 votes vote down vote up
public void getUsers() {
    requestList.clear();
    requestAdapter.notifyDataSetChanged();

    getView().findViewById(R.id.default_item).setVisibility(View.GONE);
    refreshLayout.setRefreshing(true);

    mFirestore.collection("Users")
            .document(mAuth.getCurrentUser().getUid())
            .collection("Friend_Requests")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                    if(!queryDocumentSnapshots.isEmpty()) {

                        for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

                            if (doc.getType() == DocumentChange.Type.ADDED) {
                                FriendRequest friendRequest = doc.getDocument().toObject(FriendRequest.class).withId(doc.getDocument().getId());
                                requestList.add(friendRequest);
                                requestAdapter.notifyDataSetChanged();
                                refreshLayout.setRefreshing(false);
                            }

                        }

                        if(requestList.isEmpty()) {
                            refreshLayout.setRefreshing(false);
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }

                    }else{
                        refreshLayout.setRefreshing(false);
                        getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                    }

                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                    refreshLayout.setRefreshing(false);
                    Toast.makeText(getView().getContext(), "Some technical error occurred", Toast.LENGTH_SHORT).show();
                    Log.w("Error", "listen:error", e);

                }
            });
}
 
Example #29
Source File: MyQuestions.java    From Hify with MIT License 4 votes vote down vote up
private void getQuestions() {

        refreshLayout.setRefreshing(true);
        getView().findViewById(R.id.default_item).setVisibility(View.GONE);
        mFirestore.collection("Questions")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot documentSnapshots) {

                        if (!documentSnapshots.isEmpty()) {

                            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                                if (doc.getType() == DocumentChange.Type.ADDED) {

                                    if (doc.getDocument().getString("id").equals(userId)) {
                                        AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
                                        allQuestionsModelList.add(question);
                                        adapter.notifyDataSetChanged();
                                        refreshLayout.setRefreshing(false);
                                    }

                                }
                            }


                            if(allQuestionsModelList.isEmpty()){
                                getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                refreshLayout.setRefreshing(false);
                            }



                        } else {
                            getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                            refreshLayout.setRefreshing(false);
                        }

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                        refreshLayout.setRefreshing(false);
                        Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                        Log.w("Error","listen:error",e);

                    }
                });


    }
 
Example #30
Source File: MyQuestions.java    From Hify with MIT License 4 votes vote down vote up
public void filterResult(String subject){

        if(subject.equals("All")){
            getQuestions();
        }else{

            getView().findViewById(R.id.default_item).setVisibility(View.GONE);
            refreshLayout.setRefreshing(true);
            mFirestore.collection("Questions")
                    .whereEqualTo("subject",subject)
                    .orderBy("timestamp", Query.Direction.DESCENDING)
                    .get()
                    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot documentSnapshots) {

                            try {
                                if (!documentSnapshots.isEmpty()) {

                                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                                        if (doc.getType() == DocumentChange.Type.ADDED) {

                                            if (doc.getDocument().getString("id").equals(userId)) {
                                                AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
                                                allQuestionsModelList.add(question);
                                                adapter.notifyDataSetChanged();
                                                refreshLayout.setRefreshing(false);
                                            }

                                        }


                                    }

                                    if(allQuestionsModelList.isEmpty()){
                                        getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                        refreshLayout.setRefreshing(false);
                                    }

                                } else {
                                    getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                    refreshLayout.setRefreshing(false);
                                }
                            }catch (NullPointerException eee){
                                Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                                adapter.notifyDataSetChanged();
                                if(allQuestionsModelList.isEmpty()){
                                    getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                    refreshLayout.setRefreshing(false);
                                }
                            } catch (Exception ee){
                                ee.printStackTrace();
                                Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                                if(allQuestionsModelList.isEmpty()){
                                    refreshLayout.setRefreshing(false);
                                    getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                                }
                            }

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                            refreshLayout.setRefreshing(false);
                            Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
                            Log.w("Error","listen:error",e);

                        }
                    });


        }

    }