Timeline : quickly manage dates (need to be clean)

This commit is contained in:
ganfra 2018-10-31 16:11:59 +01:00
parent 6f2beef726
commit 900217b90e
7 changed files with 95 additions and 6 deletions

View File

@ -47,6 +47,8 @@ dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1'

implementation 'com.jakewharton.timber:timber:4.7.1'

implementation("com.airbnb.android:epoxy:$epoxy_version")

View File

@ -1,6 +1,7 @@
package im.vector.riotredesign

import android.app.Application
import com.jakewharton.threetenabp.AndroidThreeTen
import im.vector.matrix.android.BuildConfig
import im.vector.riotredesign.core.di.AppModule
import org.koin.log.EmptyLogger
@ -14,6 +15,7 @@ class Riot : Application() {
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
AndroidThreeTen.init(this)
startKoin(listOf(AppModule(this)), logger = EmptyLogger())
}


View File

@ -0,0 +1,12 @@
package im.vector.riotredesign.core.extensions

import im.vector.matrix.android.api.session.events.model.Event
import org.threeten.bp.Instant
import org.threeten.bp.LocalDateTime
import org.threeten.bp.ZoneOffset


fun Event.localDateTime(): LocalDateTime {
val instant = Instant.ofEpochMilli(originServerTs ?: 0)
return LocalDateTime.ofInstant(instant, ZoneOffset.UTC)
}

View File

@ -0,0 +1,16 @@
package im.vector.riotredesign.features.home.room.detail

import android.widget.TextView
import im.vector.riotredesign.R
import im.vector.riotredesign.core.epoxy.KotlinModel

data class TimelineDaySeparatorItem(
val formattedDay: CharSequence
) : KotlinModel(R.layout.item_event_day_separator) {

private val dayTextView by bind<TextView>(R.id.itemDayTextView)

override fun bind() {
dayTextView.text = formattedDay
}
}

View File

@ -9,14 +9,17 @@ import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.events.model.roomMember
import im.vector.matrix.android.api.session.room.model.MessageContent
import im.vector.riotredesign.core.extensions.avatarDrawable
import im.vector.riotredesign.core.extensions.localDateTime
import im.vector.riotredesign.features.home.LoadingItemModel_
import org.threeten.bp.format.DateTimeFormatter
import org.threeten.bp.format.FormatStyle

class TimelineEventController(private val context: Context) : EpoxyController(
EpoxyAsyncUtil.getAsyncBackgroundHandler(),
EpoxyAsyncUtil.getAsyncBackgroundHandler()
) {

private val messagesLoadedWithInformation = HashSet<String?>()
private val messagesDisplayedWithInformation = HashSet<String?>()

private val pagedListCallback = object : PagedList.Callback() {
override fun onChanged(position: Int, count: Int) {
@ -57,22 +60,34 @@ class TimelineEventController(private val context: Context) : EpoxyController(
if (messageContent == null || roomMember == null) {
continue
}

val date = event.root.localDateTime()
val nextDate = nextEvent?.root?.localDateTime()
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()

val nextRoomMember = nextEvent?.roomMember()
if (nextRoomMember != roomMember) {
messagesLoadedWithInformation.add(event.root.eventId)
if (addDaySeparator || nextRoomMember != roomMember) {
messagesDisplayedWithInformation.add(event.root.eventId)
}
val showInformation = messagesLoadedWithInformation.contains(event.root.eventId)
val showInformation = messagesDisplayedWithInformation.contains(event.root.eventId)

val avatarDrawable = context.avatarDrawable(roomMember.displayName ?: "")
TimelineMessageItem(
message = messageContent.body,
showInformation = showInformation,
time = date.toLocalTime().format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)),
avatarDrawable = avatarDrawable,
memberName = roomMember.displayName
)
.onBind { timeline?.loadAround(index) }
.id(event.root.eventId)
.addTo(this)


if (addDaySeparator) {
val formattedDay = date.toLocalDate().format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))
TimelineDaySeparatorItem(formattedDay).id(formattedDay).addTo(this)
}
}
}


View File

@ -1,10 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:padding="16dp">
android:padding="8dp">

<View
android:id="@+id/itemDayLineViewLeft"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@color/pale_grey_two"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/itemDayTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<TextView
android:id="@+id/itemDayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/light_grey_blue"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@tools:sample/date/day_of_week" />

<View
android:id="@+id/itemDayLineViewRight"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:background="@color/pale_grey_two"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/itemDayTextView"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>