From 61d6ef47ac79275d95fea865c86640109d8e5641 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Thu, 10 Sep 2020 20:20:23 +0200 Subject: [PATCH] Adding time to notification --- .../java/com/h/pixeldroid/PostTest.kt | 11 +++++-- .../java/com/h/pixeldroid/db/Converters.kt | 9 ++++++ .../com/h/pixeldroid/db/PostDatabaseEntity.kt | 3 +- .../fragments/feeds/NotificationsFragment.kt | 3 ++ .../fragments/feeds/OfflineFeedFragment.kt | 4 +-- .../java/com/h/pixeldroid/objects/Field.kt | 4 ++- .../com/h/pixeldroid/objects/Notification.kt | 4 ++- .../java/com/h/pixeldroid/objects/Status.kt | 32 ++----------------- .../java/com/h/pixeldroid/utils/DBUtils.kt | 3 +- .../main/java/com/h/pixeldroid/utils/Utils.kt | 21 ++++++++++++ .../res/layout/fragment_notifications.xml | 24 ++++++++------ .../test/java/com/h/pixeldroid/APIUnitTest.kt | 11 ++++--- .../java/com/h/pixeldroid/PostUnitTest.kt | 4 ++- 13 files changed, 81 insertions(+), 52 deletions(-) diff --git a/app/src/androidTest/java/com/h/pixeldroid/PostTest.kt b/app/src/androidTest/java/com/h/pixeldroid/PostTest.kt index 186c69bc..d56e2ef7 100644 --- a/app/src/androidTest/java/com/h/pixeldroid/PostTest.kt +++ b/app/src/androidTest/java/com/h/pixeldroid/PostTest.kt @@ -33,6 +33,13 @@ import org.hamcrest.Matcher import org.junit.* import org.junit.rules.Timeout import org.junit.runner.RunWith +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.time.LocalDate.parse +import java.time.LocalDateTime.parse +import java.time.LocalTime +import java.time.LocalTime.parse +import java.util.* @RunWith(AndroidJUnit4::class) @@ -191,7 +198,7 @@ class PostTest { @Test fun getNLikesReturnsCorrectFormat() { val status = Status(id="140364967936397312", uri="https://pixelfed.de/p/Miike/140364967936397312", - created_at="2020-03-03T08:00:16.000000Z", + created_at= SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.hhmmss'Z'").parse("2020-03-03T08:00:16.000000Z"), account= Account(id="115114166443970560", username="Miike", acct="Miike", url="https://pixelfed.de/Miike", display_name="Miike Duart", note="", avatar="https://pixelfed.de/storage/avatars/011/511/416/644/397/056/0/ZhaopLJWTWJ3hsVCS5pS_avatar.png?v=d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35", @@ -218,7 +225,7 @@ class PostTest { @Test fun getNSharesReturnsCorrectFormat() { val status = Status(id="140364967936397312", uri="https://pixelfed.de/p/Miike/140364967936397312", - created_at="2020-03-03T08:00:16.000000Z", + created_at= SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.hhmmss'Z'").parse("2020-03-03T08:00:16.000000Z"), account= Account(id="115114166443970560", username="Miike", acct="Miike", url="https://pixelfed.de/Miike", display_name="Miike Duart", note="", avatar="https://pixelfed.de/storage/avatars/011/511/416/644/397/056/0/ZhaopLJWTWJ3hsVCS5pS_avatar.png?v=d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35", diff --git a/app/src/main/java/com/h/pixeldroid/db/Converters.kt b/app/src/main/java/com/h/pixeldroid/db/Converters.kt index 536aac3e..b4d5cfaf 100644 --- a/app/src/main/java/com/h/pixeldroid/db/Converters.kt +++ b/app/src/main/java/com/h/pixeldroid/db/Converters.kt @@ -2,6 +2,7 @@ package com.h.pixeldroid.db import androidx.room.TypeConverter import com.google.gson.Gson +import java.util.Date class Converters { @TypeConverter @@ -10,4 +11,12 @@ class Converters { @TypeConverter fun jsonToList(json: String): List = Gson().fromJson(json, Array::class.java).toList() + + @TypeConverter + fun dateToJson(date: Date): String = Gson().toJson(date) + + @TypeConverter + fun jsontoDate(json: String): Date = Gson().fromJson(json, Date::class.java) + + } \ No newline at end of file diff --git a/app/src/main/java/com/h/pixeldroid/db/PostDatabaseEntity.kt b/app/src/main/java/com/h/pixeldroid/db/PostDatabaseEntity.kt index a0009e8a..e3b52709 100644 --- a/app/src/main/java/com/h/pixeldroid/db/PostDatabaseEntity.kt +++ b/app/src/main/java/com/h/pixeldroid/db/PostDatabaseEntity.kt @@ -3,6 +3,7 @@ package com.h.pixeldroid.db import androidx.room.Entity import androidx.room.ForeignKey import androidx.room.Index +import java.util.Date @Entity( tableName = "posts", @@ -27,7 +28,7 @@ data class PostDatabaseEntity ( var reply_count: Int, var share_count: Int, var description: String, - var date: String, + var date: Date, var likes: Int, var shares: Int ) \ No newline at end of file diff --git a/app/src/main/java/com/h/pixeldroid/fragments/feeds/NotificationsFragment.kt b/app/src/main/java/com/h/pixeldroid/fragments/feeds/NotificationsFragment.kt index f7807e87..ef9e2293 100644 --- a/app/src/main/java/com/h/pixeldroid/fragments/feeds/NotificationsFragment.kt +++ b/app/src/main/java/com/h/pixeldroid/fragments/feeds/NotificationsFragment.kt @@ -29,6 +29,7 @@ import com.h.pixeldroid.objects.Account import com.h.pixeldroid.objects.Notification import com.h.pixeldroid.objects.Status import com.h.pixeldroid.utils.HtmlUtils.Companion.parseHTMLText +import com.h.pixeldroid.utils.Utils.Companion.setTextViewFromISO8601 import kotlinx.android.synthetic.main.fragment_notifications.view.* import retrofit2.Call import retrofit2.Callback @@ -197,6 +198,7 @@ class NotificationsFragment : FeedFragment() { } setNotificationType(notification.type, notification.account.username, holder.notificationType) + setTextViewFromISO8601(notification.created_at, holder.notificationTime, false, context) //Convert HTML to clickable text holder.postDescription.text = @@ -247,6 +249,7 @@ class NotificationsFragment : FeedFragment() { inner class ViewHolder(val mView: View) : RecyclerView.ViewHolder(mView) { val notificationType: TextView = mView.notification_type + val notificationTime: TextView = mView.notification_time val postDescription: TextView = mView.notification_post_description val avatar: ImageView = mView.notification_avatar val photoThumbnail: ImageView = mView.notification_photo_thumbnail diff --git a/app/src/main/java/com/h/pixeldroid/fragments/feeds/OfflineFeedFragment.kt b/app/src/main/java/com/h/pixeldroid/fragments/feeds/OfflineFeedFragment.kt index 2c14c35b..989b8fe3 100644 --- a/app/src/main/java/com/h/pixeldroid/fragments/feeds/OfflineFeedFragment.kt +++ b/app/src/main/java/com/h/pixeldroid/fragments/feeds/OfflineFeedFragment.kt @@ -25,8 +25,8 @@ import com.h.pixeldroid.R import com.h.pixeldroid.db.AppDatabase import com.h.pixeldroid.db.PostDatabaseEntity import com.h.pixeldroid.fragments.ImageFragment -import com.h.pixeldroid.objects.Status import com.h.pixeldroid.utils.* +import com.h.pixeldroid.utils.Utils.Companion.setTextViewFromISO8601 import kotlinx.android.synthetic.main.fragment_offline_feed.view.* import kotlinx.android.synthetic.main.post_fragment.view.* import javax.inject.Inject @@ -146,7 +146,7 @@ class OfflineFeedFragment: Fragment() { setTypeface(null, Typeface.BOLD) } //Convert the date to a readable string - Status.ISO8601toDate(post.date, holder.itemView.postDate, false, requireContext()) + setTextViewFromISO8601(post.date, holder.itemView.postDate, false, requireContext()) //Setup images ImageConverter.setRoundImageFromURL( diff --git a/app/src/main/java/com/h/pixeldroid/objects/Field.kt b/app/src/main/java/com/h/pixeldroid/objects/Field.kt index d86ed7e5..828e3fda 100644 --- a/app/src/main/java/com/h/pixeldroid/objects/Field.kt +++ b/app/src/main/java/com/h/pixeldroid/objects/Field.kt @@ -1,3 +1,5 @@ package com.h.pixeldroid.objects -class Field +import java.io.Serializable + +class Field: Serializable diff --git a/app/src/main/java/com/h/pixeldroid/objects/Notification.kt b/app/src/main/java/com/h/pixeldroid/objects/Notification.kt index 46b4a22b..821881e4 100644 --- a/app/src/main/java/com/h/pixeldroid/objects/Notification.kt +++ b/app/src/main/java/com/h/pixeldroid/objects/Notification.kt @@ -1,5 +1,7 @@ package com.h.pixeldroid.objects +import java.util.Date + /* Represents a notification of an event relevant to the user. https://docs.joinmastodon.org/entities/notification/ @@ -8,7 +10,7 @@ data class Notification( //Required attributes override val id: String, val type: NotificationType, - val created_at: String, //ISO 8601 Datetime + val created_at: Date, //ISO 8601 Datetime val account: Account, //Optional attributes val status: Status? = null diff --git a/app/src/main/java/com/h/pixeldroid/objects/Status.kt b/app/src/main/java/com/h/pixeldroid/objects/Status.kt index 88b9c7ea..00610f01 100644 --- a/app/src/main/java/com/h/pixeldroid/objects/Status.kt +++ b/app/src/main/java/com/h/pixeldroid/objects/Status.kt @@ -33,14 +33,13 @@ import com.h.pixeldroid.utils.PostUtils.Companion.toggleCommentInput import com.h.pixeldroid.utils.PostUtils.Companion.unLikePostCall import com.h.pixeldroid.utils.PostUtils.Companion.uncensorColorMatrix import com.h.pixeldroid.utils.PostUtils.Companion.undoReblogPost +import com.h.pixeldroid.utils.Utils.Companion.setTextViewFromISO8601 import com.karumi.dexter.Dexter import com.karumi.dexter.listener.PermissionDeniedResponse import com.karumi.dexter.listener.PermissionGrantedResponse import com.karumi.dexter.listener.single.BasePermissionListener import kotlinx.android.synthetic.main.post_fragment.view.* import java.io.Serializable -import java.text.ParseException -import java.text.SimpleDateFormat import java.util.Date import kotlin.collections.ArrayList @@ -52,7 +51,7 @@ data class Status( //Base attributes override val id: String?, val uri: String? = "", - val created_at: String? = "", //ISO 8601 Datetime (maybe can use a date type) + val created_at: Date? = Date(0), //ISO 8601 Datetime val account: Account?, val content: String? = "", //HTML val visibility: Visibility? = Visibility.public, @@ -90,31 +89,6 @@ data class Status( const val POST_TAG = "postTag" const val DOMAIN_TAG = "domainTag" const val DISCOVER_TAG = "discoverTag" - - fun ISO8601toDate(dateString : String, textView: TextView, isActivity: Boolean, context: Context) { - var format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.hhmmss'Z'") - if(dateString.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6}Z".toRegex())) { - format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.hhmmss'Z'") - } else if(dateString.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}+[0-9]{2}:[0-9]{2}".toRegex())) { - format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+hh:mm") - } - val now = Date().time - - try { - val date: Date = format.parse(dateString)!! - val then = date.time - val formattedDate = android.text.format.DateUtils - .getRelativeTimeSpanString(then, now, - android.text.format.DateUtils.SECOND_IN_MILLIS, - android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE) - - textView.text = if(isActivity) context.getString(R.string.posted_on).format(date) - else "$formattedDate" - - } catch (e: ParseException) { - e.printStackTrace() - } - } } fun getPostUrl() : String? = media_attachments?.firstOrNull()?.url @@ -237,7 +211,7 @@ data class Status( } //Convert the date to a readable string - ISO8601toDate(created_at!!, rootView.postDate, isActivity, rootView.context) + setTextViewFromISO8601(created_at!!, rootView.postDate, isActivity, rootView.context) rootView.postDomain.text = getStatusDomain(domain) diff --git a/app/src/main/java/com/h/pixeldroid/utils/DBUtils.kt b/app/src/main/java/com/h/pixeldroid/utils/DBUtils.kt index 115a615e..6bf88d39 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/DBUtils.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/DBUtils.kt @@ -8,6 +8,7 @@ import com.h.pixeldroid.objects.Account import com.h.pixeldroid.objects.Instance import com.h.pixeldroid.objects.Status import com.h.pixeldroid.utils.Utils.Companion.normalizeDomain +import java.util.Date class DBUtils { companion object { @@ -75,7 +76,7 @@ class DBUtils { reply_count = post.replies_count ?: 0, share_count = post.reblogs_count ?: 0, description = post.content ?: "", - date = post.created_at ?: "", + date = post.created_at ?: Date(0), likes = post.favourites_count ?: 0, shares = post.reblogs_count ?: 0 )) diff --git a/app/src/main/java/com/h/pixeldroid/utils/Utils.kt b/app/src/main/java/com/h/pixeldroid/utils/Utils.kt index 66954de0..c7c81d40 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/Utils.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/Utils.kt @@ -2,6 +2,10 @@ package com.h.pixeldroid.utils import android.content.Context import android.net.ConnectivityManager +import android.widget.TextView +import com.h.pixeldroid.R +import java.text.ParseException +import java.util.Date class Utils { companion object { @@ -18,5 +22,22 @@ class Utils { } + fun setTextViewFromISO8601(date: Date, textView: TextView, absoluteTime: Boolean, context: Context) { + val now = Date().time + + try { + val then = date.time + val formattedDate = android.text.format.DateUtils + .getRelativeTimeSpanString(then, now, + android.text.format.DateUtils.SECOND_IN_MILLIS, + android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE) + + textView.text = if(absoluteTime) context.getString(R.string.posted_on).format(date) + else "$formattedDate" + + } catch (e: ParseException) { + e.printStackTrace() + } + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_notifications.xml b/app/src/main/res/layout/fragment_notifications.xml index 2ad5d04a..7a04aca5 100644 --- a/app/src/main/res/layout/fragment_notifications.xml +++ b/app/src/main/res/layout/fragment_notifications.xml @@ -12,22 +12,28 @@ android:layout_margin="8dp"> + + + tools:text="fdsqfdsfsqdfdsfqdsfsdfsfddsfqsdsdfsqdf liked your post" />