-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2414 from HedvigInsurance/feature/travel-addon-de…
…eplink GEN-3226 Feature/travel addon deeplink
- Loading branch information
Showing
8 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...otlin/com/hedvig/android/feature/addon/purchase/ui/triage/TravelAddonTriageDestination.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package com.hedvig.android.feature.addon.purchase.ui.triage | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.WindowInsets | ||
import androidx.compose.foundation.layout.WindowInsetsSides | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.only | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.safeDrawing | ||
import androidx.compose.foundation.layout.windowInsetsPadding | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.PreviewParameter | ||
import androidx.compose.ui.tooling.preview.datasource.CollectionPreviewParameterProvider | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import androidx.lifecycle.compose.dropUnlessResumed | ||
import com.hedvig.android.design.system.hedvig.ButtonDefaults.ButtonSize.Large | ||
import com.hedvig.android.design.system.hedvig.HedvigErrorSection | ||
import com.hedvig.android.design.system.hedvig.HedvigFullScreenCenterAlignedProgress | ||
import com.hedvig.android.design.system.hedvig.HedvigPreview | ||
import com.hedvig.android.design.system.hedvig.HedvigTextButton | ||
import com.hedvig.android.design.system.hedvig.HedvigTheme | ||
import com.hedvig.android.design.system.hedvig.Surface | ||
import com.hedvig.android.feature.addon.purchase.ui.triage.TravelAddonTriageState.Failure | ||
import com.hedvig.android.feature.addon.purchase.ui.triage.TravelAddonTriageState.Loading | ||
import com.hedvig.android.feature.addon.purchase.ui.triage.TravelAddonTriageState.Success | ||
import hedvig.resources.R | ||
|
||
@Composable | ||
internal fun TravelAddonTriageDestination( | ||
viewModel: TravelAddonTriageViewModel, | ||
popBackStack: () -> Unit, | ||
launchFlow: (insuranceIds: List<String>) -> Unit, | ||
onNavigateToNewConversation: () -> Unit, | ||
) { | ||
val uiState: TravelAddonTriageState by viewModel.uiState.collectAsStateWithLifecycle() | ||
StartChangeTierFlowScreen( | ||
uiState = uiState, | ||
reload = { | ||
viewModel.emit(TravelAddonTriageEvent.Reload) | ||
}, | ||
launchFlow = launchFlow, | ||
onNavigateToNewConversation = onNavigateToNewConversation, | ||
popBackStack = popBackStack, | ||
) | ||
} | ||
|
||
@Composable | ||
private fun StartChangeTierFlowScreen( | ||
uiState: TravelAddonTriageState, | ||
reload: () -> Unit, | ||
popBackStack: () -> Unit, | ||
launchFlow: (List<String>) -> Unit, | ||
onNavigateToNewConversation: () -> Unit, | ||
) { | ||
Surface( | ||
color = HedvigTheme.colorScheme.backgroundPrimary, | ||
modifier = Modifier.fillMaxSize(), | ||
) { | ||
when (uiState) { | ||
is Failure -> { | ||
FailureScreen( | ||
reload = reload, | ||
popBackStack = popBackStack, | ||
reason = uiState.reason, | ||
onNavigateToNewConversation = onNavigateToNewConversation, | ||
) | ||
} | ||
|
||
Loading -> HedvigFullScreenCenterAlignedProgress() | ||
|
||
is Success -> { | ||
LaunchedEffect(uiState.insuranceIds) { | ||
val params = uiState.insuranceIds | ||
launchFlow(params) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun FailureScreen( | ||
reload: () -> Unit, | ||
popBackStack: () -> Unit, | ||
reason: FailureReason, | ||
onNavigateToNewConversation: () -> Unit, | ||
) { | ||
Box(Modifier.fillMaxSize()) { | ||
when (reason) { | ||
FailureReason.GENERAL -> { | ||
HedvigErrorSection( | ||
onButtonClick = reload, | ||
modifier = Modifier.fillMaxSize(), | ||
) | ||
} | ||
|
||
FailureReason.NO_TRAVEL_ADDON_AVAILABLE -> { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(horizontal = 16.dp) | ||
.windowInsetsPadding( | ||
WindowInsets.safeDrawing.only( | ||
WindowInsetsSides.Horizontal + | ||
WindowInsetsSides.Bottom, | ||
), | ||
), | ||
) { | ||
Spacer(Modifier.weight(1f)) | ||
val buttonText = stringResource(R.string.open_chat) | ||
|
||
HedvigErrorSection( | ||
onButtonClick = onNavigateToNewConversation, | ||
subTitle = stringResource(R.string.GENERAL_ERROR_BODY), | ||
modifier = Modifier.fillMaxSize(), | ||
buttonText = buttonText, | ||
) | ||
Spacer(Modifier.weight(1f)) | ||
HedvigTextButton( | ||
stringResource(R.string.general_close_button), | ||
onClick = dropUnlessResumed { popBackStack() }, | ||
buttonSize = Large, | ||
modifier = Modifier.fillMaxWidth(), | ||
) | ||
Spacer(Modifier.height(32.dp)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@HedvigPreview | ||
@Composable | ||
private fun StartTierFlowScreenPreview( | ||
@PreviewParameter(TravelAddonTriageStateProvider::class) state: TravelAddonTriageState, | ||
) { | ||
HedvigTheme { | ||
Surface(color = HedvigTheme.colorScheme.backgroundPrimary) { | ||
StartChangeTierFlowScreen( | ||
uiState = state, | ||
{}, | ||
{}, | ||
{}, | ||
{}, | ||
) | ||
} | ||
} | ||
} | ||
|
||
internal class TravelAddonTriageStateProvider : | ||
CollectionPreviewParameterProvider<TravelAddonTriageState>( | ||
listOf( | ||
TravelAddonTriageState.Loading, | ||
Success( | ||
listOf("id", "id2"), | ||
), | ||
Failure(FailureReason.GENERAL), | ||
Failure(FailureReason.NO_TRAVEL_ADDON_AVAILABLE), | ||
), | ||
) |
80 changes: 80 additions & 0 deletions
80
.../kotlin/com/hedvig/android/feature/addon/purchase/ui/triage/TravelAddonTriageViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.hedvig.android.feature.addon.purchase.ui.triage | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.hedvig.android.core.common.ErrorMessage | ||
import com.hedvig.android.data.addons.data.GetTravelAddonBannerInfoUseCase | ||
import com.hedvig.android.data.addons.data.TravelAddonBannerInfo | ||
import com.hedvig.android.data.addons.data.TravelAddonBannerSource | ||
import com.hedvig.android.molecule.android.MoleculeViewModel | ||
import com.hedvig.android.molecule.public.MoleculePresenter | ||
import com.hedvig.android.molecule.public.MoleculePresenterScope | ||
import kotlinx.coroutines.flow.first | ||
|
||
internal class TravelAddonTriageViewModel( | ||
getTravelAddonBannerInfoUseCase: GetTravelAddonBannerInfoUseCase, | ||
) : MoleculeViewModel<TravelAddonTriageEvent, TravelAddonTriageState>( | ||
initialState = TravelAddonTriageState.Loading, | ||
presenter = TravelAddonTriagePresenter(getTravelAddonBannerInfoUseCase), | ||
) | ||
|
||
internal class TravelAddonTriagePresenter( | ||
private val getTravelAddonBannerInfoUseCase: GetTravelAddonBannerInfoUseCase, | ||
) : MoleculePresenter<TravelAddonTriageEvent, TravelAddonTriageState> { | ||
@Composable | ||
override fun MoleculePresenterScope<TravelAddonTriageEvent>.present( | ||
lastState: TravelAddonTriageState, | ||
): TravelAddonTriageState { | ||
var currentState by remember { mutableStateOf(lastState) } | ||
var loadIteration by remember { mutableIntStateOf(0) } | ||
|
||
LaunchedEffect(loadIteration) { | ||
currentState = TravelAddonTriageState.Loading | ||
val result = getTravelAddonBannerInfoUseCase.invoke(TravelAddonBannerSource.TRAVEL_CERTIFICATES) | ||
result.first().fold( | ||
ifLeft = { left: ErrorMessage -> | ||
currentState = TravelAddonTriageState.Failure(FailureReason.GENERAL) | ||
}, | ||
ifRight = { travelBannerInfo: TravelAddonBannerInfo? -> | ||
currentState = if (travelBannerInfo == null || travelBannerInfo.eligibleInsurancesIds.isEmpty()) { | ||
TravelAddonTriageState.Failure(FailureReason.NO_TRAVEL_ADDON_AVAILABLE) | ||
} else { | ||
TravelAddonTriageState.Success(travelBannerInfo.eligibleInsurancesIds) | ||
} | ||
}, | ||
) | ||
} | ||
|
||
CollectEvents { event -> | ||
when (event) { | ||
TravelAddonTriageEvent.Reload -> loadIteration++ | ||
} | ||
} | ||
|
||
return currentState | ||
} | ||
} | ||
|
||
internal sealed interface TravelAddonTriageState { | ||
data object Loading : TravelAddonTriageState | ||
|
||
data class Success( | ||
val insuranceIds: List<String>, | ||
) : TravelAddonTriageState | ||
|
||
data class Failure(val reason: FailureReason) : TravelAddonTriageState | ||
} | ||
|
||
internal sealed interface TravelAddonTriageEvent { | ||
data object Reload : TravelAddonTriageEvent | ||
} | ||
|
||
internal enum class FailureReason { | ||
GENERAL, | ||
NO_TRAVEL_ADDON_AVAILABLE, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters