Browse Source

listen for recent updates

firestoreRecent
oogee 6 years ago
parent
commit
7a0ec41349
  1. 42
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  2. 15
      app/src/main/res/layout/fragment_recent.xml

42
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.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.ListenerRegistration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -39,11 +40,12 @@ public class RecentFragment extends BaseFragment {
// Fragment initialization parameters, e.g. ARG_SECTION_NUMBER // Fragment initialization parameters, e.g. ARG_SECTION_NUMBER
private MaterialProgressBar progressBar; private MaterialProgressBar progressBar;
private SwipeRefreshLayout swipeRefreshLayout;
private RecentAdapter recentAdapter; private RecentAdapter recentAdapter;
private ArrayList<RecentItem> recentItems = new ArrayList<>(); private ArrayList<RecentItem> recentItems = new ArrayList<>();
private ListenerRegistration recentSnapshotListener;
// Required empty public constructor // Required empty public constructor
public RecentFragment() { public RecentFragment() {
} }
@ -78,21 +80,23 @@ public class RecentFragment extends BaseFragment {
.document("recent"); .document("recent");
docRef.get().addOnCompleteListener(task -> { docRef.get().addOnCompleteListener(task -> {
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
if (task.isSuccessful()) {
DocumentSnapshot recentDocument = task.getResult(); if (task.isSuccessful()) onNewDocumentSnapshot(task.getResult());
ArrayList<HashMap<String, Object>> posts = (ArrayList<HashMap<String, Object>>) recentDocument.get("posts"); else Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show();
for (HashMap<String, Object> map : posts) {
RecentItem recentItem = new RecentItem(map);
recentItems.add(recentItem);
}
recentAdapter.notifyDataSetChanged();
} 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); progressBar.setVisibility(ProgressBar.VISIBLE);
} }
@Override
public void onDestroy() {
super.onDestroy();
recentSnapshotListener.remove();
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -112,15 +116,21 @@ public class RecentFragment extends BaseFragment {
linearLayoutManager.getOrientation()); linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration); recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(recentAdapter); recyclerView.setAdapter(recentAdapter);
swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh);
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary);
swipeRefreshLayout.setColorSchemeResources(R.color.accent);
} }
return rootView; return rootView;
} }
public void onNewDocumentSnapshot(DocumentSnapshot recentDocument) {
recentItems.clear();
ArrayList<HashMap<String, Object>> posts = (ArrayList<HashMap<String, Object>>) recentDocument.get("posts");
for (HashMap<String, Object> map : posts) {
RecentItem recentItem = new RecentItem(map);
recentItems.add(recentItem);
}
recentAdapter.notifyDataSetChanged();
}
public interface RecentFragmentInteractionListener extends FragmentInteractionListener { public interface RecentFragmentInteractionListener extends FragmentInteractionListener {
void onRecentFragmentInteraction(RecentItem topicSummary); void onRecentFragmentInteraction(RecentItem topicSummary);

15
app/src/main/res/layout/fragment_recent.xml

@ -1,28 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<gr.thmmy.mthmmy.utils.CustomRecyclerView <gr.thmmy.mthmmy.utils.CustomRecyclerView
android:id="@+id/list" android:id="@+id/list"
android:name="gr.thmmy.mthmmy.sections.recent.RecentFragment" android:name="gr.thmmy.mthmmy.sections.recent.RecentFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/background" android:background="@color/background"
android:paddingBottom="4dp"
android:paddingTop="4dp" android:paddingTop="4dp"
android:paddingBottom="4dp"
app:layoutManager="LinearLayoutManager" app:layoutManager="LinearLayoutManager"
tools:context=".activities.main.recent.RecentFragment" tools:context=".activities.main.recent.RecentFragment"
tools:listitem="@layout/fragment_recent_row"/> tools:listitem="@layout/fragment_recent_row" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<me.zhanghai.android.materialprogressbar.MaterialProgressBar <me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/progressBar" android:id="@+id/progressBar"
@ -33,5 +26,5 @@
android:indeterminate="true" android:indeterminate="true"
android:visibility="invisible" android:visibility="invisible"
app:mpb_indeterminateTint="@color/accent" app:mpb_indeterminateTint="@color/accent"
app:mpb_progressStyle="horizontal"/> app:mpb_progressStyle="horizontal" />
</RelativeLayout> </RelativeLayout>
Loading…
Cancel
Save