From 280878882145a5c464921544388113a744b75eaa Mon Sep 17 00:00:00 2001 From: oogee Date: Mon, 4 Mar 2019 17:00:56 +0200 Subject: [PATCH] modify RecentAdapter --- .../mthmmy/activities/main/MainActivity.java | 7 +-- .../activities/main/recent/RecentAdapter.java | 47 +++++-------------- .../main/recent/RecentFragment.java | 41 ++++++++-------- .../gr/thmmy/mthmmy/model/RecentItem.java | 4 +- 4 files changed, 38 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index af1a4cf2..e527ca90 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/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.base.BaseActivity; import gr.thmmy.mthmmy.model.Board; +import gr.thmmy.mthmmy.model.RecentItem; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.TopicSummary; import timber.log.Timber; @@ -144,10 +145,10 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF } @Override - public void onRecentFragmentInteraction(TopicSummary topicSummary) { + public void onRecentFragmentInteraction(RecentItem recentItem) { Intent i = new Intent(MainActivity.this, TopicActivity.class); - i.putExtra(BUNDLE_TOPIC_URL, topicSummary.getTopicUrl()); - i.putExtra(BUNDLE_TOPIC_TITLE, topicSummary.getSubject()); + i.putExtra(BUNDLE_TOPIC_URL, recentItem.getTopicUrl()); + i.putExtra(BUNDLE_TOPIC_TITLE, recentItem.getTopicTitle()); i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java index 725298ab..abd9ec41 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java +++ b/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.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.Map; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseFragment; +import gr.thmmy.mthmmy.model.RecentItem; import gr.thmmy.mthmmy.model.TopicSummary; @@ -30,26 +22,13 @@ import gr.thmmy.mthmmy.model.TopicSummary; */ class RecentAdapter extends RecyclerView.Adapter { private final Context context; - private final List postSummaries; + private final List recentItems; private final RecentFragment.RecentFragmentInteractionListener mListener; - RecentAdapter(Context context, @NonNull List postSummaries, BaseFragment.FragmentInteractionListener listener) { + RecentAdapter(Context context, @NonNull List recentItems, BaseFragment.FragmentInteractionListener listener) { this.context = context; - List> tasks = new ArrayList<>(); - for (DocumentReference documentReference : postSummaries) { - Task documentSnapshotTask = documentReference.get(); - tasks.add(documentSnapshotTask); - } - Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener>() { - @Override - public void onSuccess(List objects) { - ArrayList posts = new ArrayList<>(); - for (Object object : objects) { - posts.add((Map) object); - } - } - }) - this.postSummaries = postSummaries; + + this.recentItems = recentItems; mListener = (RecentFragment.RecentFragmentInteractionListener) listener; } @@ -64,13 +43,10 @@ class RecentAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(final ViewHolder holder, final int position) { - ArraySortedMap map = (ArraySortedMap) postSummaries.get("posts"); - ArraySortedMap post = (ArraySortedMap) map.get(position); - holder.mTitleView.setText(post.get("topicTitle").toString()); - holder.mDateTimeView.setText(post.get("timestamp").toString()); - holder.mUserView.setText(post.get("poster").toString()); - - holder.topic = recentList.get(position); + RecentItem recentItem = recentItems.get(position); + holder.mTitleView.setText(recentItem.getTopicTitle()); + holder.mDateTimeView.setText(recentItem.getTimestamp().toString()); + holder.mUserView.setText(recentItem.getPoster()); holder.mView.setOnClickListener(new View.OnClickListener() { @Override @@ -79,7 +55,7 @@ class RecentAdapter extends RecyclerView.Adapter { if (null != mListener) { // Notify the active callbacks interface (the activity, if the // 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 { @Override public int getItemCount() { - return recentList.size(); + return recentItems.size(); } class ViewHolder extends RecyclerView.ViewHolder { @@ -97,7 +73,6 @@ class RecentAdapter extends RecyclerView.Adapter { final TextView mTitleView; final TextView mUserView; final TextView mDateTimeView; - public TopicSummary topic; ViewHolder(View view) { super(view); 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 924ad497..a459ea4d 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 @@ -9,34 +9,26 @@ import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.Toast; -import com.google.android.gms.tasks.OnCompleteListener; 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.DocumentSnapshot; -import org.jsoup.nodes.Document; -import org.jsoup.select.Elements; - import java.util.ArrayList; 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.LinearLayoutManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseFragment; +import gr.thmmy.mthmmy.model.RecentItem; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; 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 okhttp3.Response; import timber.log.Timber; @@ -56,13 +48,12 @@ public class RecentFragment extends BaseFragment { private SwipeRefreshLayout swipeRefreshLayout; private RecentAdapter recentAdapter; - private DocumentSnapshot recentDocument; - - private RecentTask recentTask; + private ArrayList recentItems; // Required empty public constructor public RecentFragment() {} + /** * Use ONLY this factory method to create a new instance of * this fragment using the provided parameters. @@ -86,18 +77,28 @@ public class RecentFragment extends BaseFragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - if (recentDocument == null) { + if (recentItems == null) { DocumentReference docRef = BaseApplication.getInstance().getFirestoredb() .collection("recent_posts") .document("recent"); docRef.get().addOnCompleteListener(task -> { if (task.isSuccessful()) { - recentDocument = task.getResult(); + DocumentSnapshot recentDocument = task.getResult(); + List posts = (List) recentDocument.get("posts"); + List> tasks = new ArrayList<>(); + for (DocumentReference documentReference : posts) { + Task documentSnapshotTask = documentReference.get(); + tasks.add(documentSnapshotTask); + } + Tasks.whenAllSuccess(tasks).addOnSuccessListener(objects -> { + ArrayList 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"); } @@ -112,7 +113,7 @@ public class RecentFragment extends BaseFragment { // Set the adapter if (rootView instanceof RelativeLayout) { progressBar = rootView.findViewById(R.id.progressBar); - recentAdapter = new RecentAdapter(getActivity(), (List) recentDocument.get("posts"), fragmentInteractionListener); + recentAdapter = new RecentAdapter(getActivity(), recentItems, fragmentInteractionListener); CustomRecyclerView recyclerView = rootView.findViewById(R.id.list); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext()); @@ -146,7 +147,7 @@ public class RecentFragment extends BaseFragment { public interface RecentFragmentInteractionListener extends FragmentInteractionListener { - void onRecentFragmentInteraction(TopicSummary topicSummary); + void onRecentFragmentInteraction(RecentItem topicSummary); } private void onRecentTaskStarted() { diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java b/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java index 82417bce..d03c17d2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java @@ -7,8 +7,8 @@ public class RecentItem { private String boardTitle, topicTitle, poster; private Date timestamp; - public RecentItem(int boardId, String boardTitle, int postId, String poster, int posterId, int timestamp, int topicId, - String topicTitle) { + public RecentItem(int boardId, String boardTitle, int postId, String poster, int posterId, int timestamp, + int topicId, String topicTitle) { this.boardId = boardId; this.postId = postId; this.poster = poster;