Search in sources :

Example 1 with FirebaseFirestoreException

use of com.google.firebase.firestore.FirebaseFirestoreException in project PhotoBlog-Android-Blog-App by akshayejh.

the class HomeFragment method loadMorePost.

public void loadMorePost() {
    if (firebaseAuth.getCurrentUser() != null) {
        Query nextQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.DESCENDING).startAfter(lastVisible).limit(3);
        nextQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {

            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                if (!documentSnapshots.isEmpty()) {
                    lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            String blogPostId = doc.getDocument().getId();
                            BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                            blog_list.add(blogPost);
                            blogRecyclerAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });
    }
}
Also used : Query(com.google.firebase.firestore.Query) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) DocumentChange(com.google.firebase.firestore.DocumentChange) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot)

Example 2 with FirebaseFirestoreException

use of com.google.firebase.firestore.FirebaseFirestoreException in project quickstart-android by firebase.

the class MainFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mBinding.filterBar.setOnClickListener(this);
    mBinding.buttonClearFilter.setOnClickListener(this);
    // View model
    mViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
    // Enable Firestore logging
    FirebaseFirestore.setLoggingEnabled(true);
    // Firestore
    mFirestore = FirebaseFirestore.getInstance();
    // Get ${LIMIT} restaurants
    mQuery = mFirestore.collection("restaurants").orderBy("avgRating", Query.Direction.DESCENDING).limit(LIMIT);
    // RecyclerView
    mAdapter = new RestaurantAdapter(mQuery, this) {

        @Override
        protected void onDataChanged() {
            // Show/hide content if the query returns empty.
            if (getItemCount() == 0) {
                mBinding.recyclerRestaurants.setVisibility(View.GONE);
                mBinding.viewEmpty.setVisibility(View.VISIBLE);
            } else {
                mBinding.recyclerRestaurants.setVisibility(View.VISIBLE);
                mBinding.viewEmpty.setVisibility(View.GONE);
            }
        }

        @Override
        protected void onError(FirebaseFirestoreException e) {
            // Show a snackbar on errors
            Snackbar.make(mBinding.getRoot(), "Error: check logs for info.", Snackbar.LENGTH_LONG).show();
        }
    };
    mBinding.recyclerRestaurants.setLayoutManager(new LinearLayoutManager(requireContext()));
    mBinding.recyclerRestaurants.setAdapter(mAdapter);
    // Filter Dialog
    mFilterDialog = new FilterDialogFragment();
}
Also used : MainActivityViewModel(com.google.firebase.example.fireeats.java.viewmodel.MainActivityViewModel) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) RestaurantAdapter(com.google.firebase.example.fireeats.java.adapter.RestaurantAdapter) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 3 with FirebaseFirestoreException

use of com.google.firebase.firestore.FirebaseFirestoreException in project PhotoBlog-Android-Blog-App by akshayejh.

the class BlogRecyclerAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.setIsRecyclable(false);
    final String blogPostId = blog_list.get(position).BlogPostId;
    final String currentUserId = firebaseAuth.getCurrentUser().getUid();
    String desc_data = blog_list.get(position).getDesc();
    holder.setDescText(desc_data);
    String image_url = blog_list.get(position).getImage_url();
    String thumbUri = blog_list.get(position).getImage_thumb();
    holder.setBlogImage(image_url, thumbUri);
    String user_id = blog_list.get(position).getUser_id();
    // User Data will be retrieved here...
    firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                String userName = task.getResult().getString("name");
                String userImage = task.getResult().getString("image");
                holder.setUserData(userName, userImage);
            } else {
            // Firebase Exception
            }
        }
    });
    try {
        long millisecond = blog_list.get(position).getTimestamp().getTime();
        String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();
        holder.setTime(dateString);
    } catch (Exception e) {
        Toast.makeText(context, "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    // Get Likes Count
    firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").addSnapshotListener(new EventListener<QuerySnapshot>() {

        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (!documentSnapshots.isEmpty()) {
                int count = documentSnapshots.size();
                holder.updateLikesCount(count);
            } else {
                holder.updateLikesCount(0);
            }
        }
    });
    // Get Likes
    firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).addSnapshotListener(new EventListener<DocumentSnapshot>() {

        @Override
        public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
            if (documentSnapshot.exists()) {
                holder.blogLikeBtn.setImageDrawable(context.getDrawable(R.mipmap.action_like_accent));
            } else {
                holder.blogLikeBtn.setImageDrawable(context.getDrawable(R.mipmap.action_like_gray));
            }
        }
    });
    // Likes Feature
    holder.blogLikeBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (!task.getResult().exists()) {
                        Map<String, Object> likesMap = new HashMap<>();
                        likesMap.put("timestamp", FieldValue.serverTimestamp());
                        firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).set(likesMap);
                    } else {
                        firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).delete();
                    }
                }
            });
        }
    });
    holder.blogCommentBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent commentIntent = new Intent(context, CommentsActivity.class);
            commentIntent.putExtra("blog_post_id", blogPostId);
            context.startActivity(commentIntent);
        }
    });
}
Also used : Task(com.google.android.gms.tasks.Task) HashMap(java.util.HashMap) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Intent(android.content.Intent) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) Date(java.util.Date) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) NonNull(android.support.annotation.NonNull)

Example 4 with FirebaseFirestoreException

use of com.google.firebase.firestore.FirebaseFirestoreException in project PhotoBlog-Android-Blog-App by akshayejh.

the class HomeFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    blog_list = new ArrayList<>();
    blog_list_view = view.findViewById(R.id.blog_list_view);
    firebaseAuth = FirebaseAuth.getInstance();
    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
    blog_list_view.setLayoutManager(new LinearLayoutManager(container.getContext()));
    blog_list_view.setAdapter(blogRecyclerAdapter);
    blog_list_view.setHasFixedSize(true);
    if (firebaseAuth.getCurrentUser() != null) {
        firebaseFirestore = FirebaseFirestore.getInstance();
        blog_list_view.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Boolean reachedBottom = !recyclerView.canScrollVertically(1);
                if (reachedBottom) {
                    loadMorePost();
                }
            }
        });
        Query firstQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.DESCENDING).limit(3);
        firstQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {

            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                if (!documentSnapshots.isEmpty()) {
                    if (isFirstPageFirstLoad) {
                        lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                        blog_list.clear();
                    }
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            String blogPostId = doc.getDocument().getId();
                            BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                            if (isFirstPageFirstLoad) {
                                blog_list.add(blogPost);
                            } else {
                                blog_list.add(0, blogPost);
                            }
                            blogRecyclerAdapter.notifyDataSetChanged();
                        }
                    }
                    isFirstPageFirstLoad = false;
                }
            }
        });
    }
    // Inflate the layout for this fragment
    return view;
}
Also used : Query(com.google.firebase.firestore.Query) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DocumentChange(com.google.firebase.firestore.DocumentChange) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) Point(android.graphics.Point) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)4 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)3 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 DocumentChange (com.google.firebase.firestore.DocumentChange)2 Query (com.google.firebase.firestore.Query)2 Intent (android.content.Intent)1 Point (android.graphics.Point)1 NonNull (android.support.annotation.NonNull)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)1 Task (com.google.android.gms.tasks.Task)1 RestaurantAdapter (com.google.firebase.example.fireeats.java.adapter.RestaurantAdapter)1 MainActivityViewModel (com.google.firebase.example.fireeats.java.viewmodel.MainActivityViewModel)1 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)1 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)1