-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Display custom FIAMs with SwiftUI #7496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 37 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
b4f9eee
Revert to previous nil check on experiment JSON
christibbs fb58268
Merge branch 'master' of github.com:firebase/firebase-ios-sdk
christibbs 7edab22
Merge branch 'master' of github.com:firebase/firebase-ios-sdk
christibbs 7cbcd4c
Merge branch 'master' of github.com:firebase/firebase-ios-sdk
christibbs ba96a72
Merge branch 'master' of github.com:firebase/firebase-ios-sdk
christibbs 0baba0b
Add Swift extensions podspec
christibbs b910ab2
Add viewmodifier for displaying custom in-app messages w/ SwiftUI
christibbs bb24ec6
Add Multiplatform project to integration test SwiftUI pod
christibbs b2e184a
We don't support macOS yet, comment that out from the Podfile
christibbs f088b1e
Fix variable renaming bug
christibbs 2eb423d
View modifier implementation that works, but design is questionable
christibbs 2b13a7c
Super basic integration
christibbs 518a2bf
Split view modifiers based on in-app message subtype
christibbs b58a326
Fix versioning in podspec
christibbs c6d6f26
Configure Firebase in test app
christibbs 10d5873
Remove trailing whitespace
christibbs 291a688
Clean up SDK fetching code, sample app
christibbs 8d722a7
Updates to sample code, fully functional modal message now
christibbs f06d744
Update implementing app example
christibbs dd358fc
Better code readability for view modifiers
christibbs ac8f9a2
Add unit tests for DelegateBridge
christibbs 450b7cc
Add test target to pod spec
christibbs 040261e
Add copyright notices
christibbs 08a92d4
Remove whitespace from pod spec
christibbs dfa743f
Add auto-complete documentation for in-app message extensions
christibbs 3e90c8f
Lowered min target
christibbs d4d4b0d
Update Package.swift
christibbs f908781
First pass at CI updates
christibbs fc4a98c
@available(13) for SwiftUI structs
christibbs cfd1bd7
Use singleton DelegateBridge
christibbs a181556
Update inappmessaging.yml
christibbs 08f73ed
Run scripts/style.sh
christibbs 97b5eb4
Update build.sh
christibbs f78e347
Modify build.sh
christibbs e9e6c9e
Update CHANGELOG, try something new with GHA configuration
christibbs 7eb4734
Let's try this for GHA
christibbs 75587e4
Fully qualify FIAM message enums
christibbs 34ba6ac
Remove redundant type declaration
christibbs 0eec2bb
Let's try direct instantiation of messages in unit tests
christibbs c50317a
Test CI build works without actual test cases
christibbs eee202b
Let's try this, making init available.
christibbs 5f56642
Try exposing initializer
christibbs b6685d8
Expose simpler initializer for in-app message superclass
christibbs 7f894c9
Fix up UI test app initializers
christibbs c4095f7
Update CHANGELOG, GHA workflows
christibbs 955b0eb
Cleaner GHA test flow
christibbs 99b4a7f
Clean up custom in-app message view modifier implementation
christibbs fae7aa2
Configuration cleanup
christibbs c5fcc43
Add some tests to ensure FIAM SwiftUI API compiles
christibbs 5d8b009
Add SpecsDev to Podfile of integration testing app. Make @State var p…
christibbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
187 changes: 187 additions & 0 deletions
187
FirebaseInAppMessaging/Swift/Source/CustomInAppMessageDisplayViewModifier.swift
This file contains hidden or 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,187 @@ | ||
| // Copyright 2021 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import FirebaseInAppMessaging | ||
| import SwiftUI | ||
|
|
||
| // MARK: Image-only messages. | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| struct ImageOnlyInAppMessageDisplayViewModifier<DisplayMessage: View>: ViewModifier { | ||
| var closure: (InAppMessagingImageOnlyDisplay, InAppMessagingDisplayDelegate) -> DisplayMessage | ||
| @ObservedObject var delegateBridge: DelegateBridge = DelegateBridge.shared | ||
|
|
||
| func body(content: Content) -> some View { | ||
| return content.overlay(overlayView()) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| func overlayView() -> some View { | ||
| if let imageOnlyMessage = delegateBridge.inAppMessageData?.0 as? InAppMessagingImageOnlyDisplay, | ||
| let delegate = delegateBridge.inAppMessageData?.1 { | ||
christibbs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| closure(imageOnlyMessage, delegate) | ||
| .onAppear { delegate.impressionDetected?(for: imageOnlyMessage) } | ||
| } else { | ||
| EmptyView() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| public extension View { | ||
| /// Overrides the default display of an image only in-app message as defined on the Firebase console. | ||
| func imageOnlyInAppMessage<Content: View>(closure: @escaping (InAppMessagingImageOnlyDisplay, | ||
| InAppMessagingDisplayDelegate) | ||
| -> Content) | ||
| -> some View { | ||
| modifier(ImageOnlyInAppMessageDisplayViewModifier(closure: closure)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: Banner messages. | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| struct BannerInAppMessageDisplayViewModifier<DisplayMessage: View>: ViewModifier { | ||
| var closure: (InAppMessagingBannerDisplay, InAppMessagingDisplayDelegate) -> DisplayMessage | ||
| @ObservedObject var delegateBridge: DelegateBridge = DelegateBridge.shared | ||
christibbs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| func body(content: Content) -> some View { | ||
| return content.overlay(overlayView()) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| func overlayView() -> some View { | ||
| if let bannerMessage = delegateBridge.inAppMessageData?.0 as? InAppMessagingBannerDisplay, | ||
| let delegate = delegateBridge.inAppMessageData?.1 { | ||
christibbs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| closure(bannerMessage, delegate).onAppear { delegate.impressionDetected?(for: bannerMessage) } | ||
| } else { | ||
| EmptyView() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| public extension View { | ||
| /// Overrides the default display of a banner in-app message as defined on the Firebase console. | ||
| func bannerInAppMessage<Content: View>(closure: @escaping (InAppMessagingBannerDisplay, | ||
| InAppMessagingDisplayDelegate) | ||
| -> Content) | ||
| -> some View { | ||
| modifier(BannerInAppMessageDisplayViewModifier(closure: closure)) | ||
christibbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| // MARK: Modal messages. | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| struct ModalInAppMessageDisplayViewModifier<DisplayMessage: View>: ViewModifier { | ||
| var closure: (InAppMessagingModalDisplay, InAppMessagingDisplayDelegate) -> DisplayMessage | ||
| @ObservedObject var delegateBridge: DelegateBridge = DelegateBridge.shared | ||
|
|
||
| func body(content: Content) -> some View { | ||
| return content.overlay(overlayView()) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| func overlayView() -> some View { | ||
| if let modalMessage = delegateBridge.inAppMessageData?.0 as? InAppMessagingModalDisplay, | ||
| let delegate = delegateBridge.inAppMessageData?.1 { | ||
| closure(modalMessage, delegate).onAppear { delegate.impressionDetected?(for: modalMessage) } | ||
| } else { | ||
| EmptyView() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| public extension View { | ||
| /// Overrides the default display of a modal in-app message as defined on the Firebase console. | ||
| func modalInAppMessage<Content: View>(closure: @escaping (InAppMessagingModalDisplay, | ||
| InAppMessagingDisplayDelegate) | ||
| -> Content) | ||
| -> some View { | ||
| modifier(ModalInAppMessageDisplayViewModifier(closure: closure)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: Card messages. | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| struct CardInAppMessageDisplayViewModifier<DisplayMessage: View>: ViewModifier { | ||
| var closure: (InAppMessagingCardDisplay, InAppMessagingDisplayDelegate) -> DisplayMessage | ||
| @ObservedObject var delegateBridge: DelegateBridge = DelegateBridge.shared | ||
christibbs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| func body(content: Content) -> some View { | ||
| return content.overlay(overlayView()) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| func overlayView() -> some View { | ||
| if let cardMessage = delegateBridge.inAppMessageData?.0 as? InAppMessagingCardDisplay, | ||
| let delegate = delegateBridge.inAppMessageData?.1 { | ||
| closure(cardMessage, delegate).onAppear { delegate.impressionDetected?(for: cardMessage) } | ||
| } else { | ||
| EmptyView() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| public extension View { | ||
| /// Overrides the default display of a card in-app message as defined on the Firebase console. | ||
| func cardInAppMessage<Content: View>(closure: @escaping (InAppMessagingCardDisplay, | ||
| InAppMessagingDisplayDelegate) | ||
| -> Content) | ||
| -> some View { | ||
| modifier(CardInAppMessageDisplayViewModifier(closure: closure)) | ||
christibbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| // MARK: Bridge to Firebase In-App Messaging SDK. | ||
|
|
||
| @available(iOS 13, tvOS 13, *) | ||
| class DelegateBridge: NSObject, InAppMessagingDisplay, InAppMessagingDisplayDelegate, | ||
christibbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ObservableObject { | ||
| @Published var inAppMessageData: (InAppMessagingDisplayMessage, | ||
| InAppMessagingDisplayDelegate)? = nil | ||
|
|
||
| static let shared = DelegateBridge() | ||
|
|
||
| override init() { | ||
| super.init() | ||
| InAppMessaging.inAppMessaging().messageDisplayComponent = self | ||
| InAppMessaging.inAppMessaging().delegate = self | ||
| } | ||
|
|
||
| func displayMessage(_ messageForDisplay: InAppMessagingDisplayMessage, | ||
| displayDelegate: InAppMessagingDisplayDelegate) { | ||
| DispatchQueue.main.async { | ||
| self.inAppMessageData = (messageForDisplay, displayDelegate) | ||
| } | ||
| } | ||
|
|
||
| func messageClicked(_ inAppMessage: InAppMessagingDisplayMessage, | ||
| with action: InAppMessagingAction) { | ||
| DispatchQueue.main.async { | ||
| self.inAppMessageData = nil | ||
| } | ||
| } | ||
|
|
||
| func messageDismissed(_ inAppMessage: InAppMessagingDisplayMessage, | ||
| dismissType: FIRInAppMessagingDismissType) { | ||
| DispatchQueue.main.async { | ||
| self.inAppMessageData = nil | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.