mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
feat(android): bring the Android app level with the iOS app
Everything the iOS shell gained, built natively for Android 16 with Material You, and back to Android 7: - Home-screen widgets in Jetpack Glance: trip, today, bookings, to-dos, packing, world map and travel group, from the same snapshot the iOS widgets read. Wallpaper colours on Android 12+, resizable in three layouts, a trip picker per widget (reconfigurable), live previews in Android 15's picker and preview images before it. - The travel day as a Live Update: a promoted ongoing notification with the next stop as the status-bar chip, a countdown and the day's stops as segments (ProgressStyle), a quiet ongoing notification before Android 16. - A share sheet of its own: a Material bottom sheet with the trip picker for files, mails, pages and links shared from any app, and the coming trips as launcher and direct-share shortcuts. - Push: channels per kind of notification (chat, trip updates, invitations, general) by the event the server now names, stacked per channel. - Location for navigator.geolocation from the fused provider, a store for Mapbox's map resources (30 days, 250 MB), Google Play Billing behind the StoreKit methods, and Google Wallet save links. - Large screens: every orientation and window size without a restart, the window's size classes and a foldable's posture and hinge for the UI (windowLayout), the UI's keyboard shortcuts as the app's (keyCommands), predictive back, per-app language. - The native strings in the 28 languages of the iOS app, generated from its strings files (scripts/generate-android-strings.mjs). Debug builds carry a widget gallery that draws every widget at every size.
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package com.liketrek.trek
|
||||
|
||||
import android.view.KeyEvent
|
||||
import com.android.billingclient.api.Purchase
|
||||
import com.liketrek.trek.billing.TrekBilling
|
||||
import com.liketrek.trek.screen.TrekKeyCommands
|
||||
import com.liketrek.trek.screen.TrekWindowLayout
|
||||
import com.liketrek.trek.share.ShareHandoff
|
||||
import com.liketrek.trek.share.ShareItem
|
||||
import com.liketrek.trek.share.ShareLoader
|
||||
import java.time.LocalDate
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
/** The native side's own decisions, apart from the views. */
|
||||
class NativeLogicTest {
|
||||
@Test
|
||||
fun pushesLandInTheChannelOfTheirEvent() {
|
||||
assertEquals(TrekPushService.CHANNEL_CHAT, TrekPushService.channelFor("collab_message"))
|
||||
assertEquals(TrekPushService.CHANNEL_TRIPS, TrekPushService.channelFor("booking_change"))
|
||||
assertEquals(TrekPushService.CHANNEL_TRIPS, TrekPushService.channelFor("todo_due"))
|
||||
assertEquals(TrekPushService.CHANNEL_INVITES, TrekPushService.channelFor("trip_invite"))
|
||||
assertEquals(TrekPushService.CHANNEL, TrekPushService.channelFor("version_available"))
|
||||
// A server too old to name the event: the general channel.
|
||||
assertEquals(TrekPushService.CHANNEL, TrekPushService.channelFor(null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sharedFilesAreSortedByWhatTheyAre() {
|
||||
assertEquals(ShareItem.Kind.PASS to "application/vnd.apple.pkpass", ShareLoader.classify("Boarding.pkpass", "application/octet-stream"))
|
||||
assertEquals(ShareItem.Kind.PDF to "application/pdf", ShareLoader.classify("ticket", "application/pdf"))
|
||||
assertEquals(ShareItem.Kind.MAIL to "message/rfc822", ShareLoader.classify("Booking.eml", "application/octet-stream"))
|
||||
assertEquals(ShareItem.Kind.CALENDAR to "text/calendar", ShareLoader.classify("invite.ics", ""))
|
||||
assertEquals(ShareItem.Kind.IMAGE to "image/png", ShareLoader.classify("shot.png", "image/png"))
|
||||
assertEquals(ShareItem.Kind.FILE to "application/zip", ShareLoader.classify("a.zip", "application/zip"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSharedLinkIsFetchedOnlyWhenTheTextIsLittleMore() {
|
||||
assertEquals("https://example.com/booking/1", ShareLoader.firstLink("Your booking: https://example.com/booking/1."))
|
||||
assertNull(ShareLoader.firstLink("no link here"))
|
||||
assertNull(ShareLoader.firstLink("x".repeat(400) + " https://example.com/a"))
|
||||
assertEquals("Hotel Kanra", ShareLoader.pageTitle("<html><head><TITLE> Hotel Kanra </TITLE></head></html>"))
|
||||
assertEquals("hotel-kanra-kyoto", ShareLoader.slug("Hotel Kanra, Kyoto!"))
|
||||
assertEquals("page", ShareLoader.slug("!!!"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theShareSheetOffersComingTripsFirstThenRecentPastOnes() {
|
||||
val today = LocalDate.of(2026, 11, 7)
|
||||
fun trip(id: Long, start: String, end: String) =
|
||||
ShareHandoff.Trip(id, "T$id", LocalDate.parse(start), LocalDate.parse(end), null, null)
|
||||
val past = (1L..8L).map { trip(100 + it, "2026-0$it-01", "2026-0$it-05") }
|
||||
val ordered = ShareHandoff.order(
|
||||
listOf(trip(1, "2026-12-01", "2026-12-05"), trip(2, "2026-11-05", "2026-11-09")) + past,
|
||||
today,
|
||||
)
|
||||
assertEquals(listOf(2L, 1L), ordered.take(2).map { it.id })
|
||||
assertEquals(8, ordered.size)
|
||||
assertEquals(108L, ordered[2].id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keyCommandsFollowTheUi() {
|
||||
TrekKeyCommands.set(
|
||||
JSONArray(
|
||||
"""[{"id":"search","title":"Search","input":"k","modifiers":["command"]},
|
||||
{"id":"back","title":"Back","input":"[","modifiers":["command"]},
|
||||
{"id":"close","title":"Close","input":"escape","modifiers":[]},
|
||||
{"title":"no id","input":"x"}]""",
|
||||
),
|
||||
)
|
||||
assertEquals(3, TrekKeyCommands.current.size)
|
||||
assertEquals("search", TrekKeyCommands.match(KeyEvent.KEYCODE_K, KeyEvent.META_CTRL_ON)?.id)
|
||||
// A Mac keyboard's Command key is Meta here, and counts as Ctrl.
|
||||
assertEquals("search", TrekKeyCommands.match(KeyEvent.KEYCODE_K, KeyEvent.META_META_ON)?.id)
|
||||
assertNull(TrekKeyCommands.match(KeyEvent.KEYCODE_K, 0))
|
||||
assertNull(TrekKeyCommands.match(KeyEvent.KEYCODE_K, KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON))
|
||||
assertEquals("back", TrekKeyCommands.match(KeyEvent.KEYCODE_LEFT_BRACKET, KeyEvent.META_CTRL_ON)?.id)
|
||||
assertEquals("close", TrekKeyCommands.match(KeyEvent.KEYCODE_ESCAPE, 0)?.id)
|
||||
TrekKeyCommands.set(null)
|
||||
assertEquals(0, TrekKeyCommands.current.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun windowSizeClassesUseAndroidsBreakpoints() {
|
||||
assertEquals("compact", TrekWindowLayout.sizeClass(411f, 600f, 840f))
|
||||
assertEquals("medium", TrekWindowLayout.sizeClass(673f, 600f, 840f))
|
||||
assertEquals("expanded", TrekWindowLayout.sizeClass(1280f, 600f, 840f))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aRestoreTakesTheNewestActiveCloudSubscription() {
|
||||
fun purchase(product: String, time: Long, state: Int) = Purchase(
|
||||
JSONObject()
|
||||
.put("productIds", JSONArray().put(product))
|
||||
.put("purchaseTime", time)
|
||||
.put("purchaseState", state)
|
||||
.put("purchaseToken", "token-$product-$time")
|
||||
.toString(),
|
||||
"sig",
|
||||
)
|
||||
val found = TrekBilling.newest(
|
||||
listOf(
|
||||
purchase("cloud.start.monthly", 1000, 0),
|
||||
purchase("cloud.plus.yearly", 2000, 0),
|
||||
purchase("cloud.pro.yearly", 3000, 4),
|
||||
purchase("other.thing", 4000, 0),
|
||||
),
|
||||
"cloud.",
|
||||
)
|
||||
assertEquals("token-cloud.plus.yearly-2000", found?.purchaseToken)
|
||||
assertNull(TrekBilling.newest(emptyList(), "cloud."))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.liketrek.trek.widget
|
||||
|
||||
import java.time.LocalDate
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class WidgetSnapshotTest {
|
||||
private val today = LocalDate.of(2026, 11, 7)
|
||||
|
||||
@Test
|
||||
fun readsTheSampleTheIosWidgetsReadToo() {
|
||||
val snapshot = WidgetSnapshot.parse(WidgetSamples.json(running = true, now = today))!!
|
||||
assertEquals("Autumn in Kyoto", snapshot.trip!!.title)
|
||||
assertEquals("/trips/1", snapshot.trip!!.path)
|
||||
assertEquals(5, snapshot.today!!.stops.size)
|
||||
assertEquals(810, snapshot.today!!.stops[2].minutes)
|
||||
assertEquals("leaf.fill", snapshot.today!!.stops[2].symbol)
|
||||
assertEquals(5, snapshot.bookings.size)
|
||||
assertTrue(snapshot.bookings[0].confirmed)
|
||||
assertEquals(2, snapshot.packing!!.bags.size)
|
||||
assertEquals(23000, snapshot.packing!!.bags[0].limit)
|
||||
assertEquals(23, snapshot.atlas!!.count)
|
||||
assertEquals("Nara", snapshot.collab!!.poll!!.options[0].label)
|
||||
assertTrue(snapshot.collab!!.messages.last().mine)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun everythingPastTripTodayAndLabelsIsOptional() {
|
||||
val snapshot = WidgetSnapshot.parse("""{"trip":null,"today":null,"labels":{"nextTrip":"Nächste Reise"}}""")!!
|
||||
assertNull(snapshot.trip)
|
||||
assertNull(snapshot.weather)
|
||||
assertTrue(snapshot.bookings.isEmpty())
|
||||
assertEquals("Nächste Reise", snapshot.labels.nextTrip)
|
||||
// A label the server's UI does not send yet falls back to English.
|
||||
assertEquals("Travel group", snapshot.labels.group)
|
||||
assertEquals("Today", snapshot.labels.todayShort)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun brokenJsonIsNoSnapshot() {
|
||||
assertNull(WidgetSnapshot.parse("{"))
|
||||
assertNull(WidgetSnapshot.parse(""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun countsTheDaysToTheTripAndItsLength() {
|
||||
val snapshot = WidgetSnapshot.parse(WidgetSamples.json(running = false, now = today))!!
|
||||
assertEquals(12L, snapshot.daysUntilStart(today))
|
||||
assertEquals(9, snapshot.tripLength)
|
||||
val noCount = WidgetSnapshot.parse("""{"trip":{"title":"T","start":"2026-11-10","end":"2026-11-12","path":"/trips/2"},"labels":{}}""")!!
|
||||
assertEquals(3, noCount.tripLength)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aTapOpensThePathInTheApp() {
|
||||
assertEquals("com.liketrek.trek://navigate/trips/1", WidgetSnapshot.link("/trips/1"))
|
||||
assertEquals("com.liketrek.trek://navigate/atlas", WidgetSnapshot.link("atlas"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readsTheTripListAndSkipsWhatIsNotATrip() {
|
||||
val trips = TripRef.parseList("""[{"id":1,"title":"A","start":"2026-11-01","end":"2026-11-03"},{"title":"no id"},3]""")
|
||||
assertEquals(1, trips.size)
|
||||
assertEquals(1L, trips[0].id)
|
||||
assertTrue(TripRef.parseList("not json").isEmpty())
|
||||
assertTrue(TripRef.parseList(null).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keysAreTheOnesTheUiWrites() {
|
||||
assertTrue(WidgetStore.isValidKey("snapshot"))
|
||||
assertTrue(WidgetStore.isValidKey("trips"))
|
||||
assertTrue(WidgetStore.isValidKey("trip.42"))
|
||||
assertFalse(WidgetStore.isValidKey("trip.../x"))
|
||||
assertFalse(WidgetStore.isValidKey("../snapshot"))
|
||||
assertFalse(WidgetStore.isValidKey("trip.1234567890123"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theWorldGridHasItsShape() {
|
||||
assertEquals(34, WorldGrid.ROWS.size)
|
||||
WorldGrid.ROWS.forEach { assertEquals(WorldGrid.COLUMNS * 2, it.length) }
|
||||
assertNotNull((0 until 34).flatMap { r -> (0 until WorldGrid.COLUMNS).mapNotNull { WorldGrid.code(r, it) } }.firstOrNull { it == "JP" })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aTripKeepsItsColoursAcrossLaunches() {
|
||||
assertEquals(WidgetArt.seed("Autumn in Kyoto"), WidgetArt.seed("Autumn in Kyoto"))
|
||||
assertFalse(WidgetArt.seed("Lisbon") == WidgetArt.seed("Kyoto"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bookingKindsHaveTheirSymbol() {
|
||||
assertEquals("airplane", TrekIcons.booking("flight").first)
|
||||
assertEquals("ferry.fill", TrekIcons.booking("cruise").first)
|
||||
assertEquals("bookmark.fill", TrekIcons.booking("something").first)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user