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

Fix actionSheet position for iPad #194

Merged
merged 2 commits into from
May 2, 2024
Merged
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
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