Skip to content

Commit

Permalink
Introduce list queue interface
Browse files Browse the repository at this point in the history
Added mechanism for fetching queue id dynamically
and assigning into settings.

MOB-2594
  • Loading branch information
yurii-glia committed Sep 14, 2023
1 parent 1d94626 commit acd520a
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 118 deletions.
27 changes: 27 additions & 0 deletions GliaWidgets/Public/Glia/Glia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,33 @@ public class Glia {
failure: { completion(.failure($0)) }
)
}

/// List all Queues of the configured site.
/// It is also possible to monitor Queues changes with [subscribeForUpdates](x-source-tag://subscribeForUpdates) method.
/// If the request is unsuccessful for any reason then the completion will have an Error.
/// - Parameters:
/// - completion: A callback that will return the Result struct with `Queue` list or `GliaCoreError`
///
public func listQueues(_ completion: @escaping (Result<[Queue], Error>) -> Void) {
guard interactor != nil else {
completion(.failure(GliaError.sdkIsNotConfigured))
return
}

environment.coreSdk.listQueues { queues, error in
if let error {
completion(.failure(error))
return
}

if let queues {
completion(.success(queues))
return
}

completion(.failure(GliaError.internalError))
}
}
}

// MARK: - Private
Expand Down
1 change: 1 addition & 0 deletions GliaWidgets/Public/GliaError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public enum GliaError: Error {
case configuringDuringEngagementIsNotAllowed
case clearingVisitorSessionDuringEngagementIsNotAllowed
case startingEngagementWithNoQueueIdsIsNotAllowed
case internalError
}
14 changes: 2 additions & 12 deletions TestingApp/Settings/Cells/EnvironmentSettingsTextCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,8 @@ final class EnvironmentSettingsTextCell: SettingsCell {
}

private func layout() {
contentView.addSubview(stackView)
stackView.addArrangedSubview(segmentedControl)
stackView.addArrangedSubview(customEnvironmentUrlTextField)
stackView.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint](); defer { constraints.activate() }
let insets = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 20)
constraints += stackView.layoutInSuperview(edges: .vertical, insets: insets)
constraints += stackView.layoutInSuperview(edges: .trailing, insets: insets)
constraints += stackView.leadingAnchor.constraint(
equalTo: titleLabel.trailingAnchor,
constant: 10
)
contentStackView.addArrangedSubview(segmentedControl)
contentStackView.addArrangedSubview(customEnvironmentUrlTextField)
}

@objc
Expand Down
22 changes: 11 additions & 11 deletions TestingApp/Settings/Cells/SettingsCell.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import UIKit

class SettingsCell: UITableViewCell {
lazy var contentStackView = UIStackView.make(.horizontal, spacing: 8)(
titleLabel
)
let titleLabel = UILabel()

init(title: String) {
Expand All @@ -21,20 +24,17 @@ class SettingsCell: UITableViewCell {
titleLabel.font = .systemFont(ofSize: 15)
titleLabel.textColor = .darkGray
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.minimumScaleFactor = 0.8
}

private func layout() {
contentView.addSubview(titleLabel)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
contentStackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(contentStackView)
var constraints = [NSLayoutConstraint](); defer { constraints.activate() }
let insets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
constraints += titleLabel.layoutInSuperview(
edges: .vertical,
insets: insets
)
constraints += titleLabel.layoutInSuperview(
edges: .leading,
insets: insets
)
let insets = UIEdgeInsets(top: 4, left: 16, bottom: 4, right: 16)
constraints += contentStackView.layoutInSuperview(insets: insets)

titleLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
}
}
35 changes: 5 additions & 30 deletions TestingApp/Settings/Cells/SettingsColorCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,15 @@ class SettingsColorCell: SettingsCell {
}

private func layout() {
contentView.addSubview(sampleView)
sampleView.translatesAutoresizingMaskIntoConstraints = false
contentStackView.addArrangedSubview(UIView())
contentStackView.addArrangedSubview(sampleView)
contentStackView.addArrangedSubview(rgbTextField)
contentStackView.addArrangedSubview(alphaTextField)

var constraints = [NSLayoutConstraint](); defer { constraints.activate() }
constraints += sampleView.match(value: 30)
constraints += sampleView.leadingAnchor.constraint(
greaterThanOrEqualTo: titleLabel.trailingAnchor,
constant: 20
)
constraints += sampleView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)

contentView.addSubview(rgbTextField)
rgbTextField.translatesAutoresizingMaskIntoConstraints = false
constraints += rgbTextField.match(.width, value: 100)
constraints += rgbTextField.layoutInSuperview(
edges: .vertical,
insets: .init(top: 10, left: 0, bottom: 10, right: 0)
)
constraints += rgbTextField.leadingAnchor.constraint(
equalTo: sampleView.trailingAnchor,
constant: 10
)

contentView.addSubview(alphaTextField)
alphaTextField.translatesAutoresizingMaskIntoConstraints = false
constraints += alphaTextField.match(.width, value: 50)
constraints += alphaTextField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)
constraints += alphaTextField.leadingAnchor.constraint(
equalTo: rgbTextField.trailingAnchor,
constant: 10
)
constraints += alphaTextField.layoutInSuperview(
edges: .trailing,
insets: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 20)
)
}

