Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
feat: 🎸 CardPageView to be used in Carousel and List
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEidinger committed Nov 1, 2021
1 parent fa97ee2 commit a585c84
Show file tree
Hide file tree
Showing 16 changed files with 426 additions and 181 deletions.
2 changes: 1 addition & 1 deletion CAITestApp/CAITestApp/ChannelListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct ChannelListView: View {
.navigationBarTitle(Text(channel.slug), displayMode: .inline)
.environmentObject(self.dataModel.getModelFor(channel, self.channelService))
.environmentObject(ThemeManager.shared)
) {
) {
VStack(alignment: .leading, spacing: 6) {
Text(channel.slug)
Text(channel.id)
Expand Down
132 changes: 85 additions & 47 deletions CAITestApp/CAITestApp/MockData.swift

Large diffs are not rendered by default.

80 changes: 79 additions & 1 deletion Sources/SAPCAI/Foundation/Model/CAIResponseData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public extension CAIResponseMessageData {
conversation: "",
receivedAt: Date())
}


@available(*, deprecated, message: "Use init(title:subtitle:headerImageName:contentPictureName:description:inlineButtons:sections:status1:status1ValueState:status2:status3:isBot)")
init(_ title: String,
_ subtitle: String,
_ headerImageName: String? = nil, // e.g. image in object card
Expand Down Expand Up @@ -297,6 +298,83 @@ public extension CAIResponseMessageData {
conversation: "",
receivedAt: Date())
}

init(title: String,
subtitle: String,
headerImageName: String? = nil, // e.g. image in object card
featuredImageName: String? = nil, // e.g. image for carousel card
description: String? = nil,
inlineButtons: [UIModelDataAction]? = nil,
sections: [UIModelDataSection]? = nil,
status1: String? = nil,
status1_state: UIModelData.ValueState? = nil,
status2: String? = nil,
status2_state: UIModelData.ValueState? = nil,
status3: String? = nil,
status3_state: UIModelData.ValueState? = nil,
isBot: Bool = true)
{
var content: UIModelDataContent?
content = UIModelDataContent()
if inlineButtons != nil {
content?.buttons = inlineButtons
}
if sections != nil {
content?.sections = sections
}
content?.header = UIModelDataHeader(title: UIModelDataValue(value: title, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: nil),
subtitle: UIModelDataValue(value: subtitle, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: nil),
description: UIModelDataValue(value: description,
dataType: UIModelData.ValueType.text.rawValue,
rawValue: nil,
label: nil,
valueState: nil),
status1: UIModelDataValue(value: status1, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: status1_state?.rawValue),
status2: UIModelDataValue(value: status2, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: status2_state?.rawValue),
status3: UIModelDataValue(value: status3, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: status3_state?.rawValue),
image: nil)
if let img = headerImageName {
content?.header?.image = UIModelDataImage(imageUrl: img)
}
if let img = featuredImageName {
content?.picture = UIModelDataMedia(url: img)
}

var modelData = UIModelData(type: VisualizationType.object.rawValue,
delay: nil,
header: UIModelDataHeader(title: UIModelDataValue(value: title,
dataType: UIModelData.ValueType.text.rawValue,
rawValue: nil,
label: nil,
valueState: nil),
subtitle: UIModelDataValue(value: subtitle,
dataType: UIModelData.ValueType.text.rawValue,
rawValue: nil,
label: nil,
valueState: nil),
description: UIModelDataValue(value: description,
dataType: UIModelData.ValueType.text.rawValue,
rawValue: nil,
label: nil,
valueState: nil),
status1: UIModelDataValue(value: status1, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: nil),
status2: UIModelDataValue(value: status2, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: nil),
status3: UIModelDataValue(value: status3, dataType: UIModelData.ValueType.text.rawValue, rawValue: nil, label: nil, valueState: nil),
image: nil),
content: content,
detailsAvailable: false,
buttons: nil)

if let img = headerImageName {
modelData.header?.image = UIModelDataImage(imageUrl: img)
}

self.init(id: UUID().uuidString,
participant: CAIResponseParticipantData(isBot: isBot, senderId: "botA"),
attachment: modelData,
conversation: "",
receivedAt: Date())
}

init(imageName: String, _ isBot: Bool = true) {
var content = UIModelDataContent()
Expand Down
15 changes: 15 additions & 0 deletions Sources/SAPCAI/Foundation/Model/MessageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ extension ValueData {
var isClickable: Bool {
type == .link || type == .email || type == .phoneNumber
}

/// either original value (if present), formatted value for phoneNumber/email or empty string
var formattedValue: String {
if let content = value, let type = type {
switch type {
case .phoneNumber:
return content.toTelURLString()
case .email:
return "mailto:" + content
default:
return content
}
}
return ""
}
}

/// Enumeration for possible postback types
Expand Down
17 changes: 10 additions & 7 deletions Sources/SAPCAI/Foundation/Model/MessageTypeData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public protocol ObjectSectionData {
}

/// Protocol describing the content of an object card
public protocol ObjectMessageData {
public protocol ObjectMessageData: CardMessageData {
/// ID
var id: String { get }

Expand Down Expand Up @@ -125,19 +125,22 @@ public protocol CarouselMessageData {
}

/// Protocol describing the content of a carousel item
public protocol CarouselItemMessageData {
public protocol CarouselItemMessageData: CardMessageData {}

/// Protocol describing a card
public protocol CardMessageData {
/// ID
var id: String { get }

/// Picture
var itemPicture: MediaItem? { get }
var featuredImage: MediaItem? { get }

/// Header
var itemHeader: HeaderMessageData? { get }
var cardHeader: HeaderMessageData? { get }

/// List of actions
var itemButtons: [PostbackData]? { get }
var cardButtons: [PostbackData]? { get }

/// List of attributes
var itemSections: [UIModelDataSection]? { get }
var cardSections: [UIModelDataSection]? { get }
}
10 changes: 5 additions & 5 deletions Sources/SAPCAI/Foundation/Model/UIModelData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,20 @@ extension UIModelDataContent: ObjectMessageData {
}
}

extension UIModelDataContent: CarouselItemMessageData {
public var itemPicture: MediaItem? {
extension UIModelDataContent: CarouselItemMessageData, CardMessageData {
public var featuredImage: MediaItem? {
self.picture
}

public var itemHeader: HeaderMessageData? {
public var cardHeader: HeaderMessageData? {
self.header
}

public var itemButtons: [PostbackData]? {
public var cardButtons: [PostbackData]? {
self.buttons
}

public var itemSections: [UIModelDataSection]? {
public var cardSections: [UIModelDataSection]? {
self.sections
}
}
Expand Down
14 changes: 6 additions & 8 deletions Sources/SAPCAI/Foundation/ViewModel/MessagingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,12 @@ public struct URLOpenerData {
CAIResponseMessageData(text: "Hi! What can I do for you?", true),
CAIResponseMessageData(text: "Show me the latest product", false),
CAIResponseMessageData(text: "This product was just released today, hurry up before it is gone !!!", true, markdown: true),
CAIResponseMessageData("Laptop Lenovo",
"This is a great Laptop",
"sap-icon://order-status",
nil,
nil,
[UIModelDataAction("Order", "Order", .text), UIModelDataAction("Call Support", "Call Support", .text)],
nil,
"Available")
CAIResponseMessageData(title: "Laptop Lenovo",
subtitle: "This is a great Laptop",
headerImageName: "sap-icon://order-status",
inlineButtons: [UIModelDataAction("Order", "Order", .text), UIModelDataAction("Call Support", "Call Support", .text)],
status1: "Available",
status1_state: .success)
])
return viewModel

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

struct CarouselButtonsView: View {
struct CardPageButtonsView: View {
@EnvironmentObject private var themeManager: ThemeManager

var buttonsData: [PostbackData]?
Expand All @@ -25,8 +25,8 @@ struct CarouselButtonsView: View {
}
}

struct CarouselButtonsView_Previews: PreviewProvider {
struct CardPageButtonsView_Previews: PreviewProvider {
static var previews: some View {
CarouselButtonsView(buttonsData: nil)
CardPageButtonsView(buttonsData: nil)
}
}
Loading

0 comments on commit a585c84

Please sign in to comment.