Browse Source

RecentItem

firestoreRecent
oogee 6 years ago
parent
commit
61a2eed058
  1. 1
      app/build.gradle
  2. 37
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java
  3. 78
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  4. 7
      app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java
  5. 53
      app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java
  6. 2
      build.gradle
  7. 4
      gradle/wrapper/gradle-wrapper.properties

1
app/build.gradle

@ -72,6 +72,7 @@ dependencies {
implementation 'com.google.android.material:material:1.0.0' implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.firebase:firebase-core:16.0.6' implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4' implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-firestore:18.1.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8' implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8'
implementation 'com.squareup.okhttp3:okhttp:3.12.0' implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.squareup.picasso:picasso:2.5.2' implementation 'com.squareup.picasso:picasso:2.5.2'

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

@ -6,7 +6,16 @@ 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;
@ -21,12 +30,26 @@ 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<TopicSummary> recentList; private final List<DocumentReference> postSummaries;
private final RecentFragment.RecentFragmentInteractionListener mListener; private final RecentFragment.RecentFragmentInteractionListener mListener;
RecentAdapter(Context context, @NonNull List<TopicSummary> topicSummaryList, BaseFragment.FragmentInteractionListener listener) { RecentAdapter(Context context, @NonNull List<DocumentReference> postSummaries, BaseFragment.FragmentInteractionListener listener) {
this.context = context; this.context = context;
this.recentList = topicSummaryList; 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;
mListener = (RecentFragment.RecentFragmentInteractionListener) listener; mListener = (RecentFragment.RecentFragmentInteractionListener) listener;
} }
@ -41,9 +64,11 @@ 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) {
holder.mTitleView.setText(recentList.get(position).getSubject()); ArraySortedMap map = (ArraySortedMap) postSummaries.get("posts");
holder.mDateTimeView.setText(recentList.get(position).getDateTimeModified()); ArraySortedMap post = (ArraySortedMap) map.get(position);
holder.mUserView.setText(recentList.get(position).getLastUser()); 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); holder.topic = recentList.get(position);

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

@ -9,6 +9,11 @@ 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.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
@ -17,10 +22,12 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; 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.BaseFragment; import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
@ -49,7 +56,7 @@ public class RecentFragment extends BaseFragment {
private SwipeRefreshLayout swipeRefreshLayout; private SwipeRefreshLayout swipeRefreshLayout;
private RecentAdapter recentAdapter; private RecentAdapter recentAdapter;
private List<TopicSummary> topicSummaries; private DocumentSnapshot recentDocument;
private RecentTask recentTask; private RecentTask recentTask;
@ -74,13 +81,20 @@ public class RecentFragment extends BaseFragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
topicSummaries = new ArrayList<>();
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
if (topicSummaries.isEmpty()) { if (recentDocument == null) {
DocumentReference docRef = BaseApplication.getInstance().getFirestoredb()
.collection("recent_posts")
.document("recent");
docRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
recentDocument = task.getResult();
}
});
recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished); recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished);
recentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.indexUrl.toString()); recentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.indexUrl.toString());
@ -98,7 +112,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(), topicSummaries, fragmentInteractionListener); recentAdapter = new RecentAdapter(getActivity(), (List<DocumentReference>) recentDocument.get("posts"), 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());
@ -154,60 +168,4 @@ public class RecentFragment extends BaseFragment {
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
} }
//---------------------------------------ASYNC TASK-----------------------------------
private class RecentTask extends NewParseTask<ArrayList<TopicSummary>> {
RecentTask(OnTaskStartedListener onTaskStartedListener,
OnNetworkTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
@Override
protected ArrayList<TopicSummary> parse(Document document, Response response) throws ParseException {
ArrayList<TopicSummary> fetchedRecent = new ArrayList<>();
Elements recent = document.select("#block8 :first-child div");
if (!recent.isEmpty()) {
for (int i = 0; i < recent.size(); i += 3) {
String link = recent.get(i).child(0).attr("href");
String title = recent.get(i).child(0).attr("title");
title = title.trim();
String lastUser = recent.get(i + 1).text();
Pattern pattern = Pattern.compile("\\b (.*)");
Matcher matcher = pattern.matcher(lastUser);
if (matcher.find())
lastUser = matcher.group(1);
else
throw new ParseException("Parsing failed (lastUser)");
String dateTime = recent.get(i + 2).text();
pattern = Pattern.compile("\\[(.*)]");
matcher = pattern.matcher(dateTime);
if (matcher.find()) {
dateTime = matcher.group(1);
if (dateTime.contains(" am") || dateTime.contains(" pm") ||
dateTime.contains(" πμ") || dateTime.contains(" μμ")) {
dateTime = dateTime.replaceAll(":[0-5][0-9] ", " ");
} else {
dateTime = dateTime.substring(0, dateTime.lastIndexOf(":"));
}
if (!dateTime.contains(",")) {
dateTime = dateTime.replaceAll(".+? ([0-9])", "$1");
}
} else
throw new ParseException("Parsing failed (dateTime)");
fetchedRecent.add(new TopicSummary(link, title, lastUser, dateTime));
}
return fetchedRecent;
}
throw new ParseException("Parsing failed");
}
@Override
protected int getResultCode(Response response, ArrayList<TopicSummary> data) {
return NetworkResultCodes.SUCCESSFUL;
}
}
} }