@objc private func updateSample() {
Expand Down
12 changes: 1 addition & 11 deletions TestingApp/Settings/Cells/SettingsFontCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,7 @@ class SettingsFontCell: SettingsCell {
}

private func layout() {
contentView.addSubview(pickerView)
pickerView.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint](); defer { constraints.activate() }
constraints += pickerView.match(value: 240)
constraints += pickerView.leadingAnchor.constraint(
equalTo: titleLabel.trailingAnchor,
constant: 10
)
let insets = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 20)
constraints += pickerView.layoutInSuperview(edges: .vertical, insets: insets)
constraints += pickerView.layoutInSuperview(edges: .trailing, insets: insets)
contentStackView.addArrangedSubview(pickerView)
}

private func selectDefaultFont() {
Expand Down
35 changes: 2 additions & 33 deletions TestingApp/Settings/Cells/SettingsSwitchCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,7 @@ final class SettingsSwitchCell: SettingsCell {
}

private func layout() {
contentView.addSubview(switcher)
switcher.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint](); defer { constraints.activate() }
let insets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 20)
constraints += switcher.layoutInSuperview(edges: .vertical, insets: insets)
constraints += switcher.layoutInSuperview(edges: .trailing, insets: insets)
constraints += switcher.leadingAnchor.constraint(
equalTo: titleLabel.trailingAnchor,
constant: 10
)
}
}

final class SettingsSegmentedCell: SettingsCell {
let segmentedControl: UISegmentedControl

init(title: String, items: String...) {
self.segmentedControl = .init(items: items.map(NSString.init(string:)))
super.init(title: title)
layout()
}

private func layout() {
contentView.addSubview(segmentedControl)
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint](); defer { constraints.activate() }
let insets = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 20)
constraints += segmentedControl.layoutInSuperview(edges: .vertical, insets: insets)
constraints += segmentedControl.layoutInSuperview(edges: .trailing, insets: insets)
constraints += segmentedControl.leadingAnchor.constraint(
equalTo: titleLabel.trailingAnchor,
constant: 10
)
titleLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
contentStackView.addArrangedSubview(switcher)
}
}
61 changes: 49 additions & 12 deletions TestingApp/Settings/Cells/SettingsTextCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,68 @@ import UIKit

