From 61a2eed0580b2da724b2d79143ada67942dd59e7 Mon Sep 17 00:00:00 2001 From: oogee Date: Mon, 4 Mar 2019 16:43:36 +0200 Subject: [PATCH] RecentItem --- app/build.gradle | 1 + .../activities/main/recent/RecentAdapter.java | 37 +++++++-- .../main/recent/RecentFragment.java | 78 +++++-------------- .../gr/thmmy/mthmmy/base/BaseApplication.java | 7 ++ .../gr/thmmy/mthmmy/model/RecentItem.java | 53 +++++++++++++ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 7 files changed, 113 insertions(+), 69 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java diff --git a/app/build.gradle b/app/build.gradle index 12853e57..2a09832c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -72,6 +72,7 @@ dependencies { implementation 'com.google.android.material:material:1.0.0' implementation 'com.google.firebase:firebase-core:16.0.6' 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.squareup.okhttp3:okhttp:3.12.0' implementation 'com.squareup.picasso:picasso:2.5.2' 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 31871069..725298ab 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,7 +6,16 @@ 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; @@ -21,12 +30,26 @@ import gr.thmmy.mthmmy.model.TopicSummary; */ class RecentAdapter extends RecyclerView.Adapter { private final Context context; - private final List recentList; + private final List postSummaries; private final RecentFragment.RecentFragmentInteractionListener mListener; - RecentAdapter(Context context, @NonNull List topicSummaryList, BaseFragment.FragmentInteractionListener listener) { + RecentAdapter(Context context, @NonNull List postSummaries, BaseFragment.FragmentInteractionListener listener) { this.context = context; - this.recentList = topicSummaryList; + 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; mListener = (RecentFragment.RecentFragmentInteractionListener) listener; } @@ -41,9 +64,11 @@ class RecentAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(final ViewHolder holder, final int position) { - holder.mTitleView.setText(recentList.get(position).getSubject()); - holder.mDateTimeView.setText(recentList.get(position).getDateTimeModified()); - holder.mUserView.setText(recentList.get(position).getLastUser()); + 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); 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 6c194c0e..924ad497 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,6 +9,11 @@ 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.firebase.firestore.DocumentReference; +import com.google.firebase.firestore.DocumentSnapshot; + import org.jsoup.nodes.Document; import org.jsoup.select.Elements; @@ -17,10 +22,12 @@ 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.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; @@ -49,7 +56,7 @@ public class RecentFragment extends BaseFragment { private SwipeRefreshLayout swipeRefreshLayout; private RecentAdapter recentAdapter; - private List topicSummaries; + private DocumentSnapshot recentDocument; private RecentTask recentTask; @@ -74,13 +81,20 @@ public class RecentFragment extends BaseFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - topicSummaries = new ArrayList<>(); } @Override public void onActivityCreated(Bundle 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.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.indexUrl.toString()); @@ -98,7 +112,7 @@ public class RecentFragment extends BaseFragment { // Set the adapter if (rootView instanceof RelativeLayout) { progressBar = rootView.findViewById(R.id.progressBar); - recentAdapter = new RecentAdapter(getActivity(), topicSummaries, fragmentInteractionListener); + recentAdapter = new RecentAdapter(getActivity(), (List) recentDocument.get("posts"), fragmentInteractionListener); CustomRecyclerView recyclerView = rootView.findViewById(R.id.list); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext()); @@ -154,60 +168,4 @@ public class RecentFragment extends BaseFragment { progressBar.setVisibility(ProgressBar.INVISIBLE); swipeRefreshLayout.setRefreshing(false); } - - //---------------------------------------ASYNC TASK----------------------------------- - private class RecentTask extends NewParseTask> { - - RecentTask(OnTaskStartedListener onTaskStartedListener, - OnNetworkTaskFinishedListener> onParseTaskFinishedListener) { - super(onTaskStartedListener, onParseTaskFinishedListener); - } - - @Override - protected ArrayList parse(Document document, Response response) throws ParseException { - ArrayList 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 data) { - return NetworkResultCodes.SUCCESSFUL; - } - } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java index ce4e4068..7730fed9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java +++ b/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.persistence.SharedPrefsCookiePersistor; import com.google.firebase.analytics.FirebaseAnalytics; +import com.google.firebase.firestore.FirebaseFirestore; import com.itkacher.okhttpprofiler.OkHttpProfilerInterceptor; import com.jakewharton.picasso.OkHttp3Downloader; import com.mikepenz.fontawesome_typeface_library.FontAwesome; @@ -52,6 +53,7 @@ public class BaseApplication extends Application { //Firebase Analytics private FirebaseAnalytics firebaseAnalytics; + private FirebaseFirestore firestoredb; //Client & SessionManager private OkHttpClient client; @@ -93,6 +95,7 @@ public class BaseApplication extends Application { Timber.i("Starting app with Analytics enabled."); else Timber.i("Starting app with Analytics disabled."); + firestoredb = FirebaseFirestore.getInstance(); SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext()); PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor); @@ -181,6 +184,10 @@ public class BaseApplication extends Application { return client; } + public FirebaseFirestore getFirestoredb() { + return firestoredb; + } + public SessionManager getSessionManager() { return sessionManager; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java b/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java new file mode 100644 index 00000000..82417bce --- /dev/null +++ b/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; + } +} diff --git a/build.gradle b/build.gradle index 463af701..be2e6a2a 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { jcenter() } 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 'io.fabric.tools:gradle:1.26.1' classpath 'org.ajoberstar.grgit:grgit-core:3.0.0' // Also change in app/gradle/grgit.gradle diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 07d9d5c1..d9234066 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/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 distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME 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