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 {
|
2024-09-19 10:04:19 +03:00
|
|
|
alert.addAction(cancelAlertAction)
|
2024-09-10 09:31:53 +01:00
|
|
|
}
|
|
|
|
topController.present(alert, animated: true)
|
|
|
|
}
|
|
|
|
}
|
2024-09-19 10:04:19 +03:00
|
|
|
|
|
|
|
func showAlert(
|
|
|
|
_ title: String,
|
|
|
|
message: String? = nil,
|
|
|
|
actions: () -> [UIAlertAction] = { [okAlertAction] }
|
|
|
|
) {
|
|
|
|
if let topController = getTopViewController() {
|
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
for action in actions() { alert.addAction(action) }
|
|
|
|
topController.present(alert, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-02 11:53:09 +00:00
|
|
|
func showSheet(
|
|
|
|
_ title: String?,
|
|
|
|
message: String? = nil,
|
|
|
|
actions: () -> [UIAlertAction] = { [okAlertAction] },
|
|
|
|
sourceView: UIView? = nil // For iPad support
|
|
|
|
) {
|
|
|
|
if let topController = getTopViewController() {
|
|
|
|
let sheet = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
|
|
|
|
for action in actions() { sheet.addAction(action) }
|
|
|
|
|
|
|
|
// Required for iPad: Configure popover presentation
|
|
|
|
if let popover = sheet.popoverPresentationController {
|
|
|
|
popover.sourceView = sourceView ?? topController.view
|
|
|
|
popover.sourceRect = sourceView?.bounds ?? CGRect(x: topController.view.bounds.midX, y: topController.view.bounds.midY, width: 0, height: 0)
|
|
|
|
popover.permittedArrowDirections = []
|
|
|
|
}
|
|
|
|
|
|
|
|
topController.present(sheet, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-19 10:04:19 +03:00
|
|
|
let okAlertAction = UIAlertAction(title: NSLocalizedString("Ok", comment: "alert button"), style: .default)
|
|
|
|
|
|
|
|
let cancelAlertAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "alert button"), style: .cancel)
|
2025-06-04 07:47:10 +00:00
|
|
|
|
2025-06-26 08:22:10 +01:00
|
|
|
let alertProfileImageSize: CGFloat = 103
|
|
|
|
|
|
|
|
let alertWidth: CGFloat = 270
|
|
|
|
|
|
|
|
let alertButtonHeight: CGFloat = 44
|
|
|
|
|
2025-06-04 07:47:10 +00:00
|
|
|
class OpenChatAlertViewController: UIViewController {
|
|
|
|
private let profileName: String
|
2025-06-25 22:45:47 +01:00
|
|
|
private let profileFullName: String
|
2025-06-04 07:47:10 +00:00
|
|
|
private let profileImage: UIView
|
|
|
|
private let cancelTitle: String
|
|
|
|
private let confirmTitle: String
|
|
|
|
private let onCancel: () -> Void
|
|
|
|
private let onConfirm: () -> Void
|
|
|
|
|
|
|
|
init(
|
|
|
|
profileName: String,
|
2025-06-25 22:45:47 +01:00
|
|
|
profileFullName: String,
|
2025-06-04 07:47:10 +00:00
|
|
|
profileImage: UIView,
|
|
|
|
cancelTitle: String = "Cancel",
|
|
|
|
confirmTitle: String = "Open",
|
|
|
|
onCancel: @escaping () -> Void,
|
|
|
|
onConfirm: @escaping () -> Void
|
|
|
|
) {
|
|
|
|
self.profileName = profileName
|
2025-06-25 22:45:47 +01:00
|
|
|
self.profileFullName = profileFullName
|
2025-06-04 07:47:10 +00:00
|
|
|
self.profileImage = profileImage
|
|
|
|
self.cancelTitle = cancelTitle
|
|
|
|
self.confirmTitle = confirmTitle
|
|
|
|
self.onCancel = onCancel
|
|
|
|
self.onConfirm = onConfirm
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
|
|
|
|
modalPresentationStyle = .overFullScreen
|
|
|
|
modalTransitionStyle = .crossDissolve
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
view.backgroundColor = UIColor.black.withAlphaComponent(0.3)
|
|
|
|
|
|
|
|
// Container view
|
|
|
|
let containerView = UIView()
|
|
|
|
containerView.backgroundColor = .systemBackground
|
|
|
|
containerView.layer.cornerRadius = 12
|
|
|
|
containerView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(containerView)
|
|
|
|
|
|
|
|
// Profile image sizing
|
|
|
|
profileImage.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
2025-06-25 22:45:47 +01:00
|
|
|
profileImage.widthAnchor.constraint(equalToConstant: alertProfileImageSize),
|
|
|
|
profileImage.heightAnchor.constraint(equalToConstant: alertProfileImageSize)
|
2025-06-04 07:47:10 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
// Name label
|
|
|
|
let nameLabel = UILabel()
|
|
|
|
nameLabel.text = profileName
|
2025-06-25 22:45:47 +01:00
|
|
|
nameLabel.font = UIFont.preferredFont(forTextStyle: .headline)
|
2025-06-04 07:47:10 +00:00
|
|
|
nameLabel.textColor = .label
|
|
|
|
nameLabel.numberOfLines = 2
|
2025-06-25 22:45:47 +01:00
|
|
|
nameLabel.textAlignment = .center
|
2025-06-04 07:47:10 +00:00
|
|
|
nameLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
2025-06-25 22:45:47 +01:00
|
|
|
var profileViews = [profileImage, nameLabel]
|
|
|
|
|
|
|
|
// Full name label
|
|
|
|
if !profileFullName.isEmpty && profileFullName != profileName {
|
|
|
|
let fullNameLabel = UILabel()
|
|
|
|
fullNameLabel.text = profileFullName
|
|
|
|
fullNameLabel.font = UIFont.preferredFont(forTextStyle: .subheadline)
|
|
|
|
fullNameLabel.textColor = .label
|
|
|
|
fullNameLabel.numberOfLines = 2
|
|
|
|
fullNameLabel.textAlignment = .center
|
|
|
|
fullNameLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
profileViews.append(fullNameLabel)
|
|
|
|
}
|
|
|
|
|
2025-06-04 07:47:10 +00:00
|
|
|
// Horizontal stack for image + name
|
2025-06-25 22:45:47 +01:00
|
|
|
let stack = UIStackView(arrangedSubviews: profileViews)
|
|
|
|
stack.axis = .vertical
|
|
|
|
stack.spacing = 12
|
|
|
|
stack.alignment = .center
|
|
|
|
stack.translatesAutoresizingMaskIntoConstraints = false
|
2025-06-04 07:47:10 +00:00
|
|
|
|
|
|
|
let topRowContainer = UIView()
|
|
|
|
topRowContainer.translatesAutoresizingMaskIntoConstraints = false
|
2025-06-25 22:45:47 +01:00
|
|
|
topRowContainer.addSubview(stack)
|
2025-06-04 07:47:10 +00:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
2025-06-25 22:45:47 +01:00
|
|
|
stack.topAnchor.constraint(equalTo: topRowContainer.topAnchor),
|
|
|
|
stack.bottomAnchor.constraint(equalTo: topRowContainer.bottomAnchor),
|
|
|
|
stack.leadingAnchor.constraint(equalTo: topRowContainer.leadingAnchor, constant: 20),
|
|
|
|
stack.trailingAnchor.constraint(equalTo: topRowContainer.trailingAnchor, constant: -20)
|
2025-06-04 07:47:10 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
// Buttons
|
|
|
|
let cancelButton = UIButton(type: .system)
|
|
|
|
cancelButton.setTitle(cancelTitle, for: .normal)
|
2025-06-25 22:45:47 +01:00
|
|
|
let bodyDescr = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body)
|
|
|
|
cancelButton.titleLabel?.font = UIFont(descriptor: bodyDescr.withSymbolicTraits(.traitBold) ?? bodyDescr, size: 0)
|
2025-06-04 07:47:10 +00:00
|
|
|
cancelButton.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
|
|
|
|
|
|
|
|
let confirmButton = UIButton(type: .system)
|
|
|
|
confirmButton.setTitle(confirmTitle, for: .normal)
|
2025-06-25 22:45:47 +01:00
|
|
|
confirmButton.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body)
|
2025-06-04 07:47:10 +00:00
|
|
|
confirmButton.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
|
|
|
|
|
2025-06-26 08:22:10 +01:00
|
|
|
let verticalButtons = cancelButton.intrinsicContentSize.width + 20 >= alertWidth / 2 || confirmButton.intrinsicContentSize.width + 20 >= alertWidth / 2
|
|
|
|
|
2025-06-04 07:47:10 +00:00
|
|
|
// Button stack with equal width buttons
|
2025-06-26 08:22:10 +01:00
|
|
|
let buttonStack = UIStackView(arrangedSubviews: verticalButtons ? [confirmButton, cancelButton] : [cancelButton, confirmButton])
|
|
|
|
buttonStack.axis = verticalButtons ? .vertical : .horizontal
|
2025-06-04 07:47:10 +00:00
|
|
|
buttonStack.distribution = .fillEqually
|
|
|
|
buttonStack.spacing = 0 // no spacing, use divider instead
|
|
|
|
buttonStack.translatesAutoresizingMaskIntoConstraints = false
|
2025-06-26 08:22:10 +01:00
|
|
|
buttonStack.heightAnchor.constraint(greaterThanOrEqualToConstant: alertButtonHeight * (verticalButtons ? 2 : 1)).isActive = true
|
2025-06-04 07:47:10 +00:00
|
|
|
|
|
|
|
// Vertical stack containing hStack and buttonStack
|
|
|
|
let vStack = UIStackView(arrangedSubviews: [topRowContainer, buttonStack])
|
|
|
|
vStack.axis = .vertical
|
|
|
|
vStack.spacing = 16
|
|
|
|
vStack.alignment = .fill // important: buttons stretch full width
|
|
|
|
vStack.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
containerView.addSubview(vStack)
|
|
|
|
|
|
|
|
// Add horizontal divider above buttons
|
|
|
|
let horizontalDivider = UIView()
|
2025-06-25 22:45:47 +01:00
|
|
|
horizontalDivider.backgroundColor = UIColor.separator
|
2025-06-04 07:47:10 +00:00
|
|
|
horizontalDivider.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
containerView.addSubview(horizontalDivider)
|
|
|
|
|
2025-06-26 08:22:10 +01:00
|
|
|
// Add divider between buttons
|
|
|
|
let buttonDivider = UIView()
|
|
|
|
buttonDivider.backgroundColor = UIColor.separator
|
|
|
|
buttonDivider.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
buttonStack.addSubview(buttonDivider)
|
2025-06-04 07:47:10 +00:00
|
|
|
|
|
|
|
// Constraints
|
2025-06-26 08:22:10 +01:00
|
|
|
let buttonDividerConstraints = if verticalButtons {
|
|
|
|
[
|
|
|
|
buttonDivider.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
|
|
|
|
buttonDivider.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
|
|
|
|
buttonDivider.centerYAnchor.constraint(equalTo: buttonStack.centerYAnchor),
|
|
|
|
buttonDivider.heightAnchor.constraint(equalToConstant: 1 / UIScreen.main.scale)
|
|
|
|
]
|
|
|
|
} else {
|
|
|
|
[
|
|
|
|
buttonDivider.topAnchor.constraint(equalTo: buttonStack.topAnchor),
|
|
|
|
buttonDivider.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
|
|
|
|
buttonDivider.centerXAnchor.constraint(equalTo: buttonStack.centerXAnchor),
|
|
|
|
buttonDivider.widthAnchor.constraint(equalToConstant: 1 / UIScreen.main.scale)
|
|
|
|
]
|
|
|
|
}
|
2025-06-04 07:47:10 +00:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
// Container view centering and fixed width
|
|
|
|
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
|
|
|
|
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
2025-06-26 08:22:10 +01:00
|
|
|
containerView.widthAnchor.constraint(equalToConstant: alertWidth),
|
2025-06-04 07:47:10 +00:00
|
|
|
|
|
|
|
// Vertical stack padding inside containerView
|
|
|
|
vStack.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 20),
|
|
|
|
vStack.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0),
|
|
|
|
vStack.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0),
|
|
|
|
vStack.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0),
|
|
|
|
|
|
|
|
// Center hStack horizontally inside vStack's padded width
|
2025-06-25 22:45:47 +01:00
|
|
|
stack.centerXAnchor.constraint(equalTo: vStack.centerXAnchor),
|
2025-06-04 07:47:10 +00:00
|
|
|
|
|
|
|
// Horizontal divider above buttons
|
|
|
|
horizontalDivider.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
|
|
|
|
horizontalDivider.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
|
|
|
|
horizontalDivider.bottomAnchor.constraint(equalTo: buttonStack.topAnchor),
|
2025-06-26 08:22:10 +01:00
|
|
|
horizontalDivider.heightAnchor.constraint(equalToConstant: 1 / UIScreen.main.scale)
|
|
|
|
] + buttonDividerConstraints)
|
2025-06-04 07:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func cancelTapped() {
|
|
|
|
dismiss(animated: true) {
|
|
|
|
self.onCancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func confirmTapped() {
|
|
|
|
dismiss(animated: true) {
|
|
|
|
self.onConfirm()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func showOpenChatAlert<Content: View>(
|
|
|
|
profileName: String,
|
2025-06-25 22:45:47 +01:00
|
|
|
profileFullName: String,
|
2025-06-04 07:47:10 +00:00
|
|
|
profileImage: Content,
|
|
|
|
theme: AppTheme,
|
|
|
|
cancelTitle: String = "Cancel",
|
|
|
|
confirmTitle: String = "Open",
|
|
|
|
onCancel: @escaping () -> Void = {},
|
|
|
|
onConfirm: @escaping () -> Void
|
|
|
|
) {
|
|
|
|
let themedView = profileImage.environmentObject(theme)
|
|
|
|
let hostingController = UIHostingController(rootView: themedView)
|
|
|
|
let hostedView = hostingController.view!
|
|
|
|
hostedView.backgroundColor = .clear
|
|
|
|
|
|
|
|
if let topVC = getTopViewController() {
|
|
|
|
let alertVC = OpenChatAlertViewController(
|
|
|
|
profileName: profileName,
|
2025-06-25 22:45:47 +01:00
|
|
|
profileFullName: profileFullName,
|
2025-06-04 07:47:10 +00:00
|
|
|
profileImage: hostedView,
|
|
|
|
cancelTitle: cancelTitle,
|
|
|
|
confirmTitle: confirmTitle,
|
|
|
|
onCancel: onCancel,
|
|
|
|
onConfirm: onConfirm
|
|
|
|
)
|
|
|
|
topVC.present(alertVC, animated: true)
|
|
|
|
}
|
|
|
|
}
|