class SettingsTextCell: SettingsCell {
let textField = UITextField()
var button: UIButton?

init(title: String, text: String) {
init(
title: String,
text: String,
isRequired: Bool = false,
extraButton: ExtraButton? = nil
) {
textField.text = text
if let extraButton {
self.button = .init(type: .system)
self.button?.setTitle(extraButton.title, for: .normal)
self.button?.addTarget(extraButton, action: #selector(extraButton.onTouchUpInside), for: .touchUpInside)
}
self.extraButton = extraButton
self.isRequired = isRequired

super.init(title: title)

if isRequired {
let attributedString = NSMutableAttributedString(string: "\(title) *")
attributedString.setAttributes([.foregroundColor: UIColor.red.cgColor], range: .init(location: attributedString.length - 1, length: 1))
titleLabel.attributedText = attributedString
}
setup()
layout()
}

// MARK: - Private

private let extraButton: ExtraButton?
private let isRequired: Bool

private func setup() {
textField.borderStyle = .roundedRect
textField.autocorrectionType = .no
textField.autocapitalizationType = .none
if let button {
addSubview(button)
}
}

private func layout() {
contentView.addSubview(textField)
textField.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint](); defer { constraints.activate() }
constraints += textField.widthAnchor.constraint(
equalTo: contentView.widthAnchor,
multiplier: 0.7
)
constraints += textField.leadingAnchor.constraint(greaterThanOrEqualTo: titleLabel.trailingAnchor, constant: 10)
let insets = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 20)
constraints += textField.layoutInSuperview(edges: .vertical, insets: insets)
constraints += textField.layoutInSuperview(edges: .trailing, insets: insets)
contentStackView.addArrangedSubview(textField)
if let button {
contentStackView.addArrangedSubview(button)
}
}
}

extension SettingsTextCell {
class ExtraButton {
let title: String
let tap: () -> Void

init(title: String, tap: @escaping () -> Void) {
self.title = title
self.tap = tap
}

@objc func onTouchUpInside() {
tap()
}
}
}
34 changes: 30 additions & 4 deletions TestingApp/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,48 @@ private extension SettingsViewController {
private func createCells() {
siteApiKeyIdCell = SettingsTextCell(
title: "Identifier:",
text: props.config.siteApiKeyId
text: props.config.siteApiKeyId,
isRequired: true
)
siteApiKeyIdCell.textField.accessibilityIdentifier = "settings_siteApiKeyId_textfield"
siteApiKeySecretCell = SettingsTextCell(
title: "Secret:",
text: props.config.siteApiKeySecret
text: props.config.siteApiKeySecret,
isRequired: true
)
siteApiKeySecretCell.textField.accessibilityIdentifier = "settings_siteApiKeySecret_textfield"
siteCell = SettingsTextCell(
title: "Site:",
text: props.config.site
text: props.config.site,
isRequired: true
)
siteCell.textField.accessibilityIdentifier = "settings_siteId_textfield"
queueIDCell = SettingsTextCell(
title: "Queue ID:",
text: props.queueId
text: props.queueId,
extraButton: .init(title: "List queues", tap: { [weak self] in
let alert = UIAlertController(title: "List queues", message: "Please choose queue", preferredStyle: .alert)

Glia.sharedInstance.listQueues { [weak self] result in
switch result {
case .success(let queues):
queues.forEach { queue in
alert.addAction(.init(title: queue.name, style: .default) { _ in
self?.queueIDCell.textField.text = queue.id
self?.props.changeQueueId(queue.id)
alert.dismiss(animated: true)
})
}
case .failure(let failure):
let action = UIAlertAction(title: "\(failure)", style: .default)
action.isEnabled = false
alert.addAction(action)
}
alert.addAction(.init(title: "Close", style: .cancel))
}

self?.present(alert, animated: true)
})
)
queueIDCell.textField.accessibilityIdentifier = "settings_queueId_textfield"
environmentCell = EnvironmentSettingsTextCell(
Expand Down
6 changes: 1 addition & 5 deletions TestingApp/ViewController/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ class ViewController: UIViewController {
// only if such engagement exists, we need
// to configure SDK, and only then attempt
// to end engagement.
try Glia.sharedInstance.configure(
with: configuration,
queueId: queueId
) {
try Glia.sharedInstance.configure(with: configuration) {
Glia.sharedInstance.endEngagement { result in
print("End engagement operation has been executed. Result='\(result)'.")
}
Expand Down Expand Up @@ -220,7 +217,6 @@ extension ViewController {
do {
try Glia.sharedInstance.configure(
with: configuration,
queueId: queueId,
uiConfig: uiConfig
) {
completion("SDK has been configured")
Expand Down

0 comments on commit acd520a

Please sign in to comment.