Skip to content

Commit

Permalink
Fix actionSheet position for iPad (#194)
Browse files Browse the repository at this point in the history
* Fix actionSheet for iPad

* Fix Meny example for iPads
  • Loading branch information
solilin committed May 2, 2024
1 parent 90227d6 commit cba8776
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions Demo/Strada/MenuComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,58 @@ import UIKit
/// which will send the selected index of the tapped menu item back to the web.
final class MenuComponent: BridgeComponent {
override class var name: String { "menu" }

override func onReceive(message: Message) {
guard let event = Event(rawValue: message.event) else {
return
}

switch event {
case .display:
handleDisplayEvent(message: message)
}
}

// MARK: Private

private var viewController: UIViewController? {
delegate.destination as? UIViewController
}

private func handleDisplayEvent(message: Message) {
guard let data: MessageData = message.data() else { return }
showAlertSheet(with: data.title, items: data.items)
}

private func showAlertSheet(with title: String, items: [Item]) {
let alertController = UIAlertController(title: title,
message: nil,
preferredStyle: .actionSheet)

for item in items {
let action = UIAlertAction(title: item.title, style: .default) {[weak self] _ in
self?.onItemSelected(item: item)
}
alertController.addAction(action)
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(cancelAction)


// Set popoverController for iPads
if let popoverController = alertController.popoverPresentationController {
if let barButtonItem = viewController?.navigationItem.rightBarButtonItem {
popoverController.barButtonItem = barButtonItem
} else {
popoverController.sourceView = viewController?.view
popoverController.sourceRect = viewController?.view.bounds ?? .zero
popoverController.permittedArrowDirections = []
}
}

viewController?.present(alertController, animated: true)
}

private func onItemSelected(item: Item) {
reply(to: Event.display.rawValue,
with: SelectionMessageData(selectedIndex: item.index))
Expand All @@ -68,12 +79,12 @@ private extension MenuComponent {
let title: String
let items: [Item]
}

struct Item: Decodable {
let title: String
let index: Int
}

struct SelectionMessageData: Encodable {
let selectedIndex:Int
}
Expand Down

0 comments on commit cba8776

Please sign in to comment.