Fix test compilation

This commit is contained in:
ganfra
2023-03-01 11:11:09 +01:00
parent b6606577d5
commit d687acf57f
5 changed files with 15 additions and 33 deletions

View File

@@ -16,10 +16,8 @@
package io.element.android.libraries.dateformatter.impl
import android.content.Context
import android.text.format.DateFormat
import android.text.format.DateUtils
import io.element.android.libraries.di.ApplicationContext
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
@@ -32,46 +30,30 @@ import java.util.Locale
import javax.inject.Inject
import kotlin.math.absoluteValue
// TODO rework this date formatting
class DateFormatters @Inject constructor(
@ApplicationContext private val context: Context,
private val locale: Locale,
private val clock: Clock,
private val timeZone: TimeZone,
) {
private val hourFormatter by lazy {
if (DateFormat.is24HourFormat(context)) {
DateTimeFormatter.ofPattern("HH:mm", locale)
} 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 onlyTimeFormatter: DateTimeFormatter by lazy {
val pattern = DateFormat.getBestDateTimePattern(locale, "HH:mm") ?: "HH:mm"
DateTimeFormatter.ofPattern(pattern)
}
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)
}
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)
}
internal fun formatFullDate(localDateTime: LocalDateTime): String {
return fullDateFormatter.format(localDateTime.toJavaLocalDateTime())
}
internal fun formatHour(localDateTime: LocalDateTime): String {
return hourFormatter.format(localDateTime.toJavaLocalDateTime())
internal fun formatTime(localDateTime: LocalDateTime): String {
return onlyTimeFormatter.format(localDateTime.toJavaLocalDateTime())
}
internal fun formatDateWithMonth(localDateTime: LocalDateTime): String {
@@ -103,6 +85,6 @@ class DateFormatters @Inject constructor(
clock.now().toEpochMilliseconds(),
DateUtils.DAY_IN_MILLIS,
DateUtils.FORMAT_SHOW_WEEKDAY
).toString()
)?.toString() ?: ""
}
}

View File

@@ -34,7 +34,7 @@ class DefaultLastMessageFormatter @Inject constructor(
val isSameDay = currentDate.date == dateToFormat.date
return when {
isSameDay -> {
dateFormatters.formatHour(dateToFormat)
dateFormatters.formatTime(dateToFormat)
}
else -> {
dateFormatters.formatDate(

View File

@@ -101,6 +101,8 @@ class DefaultLastMessageFormatterTest {
*/
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageFormatter {
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)
}
}

View File

@@ -74,7 +74,7 @@ class MainActivity : ComponentActivity() {
val sessionId = matrixAuthenticationService.getLatestSessionId()!!
matrixAuthenticationService.restoreSession(sessionId)
}
RoomListScreen(context = applicationContext, matrixClient = matrixClient!!).Content(modifier)
RoomListScreen(matrixClient = matrixClient!!).Content(modifier)
}
}
}

View File

@@ -16,7 +16,6 @@
package io.element.android.samples.minimal
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Modifier
@@ -33,7 +32,6 @@ import kotlinx.datetime.TimeZone
import java.util.Locale
class RoomListScreen(
private val context: Context,
private val matrixClient: MatrixClient
) {
@@ -41,7 +39,7 @@ class RoomListScreen(
private val locale = Locale.getDefault()
private val timeZone = TimeZone.currentSystemDefault()
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))
@Composable