From 7a0ec4134962ef279012905a7f923bb8a4e1474a Mon Sep 17 00:00:00 2001 From: oogee Date: Tue, 2 Apr 2019 19:24:16 +0300 Subject: [PATCH] listen for recent updates --- .../main/recent/RecentFragment.java | 42 ++++++++++++------- app/src/main/res/layout/fragment_recent.xml | 31 ++++++-------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index ac70841b..78483887 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -11,6 +11,7 @@ import android.widget.Toast; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.ListenerRegistration; import java.util.ArrayList; import java.util.HashMap; @@ -39,11 +40,12 @@ public class RecentFragment extends BaseFragment { // Fragment initialization parameters, e.g. ARG_SECTION_NUMBER private MaterialProgressBar progressBar; - private SwipeRefreshLayout swipeRefreshLayout; private RecentAdapter recentAdapter; private ArrayList recentItems = new ArrayList<>(); + private ListenerRegistration recentSnapshotListener; + // Required empty public constructor public RecentFragment() { } @@ -78,21 +80,23 @@ public class RecentFragment extends BaseFragment { .document("recent"); docRef.get().addOnCompleteListener(task -> { progressBar.setVisibility(ProgressBar.INVISIBLE); - if (task.isSuccessful()) { - DocumentSnapshot recentDocument = task.getResult(); - ArrayList> posts = (ArrayList>) recentDocument.get("posts"); - for (HashMap map : posts) { - RecentItem recentItem = new RecentItem(map); - recentItems.add(recentItem); - } - recentAdapter.notifyDataSetChanged(); - } else { - Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); - } + + if (task.isSuccessful()) onNewDocumentSnapshot(task.getResult()); + else Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); }); + recentSnapshotListener = docRef.addSnapshotListener((documentSnapshot, e) -> { + Timber.i("New doc"); + onNewDocumentSnapshot(documentSnapshot); + } + ); progressBar.setVisibility(ProgressBar.VISIBLE); } + @Override + public void onDestroy() { + super.onDestroy(); + recentSnapshotListener.remove(); + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -112,15 +116,21 @@ public class RecentFragment extends BaseFragment { linearLayoutManager.getOrientation()); recyclerView.addItemDecoration(dividerItemDecoration); recyclerView.setAdapter(recentAdapter); - - swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); - swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); - swipeRefreshLayout.setColorSchemeResources(R.color.accent); } return rootView; } + public void onNewDocumentSnapshot(DocumentSnapshot recentDocument) { + recentItems.clear(); + ArrayList> posts = (ArrayList>) recentDocument.get("posts"); + for (HashMap map : posts) { + RecentItem recentItem = new RecentItem(map); + recentItems.add(recentItem); + } + recentAdapter.notifyDataSetChanged(); + } + public interface RecentFragmentInteractionListener extends FragmentInteractionListener { void onRecentFragmentInteraction(RecentItem topicSummary); diff --git a/app/src/main/res/layout/fragment_recent.xml b/app/src/main/res/layout/fragment_recent.xml index 356f96ae..0ed39317 100644 --- a/app/src/main/res/layout/fragment_recent.xml +++ b/app/src/main/res/layout/fragment_recent.xml @@ -1,28 +1,21 @@ - - - - - + android:layout_height="match_parent" + android:background="@color/background" + android:paddingTop="4dp" + android:paddingBottom="4dp" + app:layoutManager="LinearLayoutManager" + tools:context=".activities.main.recent.RecentFragment" + tools:listitem="@layout/fragment_recent_row" /> + app:mpb_progressStyle="horizontal" /> \ No newline at end of file