-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add support for custom components in experiences
- Loading branch information
Showing
15 changed files
with
286 additions
and
7 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
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
13 changes: 13 additions & 0 deletions
13
Sources/AppcuesKit/Presentation/CustomComponents/AppcuesCustomComponentViewController.swift
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,13 @@ | ||
// | ||
// AppcuesCustomComponentViewController.swift | ||
// AppcuesKit | ||
// | ||
// Created by Matt on 2024-10-22. | ||
// Copyright © 2024 Appcues. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public protocol AppcuesCustomComponentViewController: UIViewController { | ||
init?(configuration: AppcuesExperiencePluginConfiguration, actionController: AppcuesExperienceActions) | ||
} |
66 changes: 66 additions & 0 deletions
66
Sources/AppcuesKit/Presentation/CustomComponents/AppcuesExperienceActions.swift
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,66 @@ | ||
// | ||
// AppcuesExperienceActions.swift | ||
// AppcuesKit | ||
// | ||
// Created by Matt on 2024-10-22. | ||
// Copyright © 2024 Appcues. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public class AppcuesExperienceActions { | ||
private weak var appcues: Appcues? | ||
private let renderContext: RenderContext | ||
private let identifier: String | ||
|
||
var actions: [Experience.Action]? | ||
|
||
init(appcues: Appcues?, renderContext: RenderContext, identifier: String) { | ||
self.appcues = appcues | ||
self.renderContext = renderContext | ||
self.identifier = identifier | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
public func triggerBlockActions() { | ||
guard let actions = actions, let actionRegistry = appcues?.container.resolve(ActionRegistry.self) else { return } | ||
actionRegistry.enqueue( | ||
actionModels: actions, | ||
level: .step, | ||
renderContext: renderContext, | ||
interactionType: "Button Tapped", | ||
viewDescription: "Custom component \(identifier)" | ||
) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
public func track(name: String, properties: [String: Any]? = nil) { | ||
enqueue(AppcuesTrackAction(appcues: appcues, eventName: name, attributes: properties)) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
public func nextStep() { | ||
enqueue(AppcuesContinueAction(appcues: appcues, renderContext: renderContext, stepReference: .offset(1))) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
public func previousStep() { | ||
enqueue(AppcuesContinueAction(appcues: appcues, renderContext: renderContext, stepReference: .offset(-1))) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
public func close(markComplete: Bool = false) { | ||
enqueue(AppcuesCloseAction(appcues: appcues, renderContext: renderContext, markComplete: markComplete)) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
public func updateProfile(properties: [String: Any]) { | ||
enqueue(AppcuesUpdateProfileAction(appcues: appcues, properties: properties)) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
private func enqueue(_ action: AppcuesExperienceAction) { | ||
guard let actionRegistry = appcues?.container.resolve(ActionRegistry.self) else { return } | ||
actionRegistry.enqueue(actionInstances: [action]) {} | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
Sources/AppcuesKit/Presentation/UI/Components/AppcuesCustomComponent.swift
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,46 @@ | ||
// | ||
// AppcuesCustomComponent.swift | ||
// AppcuesKit | ||
// | ||
// Created by Matt on 2024-10-22. | ||
// Copyright © 2024 Appcues. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@available(iOS 13.0, *) | ||
internal struct CustomComponentRepresentable: UIViewControllerRepresentable { | ||
private let type: AppcuesCustomComponentViewController.Type | ||
private let configuration: AppcuesExperiencePluginConfiguration | ||
private let actionController: AppcuesExperienceActions | ||
|
||
init?(on viewModel: ExperienceStepViewModel, for model: ExperienceComponent.CustomComponentModel) { | ||
guard let customComponentData = viewModel.customComponent(for: model) else { return nil } | ||
self.type = customComponentData.type | ||
self.configuration = customComponentData.config | ||
self.actionController = customComponentData.actionController | ||
} | ||
|
||
func makeUIViewController(context: Context) -> UIViewController { | ||
let viewController = type.init(configuration: configuration, actionController: actionController) | ||
return viewController ?? UIViewController() | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { | ||
// no-op | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
internal struct AppcuesCustomComponent: View { | ||
let model: ExperienceComponent.CustomComponentModel | ||
|
||
@EnvironmentObject var viewModel: ExperienceStepViewModel | ||
|
||
var body: some View { | ||
let style = AppcuesStyle(from: model.style) | ||
|
||
CustomComponentRepresentable(on: viewModel, for: model) | ||
.applyAllAppcues(style) | ||
} | ||
} |
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
Oops, something went wrong.