Skip to content

Commit

Permalink
Add new public interfaces to the Entry Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmustautsglia authored and Egor Egorov committed Oct 22, 2024
1 parent 8a85626 commit bd13d65
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
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

0 comments on commit bd13d65

Please sign in to comment.