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

Add new public interfaces to the Entry Widget #1075

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
49 changes: 29 additions & 20 deletions GliaWidgets/Sources/EntryWidget/EntryWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import GliaCoreSDK
import UIKit
import SwiftUI

/// The `EntryWidget` class is a customizable UI component designed to display
/// engagement options (such as chat, audio, video, or secure messaging) based on
/// the availability of media types in the given queues. It offers functionality to
/// either embed itself within a view or present as a modal sheet.
public final class EntryWidget: NSObject {
private var hostedViewController: UIViewController?
private var embeddedView: UIView?
private var queueIds: [String]
private var cancellables = CancelBag()
private let environment: Environment

@Published private var viewState: ViewState = .loading

Expand All @@ -21,14 +27,14 @@ public final class EntryWidget: NSObject {
dividerHeight: 1
)

private let environment: Environment

private var cancellables = CancelBag()
// MARK: - Initialization

init(
queueIds: [String],
environment: Environment
) {
/// Initializes the `EntryWidget` with queue identifiers and an environment.
///
/// - Parameters:
/// - queueIds: An array of strings representing the queue identifiers.
/// - environment: An `Environment` object containing external dependencies.
init(queueIds: [String], environment: Environment) {
self.queueIds = queueIds
self.environment = environment
super.init()
Expand All @@ -37,25 +43,28 @@ public final class EntryWidget: NSObject {
.sink(receiveValue: handleQueuesMonitorUpdates(state:))
.store(in: &cancellables)
}
}

public func show(by presentation: EntryWidget.Presentation) {
defer {
environment.queuesMonitor.startMonitoring(queuesIds: queueIds)
}
switch presentation {
case let .sheet(parentViewController):
showSheet(in: parentViewController)
case let .embedded(parentView):
showView(in: parentView)
}
// MARK: - Public methods
public extension EntryWidget {
/// Displays the widget as a modal sheet in the given view controller.
///
/// - Parameter viewController: The `UIViewController` in which the widget will be shown as a sheet.
func show(in viewController: UIViewController) {
showSheet(in: viewController)
}

/// Embeds the widget as a view in the given parent view.
///
/// - Parameter view: The `UIView` in which the widget will be embedded.
func embed(in view: UIView) {
showView(in: view)
}

/// Hides the widget, dismissing any presented sheets and stopping queue monitoring.
func hide() {
hostedViewController?.dismiss(animated: true, completion: nil)
hostedViewController = nil
embeddedView?.removeFromSuperview()
embeddedView = nil

environment.queuesMonitor.stopMonitoring()
}
}
Expand Down
4 changes: 2 additions & 2 deletions TestingApp/ViewController/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class ViewController: UIViewController {
}

@IBAction private func entryWidgetSheetTapped() {
self.entryWidget?.show(by: .sheet(self))
self.entryWidget?.show(in: self)
}

@IBAction private func entryWidgetEmbbededTapped() {
self.entryWidget?.show(by: .embedded(entryWidgetView))
self.entryWidget?.embed(in: entryWidgetView)
}

@IBAction private func audioTapped() {
Expand Down
Loading