Browse Source

modify RecentAdapter

firestoreRecent
oogee 6 years ago
parent
commit
2808788821
  1. 7
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 47
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java
  3. 41
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java

7
app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java

@ -31,6 +31,7 @@ import gr.thmmy.mthmmy.activities.settings.SettingsActivity;
import gr.thmmy.mthmmy.activities.topic.TopicActivity; import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Board; import gr.thmmy.mthmmy.model.Board;
import gr.thmmy.mthmmy.model.RecentItem;
import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.model.TopicSummary;
import timber.log.Timber; import timber.log.Timber;
@ -144,10 +145,10 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
} }
@Override @Override
public void onRecentFragmentInteraction(TopicSummary topicSummary) { public void onRecentFragmentInteraction(RecentItem recentItem) {
Intent i = new Intent(MainActivity.this, TopicActivity.class); Intent i = new Intent(MainActivity.this, TopicActivity.class);
i.putExtra(BUNDLE_TOPIC_URL, topicSummary.getTopicUrl()); i.putExtra(BUNDLE_TOPIC_URL, recentItem.getTopicUrl());
i.putExtra(BUNDLE_TOPIC_TITLE, topicSummary.getSubject()); i.putExtra(BUNDLE_TOPIC_TITLE, recentItem.getTopicTitle());
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i); startActivity(i);
} }

47
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java

