From 692fd99c5d54ad17be8eaf82d74676b9c559ee8e Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sat, 17 Dec 2022 11:40:45 +0100 Subject: [PATCH] Renamed the InputAlertView component in the Browse module as InputAlert. --- .../Browse/UI/Components/InputAlert.swift | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Modules/Sources/Browse/UI/Components/InputAlert.swift diff --git a/Modules/Sources/Browse/UI/Components/InputAlert.swift b/Modules/Sources/Browse/UI/Components/InputAlert.swift new file mode 100644 index 0000000..1b5d071 --- /dev/null +++ b/Modules/Sources/Browse/UI/Components/InputAlert.swift @@ -0,0 +1,134 @@ +// +// InputAlert.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 InputAlert: 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, + title: String, + message: String, + textFieldPlaceholder: String, + textFieldString: Binding, + 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 + ) -> UIViewController { + .init() + } + + func updateUIViewController( + _ viewController: UIViewController, + context: UIViewControllerRepresentableContext + ) { + 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() -> Coordinator { + .init(self) + } + +} + +// MARK: - Coordinator + +extension InputAlert { + class Coordinator: NSObject, UITextFieldDelegate { + + // MARK: Properties + + var alert: UIAlertController? + + private let parent: InputAlert + + // MARK: Initialisers + + init(_ parent: InputAlert) { + self.parent = parent + } + + // MARK: UITextFieldDelegate + + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + parent.textFieldString = { + if let text = textField.text as NSString? { + return text.replacingCharacters(in: range, with: string) + } else { + return .empty + } + }() + + return true + } + + } +}