Adding time to notification

This commit is contained in:
Matthieu 2020-09-10 20:20:23 +02:00
parent 7c2221e7a6
commit 61d6ef47ac
13 changed files with 81 additions and 52 deletions

View File

@ -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",

View File

@ -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<String> =
Gson().fromJson(json, Array<String>::class.java).toList()
@TypeConverter
fun dateToJson(date: Date): String = Gson().toJson(date)
@TypeConverter
fun jsontoDate(json: String): Date = Gson().fromJson(json, Date::class.java)
}

View File

@ -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
)

View File

@ -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

View File

@ -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(

View File

@ -1,3 +1,5 @@
package com.h.pixeldroid.objects
class Field
import java.io.Serializable
class Field: Serializable

View File

@ -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

View File

@ -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)

View File

@ -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
))

View File

@ -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()
}
}
}
}

View File

@ -12,22 +12,28 @@
android:layout_margin="8dp">
<TextView
android:id="@+id/notification_type"
android:layout_width="match_parent"
android:id="@+id/notification_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/notification_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="July 23" />
<TextView
android:id="@+id/notification_type"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_heart"
android:drawablePadding="6dp"
android:gravity="center_vertical"
android:paddingStart="38dp"
android:textColor="?android:textColorTertiary"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintStart_toEndOf="@+id/notification_avatar"
app:drawableStartCompat="@drawable/ic_heart"
app:layout_constraintEnd_toStartOf="@+id/notification_time"
app:layout_constraintStart_toStartOf="@+id/notification_avatar"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlSymmetry"
tools:text="User liked your post"
tools:visibility="visible" />
tools:text="fdsqfdsfsqdfdsfqdsfsdfsfddsfqsdsdfsqdf liked your post" />
<ImageView
android:id="@+id/notification_avatar"

View File

@ -5,11 +5,12 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule
import com.h.pixeldroid.api.PixelfedAPI
import com.h.pixeldroid.objects.*
import io.reactivex.Single
import okhttp3.internal.wait
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import retrofit2.Call
import java.text.SimpleDateFormat
import java.util.*
/**
@ -19,7 +20,7 @@ import retrofit2.Call
*/
class APIUnitTest {
private val referenceFirstStatus = 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",
@ -37,14 +38,14 @@ class APIUnitTest {
emojis= emptyList(), reblogs_count=0, favourites_count=0, replies_count=0, url="https://pixelfed.de/p/Miike/140364967936397312",
in_reply_to_id=null, in_reply_to_account=null, reblog=null, poll=null, card=null, language=null, text=null, favourited=null, reblogged=null, muted=null, bookmarked=null, pinned=null)
val sampleNotification = Notification("45723", Notification.NotificationType.favourite,
"2020-03-14T15:01:49+00:00",
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+hh:mm").parse("2020-03-14T15:01:49+00:00")!!,
Account("79574199701737472", "Spaziergaenger",
"Spaziergaenger", "https://pixelfed.de/Spaziergaenger",
"anonymous", "", "https://pixelfed.de/storage/avatars/007/957/419/970/173/747/2/KEg4YgCgsmzdgyVztszz_avatar.jpeg?v=d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35",
"https://pixelfed.de/storage/avatars/007/957/419/970/173/747/2/KEg4YgCgsmzdgyVztszz_avatar.jpeg?v=d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35",
locked=false, followers_count = 40, following_count = 0, statuses_count = 891, created_at = "1568728767", header = "", discoverable = true, emojis = emptyList(), header_static = ""),
Status("144456497894658048","https://pixelfed.de/p/dante/144456497894658048",
"https://pixelfed.de/p/dante/144456497894658048", in_reply_to_id = null,
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.hhmmss'Z'").parse("2020-03-03T08:00:16.000000Z"), in_reply_to_id = null,
in_reply_to_account = null, reblog = null,content = "Saturn V launch", emojis = emptyList(), reblogs_count = 0,
favourites_count = 1, reblogged = false, favourited = false, muted = false, sensitive = false,
spoiler_text = "", visibility = Status.Visibility.public, application = Application("web", null),
@ -154,7 +155,7 @@ fun assertStatusEqualsToReference(actual: Status){
assert(
((actual.id=="140364967936397312"
&& actual.uri=="https://pixelfed.de/p/Miike/140364967936397312"
&& actual.created_at=="2020-03-03T08:00:16.000000Z"
&& actual.created_at==SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.hhmmss'Z'").parse("2020-03-03T08:00:16.000000Z")
&& actual.account!!.id=="115114166443970560"&& actual.account!!.username=="Miike"&& actual.account!!.acct=="Miike" &&
actual.account!!.url=="https://pixelfed.de/Miike"&& actual.account!!.display_name=="Miike Duart"&& actual.account!!.note==""&&
actual.account!!.avatar=="https://pixelfed.de/storage/avatars/011/511/416/644/397/056/0/ZhaopLJWTWJ3hsVCS5pS_avatar.png?v=d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35"&&

View File

@ -3,10 +3,12 @@ package com.h.pixeldroid
import com.h.pixeldroid.objects.*
import org.junit.Assert
import org.junit.Test
import java.text.SimpleDateFormat
import java.util.*
class PostUnitTest {
private 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",