@ -6,21 +6,13 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.database.collection.ArraySortedMap;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.RecentItem;
import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.model.TopicSummary;
@ -30,26 +22,13 @@ import gr.thmmy.mthmmy.model.TopicSummary;
*/ */
class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> { class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
private final Context context; private final Context context;
private final List<DocumentReference> postSummaries; private final List<RecentItem> recentItems;
private final RecentFragment.RecentFragmentInteractionListener mListener; private final RecentFragment.RecentFragmentInteractionListener mListener;
RecentAdapter(Context context, @NonNull List<DocumentReference> postSummaries, BaseFragment.FragmentInteractionListener listener) { RecentAdapter(Context context, @NonNull List<RecentItem> recentItems, BaseFragment.FragmentInteractionListener listener) {
this.context = context; this.context = context;
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (DocumentReference documentReference : postSummaries) { this.recentItems = recentItems;
Task<DocumentSnapshot> documentSnapshotTask = documentReference.get();
tasks.add(documentSnapshotTask);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> objects) {
ArrayList<Map> posts = new ArrayList<>();
for (Object object : objects) {
posts.add((Map) object);
}
}
})
this.postSummaries = postSummaries;
mListener = (RecentFragment.RecentFragmentInteractionListener) listener; mListener = (RecentFragment.RecentFragmentInteractionListener) listener;
} }
@ -64,13 +43,10 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
@Override @Override
public void onBindViewHolder(final ViewHolder holder, final int position) { public void onBindViewHolder(final ViewHolder holder, final int position) {
ArraySortedMap map = (ArraySortedMap) postSummaries.get("posts"); RecentItem recentItem = recentItems.get(position);
ArraySortedMap post = (ArraySortedMap) map.get(position); holder.mTitleView.setText(recentItem.getTopicTitle());
holder.mTitleView.setText(post.get("topicTitle").toString()); holder.mDateTimeView.setText(recentItem.getTimestamp().toString());
holder.mDateTimeView.setText(post.get("timestamp").toString()); holder.mUserView.setText(recentItem.getPoster());
holder.mUserView.setText(post.get("poster").toString());
holder.topic = recentList.get(position);
holder.mView.setOnClickListener(new View.OnClickListener() { holder.mView.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -79,7 +55,7 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
if (null != mListener) { if (null != mListener) {
// Notify the active callbacks interface (the activity, if the // Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected. // fragment is attached to one) that an item has been selected.
mListener.onRecentFragmentInteraction(holder.topic); //? mListener.onRecentFragmentInteraction(recentItems.get(holder.getAdapterPosition())); //?
} }
@ -89,7 +65,7 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
@Override @Override
public int getItemCount() { public int getItemCount() {
return recentList.size(); return recentItems.size();
} }
class ViewHolder extends RecyclerView.ViewHolder { class ViewHolder extends RecyclerView.ViewHolder {
@ -97,7 +73,6 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
final TextView mTitleView; final TextView mTitleView;
final TextView mUserView; final TextView mUserView;
final TextView mDateTimeView; final TextView mDateTimeView;
public TopicSummary topic;
ViewHolder(View view) { ViewHolder(View view) {
super(view); super(view);

41
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java

@ -9,34 +9,26 @@ import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
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 org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.RecentItem;
import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.Response;
import timber.log.Timber; import timber.log.Timber;
@ -56,13 +48,12 @@ public class RecentFragment extends BaseFragment {
private SwipeRefreshLayout swipeRefreshLayout; private SwipeRefreshLayout swipeRefreshLayout;
private RecentAdapter recentAdapter; private RecentAdapter recentAdapter;
private DocumentSnapshot recentDocument; private ArrayList<RecentItem> recentItems;
private RecentTask recentTask;
// Required empty public constructor // Required empty public constructor
public RecentFragment() {} public RecentFragment() {}
/** /**
* Use ONLY this factory method to create a new instance of * Use ONLY this factory method to create a new instance of
* this fragment using the provided parameters. * this fragment using the provided parameters.
@ -86,18 +77,28 @@ public class RecentFragment extends BaseFragment {
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
if (recentDocument == null) { if (recentItems == null) {
DocumentReference docRef = BaseApplication.getInstance().getFirestoredb() DocumentReference docRef = BaseApplication.getInstance().getFirestoredb()
.collection("recent_posts") .collection("recent_posts")
.document("recent"); .document("recent");
docRef.get().addOnCompleteListener(task -> { docRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) { if (task.isSuccessful()) {
recentDocument = task.getResult(); DocumentSnapshot recentDocument = task.getResult();
List<DocumentReference> posts = (List<DocumentReference>) recentDocument.get("posts");
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (DocumentReference documentReference : posts) {
Task<DocumentSnapshot> documentSnapshotTask = documentReference.get();
tasks.add(documentSnapshotTask);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(objects -> {
ArrayList<RecentItem> recentItems = new ArrayList<>();
for (Object object : objects) {
RecentItem recentItem = ((DocumentSnapshot) object).toObject(RecentItem.class);
recentItems.add(recentItem);
}
});
} }
}); });
recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished);
recentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.indexUrl.toString());
} }
Timber.d("onActivityCreated"); Timber.d("onActivityCreated");
} }
@ -112,7 +113,7 @@ public class RecentFragment extends BaseFragment {
// Set the adapter // Set the adapter
if (rootView instanceof RelativeLayout) { if (rootView instanceof RelativeLayout) {
progressBar = rootView.findViewById(R.id.progressBar); progressBar = rootView.findViewById(R.id.progressBar);
recentAdapter = new RecentAdapter(getActivity(), (List<DocumentReference>) recentDocument.get("posts"), fragmentInteractionListener); recentAdapter = new RecentAdapter(getActivity(), recentItems, fragmentInteractionListener);
CustomRecyclerView recyclerView = rootView.findViewById(R.id.list); CustomRecyclerView recyclerView = rootView.findViewById(R.id.list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext()); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext());
@ -146,7 +147,7 @@ public class RecentFragment extends BaseFragment {
public interface RecentFragmentInteractionListener extends FragmentInteractionListener { public interface RecentFragmentInteractionListener extends FragmentInteractionListener {
void onRecentFragmentInteraction(TopicSummary topicSummary); void onRecentFragmentInteraction(RecentItem topicSummary);
} }
private void onRecentTaskStarted() { private void onRecentTaskStarted() {

4
app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java

@ -7,8 +7,8 @@ public class RecentItem {
private String boardTitle, topicTitle, poster; private String boardTitle, topicTitle, poster;
private Date timestamp; private Date timestamp;
public RecentItem(int boardId, String boardTitle, int postId, String poster, int posterId, int timestamp, int topicId, public RecentItem(int boardId, String boardTitle, int postId, String poster, int posterId, int timestamp,
String topicTitle) { int topicId, String topicTitle) {
this.boardId = boardId; this.boardId = boardId;
this.postId = postId; this.postId = postId;
this.poster = poster; this.poster = poster;

Loading…
Cancel
Save