Fix test compilation
This commit is contained in:
parent
b6606577d5
commit
d687acf57f
5 changed files with 15 additions and 33 deletions
|
|
@ -16,10 +16,8 @@
|
||||||
|
|
||||||
package io.element.android.libraries.dateformatter.impl
|
package io.element.android.libraries.dateformatter.impl
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.text.format.DateFormat
|
import android.text.format.DateFormat
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import io.element.android.libraries.di.ApplicationContext
|
|
||||||
import kotlinx.datetime.Clock
|
import kotlinx.datetime.Clock
|
||||||
import kotlinx.datetime.LocalDateTime
|
import kotlinx.datetime.LocalDateTime
|
||||||
import kotlinx.datetime.TimeZone
|
import kotlinx.datetime.TimeZone
|
||||||
|
|
@ -32,46 +30,30 @@ import java.util.Locale
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.math.absoluteValue
|
import kotlin.math.absoluteValue
|
||||||
|
|
||||||
|
// TODO rework this date formatting
|
||||||
class DateFormatters @Inject constructor(
|
class DateFormatters @Inject constructor(
|
||||||
@ApplicationContext private val context: Context,
|
|
||||||
private val locale: Locale,
|
private val locale: Locale,
|
||||||
private val clock: Clock,
|
private val clock: Clock,
|
||||||
private val timeZone: TimeZone,
|
private val timeZone: TimeZone,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val hourFormatter by lazy {
|
private val onlyTimeFormatter: DateTimeFormatter by lazy {
|
||||||
if (DateFormat.is24HourFormat(context)) {
|
val pattern = DateFormat.getBestDateTimePattern(locale, "HH:mm") ?: "HH:mm"
|
||||||
DateTimeFormatter.ofPattern("HH:mm", locale)
|
DateTimeFormatter.ofPattern(pattern)
|
||||||
} else {
|
|
||||||
DateTimeFormatter.ofPattern("h:mm a", locale)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private val fullDateFormatter by lazy {
|
|
||||||
val pattern = if (DateFormat.is24HourFormat(context)) {
|
|
||||||
DateFormat.getBestDateTimePattern(locale, "EEE, d MMM yyyy HH:mm")
|
|
||||||
} else {
|
|
||||||
DateFormat.getBestDateTimePattern(locale, "EEE, d MMM yyyy h:mm a")
|
|
||||||
}
|
|
||||||
DateTimeFormatter.ofPattern(pattern, locale)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val dateWithMonthFormatter: DateTimeFormatter by lazy {
|
private val dateWithMonthFormatter: DateTimeFormatter by lazy {
|
||||||
val pattern = DateFormat.getBestDateTimePattern(locale, "d MMM")
|
val pattern = DateFormat.getBestDateTimePattern(locale, "d MMM") ?: "d MMM"
|
||||||
DateTimeFormatter.ofPattern(pattern)
|
DateTimeFormatter.ofPattern(pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val dateWithYearFormatter: DateTimeFormatter by lazy {
|
private val dateWithYearFormatter: DateTimeFormatter by lazy {
|
||||||
val pattern = DateFormat.getBestDateTimePattern(locale, "d MMM y")
|
val pattern = DateFormat.getBestDateTimePattern(locale, "dd.MM.yyyy") ?: "dd.MM.yyyy"
|
||||||
DateTimeFormatter.ofPattern(pattern)
|
DateTimeFormatter.ofPattern(pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun formatFullDate(localDateTime: LocalDateTime): String {
|
internal fun formatTime(localDateTime: LocalDateTime): String {
|
||||||
return fullDateFormatter.format(localDateTime.toJavaLocalDateTime())
|
return onlyTimeFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||||
}
|
|
||||||
|
|
||||||
internal fun formatHour(localDateTime: LocalDateTime): String {
|
|
||||||
return hourFormatter.format(localDateTime.toJavaLocalDateTime())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun formatDateWithMonth(localDateTime: LocalDateTime): String {
|
internal fun formatDateWithMonth(localDateTime: LocalDateTime): String {
|
||||||
|
|
@ -103,6 +85,6 @@ class DateFormatters @Inject constructor(
|
||||||
clock.now().toEpochMilliseconds(),
|
clock.now().toEpochMilliseconds(),
|
||||||
DateUtils.DAY_IN_MILLIS,
|
DateUtils.DAY_IN_MILLIS,
|
||||||
DateUtils.FORMAT_SHOW_WEEKDAY
|
DateUtils.FORMAT_SHOW_WEEKDAY
|
||||||
).toString()
|
)?.toString() ?: ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ class DefaultLastMessageFormatter @Inject constructor(
|
||||||
val isSameDay = currentDate.date == dateToFormat.date
|
val isSameDay = currentDate.date == dateToFormat.date
|
||||||
return when {
|
return when {
|
||||||
isSameDay -> {
|
isSameDay -> {
|
||||||
dateFormatters.formatHour(dateToFormat)
|
dateFormatters.formatTime(dateToFormat)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
dateFormatters.formatDate(
|
dateFormatters.formatDate(
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,8 @@ class DefaultLastMessageFormatterTest {
|
||||||
*/
|
*/
|
||||||
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageFormatter {
|
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageFormatter {
|
||||||
val clock = FakeClock().also { it.givenInstant(Instant.parse(currentDate)) }
|
val clock = FakeClock().also { it.givenInstant(Instant.parse(currentDate)) }
|
||||||
return DefaultLastMessageFormatter(clock, Locale.US, TimeZone.UTC)
|
val localDateTimeProvider = LocalDateTimeProvider(clock, TimeZone.UTC)
|
||||||
|
val dateFormatters = DateFormatters(Locale.US, clock, TimeZone.UTC)
|
||||||
|
return DefaultLastMessageFormatter(localDateTimeProvider, dateFormatters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ class MainActivity : ComponentActivity() {
|
||||||
val sessionId = matrixAuthenticationService.getLatestSessionId()!!
|
val sessionId = matrixAuthenticationService.getLatestSessionId()!!
|
||||||
matrixAuthenticationService.restoreSession(sessionId)
|
matrixAuthenticationService.restoreSession(sessionId)
|
||||||
}
|
}
|
||||||
RoomListScreen(context = applicationContext, matrixClient = matrixClient!!).Content(modifier)
|
RoomListScreen(matrixClient = matrixClient!!).Content(modifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package io.element.android.samples.minimal
|
package io.element.android.samples.minimal
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.DisposableEffect
|
import androidx.compose.runtime.DisposableEffect
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
|
@ -33,7 +32,6 @@ import kotlinx.datetime.TimeZone
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
class RoomListScreen(
|
class RoomListScreen(
|
||||||
private val context: Context,
|
|
||||||
private val matrixClient: MatrixClient
|
private val matrixClient: MatrixClient
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
|
@ -41,7 +39,7 @@ class RoomListScreen(
|
||||||
private val locale = Locale.getDefault()
|
private val locale = Locale.getDefault()
|
||||||
private val timeZone = TimeZone.currentSystemDefault()
|
private val timeZone = TimeZone.currentSystemDefault()
|
||||||
private val dateTimeProvider = LocalDateTimeProvider(clock, timeZone)
|
private val dateTimeProvider = LocalDateTimeProvider(clock, timeZone)
|
||||||
private val dateFormatters = DateFormatters(context, locale, clock, timeZone)
|
private val dateFormatters = DateFormatters(locale, clock, timeZone)
|
||||||
private val presenter = RoomListPresenter(matrixClient, DefaultLastMessageFormatter(dateTimeProvider, dateFormatters))
|
private val presenter = RoomListPresenter(matrixClient, DefaultLastMessageFormatter(dateTimeProvider, dateFormatters))
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue