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.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);
}

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.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<RecentAdapter.ViewHolder> {
private final Context context;
private final List<DocumentReference> postSummaries;
private final List<RecentItem> recentItems;
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;
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (DocumentReference documentReference : postSummaries) {
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;
this.recentItems = recentItems;
mListener = (RecentFragment.RecentFragmentInteractionListener) listener;
}
@ -64,13 +43,10 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
@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<RecentAdapter.ViewHolder> {
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<RecentAdapter.ViewHolder> {
@Override
public int getItemCount() {
return recentList.size();
return recentItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
@ -97,7 +73,6 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
final TextView mTitleView;
final TextView mUserView;
final TextView mDateTimeView;
public TopicSummary topic;
ViewHolder(View 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.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<RecentItem> 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<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");
}
@ -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<DocumentReference>) 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() {

4
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;

Loading…
Cancel
Save