Skip to content
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

Postback button actions caching #734

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 51 additions & 23 deletions GliaWidgets/Sources/ViewModel/Chat/ChatViewModel+GVA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,32 @@ extension ChatViewModel {

func postbackButtonAction(for option: GvaOption) -> Cmd {
.init { [weak self] in
guard let value = option.value else { return }
let singleChoiceOption = SingleChoiceOption(text: option.text, value: value)
self?.environment.sendSelectedOptionValue(singleChoiceOption) { [weak self] result in
guard let self = self else { return }
switch result {
case let .success(message):
let chatMessage = ChatMessage(with: message)
let item = ChatItem(
with: chatMessage,
isCustomCardSupported: self.isCustomCardSupported
)
if let item {
self.appendItem(item, to: self.messagesSection, animated: true)
}
self.action?(.scrollToBottom(animated: true))
case .failure:
self.showAlert(
with: self.alertConfiguration.unexpectedError,
dismissed: nil
)
}
guard let self, let value = option.value else { return }

let attachment = CoreSdkClient.Attachment(
type: .singleChoiceResponse,
selectedOption: option.value,
options: nil,
files: nil,
imageUrl: nil
)

let outgoingMessage = OutgoingMessage(
content: option.text,
attachment: attachment
)

switch self.interactor.state {
case .engaged:
let singleChoiceOption = SingleChoiceOption(text: option.text, value: value)
self.sendOption(singleChoiceOption)

case .enqueued:
self.handle(pendingMessage: outgoingMessage)

case .enqueueing, .ended, .none:
self.handle(pendingMessage: outgoingMessage)
self.enqueue(mediaType: .text)
}
}
}
Expand All @@ -66,7 +71,7 @@ extension ChatViewModel {
private extension ChatViewModel {
func urlButtonAction(url: URL, urlTarget: String?) -> Cmd {
.init { [weak self] in
guard let self = self else { return }
guard let self else { return }

let openUrl = { [weak self] url in
guard let self = self else { return }
Expand Down Expand Up @@ -98,11 +103,34 @@ private extension ChatViewModel {

func broadcastEventButtonAction() -> Cmd {
.init { [weak self] in
guard let self = self else { return }
guard let self else { return }
self.showAlert(
with: self.alertConfiguration.unsupportedGvaBroadcastError,
dismissed: nil
)
}
}

func sendOption(_ option: SingleChoiceOption) {
environment.sendSelectedOptionValue(option) { [weak self] result in
guard let self else { return }
switch result {
case let .success(message):
let chatMessage = ChatMessage(with: message)
let item = ChatItem(
with: chatMessage,
isCustomCardSupported: self.isCustomCardSupported
)
if let item {
self.appendItem(item, to: self.messagesSection, animated: true)
}
self.action?(.scrollToBottom(animated: true))
case .failure:
self.showAlert(
with: self.alertConfiguration.unexpectedError,
dismissed: nil
)
}
}
}
}
7 changes: 5 additions & 2 deletions GliaWidgets/Sources/ViewModel/Chat/ChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ class ChatViewModel: EngagementViewModel, ViewModel {
fetchSiteConfigurations()

pendingMessages.forEach { [weak self] outgoingMessage in
self?.interactor.send(outgoingMessage.content, attachment: nil) { [weak self] message in
self?.interactor.send(
outgoingMessage.content,
attachment: outgoingMessage.attachment
) { [weak self] message in
guard let self = self else { return }

self.replace(
Expand Down Expand Up @@ -501,7 +504,7 @@ extension ChatViewModel {
messageText = ""
}

private func handle(pendingMessage: OutgoingMessage) {
func handle(pendingMessage: OutgoingMessage) {
switch interactor.state {
case .engaged: return
case .enqueueing, .enqueued, .ended, .none:
Expand Down
9 changes: 7 additions & 2 deletions GliaWidgets/Sources/ViewModel/Chat/Data/OutgoingMessage.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import Foundation
import GliaCoreSDK

class OutgoingMessage: Equatable {
let id: String
let content: String
let files: [LocalFile]
let attachment: Attachment?

init(
id: String = UUID().uuidString,
content: String,
files: [LocalFile] = []
files: [LocalFile] = [],
attachment: Attachment? = nil
) {
self.id = id
self.content = content
self.files = files
self.attachment = attachment
}

static func == (lhs: OutgoingMessage, rhs: OutgoingMessage) -> Bool {
lhs.id == rhs.id &&
lhs.content == rhs.content &&
lhs.files == rhs.files
lhs.files == rhs.files &&
lhs.attachment == rhs.attachment
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,37 @@ extension ChatViewModelTests {
env.sendSelectedOptionValue = { option, _ in
calls.append(.sendOption(option.text, option.value))
}
viewModel = .mock(environment: env)
let interactorMock = Interactor.mock()
interactorMock.state = .engaged(nil)
viewModel = .mock(interactor: interactorMock, environment: env)

viewModel.gvaOptionAction(for: option)()

XCTAssertEqual(calls, [.sendOption("text", "value")])
}

func test_gvaPostbackButtonActionTriggersStartEngagement() {
let option = GvaOption.mock(text: "text", value: "value")
var calls: [Call] = []
var env = ChatViewModel.Environment.mock
// To ensure `open` is not called in case of URL Button
env.uiApplication.open = { url in
calls.append(.openUrl(url.absoluteString))
}
// To ensure `sendSelectedOptionValue` is not called in case of Postback Button
env.sendSelectedOptionValue = { option, _ in
calls.append(.sendOption(option.text, option.value))
}
let interactorMock = Interactor.mock()
interactorMock.state = .none
viewModel = .mock(interactor: interactorMock, environment: env)

viewModel.gvaOptionAction(for: option)()

XCTAssertEqual(calls, [])
XCTAssertEqual(interactorMock.state, .enqueueing)
}

func test_broadcastEventAction() {
let option = GvaOption.mock(text: "text", destinationPdBroadcastEvent: "mock")
var calls: [Call] = []
Expand Down