I am using a Github Library to Display time in a TextView
as the format like just now, yesterday, time ago... etc. It displays the time as I want. But the problem is that the time is not changing as it was just now at the moment of post-it remains that forever.
Model class for postAdapter
class Post {
private var date: String = ""
constructor(date: Long) {
fun getDate(): String
{
return date
}
//Initialization according to the library
val timeInMillis = System.currentTimeMillis()
val timeAgo = TimeAgo.using(timeInMillis)
//saving the data to firestore
val fStore: FirebaseFirestore = FirebaseFirestore.getInstance()
val post = Post(title, description, date = timeAgo)
fStore.collection("Posts").document()
.set(post)
.addOnSuccessListener{...}
Retrieving the data from the Firestore
private fun postInfo(title: TextView, description: TextView, date: TextView) {
val postRef = FirebaseFirestore.getInstance().collection("Posts").document()
postRef.get()
.addOnSuccessListener { documentSnapshot ->
if (documentSnapshot != null && documentSnapshot.exists()) {
val post = documentSnapshot.toObject(Post::class.java)
date.text = post?.getDate()
title.text = post?.getTitle()
description.text = post?.getDescription()
}
}
}
What you do is using a library to convert a date to a string, and then set the TextView text to that string. Strings are only lists of chars and they have no "consciousness" about what they mean or if they ever need to change/update.
I would suggest using a RelativeTimeTextView
from android-ago
. Once you set a reference time to that view, there's some code inside that will automatically trigger updates for you.
https://github.com/curioustechizen/android-ago
Edit: you're also doing conversion from timeInMillis: Long
to date: String
at the moment of storage in firestore. You should instead store timeInMillis as is (Long value) and then when reading use it as argument of relativeTimetextView.setReferenceTime(timeInMillis)
.