Browse Source

Fix test compilation

misc/jme/add-logging-to-state-machine
ganfra 2 years ago
parent
commit
c362c7b069
  1. 36
      libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DateFormatters.kt
  2. 2
      libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt
  3. 4
      libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt
  4. 2
      samples/minimal/src/main/kotlin/io/element/android/samples/minimal/MainActivity.kt
  5. 4
      samples/minimal/src/main/kotlin/io/element/android/samples/minimal/RoomListScreen.kt

36
libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DateFormatters.kt

@ -16,10 +16,8 @@ @@ -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 @@ -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( @@ -103,6 +85,6 @@ class DateFormatters @Inject constructor(
clock.now().toEpochMilliseconds(),
DateUtils.DAY_IN_MILLIS,
DateUtils.FORMAT_SHOW_WEEKDAY
).toString()
)?.toString() ?: ""
}
}

2
libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt

@ -34,7 +34,7 @@ class DefaultLastMessageFormatter @Inject constructor( @@ -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(

4
libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt

@ -101,6 +101,8 @@ class DefaultLastMessageFormatterTest { @@ -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)
}
}

2
samples/minimal/src/main/kotlin/io/element/android/samples/minimal/MainActivity.kt

@ -74,7 +74,7 @@ class MainActivity : ComponentActivity() { @@ -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)
}
}
}

4
samples/minimal/src/main/kotlin/io/element/android/samples/minimal/RoomListScreen.kt

@ -16,7 +16,6 @@ @@ -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 @@ -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( @@ -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

Loading…
Cancel
Save