2022-12-16 23:59:26 +01:00
|
|
|
//
|
|
|
|
// InputAlertView.swift
|
|
|
|
// Browse
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 16/12/2022.
|
|
|
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import DataModels
|
|
|
|
import SwiftUI
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
struct InputAlertView: UIViewControllerRepresentable {
|
|
|
|
|
|
|
|
// MARK: Bindings
|
|
|
|
|
|
|
|
@Binding private var isPresenting: Bool
|
|
|
|
@Binding private var textFieldString: String
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
private let title: String
|
|
|
|
private let message: String
|
|
|
|
private let textFieldPlaceholder: String
|
|
|
|
private let actions: [UIAlertAction]
|
|
|
|
|
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
init(
|
|
|
|
isPresenting: Binding<Bool>,
|
|
|
|
title: String,
|
|
|
|
message: String,
|
|
|
|
textFieldPlaceholder: String,
|
|
|
|
textFieldString: Binding<String>,
|
|
|
|
actions: [UIAlertAction]
|
|
|
|
) {
|
|
|
|
self.title = title
|
|
|
|
self.message = message
|
|
|
|
self.textFieldPlaceholder = textFieldPlaceholder
|
|
|
|
self.actions = actions
|
|
|
|
|
|
|
|
self._isPresenting = isPresenting
|
|
|
|
self._textFieldString = textFieldString
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
func makeUIViewController(
|
|
|
|
context: UIViewControllerRepresentableContext<InputAlertView>
|
|
|
|
) -> UIViewController {
|
|
|
|
.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUIViewController(
|
|
|
|
_ viewController: UIViewController,
|
|
|
|
context: UIViewControllerRepresentableContext<InputAlertView>
|
|
|
|
) {
|
|
|
|
guard
|
|
|
|
context.coordinator.alert == nil,
|
|
|
|
isPresenting
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
let alertController = {
|
|
|
|
let alert = UIAlertController(
|
|
|
|
title: title,
|
|
|
|
message: message,
|
|
|
|
preferredStyle: .alert
|
|
|
|
)
|
|
|
|
|
|
|
|
alert.addTextField { textField in
|
|
|
|
textField.placeholder = textFieldPlaceholder
|
|
|
|
textField.text = textFieldString
|
|
|
|
textField.delegate = context.coordinator
|
|
|
|
}
|
|
|
|
|
|
|
|
actions.forEach { action in
|
|
|
|
alert.addAction(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
return alert
|
|
|
|
}()
|
|
|
|
|
|
|
|
context.coordinator.alert = alertController
|
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
viewController.present(
|
|
|
|
alertController,
|
|
|
|
animated: true
|
|
|
|
) {
|
|
|
|
isPresenting = false
|
|
|
|
context.coordinator.alert = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCoordinator() -> InputAlertView.Coordinator {
|
|
|
|
Coordinator(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Coordinator
|
|
|
|
|
|
|
|
extension InputAlertView {
|
|
|
|
class Coordinator: NSObject, UITextFieldDelegate {
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
var alert: UIAlertController?
|
|
|
|
|
|
|
|
private let component: InputAlertView
|
|
|
|
|
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
init(_ component: InputAlertView) {
|
|
|
|
self.component = component
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UITextFieldDelegate
|
|
|
|
|
2022-12-20 02:09:19 +01:00
|
|
|
func textField(
|
|
|
|
_ textField: UITextField,
|
|
|
|
shouldChangeCharactersIn range: NSRange,
|
|
|
|
replacementString string: String
|
|
|
|
) -> Bool {
|
2022-12-16 23:59:26 +01:00
|
|
|
component.textFieldString = {
|
|
|
|
if let text = textField.text as NSString? {
|
2022-12-20 02:09:19 +01:00
|
|
|
return text
|
|
|
|
.replacingCharacters(in: range, with: string)
|
|
|
|
.trimmingCharacters(in: .newlines)
|
2022-12-16 23:59:26 +01:00
|
|
|
} else {
|
|
|
|
return .empty
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|