Browse Source

add relative time textview to display timestamp

firestoreRecent
oogee 6 years ago
parent
commit
016067b336
  1. 1
      app/build.gradle
  2. 21
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java
  3. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  4. 6
      app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java
  5. 2
      app/src/main/res/layout/fragment_recent_row.xml

1
app/build.gradle

@ -90,6 +90,7 @@ dependencies {
implementation 'ru.noties:markwon:2.0.0' implementation 'ru.noties:markwon:2.0.0'
implementation 'net.gotev:uploadservice:3.4.2' implementation 'net.gotev:uploadservice:3.4.2'
implementation 'net.gotev:uploadservice-okhttp: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 implementation 'com.itkacher.okhttpprofiler:okhttpprofiler:1.0.4' //Plugin: https://plugins.jetbrains.com/plugin/11249-okhttp-profiler
} }

21
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.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import com.github.curioustechizen.ago.RelativeTimeTextView;
import java.util.List; import java.util.List;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -45,21 +47,18 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
public void onBindViewHolder(final ViewHolder holder, final int position) { public void onBindViewHolder(final ViewHolder holder, final int position) {
RecentItem recentItem = recentItems.get(position); RecentItem recentItem = recentItems.get(position);
holder.mTitleView.setText(recentItem.getTopicTitle()); holder.mTitleView.setText(recentItem.getTopicTitle());
holder.mDateTimeView.setText(recentItem.getTimestamp().toString()); holder.mDateTimeView.setReferenceTime(recentItem.getTimestamp());
holder.mUserView.setText(recentItem.getPoster()); holder.mUserView.setText(recentItem.getPoster());
holder.mView.setOnClickListener(new View.OnClickListener() { holder.mView.setOnClickListener(v -> {
@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())); //?
} 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<RecentAdapter.ViewHolder> {
final View mView; final View mView;
final TextView mTitleView; final TextView mTitleView;
final TextView mUserView; final TextView mUserView;
final TextView mDateTimeView; final RelativeTimeTextView mDateTimeView;
ViewHolder(View view) { ViewHolder(View view) {
super(view); super(view);

6
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) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
Timber.d("onActivityCreated");
DocumentReference docRef = FirebaseFirestore.getInstance() DocumentReference docRef = FirebaseFirestore.getInstance()
.collection("recent_posts") .collection("recent_posts")
.document("recent"); .document("recent");
Timber.i("I'm here");
docRef.get().addOnCompleteListener(task -> { docRef.get().addOnCompleteListener(task -> {
Timber.i("I'm there");
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
if (task.isSuccessful()) { if (task.isSuccessful()) {
DocumentSnapshot recentDocument = task.getResult(); DocumentSnapshot recentDocument = task.getResult();
Timber.i("Type: " + recentDocument.get("posts").getClass().getName());
ArrayList<HashMap<String, Object>> posts = (ArrayList<HashMap<String, Object>>) recentDocument.get("posts"); ArrayList<HashMap<String, Object>> posts = (ArrayList<HashMap<String, Object>>) recentDocument.get("posts");
for (HashMap<String, Object> map : posts) { for (HashMap<String, Object> map : posts) {
RecentItem recentItem = new RecentItem(map); RecentItem recentItem = new RecentItem(map);
recentItems.add(recentItem); recentItems.add(recentItem);
} }
recentAdapter.notifyDataSetChanged();
} else { } else {
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show();
} }

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

@ -6,7 +6,7 @@ import java.util.Map;
public class RecentItem { public class RecentItem {
private int boardId, postId, topicId, posterId; private int boardId, postId, topicId, posterId;
private String boardTitle, topicTitle, poster; private String boardTitle, topicTitle, poster;
private Date timestamp; private long timestamp;
public RecentItem(Map<String, Object> map) { public RecentItem(Map<String, Object> map) {
this.boardId = ((Long) map.get("boardId")).intValue(); this.boardId = ((Long) map.get("boardId")).intValue();
@ -16,10 +16,10 @@ public class RecentItem {
this.topicId = ((Long) map.get("topicId")).intValue(); this.topicId = ((Long) map.get("topicId")).intValue();
this.boardTitle = String.valueOf(map.get("boardTitle")); this.boardTitle = String.valueOf(map.get("boardTitle"));
this.topicTitle = String.valueOf(map.get("topicTitle")); 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; return timestamp;
} }

2
app/src/main/res/layout/fragment_recent_row.xml

@ -32,7 +32,7 @@
android:layout_below="@+id/title" android:layout_below="@+id/title"
android:layout_toEndOf="@+id/dateTime"/> android:layout_toEndOf="@+id/dateTime"/>
<TextView <com.github.curioustechizen.ago.RelativeTimeTextView
android:id="@+id/dateTime" android:id="@+id/dateTime"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"

Loading…
Cancel
Save