7
app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java

@ -16,6 +16,7 @@ import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.google.firebase.analytics.FirebaseAnalytics; import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.firestore.FirebaseFirestore;
import com.itkacher.okhttpprofiler.OkHttpProfilerInterceptor; import com.itkacher.okhttpprofiler.OkHttpProfilerInterceptor;
import com.jakewharton.picasso.OkHttp3Downloader; import com.jakewharton.picasso.OkHttp3Downloader;
import com.mikepenz.fontawesome_typeface_library.FontAwesome; import com.mikepenz.fontawesome_typeface_library.FontAwesome;
@ -52,6 +53,7 @@ public class BaseApplication extends Application {
//Firebase Analytics //Firebase Analytics
private FirebaseAnalytics firebaseAnalytics; private FirebaseAnalytics firebaseAnalytics;
private FirebaseFirestore firestoredb;
//Client & SessionManager //Client & SessionManager
private OkHttpClient client; private OkHttpClient client;
@ -93,6 +95,7 @@ public class BaseApplication extends Application {
Timber.i("Starting app with Analytics enabled."); Timber.i("Starting app with Analytics enabled.");
else else
Timber.i("Starting app with Analytics disabled."); Timber.i("Starting app with Analytics disabled.");
firestoredb = FirebaseFirestore.getInstance();
SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext()); SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext());
PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor); PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor);
@ -181,6 +184,10 @@ public class BaseApplication extends Application {
return client; return client;
} }
public FirebaseFirestore getFirestoredb() {
return firestoredb;
}
public SessionManager getSessionManager() { public SessionManager getSessionManager() {
return sessionManager; return sessionManager;
} }

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

@ -0,0 +1,53 @@
package gr.thmmy.mthmmy.model;
import java.util.Date;
public class RecentItem {
private int boardId, postId, topicId, posterId;
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) {
this.boardId = boardId;
this.postId = postId;
this.poster = poster;
this.posterId = posterId;
this.topicId = topicId;
this.boardTitle = boardTitle;
this.topicTitle = topicTitle;
this.timestamp = new Date(timestamp);
}
public Date getTimestamp() {
return timestamp;
}
public int getBoardId() {
return boardId;
}
public int getPosterId() {
return posterId;
}
public int getPostId() {
return postId;
}
public int getTopicId() {
return topicId;
}
public String getBoardTitle() {
return boardTitle;
}
public String getPoster() {
return poster;
}
public String getTopicTitle() {
return topicTitle;
}
}

2
build.gradle

@ -8,7 +8,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.2.1' classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.2.0' classpath 'com.google.gms:google-services:4.2.0'
classpath 'io.fabric.tools:gradle:1.26.1' classpath 'io.fabric.tools:gradle:1.26.1'
classpath 'org.ajoberstar.grgit:grgit-core:3.0.0' // Also change in app/gradle/grgit.gradle classpath 'org.ajoberstar.grgit:grgit-core:3.0.0' // Also change in app/gradle/grgit.gradle

4
gradle/wrapper/gradle-wrapper.properties

@ -1,6 +1,6 @@
#Fri Sep 28 13:21:54 EEST 2018 #Sat Mar 02 17:05:57 EET 2019
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

Loading…
Cancel
Save