From 016067b336733cca8ba72d85b76103638f251512 Mon Sep 17 00:00:00 2001 From: oogee Date: Tue, 2 Apr 2019 13:40:28 +0300 Subject: [PATCH] add relative time textview to display timestamp --- app/build.gradle | 1 + .../activities/main/recent/RecentAdapter.java | 21 +++++++++---------- .../main/recent/RecentFragment.java | 6 +----- .../gr/thmmy/mthmmy/model/RecentItem.java | 6 +++--- .../main/res/layout/fragment_recent_row.xml | 2 +- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 2a09832c..10106341 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -90,6 +90,7 @@ dependencies { implementation 'ru.noties:markwon:2.0.0' implementation 'net.gotev:uploadservice:3.4.2' implementation 'net.gotev:uploadservice-okhttp:3.4.2' + implementation 'com.github.curioustechizen.android-ago:library:1.4.0' implementation 'com.itkacher.okhttpprofiler:okhttpprofiler:1.0.4' //Plugin: https://plugins.jetbrains.com/plugin/11249-okhttp-profiler } 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 abd9ec41..3350c740 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,6 +6,8 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import com.github.curioustechizen.ago.RelativeTimeTextView; + import java.util.List; import androidx.annotation.NonNull; @@ -45,21 +47,18 @@ class RecentAdapter extends RecyclerView.Adapter { public void onBindViewHolder(final ViewHolder holder, final int position) { RecentItem recentItem = recentItems.get(position); holder.mTitleView.setText(recentItem.getTopicTitle()); - holder.mDateTimeView.setText(recentItem.getTimestamp().toString()); + holder.mDateTimeView.setReferenceTime(recentItem.getTimestamp()); holder.mUserView.setText(recentItem.getPoster()); - holder.mView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - - 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(recentItems.get(holder.getAdapterPosition())); //? + holder.mView.setOnClickListener(v -> { - } + 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(recentItems.get(holder.getAdapterPosition())); //? } + }); } @@ -72,7 +71,7 @@ class RecentAdapter extends RecyclerView.Adapter { final View mView; final TextView mTitleView; final TextView mUserView; - final TextView mDateTimeView; + final RelativeTimeTextView mDateTimeView; 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 0e0594ef..ac70841b 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 @@ -73,23 +73,19 @@ public class RecentFragment extends BaseFragment { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - Timber.d("onActivityCreated"); - DocumentReference docRef = FirebaseFirestore.getInstance() .collection("recent_posts") .document("recent"); - Timber.i("I'm here"); docRef.get().addOnCompleteListener(task -> { - Timber.i("I'm there"); progressBar.setVisibility(ProgressBar.INVISIBLE); if (task.isSuccessful()) { DocumentSnapshot recentDocument = task.getResult(); - Timber.i("Type: " + recentDocument.get("posts").getClass().getName()); ArrayList> posts = (ArrayList>) recentDocument.get("posts"); for (HashMap map : posts) { RecentItem recentItem = new RecentItem(map); recentItems.add(recentItem); } + recentAdapter.notifyDataSetChanged(); } else { Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); } 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 4221566a..667ab826 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java @@ -6,7 +6,7 @@ import java.util.Map; public class RecentItem { private int boardId, postId, topicId, posterId; private String boardTitle, topicTitle, poster; - private Date timestamp; + private long timestamp; public RecentItem(Map map) { this.boardId = ((Long) map.get("boardId")).intValue(); @@ -16,10 +16,10 @@ public class RecentItem { this.topicId = ((Long) map.get("topicId")).intValue(); this.boardTitle = String.valueOf(map.get("boardTitle")); this.topicTitle = String.valueOf(map.get("topicTitle")); - this.timestamp = new Date((long)(map.get("timestamp")) * 1000); + this.timestamp = (long)(map.get("timestamp")) * 1000; } - public Date getTimestamp() { + public long getTimestamp() { return timestamp; } diff --git a/app/src/main/res/layout/fragment_recent_row.xml b/app/src/main/res/layout/fragment_recent_row.xml index 7d136a4e..b816afb6 100644 --- a/app/src/main/res/layout/fragment_recent_row.xml +++ b/app/src/main/res/layout/fragment_recent_row.xml @@ -32,7 +32,7 @@ android:layout_below="@+id/title" android:layout_toEndOf="@+id/dateTime"/> -