diff --git a/app/apollo/apollo-octopus-public/src/commonMain/graphql/com/hedvig/android/apollo/octopus/schema.graphqls b/app/apollo/apollo-octopus-public/src/commonMain/graphql/com/hedvig/android/apollo/octopus/schema.graphqls index 1ff9b132a4..8da9c82727 100644 --- a/app/apollo/apollo-octopus-public/src/commonMain/graphql/com/hedvig/android/apollo/octopus/schema.graphqls +++ b/app/apollo/apollo-octopus-public/src/commonMain/graphql/com/hedvig/android/apollo/octopus/schema.graphqls @@ -1945,6 +1945,31 @@ type CrossSell { Products to choose between, e.g. dog or cat for pet. Empty for a single product. """ options: [CrossSell!]! + """ + Set when this cross-sell can be bought natively in the app: pass it to `Query.inAppPurchaseForm`. + Null means open `storeUrl`, as for every cross-sell before the native flow. + """ + inAppProduct: InAppProduct +} +type CrossSellCarouselItem { + title: String! + subtitle: String + """ + The slide image, or the poster when the slide is a video + """ + image: StoryblokImageAsset! + """ + HEVC mp4 for iOS, set when the slide is a video; play it over the image + """ + videoUrlMp4: String + """ + VP9 webm for Android, same clip as videoUrlMp4 + """ + videoUrlWebm: String + """ + Replay the video while the slide is shown; otherwise play once and hold the last frame + """ + loop: Boolean! } """ A/B testing experiment (name and variant must be SCREAMING_SNAKE_CASE) @@ -1953,6 +1978,10 @@ input CrossSellExperimentInput { name: String! variant: String! } +type CrossSellHeroIntro { + title: String! + description: String! +} input CrossSellInput { userFlow: UserFlow! """ @@ -1960,7 +1989,8 @@ input CrossSellInput { """ flowSource: FlowSource """ - A/B test experiments for attribution tracking + A/B test experiments for attribution tracking. ACCIDENT_DISCOUNT with variant NO_DISCOUNT withholds the accident + cross-sell's discount. """ experiments: [CrossSellExperimentInput!]! """ @@ -1972,6 +2002,29 @@ input CrossSellInput { """ claimId: UUID } +type CrossSellProductInfo { + heroImage: StoryblokImageAsset! + """ + The intro text under the hero on the product's web page; null when the page has none + """ + heroIntro: CrossSellHeroIntro + """ + Selling points from the product's web page's image-text sections, in page order + """ + usps: [CrossSellUsp!]! + """ + Story-style slides from the product's web page, in page order + """ + carousel: [CrossSellCarouselItem!]! + """ + Set when the product can be bought natively in the app, as on `CrossSell.inAppProduct` + """ + inAppProduct: InAppProduct + """ + Questions and answers from the FAQ on the product's web page, in the member's language. Empty when it has none. + """ + faq: [ProductFaqItem!]! +} enum CrossSellSource { HOME CLOSED_CLAIM @@ -1980,6 +2033,10 @@ enum CrossSellSource { EDIT_COINSURED MOVING_FLOW } +type CrossSellUsp { + title: String! + description: String! +} type CrossSellV2 { """ Prioritized recommendation (null for INSURANCES) @@ -2712,6 +2769,144 @@ type IconVariants { light: IconVariant! dark: IconVariant! } +enum InAppProduct { + ACCIDENT + PAYMENT_PROTECTION + DOG + CAT + CAR + APARTMENT_RENT + APARTMENT_BRF + APARTMENT_STUDENT + HOUSE + VACATION_HOME +} +type InAppPurchaseEligibility { + isEligible: Boolean! + """ + Set when `isEligible` is false. + """ + reason: InAppPurchaseIneligibilityReason + """ + Set when `reason` is ALREADY_INSURED. + """ + existingContractId: UUID +} +type InAppPurchaseFieldError { + fieldId: ID! + """ + Localized message. + """ + message: String! +} +input InAppPurchaseFieldInput { + fieldId: ID! + """ + See `InAppPurchaseFormFieldType` for how many values each type takes and their format. + """ + values: [String!]! +} +""" +A single field to render in the form. Send its value back with `InAppPurchaseFieldInput.fieldId = id`. +""" +type InAppPurchaseFormField { + """ + Echo back when submitting. Clients shouldn't branch on it. + """ + id: ID! + type: InAppPurchaseFormFieldType! + """ + Localized label. + """ + title: String! + """ + Present for SINGLE_SELECT and SEARCHABLE_SELECT; for EXTRA_BUILDINGS, the building types. + """ + options: [InAppPurchaseFormFieldOption!] + """ + Values to prefill the field with. Empty means no default. + """ + defaultValues: [String!]! + """ + Present for NUMBER and DATE, in the same format as the value. + """ + minValue: String + maxValue: String + """ + SEARCHABLE_SELECT only: how many options may be chosen. Null means one. + """ + maxSelections: Int +} +type InAppPurchaseFormFieldOption { + value: String! + """ + Localized label. + """ + title: String! + """ + False: show the option, but it can't be selected. + """ + isSelectable: Boolean! + """ + Localized explanation, e.g. why the option can't be selected. + """ + additionalInfo: String +} +""" +Value format per type: NUMBER is an integer like "2", DATE is ISO "2026-11-01", SINGLE_SELECT is the +option's `value`, TEXT is free text. SEARCHABLE_SELECT sends one value per chosen option. EXTRA_BUILDINGS sends +one value per building, as JSON `{"type":"GARAGE","area":25,"hasWaterConnected":false}` with `type` one of the +field's options; no values means no buildings. Every other type takes exactly one value. +""" +enum InAppPurchaseFormFieldType { + NUMBER + DATE + SINGLE_SELECT + TEXT + SEARCHABLE_SELECT + EXTRA_BUILDINGS +} +type InAppPurchaseFormOutput { + product: Product! + eligibility: InAppPurchaseEligibility! + fields: [InAppPurchaseFormField!]! +} +enum InAppPurchaseIneligibilityReason { + ALREADY_INSURED + NOT_AVAILABLE +} +input InAppPurchaseQuoteInput { + product: InAppProduct! + fields: [InAppPurchaseFieldInput!]! + attribution: PriceIntentConfirmAttributionInput + """ + Discount code the member entered. Send it with every quote: each quote sets the discount from this input + alone, so leaving it out or sending an empty string removes a code sent earlier. + A valid code replaces the Accident discount the app's cross-sell banner promises, since only one discount + applies. An invalid code returns a field error with `fieldId: "campaignCode"` and no offers. + """ + campaignCode: String +} +type InAppPurchaseQuoteOutput { + """ + Null unless `offers` is non-empty. + """ + shopSessionId: UUID + offers: [ProductOffer!]! + """ + Problems with specific fields, to show next to them. Nothing was quoted when this is non-empty. + """ + fieldErrors: [InAppPurchaseFieldError!]! + """ + A problem that isn't about one field. + """ + userError: UserError +} +input InAppPurchaseSignInput { + shopSessionId: UUID! + offerId: UUID! + useBankIdSandbox: Boolean! = false +} scalar Instant type InsurableLimit { label: String! @@ -2971,6 +3166,10 @@ type Member { """ crossSellV2(input: CrossSellInput!): CrossSellV2! """ + Product details for a tapped cross-sell, in the Hedvig-Language of the request. Errors for an unknown id and for the generic HOME, PET and ALL cross-sells. + """ + crossSellProductInfo(crossSellId: ID!): CrossSellProductInfo! + """ Shop sessions started by this member that have not yet been signed and are not expired. Ordered by most recent activity first. """ @@ -4135,6 +4334,17 @@ type Mutation { """ productOffersCancellationRequestedUpdate(productOfferIds: [UUID!]!, requested: Boolean!): ProductOffersMutationOutput! """ + Submit the form from `inAppPurchaseForm` and get priced offers. + Call again after the member changes any value. Every call returns new offer ids, and only offers from the + latest response can be signed. + """ + inAppPurchaseQuote(input: InAppPurchaseQuoteInput!): InAppPurchaseQuoteOutput! + """ + Sign the chosen offer with Swedish BankID. Poll `Query.shopSessionSigning` with the returned signing id. + `useBankIdSandbox` behaves as in `Mutation.shopSessionStartSign`. + """ + inAppPurchaseSign(input: InAppPurchaseSignInput!): ShopSessionSigningMutationOutput! + """ Called to save insurance recommendation form and save recommendation email. """ insuranceGuide(input: InsuranceGuideInput!): InsuranceGuideOutput! @@ -4696,10 +4906,23 @@ type Product { """ multiple: Boolean """ + Questions and answers from the FAQ on the product's page on the website, in the member's language. + Empty when the page has none. + """ + faq: [ProductFaqItem!]! + """ A list of the external insurers valid for this specific product. """ externalInsurers: [ExternalInsurer!]! } +type ProductFaqItem { + question: String! + """ + Storyblok rich text document (`"type": "doc"`), rendered by the client. Links to other website pages are + absolute URLs. + """ + answer: JSON! +} type ProductOffer { """ The id of this item, which can be used to add to cart. @@ -5086,6 +5309,11 @@ type Query { shopSession(id: UUID!): ShopSession! carTrial(contractId: UUID!): TrialExtension """ + Screen 1 of the in-app purchase: product content, whether the member can buy it, and the form to fill in. + Has no side effects. When the member isn't eligible, `fields` is empty. + """ + inAppPurchaseForm(product: InAppProduct!): InAppPurchaseFormOutput! + """ Outcome of shopSession. Requires authentication Available forever, unlike shopSession which becomes hidden some time after creation """ diff --git a/app/app-desktop/build.gradle.kts b/app/app-desktop/build.gradle.kts index 5337cd30d5..f4bd29cd7a 100644 --- a/app/app-desktop/build.gradle.kts +++ b/app/app-desktop/build.gradle.kts @@ -87,6 +87,8 @@ kotlin { implementation(projects.featureHelpCenterNavigation) implementation(projects.featureHome) implementation(projects.featureImageViewer) + implementation(projects.featureInAppPurchase) + implementation(projects.featureInAppPurchaseNavigation) implementation(projects.featureInsuranceCertificate) implementation(projects.featureInsurances) implementation(projects.featureLogin) @@ -98,6 +100,7 @@ kotlin { implementation(projects.featurePayments) implementation(projects.featurePaymentsNavigation) implementation(projects.featurePayoutAccount) + implementation(projects.featureProductInApp) implementation(projects.featureProfile) implementation(projects.featureRemoveAddons) implementation(projects.featureTerminateInsurance) diff --git a/app/app-desktop/src/jvmTest/kotlin/com/hedvig/android/desktop/DesktopEntryProviderTest.kt b/app/app-desktop/src/jvmTest/kotlin/com/hedvig/android/desktop/DesktopEntryProviderTest.kt index afb43b89c4..f32d1c7768 100644 --- a/app/app-desktop/src/jvmTest/kotlin/com/hedvig/android/desktop/DesktopEntryProviderTest.kt +++ b/app/app-desktop/src/jvmTest/kotlin/com/hedvig/android/desktop/DesktopEntryProviderTest.kt @@ -12,6 +12,7 @@ import assertk.assertions.containsExactly import assertk.assertions.isEmpty import assertk.assertions.isTrue import assertk.assertions.startsWith +import com.hedvig.android.data.contract.CrossSell import com.hedvig.android.design.system.hedvig.GlobalSnackBarState import com.hedvig.android.feature.chat.navigation.ChatKey import com.hedvig.android.feature.chat.navigation.InboxKey @@ -165,6 +166,7 @@ private fun registerSharedEntries(scope: EntryProviderScope, graph DesktopAppHost(showNotice = {}), { _: String -> }, { _: String -> true }, + { _: CrossSell -> true }, graph.imageLoader, graph.languageService, graph.hedvigBuildConstants, diff --git a/app/app-shell/build.gradle.kts b/app/app-shell/build.gradle.kts index 4e3bd9a8b0..047cfdea2d 100644 --- a/app/app-shell/build.gradle.kts +++ b/app/app-shell/build.gradle.kts @@ -66,6 +66,8 @@ kotlin { implementation(projects.featureHelpCenterNavigation) implementation(projects.featureHome) implementation(projects.featureImageViewer) + implementation(projects.featureInAppPurchase) + implementation(projects.featureInAppPurchaseNavigation) implementation(projects.featureInsuranceCertificate) implementation(projects.featureInsurances) implementation(projects.featureLogin) @@ -77,6 +79,7 @@ kotlin { implementation(projects.featurePayments) implementation(projects.featurePaymentsNavigation) implementation(projects.featurePayoutAccount) + implementation(projects.featureProductInApp) implementation(projects.featureProfile) implementation(projects.featureRemoveAddons) implementation(projects.featureTerminateInsurance) diff --git a/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/navigation/HedvigEntryProvider.kt b/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/navigation/HedvigEntryProvider.kt index 377d61f4d4..f89cc97ad3 100644 --- a/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/navigation/HedvigEntryProvider.kt +++ b/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/navigation/HedvigEntryProvider.kt @@ -10,6 +10,7 @@ import com.hedvig.android.core.buildconstants.HedvigBuildConstants import com.hedvig.android.data.addons.data.AddonBannerSource import com.hedvig.android.data.coinsured.CoInsuredFlowType import com.hedvig.android.data.contract.ContractId +import com.hedvig.android.data.contract.CrossSell import com.hedvig.android.design.system.hedvig.GlobalSnackBarState import com.hedvig.android.feature.addon.purchase.navigation.AddonPurchaseKey import com.hedvig.android.feature.addon.purchase.navigation.addonPurchaseEntries @@ -42,9 +43,11 @@ import com.hedvig.android.feature.help.center.navigation.HelpCenterKey import com.hedvig.android.feature.home.home.navigation.homeEntries import com.hedvig.android.feature.imageviewer.navigation.ImageViewerKey import com.hedvig.android.feature.imageviewer.navigation.imageViewerEntries +import com.hedvig.android.feature.inapppurchase.navigation.inAppPurchaseEntries import com.hedvig.android.feature.insurance.certificate.navigation.InsuranceEvidenceKey import com.hedvig.android.feature.insurance.certificate.navigation.insuranceEvidenceEntries import com.hedvig.android.feature.insurances.data.CancelInsuranceData +import com.hedvig.android.feature.insurances.navigation.InsuranceContractDetailKey import com.hedvig.android.feature.insurances.navigation.insuranceEntries import com.hedvig.android.feature.login.navigation.loginEntries import com.hedvig.android.feature.movingflow.MovingSource @@ -60,6 +63,9 @@ import com.hedvig.android.feature.payments.navigation.ManualChargeKey import com.hedvig.android.feature.payments.navigation.paymentsEntries import com.hedvig.android.feature.payoutaccount.navigation.PayoutAccountKey import com.hedvig.android.feature.payoutaccount.navigation.payoutAccountEntries +import com.hedvig.android.feature.product.inapp.navigation.CrossSellProductKey +import com.hedvig.android.feature.product.inapp.navigation.productInAppEntries +import com.hedvig.android.feature.product.inapp.navigation.showCrossSellPurchaseResult import com.hedvig.android.feature.profile.navigation.ContactInfoKey import com.hedvig.android.feature.profile.navigation.UsageDataKey import com.hedvig.android.feature.profile.tab.profileEntries @@ -103,6 +109,7 @@ internal fun EntryProviderScope.hedvigEntryProvider( appHost: AppHost, openUrl: (String) -> Unit, openCrossSellUrl: (String) -> Boolean, + openCrossSell: (CrossSell) -> Boolean, imageLoader: ImageLoader, languageService: LanguageService, hedvigBuildConstants: HedvigBuildConstants, @@ -159,7 +166,7 @@ internal fun EntryProviderScope.hedvigEntryProvider( externalNavigator = externalNavigator, imageLoader = imageLoader, openUrl = openUrl, - openCrossSellUrl = openCrossSellUrl, + openCrossSell = openCrossSell, onNavigateToImageViewer = onNavigateToImageViewer, navigateToNewConversation = navigateToNewConversation, navigateToConversation = navigateToConversation, @@ -173,7 +180,7 @@ internal fun EntryProviderScope.hedvigEntryProvider( windowSizeClass = windowSizeClass, imageLoader = imageLoader, openUrl = openUrl, - openCrossSellUrl = openCrossSellUrl, + openCrossSell = openCrossSell, externalNavigator = externalNavigator, navigateToNewConversation = navigateToNewConversation, navigateToAddonPurchaseFlow = navigateToAddonPurchaseFlow, @@ -182,7 +189,7 @@ internal fun EntryProviderScope.hedvigEntryProvider( onboardingEntries( backstack = backstack, imageLoader = imageLoader, - openCrossSellUrl = openCrossSellUrl, + openCrossSell = openCrossSell, openPrivacyPolicy = openPrivacyPolicy, navigateToChipId = { contractId -> backstack.add(ChipIdKey(contractId)) }, ) @@ -228,6 +235,7 @@ internal fun EntryProviderScope.hedvigEntryProvider( globalSnackBarState = globalSnackBarState, imageLoader = imageLoader, openUrl = openUrl, + openCrossSellUrl = openCrossSellUrl, externalNavigator = externalNavigator, navigateToNewConversation = navigateToNewConversation, navigateToInbox = navigateToInbox, @@ -283,7 +291,7 @@ private fun EntryProviderScope.addHomeEntries( externalNavigator: ExternalNavigator, imageLoader: ImageLoader, openUrl: (String) -> Unit, - openCrossSellUrl: (String) -> Boolean, + openCrossSell: (CrossSell) -> Boolean, onNavigateToImageViewer: (imageUrl: String, cacheKey: String) -> Unit, navigateToNewConversation: () -> Unit, navigateToConversation: (String) -> Unit, @@ -327,7 +335,7 @@ private fun EntryProviderScope.addHomeEntries( navigateToUsageData = { backstack.add(UsageDataKey) }, openAppSettings = externalNavigator::openAppSettings, openUrl = openUrl, - openCrossSellUrl = openCrossSellUrl, + openCrossSell = openCrossSell, imageLoader = imageLoader, navigateToAddonPurchaseFlow = navigateToAddonPurchaseFlow, ) @@ -379,7 +387,7 @@ private fun EntryProviderScope.addInsuranceEntries( windowSizeClass: WindowSizeClass, imageLoader: ImageLoader, openUrl: (String) -> Unit, - openCrossSellUrl: (String) -> Boolean, + openCrossSell: (CrossSell) -> Boolean, externalNavigator: ExternalNavigator, navigateToNewConversation: () -> Unit, navigateToAddonPurchaseFlow: (List, AddonBannerSource) -> Unit, @@ -389,7 +397,7 @@ private fun EntryProviderScope.addInsuranceEntries( nestedEntries = {}, backstack = backstack, openUrl = openUrl, - openCrossSellUrl = openCrossSellUrl, + openCrossSell = openCrossSell, onNavigateToNewConversation = navigateToNewConversation, startMovingFlow = { navigateToMovingFlow(MovingSource.INSURANCE) @@ -519,6 +527,7 @@ private fun EntryProviderScope.addSharedFlowEntries( globalSnackBarState: GlobalSnackBarState, imageLoader: ImageLoader, openUrl: (String) -> Unit, + openCrossSellUrl: (String) -> Boolean, externalNavigator: ExternalNavigator, navigateToNewConversation: () -> Unit, navigateToInbox: () -> Unit, @@ -534,6 +543,13 @@ private fun EntryProviderScope.addSharedFlowEntries( backstack = backstack, onNavigateToNewConversation = navigateToNewConversation, ) + inAppPurchaseEntries( + backstack = backstack, + navigateToContract = { contractId -> + backstack.navigateAndPopUpTo(InsuranceContractDetailKey(contractId), inclusive = true) + }, + showPurchaseResult = backstack::showCrossSellPurchaseResult, + ) chipIdEntries( backstack = backstack, globalSnackBarState = globalSnackBarState, @@ -555,8 +571,9 @@ private fun EntryProviderScope.addSharedFlowEntries( tryToDialPhone = externalNavigator::tryToDialPhone, imageLoader = imageLoader, ) - removeAddonsEntries(backstack = backstack) imageViewerEntries(backstack, imageLoader) + productInAppEntries(backstack = backstack, imageLoader = imageLoader, openCrossSellUrl = openCrossSellUrl) + removeAddonsEntries(backstack = backstack) } /** The inbox and the conversations. */ diff --git a/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/ui/HedvigApp.kt b/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/ui/HedvigApp.kt index 4675cc1e2d..d33da89c0b 100644 --- a/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/ui/HedvigApp.kt +++ b/app/app-shell/src/commonMain/kotlin/com/hedvig/android/app/ui/HedvigApp.kt @@ -61,6 +61,7 @@ import com.hedvig.android.core.buildconstants.HedvigBuildConstants import com.hedvig.android.core.demomode.DemoManager import com.hedvig.android.core.tracking.EventTrackingClient import com.hedvig.android.data.addons.data.AddonBannerSource +import com.hedvig.android.data.contract.CrossSell import com.hedvig.android.data.settings.datastore.SettingsDataStore import com.hedvig.android.design.system.hedvig.DemoModeLabel import com.hedvig.android.design.system.hedvig.Surface @@ -72,7 +73,9 @@ import com.hedvig.android.feature.home.home.navigation.HomeKey import com.hedvig.android.feature.onboarding.data.ResetOnboardingSeenUseCase import com.hedvig.android.feature.onboarding.gate.OnboardingGate import com.hedvig.android.feature.onboarding.navigation.OnboardingKey +import com.hedvig.android.feature.product.inapp.navigation.navigateToCrossSellProduct import com.hedvig.android.featureflags.FeatureManager +import com.hedvig.android.featureflags.flags.Feature import com.hedvig.android.language.LanguageService import com.hedvig.android.logger.logcat import com.hedvig.android.navigation.activity.ExternalNavigator @@ -170,9 +173,21 @@ fun HedvigApp( scope = scope, ) } + val showProductPages by remember(featureManager) { + featureManager.isFeatureEnabled(Feature.SHOW_PRODUCT_PAGES) + }.collectAsStateWithLifecycle(initialValue = false) + val openCrossSell: (CrossSell) -> Boolean = { crossSell -> + if (showProductPages) { + backstackController.navigateToCrossSellProduct(crossSell) + true + } else { + authorizationCodeUriHandler.tryOpenUri(crossSell.storeUrl) + } + } CrossSellSheet( isInScreenEligibleForCrossSells = hedvigAppState.isInScreenEligibleForCrossSells, - onCrossSellClick = authorizationCodeUriHandler::tryOpenUri, + onCrossSellClick = openCrossSell, + onRecommendedAddonClick = { deepLink -> authorizationCodeUriHandler.tryOpenUri(deepLink) }, onAddonClick = { eligibleInsuranceIds -> backstackController.add( AddonPurchaseKey( @@ -238,6 +253,7 @@ fun HedvigApp( appHost = appHost, openUrl = authorizationCodeUriHandler::openUri, openCrossSellUrl = authorizationCodeUriHandler::tryOpenUri, + openCrossSell = openCrossSell, imageLoader = imageLoader, languageService = languageService, hedvigBuildConstants = hedvigBuildConstants, diff --git a/app/app/build.gradle.kts b/app/app/build.gradle.kts index 25e366b62f..ed13c26a89 100644 --- a/app/app/build.gradle.kts +++ b/app/app/build.gradle.kts @@ -212,6 +212,8 @@ dependencies { implementation(projects.featureHelpCenterNavigation) implementation(projects.featureHome) implementation(projects.featureImageViewer) + implementation(projects.featureInAppPurchase) + implementation(projects.featureInAppPurchaseNavigation) implementation(projects.featureInsuranceCertificate) implementation(projects.featureInsurances) implementation(projects.featureLogin) @@ -225,6 +227,7 @@ dependencies { implementation(projects.featurePayoutAccount) implementation(projects.featurePayments) implementation(projects.featurePaymentsNavigation) + implementation(projects.featureProductInApp) implementation(projects.featureProfile) implementation(projects.featureTerminateInsurance) implementation(projects.featureTerminateInsuranceNavigation) diff --git a/app/data/data-contract/src/commonMain/kotlin/com/hedvig/android/data/contract/CrossSell.kt b/app/data/data-contract/src/commonMain/kotlin/com/hedvig/android/data/contract/CrossSell.kt index f3d3b38b20..baef3b9d12 100644 --- a/app/data/data-contract/src/commonMain/kotlin/com/hedvig/android/data/contract/CrossSell.kt +++ b/app/data/data-contract/src/commonMain/kotlin/com/hedvig/android/data/contract/CrossSell.kt @@ -5,6 +5,8 @@ package com.hedvig.android.data.contract * Pick the one matching the box it is drawn into: the same instance is shown both as a 48.dp list row and as * a 140.dp hero, and reusing one URL across both sizes makes Coil serve the larger cached bitmap to the * smaller box, which visibly softens its edges. + * + * A generic cross-sell, such as pet, has the products to choose between in [options]; a single product has none. */ data class CrossSell( val id: String, @@ -14,6 +16,7 @@ data class CrossSell( val pillowImageSmall: ImageAsset, val pillowImageLarge: ImageAsset, val buttonText: String, + val options: List = emptyList(), ) data class ImageAsset( diff --git a/app/data/data-contract/src/commonMain/kotlin/com/hedvig/android/data/contract/InAppProduct.kt b/app/data/data-contract/src/commonMain/kotlin/com/hedvig/android/data/contract/InAppProduct.kt new file mode 100644 index 0000000000..99a0efec85 --- /dev/null +++ b/app/data/data-contract/src/commonMain/kotlin/com/hedvig/android/data/contract/InAppProduct.kt @@ -0,0 +1,20 @@ +package com.hedvig.android.data.contract + +import kotlinx.serialization.Serializable + +/** + * A product that can be bought natively in the app. + */ +@Serializable +enum class InAppProduct { + ACCIDENT, + PAYMENT_PROTECTION, + DOG, + CAT, + CAR, + APARTMENT_RENT, + APARTMENT_BRF, + APARTMENT_STUDENT, + HOUSE, + VACATION_HOME, +} diff --git a/app/feature/feature-cross-sell-sheet/src/commonMain/graphql/QueryBottomSheetCrossSells.graphql b/app/feature/feature-cross-sell-sheet/src/commonMain/graphql/QueryBottomSheetCrossSells.graphql index 8c0a4e9e17..cb1ba55ada 100644 --- a/app/feature/feature-cross-sell-sheet/src/commonMain/graphql/QueryBottomSheetCrossSells.graphql +++ b/app/feature/feature-cross-sell-sheet/src/commonMain/graphql/QueryBottomSheetCrossSells.graphql @@ -58,4 +58,21 @@ fragment CrossSellFragment on CrossSell { id alt } + options { + id + title + description + storeUrl + buttonTitle + pillowImageSmall { + src + id + alt + } + pillowImageLarge { + src + id + alt + } + } } diff --git a/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheet.kt b/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheet.kt index a3e1acc982..ddc121b9fe 100644 --- a/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheet.kt +++ b/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheet.kt @@ -24,7 +24,8 @@ import kotlinx.coroutines.flow.distinctUntilChanged @Composable fun CrossSellSheet( isInScreenEligibleForCrossSells: Boolean, - onCrossSellClick: (String) -> Boolean, + onCrossSellClick: (CrossSell) -> Boolean, + onRecommendedAddonClick: (deepLink: String) -> Unit, onAddonClick: (eligibleInsuranceIds: List) -> Unit, imageLoader: ImageLoader, ) { @@ -76,11 +77,11 @@ fun CrossSellSheet( viewModel.emit(CrossSellSheetEvent.CrossSellShown(offerId, offerUrl)) }, onCrossSellClick = dropUnlessResumed { crossSell: CrossSell -> - if (onCrossSellClick(crossSell.storeUrl)) { + if (onCrossSellClick(crossSell)) { viewModel.emit(CrossSellSheetEvent.CrossSellClicked(crossSell.id, crossSell.storeUrl)) } }, - onRecommendedAddonClick = { deepLink -> onCrossSellClick(deepLink) }, + onRecommendedAddonClick = onRecommendedAddonClick, onAddonClick = onAddonClick, imageLoader = imageLoader, ) diff --git a/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheetViewModel.kt b/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheetViewModel.kt index b343586e3b..097d79ddce 100644 --- a/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheetViewModel.kt +++ b/app/feature/feature-cross-sell-sheet/src/commonMain/kotlin/com/hedvig/android/feature/cross/sell/sheet/CrossSellSheetViewModel.kt @@ -253,6 +253,25 @@ internal fun CrossSellFragment.toCrossSell(): CrossSell { description = pillowImageLarge.alt, ), buttonText = this.buttonTitle, + options = options.map { option -> + CrossSell( + id = option.id, + title = option.title, + subtitle = option.description, + storeUrl = option.storeUrl, + pillowImageSmall = ImageAsset( + id = option.pillowImageSmall.id, + src = option.pillowImageSmall.src, + description = option.pillowImageSmall.alt, + ), + pillowImageLarge = ImageAsset( + id = option.pillowImageLarge.id, + src = option.pillowImageLarge.src, + description = option.pillowImageLarge.alt, + ), + buttonText = option.buttonTitle, + ) + }, ) } } diff --git a/app/feature/feature-home/src/commonMain/graphql/QueryHome.graphql b/app/feature/feature-home/src/commonMain/graphql/QueryHome.graphql index 33a00c0c62..2011500267 100644 --- a/app/feature/feature-home/src/commonMain/graphql/QueryHome.graphql +++ b/app/feature/feature-home/src/commonMain/graphql/QueryHome.graphql @@ -128,4 +128,22 @@ fragment HomeCrossSellFragment on CrossSell { id alt } + # TODO: Temporary: restore once the backend serves CrossSell.options. + # options { + # id + # title + # description + # storeUrl + # buttonTitle + # pillowImageSmall { + # src + # id + # alt + # } + # pillowImageLarge { + # src + # id + # alt + # } + # } } diff --git a/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/data/GetHomeDataUseCase.kt b/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/data/GetHomeDataUseCase.kt index 82501fa76a..0870bc2468 100644 --- a/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/data/GetHomeDataUseCase.kt +++ b/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/data/GetHomeDataUseCase.kt @@ -385,6 +385,43 @@ internal class GetHomeDataUseCaseImpl( description = pillowImageLarge.alt, ), buttonText = this.buttonTitle, + // TODO: Temporary fake until the backend serves CrossSell.options. Restore the mapping below before merging. + options = fakeCrossSellOptions(id, storeUrl), + // options = options.map { option -> + // CrossSell( + // id = option.id, + // title = option.title, + // subtitle = option.description, + // storeUrl = option.storeUrl, + // pillowImageSmall = ImageAsset( + // id = option.pillowImageSmall.id, + // src = option.pillowImageSmall.src, + // description = option.pillowImageSmall.alt, + // ), + // pillowImageLarge = ImageAsset( + // id = option.pillowImageLarge.id, + // src = option.pillowImageLarge.src, + // description = option.pillowImageLarge.alt, + // ), + // buttonText = option.buttonTitle, + // ) + // }, + ) + } + } + + private fun fakeCrossSellOptions(crossSellId: String, storeUrl: String): List { + if (crossSellId != "crossSell:PET") return emptyList() + val pillow = "https://a.storyblok.com/f/165473/832x832/c89ddd009f/pet-pillow-832x832px.png" + return listOf("Dog" to "crossSell:PET_DOG", "Cat" to "crossSell:PET_CAT").map { (animal, optionId) -> + CrossSell( + id = optionId, + title = "$animal Insurance", + subtitle = "For your ${animal.lowercase()}", + storeUrl = storeUrl, + pillowImageSmall = ImageAsset(id = "$optionId-small", src = pillow, description = ""), + pillowImageLarge = ImageAsset(id = "$optionId-large", src = pillow, description = ""), + buttonText = "See price", ) } } diff --git a/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/navigation/HomeEntries.kt b/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/navigation/HomeEntries.kt index 432fcfa8b5..d81364f749 100644 --- a/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/navigation/HomeEntries.kt +++ b/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/navigation/HomeEntries.kt @@ -6,6 +6,7 @@ import coil3.ImageLoader import com.hedvig.android.compose.ui.dropUnlessResumed import com.hedvig.android.data.addons.data.AddonBannerSource import com.hedvig.android.data.coinsured.CoInsuredFlowType +import com.hedvig.android.data.contract.CrossSell import com.hedvig.android.feature.home.home.ui.FirstVetDestination import com.hedvig.android.feature.home.home.ui.HomeDestination import com.hedvig.android.feature.home.home.ui.HomeViewModel @@ -36,7 +37,7 @@ fun EntryProviderScope.homeEntries( navigateToUsageData: () -> Unit, openAppSettings: () -> Unit, openUrl: (String) -> Unit, - openCrossSellUrl: (String) -> Boolean, + openCrossSell: (CrossSell) -> Boolean, imageLoader: ImageLoader, navigateToAddonPurchaseFlow: (List, AddonBannerSource) -> Unit, ) { @@ -65,7 +66,7 @@ fun EntryProviderScope.homeEntries( } }, openUrl = openUrl, - openCrossSellUrl = openCrossSellUrl, + openCrossSell = openCrossSell, openAppSettings = openAppSettings, navigateToFirstVet = dropUnlessResumed { sections -> navigateToFirstVet(sections) }, navigateToContactInfo = dropUnlessResumed { diff --git a/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/ui/HomeDestination.kt b/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/ui/HomeDestination.kt index 84eab30fe0..46c57566ff 100644 --- a/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/ui/HomeDestination.kt +++ b/app/feature/feature-home/src/commonMain/kotlin/com/hedvig/android/feature/home/home/ui/HomeDestination.kt @@ -288,7 +288,7 @@ internal fun HomeDestination( navigateToHelpCenter: () -> Unit, navigateToQuickLink: (QuickLinkDestination) -> Unit, openUrl: (String) -> Unit, - openCrossSellUrl: (String) -> Boolean, + openCrossSell: (CrossSell) -> Boolean, openAppSettings: () -> Unit, navigateToMissingInfo: (String, CoInsuredFlowType) -> Unit, navigateToFirstVet: (List) -> Unit, @@ -318,7 +318,7 @@ internal fun HomeDestination( viewModel.emit(HomeEvent.CrossSellShown(userFlow, offerId, offerUrl)) }, onCrossSellClick = dropUnlessResumed { userFlow: CrossSellUserFlow, crossSell: CrossSell -> - if (openCrossSellUrl(crossSell.storeUrl)) { + if (openCrossSell(crossSell)) { viewModel.emit(HomeEvent.CrossSellClicked(userFlow, crossSell.id, crossSell.storeUrl)) } }, diff --git a/app/feature/feature-in-app-purchase-navigation/build.gradle.kts b/app/feature/feature-in-app-purchase-navigation/build.gradle.kts new file mode 100644 index 0000000000..48f9e0e5e9 --- /dev/null +++ b/app/feature/feature-in-app-purchase-navigation/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + id("hedvig.multiplatform.library") + id("hedvig.multiplatform.library.android") + id("hedvig.gradle.plugin") +} + +hedvig { + serialization() + navKeys() +} + +kotlin { + sourceSets { + commonMain.dependencies { + implementation(libs.kotlinx.serialization.core) + implementation(projects.coreCommonPublic) + implementation(projects.dataContract) + implementation(projects.navigationCommon) + } + } +} diff --git a/app/feature/feature-in-app-purchase-navigation/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/navigation/InAppPurchaseKey.kt b/app/feature/feature-in-app-purchase-navigation/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/navigation/InAppPurchaseKey.kt new file mode 100644 index 0000000000..cc16c832e7 --- /dev/null +++ b/app/feature/feature-in-app-purchase-navigation/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/navigation/InAppPurchaseKey.kt @@ -0,0 +1,11 @@ +package com.hedvig.android.feature.inapppurchase.navigation + +import com.hedvig.android.data.contract.InAppProduct +import com.hedvig.android.navigation.common.HedvigNavKey +import kotlinx.serialization.Serializable + +/** + * The start of the in-app purchase flow for [product]. + */ +@Serializable +data class InAppPurchaseKey(val product: InAppProduct) : HedvigNavKey diff --git a/app/feature/feature-in-app-purchase/build.gradle.kts b/app/feature/feature-in-app-purchase/build.gradle.kts new file mode 100644 index 0000000000..1eb9d85c3f --- /dev/null +++ b/app/feature/feature-in-app-purchase/build.gradle.kts @@ -0,0 +1,53 @@ +plugins { + id("hedvig.multiplatform.library") + id("hedvig.multiplatform.library.android") + id("hedvig.gradle.plugin") +} + +hedvig { + apollo("octopus") + serialization() + compose() + navKeys() + viewModels() +} + +kotlin { + sourceSets { + commonMain.dependencies { + implementation(libs.apollo.normalizedCache) + implementation(libs.apollo.runtime) + implementation(libs.arrow.core) + implementation(libs.coil.coil) + implementation(libs.coroutines.core) + implementation(libs.jetbrains.compose.ui.tooling.preview) + implementation(libs.jetbrains.lifecycle.runtime.compose) + implementation(libs.kotlinx.datetime) + implementation(libs.kotlinx.serialization.core) + implementation(libs.kotlinx.serialization.json) + implementation(projects.apolloCore) + implementation(projects.apolloOctopusPublic) + implementation(projects.composeUi) + implementation(projects.coreCommonPublic) + implementation(projects.coreResources) + implementation(projects.coreUiData) + implementation(projects.dataContract) + implementation(projects.dataProductVariantPublic) + implementation(projects.designSystemHedvig) + implementation(projects.featureInAppPurchaseNavigation) + implementation(projects.moleculePublic) + implementation(projects.navigationCommon) + implementation(projects.navigationCompose) + implementation(projects.uiBankid) + implementation(projects.uiTiersAndAddons) + } + jvmTest.dependencies { + implementation(libs.assertK) + implementation(libs.coroutines.test) + implementation(libs.junit) + implementation(libs.turbine) + implementation(projects.loggingTest) + implementation(projects.moleculeTest) + } + } +} diff --git a/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseFormQuery.graphql b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseFormQuery.graphql new file mode 100644 index 0000000000..2920d17539 --- /dev/null +++ b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseFormQuery.graphql @@ -0,0 +1,41 @@ +query InAppPurchaseForm($product: InAppProduct!) { + inAppPurchaseForm(product: $product) { + product { + name + displayNameShort + displayNameFull + tagline + pillowImage { + src + alt + } + featuredImage { + src + alt + } + variants { + ...ProductVariantFragment + } + } + eligibility { + isEligible + reason + existingContractId + } + fields { + id + type + title + options { + value + title + isSelectable + additionalInfo + } + defaultValues + minValue + maxValue + maxSelections + } + } +} diff --git a/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseQuoteMutation.graphql b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseQuoteMutation.graphql new file mode 100644 index 0000000000..b56ca4d494 --- /dev/null +++ b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseQuoteMutation.graphql @@ -0,0 +1,58 @@ +mutation InAppPurchaseQuote( + $product: InAppProduct! + $fields: [InAppPurchaseFieldInput!]! + $campaignCode: String +) { + inAppPurchaseQuote(input: { product: $product, fields: $fields, campaignCode: $campaignCode }) { + shopSessionId + offers { + id + variant { + ...ProductVariantFragment + } + exposure { + displayNameShort + } + cost { + baseInsuranceGross { + ...MoneyFragment + } + gross { + ...MoneyFragment + } + net { + ...MoneyFragment + } + discountsV2 { + itemDiscount { + displayName + } + amount { + ...MoneyFragment + } + } + } + displayItems { + displayTitle + displayValue + } + deductible { + displayName + tagline + } + insuranceAmount { + displayName + amount + } + usps + startDate + } + fieldErrors { + fieldId + message + } + userError { + message + } + } +} diff --git a/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseSignMutation.graphql b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseSignMutation.graphql new file mode 100644 index 0000000000..54e8b4a6a1 --- /dev/null +++ b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseSignMutation.graphql @@ -0,0 +1,10 @@ +mutation InAppPurchaseSign($shopSessionId: UUID!, $offerId: UUID!) { + inAppPurchaseSign(input: { shopSessionId: $shopSessionId, offerId: $offerId }) { + signing { + ...InAppPurchaseSigningFragment + } + userError { + message + } + } +} diff --git a/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseSigningQuery.graphql b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseSigningQuery.graphql new file mode 100644 index 0000000000..f4b55f1d6f --- /dev/null +++ b/app/feature/feature-in-app-purchase/src/commonMain/graphql/InAppPurchaseSigningQuery.graphql @@ -0,0 +1,18 @@ +query InAppPurchaseSigning($signingId: UUID!) { + shopSessionSigning(id: $signingId) { + ...InAppPurchaseSigningFragment + } +} + +fragment InAppPurchaseSigningFragment on ShopSessionSigning { + id + status + seBankidProperties { + autoStartToken + liveQrCodeData + bankidAppOpened + } + userError { + message + } +} diff --git a/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/GetInAppPurchaseFormUseCase.kt b/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/GetInAppPurchaseFormUseCase.kt new file mode 100644 index 0000000000..70dd1af236 --- /dev/null +++ b/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/GetInAppPurchaseFormUseCase.kt @@ -0,0 +1,154 @@ +package com.hedvig.android.feature.inapppurchase.data + +import arrow.core.Either +import arrow.core.raise.either +import com.apollographql.apollo.ApolloClient +import com.apollographql.cache.normalized.FetchPolicy +import com.apollographql.cache.normalized.fetchPolicy +import com.hedvig.android.apollo.safeExecute +import com.hedvig.android.core.common.ErrorMessage +import com.hedvig.android.core.common.di.AppScope +import com.hedvig.android.data.contract.InAppProduct +import com.hedvig.android.data.productvariant.toProductVariant +import com.hedvig.android.logger.LogPriority +import com.hedvig.android.logger.logcat +import dev.zacsweers.metro.ContributesBinding +import dev.zacsweers.metro.Inject +import dev.zacsweers.metro.SingleIn +import kotlinx.datetime.LocalDate +import octopus.InAppPurchaseFormQuery +import octopus.type.InAppPurchaseFormFieldType + +internal interface GetInAppPurchaseFormUseCase { + suspend fun invoke(product: InAppProduct): Either +} + +@ContributesBinding(AppScope::class) +@SingleIn(AppScope::class) +@Inject +internal class GetInAppPurchaseFormUseCaseImpl( + private val apolloClient: ApolloClient, +) : GetInAppPurchaseFormUseCase { + override suspend fun invoke(product: InAppProduct): Either { + return either { + val form = apolloClient + .query(InAppPurchaseFormQuery(product = product.toOctopus())) + .fetchPolicy(FetchPolicy.NetworkOnly) + .safeExecute() + .onLeft { logcat(LogPriority.ERROR, it) { "InAppPurchaseFormQuery for $product failed" } } + .mapLeft { ErrorMessage() } + .bind() + .inAppPurchaseForm + InAppPurchaseForm( + product = with(form.product) { + InAppPurchaseProduct( + name = name, + displayNameShort = displayNameShort, + displayNameFull = displayNameFull, + tagline = tagline, + pillowImage = InAppPurchaseImage(pillowImage.src, pillowImage.alt), + featuredImage = featuredImage?.let { InAppPurchaseImage(it.src, it.alt) }, + variants = variants.map { it.toProductVariant() }, + ) + }, + eligibility = if (form.eligibility.isEligible) { + InAppPurchaseEligibility.Eligible + } else { + InAppPurchaseEligibility.NotEligible( + reason = when (form.eligibility.reason) { + octopus.type.InAppPurchaseIneligibilityReason.ALREADY_INSURED -> { + InAppPurchaseIneligibilityReason.ALREADY_INSURED + } + + else -> { + InAppPurchaseIneligibilityReason.NOT_AVAILABLE + } + }, + existingContractId = form.eligibility.existingContractId, + ) + }, + fields = form.fields.mapNotNull { it.toFormField() }, + ) + } + } +} + +internal fun InAppProduct.toOctopus(): octopus.type.InAppProduct = when (this) { + InAppProduct.ACCIDENT -> octopus.type.InAppProduct.ACCIDENT + InAppProduct.PAYMENT_PROTECTION -> octopus.type.InAppProduct.PAYMENT_PROTECTION + InAppProduct.DOG -> octopus.type.InAppProduct.DOG + InAppProduct.CAT -> octopus.type.InAppProduct.CAT + InAppProduct.CAR -> octopus.type.InAppProduct.CAR + InAppProduct.APARTMENT_RENT -> octopus.type.InAppProduct.APARTMENT_RENT + InAppProduct.APARTMENT_BRF -> octopus.type.InAppProduct.APARTMENT_BRF + InAppProduct.APARTMENT_STUDENT -> octopus.type.InAppProduct.APARTMENT_STUDENT + InAppProduct.HOUSE -> octopus.type.InAppProduct.HOUSE + InAppProduct.VACATION_HOME -> octopus.type.InAppProduct.VACATION_HOME +} + +// A field of a type this app version doesn't know is dropped, the quote then reports it in fieldErrors. +private fun InAppPurchaseFormQuery.Data.InAppPurchaseForm.Field.toFormField(): InAppPurchaseFormField? { + return when (type) { + InAppPurchaseFormFieldType.NUMBER -> { + InAppPurchaseFormField.Number( + id = id, + title = title, + defaultValue = defaultValues.firstOrNull()?.toIntOrNull(), + minValue = minValue?.toIntOrNull(), + maxValue = maxValue?.toIntOrNull(), + ) + } + + InAppPurchaseFormFieldType.DATE -> { + InAppPurchaseFormField.Date( + id = id, + title = title, + defaultValue = defaultValues.firstOrNull()?.let { runCatching { LocalDate.parse(it) }.getOrNull() }, + minValue = minValue?.let { runCatching { LocalDate.parse(it) }.getOrNull() }, + maxValue = maxValue?.let { runCatching { LocalDate.parse(it) }.getOrNull() }, + ) + } + + InAppPurchaseFormFieldType.SINGLE_SELECT -> { + InAppPurchaseFormField.SingleSelect( + id = id, + title = title, + options = options.orEmpty().map { + InAppPurchaseFormField.SingleSelect.Option(it.value, it.title, it.isSelectable, it.additionalInfo) + }, + defaultValue = defaultValues.firstOrNull(), + ) + } + + InAppPurchaseFormFieldType.TEXT -> { + InAppPurchaseFormField.Text(id = id, title = title, defaultValue = defaultValues.firstOrNull()) + } + + InAppPurchaseFormFieldType.SEARCHABLE_SELECT -> { + InAppPurchaseFormField.SearchableSelect( + id = id, + title = title, + options = options.orEmpty().map { + InAppPurchaseFormField.SingleSelect.Option(it.value, it.title, it.isSelectable, it.additionalInfo) + }, + defaultValues = defaultValues, + maxSelections = maxSelections ?: 1, + ) + } + + InAppPurchaseFormFieldType.EXTRA_BUILDINGS -> { + InAppPurchaseFormField.ExtraBuildings( + id = id, + title = title, + buildingTypes = options.orEmpty().map { + InAppPurchaseFormField.SingleSelect.Option(it.value, it.title, it.isSelectable, it.additionalInfo) + }, + ) + } + + InAppPurchaseFormFieldType.UNKNOWN__ -> { + logcat(LogPriority.ERROR) { "InAppPurchaseForm field $id has unknown type ${type.rawValue}" } + null + } + } +} diff --git a/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/GetInAppPurchaseSigningUseCase.kt b/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/GetInAppPurchaseSigningUseCase.kt new file mode 100644 index 0000000000..2d922bdfd2 --- /dev/null +++ b/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/GetInAppPurchaseSigningUseCase.kt @@ -0,0 +1,62 @@ +package com.hedvig.android.feature.inapppurchase.data + +import arrow.core.Either +import arrow.core.raise.either +import com.apollographql.apollo.ApolloClient +import com.apollographql.cache.normalized.FetchPolicy +import com.apollographql.cache.normalized.fetchPolicy +import com.hedvig.android.apollo.safeExecute +import com.hedvig.android.core.common.ErrorMessage +import com.hedvig.android.core.common.di.AppScope +import com.hedvig.android.logger.LogPriority +import com.hedvig.android.logger.logcat +import dev.zacsweers.metro.ContributesBinding +import dev.zacsweers.metro.Inject +import dev.zacsweers.metro.SingleIn +import octopus.InAppPurchaseSigningQuery +import octopus.fragment.InAppPurchaseSigningFragment +import octopus.type.ShopSessionSigningStatus + +internal interface GetInAppPurchaseSigningUseCase { + suspend fun invoke(signingId: String): Either +} + +@ContributesBinding(AppScope::class) +@SingleIn(AppScope::class) +@Inject +internal class GetInAppPurchaseSigningUseCaseImpl( + private val apolloClient: ApolloClient, +) : GetInAppPurchaseSigningUseCase { + override suspend fun invoke(signingId: String): Either { + return either { + apolloClient + .query(InAppPurchaseSigningQuery(signingId)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .safeExecute() + .onLeft { logcat(LogPriority.ERROR, it) { "InAppPurchaseSigningQuery for $signingId failed" } } + .mapLeft { ErrorMessage() } + .bind() + .shopSessionSigning + .toInAppPurchaseSigning() + } + } +} + +internal fun InAppPurchaseSigningFragment.toInAppPurchaseSigning() = InAppPurchaseSigning( + id = id, + status = when (status) { + ShopSessionSigningStatus.PENDING -> InAppPurchaseSigningStatus.PENDING + + ShopSessionSigningStatus.CREATING -> InAppPurchaseSigningStatus.CREATING + + ShopSessionSigningStatus.SIGNED -> InAppPurchaseSigningStatus.SIGNED + + ShopSessionSigningStatus.FAILED, + ShopSessionSigningStatus.UNKNOWN__, + -> InAppPurchaseSigningStatus.FAILED + }, + autoStartToken = seBankidProperties?.autoStartToken, + liveQrCodeData = seBankidProperties?.liveQrCodeData, + bankIdAppOpened = seBankidProperties?.bankidAppOpened == true, + failureMessage = userError?.message, +) diff --git a/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/InAppPurchase.kt b/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/InAppPurchase.kt new file mode 100644 index 0000000000..e27e267bbd --- /dev/null +++ b/app/feature/feature-in-app-purchase/src/commonMain/kotlin/com/hedvig/android/feature/inapppurchase/data/InAppPurchase.kt @@ -0,0 +1,218 @@ +package com.hedvig.android.feature.inapppurchase.data + +import com.hedvig.android.core.uidata.UiMoney +import com.hedvig.android.data.productvariant.ProductVariant +import com.hedvig.android.ui.tiersandaddons.CostBreakdownEntry +import kotlinx.datetime.LocalDate +import kotlinx.serialization.Serializable + +internal data class InAppPurchaseForm( + val product: InAppPurchaseProduct, + val eligibility: InAppPurchaseEligibility, + /** + * Empty when not eligible. + */ + val fields: List, +) + +@Serializable +internal data class InAppPurchaseProduct( + val name: String, + val displayNameShort: String, + val displayNameFull: String, + val tagline: String, + val pillowImage: InAppPurchaseImage, + val featuredImage: InAppPurchaseImage?, + val variants: List, +) + +@Serializable +internal data class InAppPurchaseImage( + val src: String, + val alt: String?, +) + +internal sealed interface InAppPurchaseEligibility { + data object Eligible : InAppPurchaseEligibility + + data class NotEligible( + val reason: InAppPurchaseIneligibilityReason, + val existingContractId: String?, + ) : InAppPurchaseEligibility +} + +internal enum class InAppPurchaseIneligibilityReason { + ALREADY_INSURED, + NOT_AVAILABLE, +} + +/** + * A server-driven input. [id] is opaque and only sent back in [InAppPurchaseFieldInput.fieldId]. + */ +internal sealed interface InAppPurchaseFormField { + val id: String + val title: String + + data class Number( + override val id: String, + override val title: String, + val defaultValue: Int?, + val minValue: Int?, + val maxValue: Int?, + ) : InAppPurchaseFormField + + data class Date( + override val id: String, + override val title: String, + val defaultValue: LocalDate?, + val minValue: LocalDate?, + val maxValue: LocalDate?, + ) : InAppPurchaseFormField + + data class SingleSelect( + override val id: String, + override val title: String, + val options: List