2022-02-12 15:59:43 +00:00
|
|
|
//
|
|
|
|
// ShareSheet.swift
|
|
|
|
// SimpleX
|
|
|
|
//
|
|
|
|
// Created by Evgeny Poberezkin on 30/01/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-09-10 09:31:53 +01:00
|
|
|
func getTopViewController() -> UIViewController? {
|
2022-02-12 15:59:43 +00:00
|
|
|
let keyWindowScene = UIApplication.shared.connectedScenes.first { $0.activationState == .foregroundActive } as? UIWindowScene
|
|
|
|
if let keyWindow = keyWindowScene?.windows.filter(\.isKeyWindow).first,
|
2024-09-10 09:31:53 +01:00
|
|
|
let rootViewController = keyWindow.rootViewController {
|
2024-09-04 23:12:05 +01:00
|
|
|
// Find the top-most presented view controller
|
|
|
|
var topController = rootViewController
|
|
|
|
while let presentedViewController = topController.presentedViewController {
|
|
|
|
topController = presentedViewController
|
|
|
|
}
|
2024-09-10 09:31:53 +01:00
|
|
|
return topController
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func showShareSheet(items: [Any], completed: (() -> Void)? = nil) {
|
|
|
|
if let topController = getTopViewController() {
|
2022-02-12 15:59:43 +00:00
|
|
|
let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
|
2023-09-07 11:28:37 +01:00
|
|
|
if let completed = completed {
|
2024-09-04 23:12:05 +01:00
|
|
|
activityViewController.completionWithItemsHandler = { _, _, _, _ in
|
|
|
|
completed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
topController.present(activityViewController, animated: true)
|
2022-02-12 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-10 09:31:53 +01:00
|
|
|
|
|
|
|
func showAlert(
|
|
|
|
title: String,
|
|
|
|
message: String? = nil,
|
|
|
|
buttonTitle: String,
|
|
|
|
buttonAction: @escaping () -> Void,
|
|
|
|
cancelButton: Bool
|
|
|
|
) -> Void {
|
|
|
|
if let topController = getTopViewController() {
|
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
alert.addAction(UIAlertAction(title: buttonTitle, style: .default) { _ in
|
|
|
|
buttonAction()
|
|
|
|
})
|
|
|
|
if cancelButton {
|
|
|
|
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "alert button"), style: .cancel))
|
|
|
|
}
|
|
|
|
topController.present(alert, animated: true)
|
|
|
|
}
|
|
|
|
}
|