Skip to content

Commit

Permalink
Alerts fix iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Dec 4, 2024
1 parent 653b610 commit 4a2c533
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package xyz.ksharma.krail
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import org.koin.compose.KoinApplication
import xyz.ksharma.krail.core.appinfo.LocalPlatformTypeProvider
import xyz.ksharma.krail.core.appinfo.LocalAppPlatformProvider
import xyz.ksharma.krail.core.appinfo.getAppPlatform
import xyz.ksharma.krail.di.koinConfig
import xyz.ksharma.krail.taj.theme.KrailTheme

@Composable
fun KrailApp() {
KoinApplication(application = koinConfig) {
CompositionLocalProvider(LocalPlatformTypeProvider provides getAppPlatform()) {
CompositionLocalProvider(LocalAppPlatformProvider provides getAppPlatform()) {
KrailTheme {
KrailNavHost()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package xyz.ksharma.krail.core.appinfo

import androidx.compose.runtime.staticCompositionLocalOf

val LocalPlatformTypeProvider = staticCompositionLocalOf { getAppPlatform() }
val LocalAppPlatformProvider = staticCompositionLocalOf { getAppPlatform() }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import kotlinx.serialization.json.Json
data class ServiceAlert(
val heading: String,
val message: String,
val priority: ServiceAlertPriority,
) {

fun toJsonString() = Json.encodeToString(serializer(), this)
Expand Down
7 changes: 4 additions & 3 deletions feature/trip-planner/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ kotlin {
sourceSets {
commonMain {
dependencies {
implementation(projects.taj)
implementation(projects.feature.tripPlanner.state)
implementation(projects.core.dateTime)
implementation(projects.sandook)
implementation(projects.core.appInfo)
implementation(projects.feature.tripPlanner.network)
implementation(projects.feature.tripPlanner.state)
implementation(projects.sandook)
implementation(projects.taj)

implementation(compose.foundation)
implementation(compose.animation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlert

internal fun NavGraphBuilder.alertsDestination(navController: NavHostController) {
composable<ServiceAlertRoute> { backStackEntry ->

val route = backStackEntry.toRoute<ServiceAlertRoute>()
val serviceAlerts = route.alertsJsonList.mapNotNull { alertJson ->
ServiceAlert.fromJsonString(alertJson)
}.toImmutableSet()

ServiceAlertScreen(serviceAlerts = serviceAlerts, onBackClick = {
navController.popBackStack()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import xyz.ksharma.krail.taj.theme.getForegroundColor
import xyz.ksharma.krail.taj.toAdaptiveSize
import xyz.ksharma.krail.trip.planner.ui.state.TransportMode
import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlert
import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlertPriority

@Composable
fun CollapsibleAlert(
Expand Down Expand Up @@ -141,7 +140,6 @@ private fun PreviewCollapsibleAlertCollapsed() {
serviceAlert = ServiceAlert(
heading = "Sample Alert",
message = "This is a sample alert message.",
priority = ServiceAlertPriority.HIGH,
),
index = 1,
onClick = {},
Expand All @@ -161,7 +159,6 @@ private fun PreviewCollapsibleAlertExpanded() {
serviceAlert = ServiceAlert(
heading = "Sample Alert",
message = "This is a sample alert message.",
priority = ServiceAlertPriority.HIGH,
),
collapsed = false,
onClick = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ import androidx.compose.ui.unit.dp
import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toImmutableList
import xyz.ksharma.krail.taj.components.Divider
import xyz.ksharma.krail.taj.components.Text
import xyz.ksharma.krail.taj.components.TitleBar
import xyz.ksharma.krail.taj.theme.KrailTheme
import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlert
import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlertPriority
import xyz.ksharma.krail.trip.planner.ui.timetable.ActionButton

@Composable
Expand Down Expand Up @@ -103,17 +101,14 @@ private fun PreviewServiceAlertScreen() {
ServiceAlert(
heading = "Service Alert 1",
message = "This is a service alert 1",
priority = ServiceAlertPriority.HIGH,
),
ServiceAlert(
heading = "Service Alert 2",
message = "This is a service alert 2",
priority = ServiceAlertPriority.MEDIUM,
),
ServiceAlert(
heading = "Service Alert 3",
message = "This is a service alert 3",
priority = ServiceAlertPriority.LOW,
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import krail.feature.trip_planner.ui.generated.resources.ic_a11y
import krail.feature.trip_planner.ui.generated.resources.ic_clock
import krail.feature.trip_planner.ui.generated.resources.ic_walk
import org.jetbrains.compose.resources.painterResource
import xyz.ksharma.krail.core.appinfo.LocalAppPlatformProvider
import xyz.ksharma.krail.core.appinfo.PlatformType
import xyz.ksharma.krail.taj.LocalContentAlpha
import xyz.ksharma.krail.taj.components.SeparatorIcon
import xyz.ksharma.krail.taj.components.Text
Expand Down Expand Up @@ -196,6 +198,8 @@ fun ExpandedJourneyCardContent(
onAlertClick: () -> Unit,
modifier: Modifier = Modifier,
) {
val appInfo = LocalAppPlatformProvider.current

Column(modifier = modifier) {
FlowRow(
modifier = Modifier
Expand Down Expand Up @@ -228,7 +232,11 @@ fun ExpandedJourneyCardContent(
},
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
if (totalUniqueServiceAlerts > 0) {
// TODO - display alerts only on Android as of now - ComposeNavigation
// to support iOS, we need to store them in db and use some id to fetch them and display.
// Current approach of passing them as json string does not work on iOS devices.
// See - https://youtrack.jetbrains.com/issue/CMP-7180/iOS-App-Crashes-when-navigating-with-html-content-as-string-argument.
if (totalUniqueServiceAlerts > 0 && appInfo.type == PlatformType.ANDROID) {
Box(
modifier = Modifier
.padding()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import xyz.ksharma.krail.trip.planner.ui.datetimeselector.dateTimeSelectorDestin
import xyz.ksharma.krail.trip.planner.ui.savedtrips.savedTripsDestination
import xyz.ksharma.krail.trip.planner.ui.searchstop.searchStopDestination
import xyz.ksharma.krail.trip.planner.ui.settings.settingsDestination
import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlert
import xyz.ksharma.krail.trip.planner.ui.timetable.timeTableDestination
import xyz.ksharma.krail.trip.planner.ui.usualride.usualRideDestination

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,22 +243,22 @@ class TimeTableViewModel(
_uiState.update(block)
}

fun fetchAlertsForJourney(journeyId: String, onResult: (Set<ServiceAlert>) -> Unit) {
fun fetchAlertsForJourney(journeyId: String, onResult: (List<ServiceAlert>) -> Unit) {
viewModelScope.launch {
val alerts = withContext(Dispatchers.IO) {
runCatching {
_uiState.value.journeyList.find { it.journeyId == journeyId }?.let { journey ->
getAlertsFromJourney(journey)
}.orEmpty()
}.getOrElse { emptySet() }
}.getOrElse { emptyList() }
}
onResult(alerts)
}
}

private fun getAlertsFromJourney(journey: TimeTableState.JourneyCardInfo): Set<ServiceAlert> {
private fun getAlertsFromJourney(journey: TimeTableState.JourneyCardInfo): List<ServiceAlert> {
return journey.legs.filterIsInstance<TimeTableState.JourneyCardInfo.Leg.TransportLeg>()
.flatMap { it.serviceAlertList.orEmpty() }.toSet()
.flatMap { it.serviceAlertList.orEmpty() }
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ fun TripResponse.Info.toAlert(): ServiceAlert? {
ServiceAlert(
heading = heading,
message = alertContent,
priority = alertPriority,
)
} else {
null
Expand Down

0 comments on commit 4a2c533

Please sign in to comment.