From d8b884d1d3c8c4bff024f8190353bfe3a49868b0 Mon Sep 17 00:00:00 2001 From: Ilya Kharlamov Date: Thu, 14 Mar 2024 15:01:11 +0300 Subject: [PATCH] Revert "fix #48 FastisView for presentation in SwiftUI" This reverts commit 2c1723c329197e6a249038cbf18be7ec06b7d0f7. --- .../FastisExample.xcodeproj/project.pbxproj | 10 +- Example/Source/HostingController.swift | 24 --- Example/Source/MainView.swift | 110 ------------ Example/Source/ViewController.swift | 26 +-- Sources/Views/FastisView.swift | 160 ------------------ 5 files changed, 6 insertions(+), 324 deletions(-) delete mode 100644 Example/Source/HostingController.swift delete mode 100644 Example/Source/MainView.swift delete mode 100644 Sources/Views/FastisView.swift diff --git a/Example/FastisExample.xcodeproj/project.pbxproj b/Example/FastisExample.xcodeproj/project.pbxproj index 14459b8..92eff59 100644 --- a/Example/FastisExample.xcodeproj/project.pbxproj +++ b/Example/FastisExample.xcodeproj/project.pbxproj @@ -3,13 +3,11 @@ archiveVersion = 1; classes = { }; - objectVersion = 54; + objectVersion = 52; objects = { /* Begin PBXBuildFile section */ 1A3E7A5E2A40901600434229 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1A3E7A602A40901600434229 /* Localizable.strings */; }; - 1AC064EF2BA2E6DB003AFCCE /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AC064EE2BA2E6DB003AFCCE /* MainView.swift */; }; - 1AC064F32BA2E708003AFCCE /* HostingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AC064F22BA2E708003AFCCE /* HostingController.swift */; }; F381AF9628B8C7190046383A /* Fastis in Frameworks */ = {isa = PBXBuildFile; productRef = F381AF9528B8C7190046383A /* Fastis */; }; F3FCEE47244780FE000F966E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FCEE46244780FE000F966E /* AppDelegate.swift */; }; F3FCEE4B244780FE000F966E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FCEE4A244780FE000F966E /* ViewController.swift */; }; @@ -21,8 +19,6 @@ 1A3E7A5F2A40901600434229 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = ""; }; 1A3E7A612A40902000434229 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/Localizable.strings; sourceTree = ""; }; 1A3E7A622A409B3A00434229 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/Localizable.strings; sourceTree = ""; }; - 1AC064EE2BA2E6DB003AFCCE /* MainView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; - 1AC064F22BA2E708003AFCCE /* HostingController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HostingController.swift; sourceTree = ""; }; CB137E4877EBDC6E87A77A4B /* Pods_FastisExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_FastisExample.framework; sourceTree = BUILT_PRODUCTS_DIR; }; F31CFB6B28B8C32D00364F6A /* Fastis */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Fastis; path = ..; sourceTree = ""; }; F3FCEE43244780FE000F966E /* FastisExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FastisExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -83,8 +79,6 @@ isa = PBXGroup; children = ( F3FCEE46244780FE000F966E /* AppDelegate.swift */, - 1AC064EE2BA2E6DB003AFCCE /* MainView.swift */, - 1AC064F22BA2E708003AFCCE /* HostingController.swift */, F3FCEE4A244780FE000F966E /* ViewController.swift */, F3FCEE4F24478100000F966E /* Assets.xcassets */, F3FCEE5124478100000F966E /* LaunchScreen.storyboard */, @@ -170,10 +164,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 1AC064F32BA2E708003AFCCE /* HostingController.swift in Sources */, F3FCEE4B244780FE000F966E /* ViewController.swift in Sources */, F3FCEE47244780FE000F966E /* AppDelegate.swift in Sources */, - 1AC064EF2BA2E6DB003AFCCE /* MainView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Example/Source/HostingController.swift b/Example/Source/HostingController.swift deleted file mode 100644 index 638f659..0000000 --- a/Example/Source/HostingController.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// HostingController.swift -// FastisExample -// -// Created by Yriy Devyataev on 13.03.2024. -// Copyright © 2024 RetailDriver LLC. All rights reserved. -// - -import SwiftUI - -/** - The view is used to display SwiftUI view in the UIKit project - */ - -class HostingController: UIHostingController { - - init() { - super.init(rootView: MainView()) - } - - @MainActor required dynamic init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } -} diff --git a/Example/Source/MainView.swift b/Example/Source/MainView.swift deleted file mode 100644 index 30b00d6..0000000 --- a/Example/Source/MainView.swift +++ /dev/null @@ -1,110 +0,0 @@ -// -// CalendarView.swift -// FastisExample -// -// Created by Yriy Devyataev on 13.03.2024. -// Copyright © 2024 RetailDriver LLC. All rights reserved. -// - - -import Foundation -import SwiftUI -import UIKit -import Fastis - -/** - View is used as a possible example of a SwiftUI project. - */ - -struct MainView: View { - - @State private var showSingleCalendar = false - @State private var showRangeCalendar = false - @State private var currentValueText: String = "Choose a date" - - @State var currentValue: FastisValue? { - didSet { - if let rangeValue = self.currentValue as? FastisRange { - self.currentValueText = self.dateFormatter.string(from: rangeValue.fromDate) + " - " + self.dateFormatter.string(from: rangeValue.toDate) - } else if let date = self.currentValue as? Date { - self.currentValueText = self.dateFormatter.string(from: date) - } else { - self.currentValueText = "Choose a date" - } - } - } - - @State private var dateFormatter: DateFormatter = { - let formatter = DateFormatter() - formatter.dateFormat = "dd/MM/yyyy" - return formatter - }() - - var body: some View { - VStack(spacing: 32, content: { - Text(self.currentValueText) - VStack(alignment: .center, spacing: 16, content: { - Button("Choose range of dates") { - self.showRangeCalendar.toggle() - } - Button("Choose single date") { - self.showSingleCalendar.toggle() - } - }) - }) - .frame(maxWidth: .infinity, maxHeight: .infinity) - .sheet(isPresented: $showRangeCalendar) { - self.rangeCalendarView() - } - .sheet(isPresented: $showSingleCalendar) { - self.singleCalendarView() - } - } - - private func rangeCalendarView() -> some View { - let config: FastisConfig = .default - var fastisView = FastisView(mode: .range, config: config) - fastisView.title = "Choose range" - fastisView.initialValue = self.currentValue as? FastisRange - fastisView.minimumDate = Calendar.current.date(byAdding: .month, value: -2, to: Date()) - fastisView.maximumDate = Calendar.current.date(byAdding: .month, value: 3, to: Date()) - fastisView.allowToChooseNilDate = true - fastisView.allowDateRangeChanges = false - fastisView.shortcuts = [.lastWeek, .lastMonth] - fastisView.selectMonthOnHeaderTap = true - - fastisView.dismissHandler = { action in - switch action { - case .done(let newValue): - self.currentValue = newValue - case .cancel: - print("any actions") - } - } - return fastisView.ignoresSafeArea() - } - - private func singleCalendarView() -> some View { - let config: FastisConfig = .default - var fastisView = FastisView(mode: .single, config: config) - fastisView.title = "Choose date" - fastisView.initialValue = self.currentValue as? Date - fastisView.minimumDate = Calendar.current.date(byAdding: .month, value: -2, to: Date()) - fastisView.maximumDate = Date() - fastisView.allowToChooseNilDate = true - fastisView.allowDateRangeChanges = false - fastisView.shortcuts = [.yesterday, .today, .tomorrow] - fastisView.closeOnSelectionImmediately = true - - fastisView.dismissHandler = { action in - switch action { - case .done(let newValue): - self.currentValue = newValue - case .cancel: - print("any actions") - } - } - return fastisView.ignoresSafeArea() - } - -} diff --git a/Example/Source/ViewController.swift b/Example/Source/ViewController.swift index df52074..5049ad5 100644 --- a/Example/Source/ViewController.swift +++ b/Example/Source/ViewController.swift @@ -40,17 +40,10 @@ class ViewController: UIViewController { return button }() - private lazy var chooseRangeButtonWithCustomCalendar: UIButton = { + private lazy var chooseSingleButtonWithCustomCalendar: UIButton = { let button = UIButton(type: .system) - button.setTitle("Choose range of dates with custom calendar", for: .normal) - button.addTarget(self, action: #selector(self.chooseRangeWithCustomCalendar), for: .touchUpInside) - return button - }() - - private lazy var chooseWithSwiftUI: UIButton = { - let button = UIButton(type: .system) - button.setTitle("Choose with SwiftUI", for: .normal) - button.addTarget(self, action: #selector(self.swiftUIPresentation), for: .touchUpInside) + button.setTitle("Choose single date with custom calendar", for: .normal) + button.addTarget(self, action: #selector(self.chooseSingleDateWithCustomCalendar), for: .touchUpInside) return button }() @@ -98,8 +91,7 @@ class ViewController: UIViewController { self.containerView.setCustomSpacing(32, after: self.currentDateLabel) self.containerView.addArrangedSubview(self.chooseRangeButton) self.containerView.addArrangedSubview(self.chooseSingleButton) - self.containerView.addArrangedSubview(self.chooseRangeButtonWithCustomCalendar) - self.containerView.addArrangedSubview(self.chooseWithSwiftUI) + self.containerView.addArrangedSubview(self.chooseSingleButtonWithCustomCalendar) self.view.addSubview(self.containerView) } @@ -157,7 +149,7 @@ class ViewController: UIViewController { } @objc - private func chooseRangeWithCustomCalendar() { + private func chooseSingleDateWithCustomCalendar() { var customConfig: FastisConfig = .default var calendar: Calendar = .init(identifier: .islamicUmmAlQura) calendar.locale = .autoupdatingCurrent @@ -183,12 +175,4 @@ class ViewController: UIViewController { fastisController.present(above: self) } - @objc - private func swiftUIPresentation() { - let hostingController = HostingController() - hostingController.modalPresentationStyle = .custom - let navVC = self.parent as? UINavigationController - navVC?.pushViewController(hostingController, animated: true) - } - } diff --git a/Sources/Views/FastisView.swift b/Sources/Views/FastisView.swift deleted file mode 100644 index e3aa830..0000000 --- a/Sources/Views/FastisView.swift +++ /dev/null @@ -1,160 +0,0 @@ -// -// FastisView.swift -// FastisExample -// -// Created by Yriy Devyataev on 13.03.2024. -// Copyright © 2024 RetailDriver LLC. All rights reserved. -// - -import SwiftUI - -/** -View of Fastis framework. Use it to create and present dade picker - - Usage example: - ```swift - let fastisView = FastisView(mode: .range) - fastisView.title = "Choose range" - fastisView.maximumDate = Date() - fastisView.allowToChooseNilDate = true - fastisView.shortcuts = [.today, .lastWeek] - fastisView.dismissHandler = { [weak self] action in - switch action { - case .done(let newValue): - ... - case .cancel: - ... - } - } - ``` - - **Single and range modes** - - If you want to get a single date you have to use `Date` type: - - ```swift - let fastisView = FastisView(mode: .single) - fastisView.initialValue = Date() - fastisView.closeOnSelectionImmediately = true - fastisView.dismissHandler = { [weak self] action in - switch action { - case .done(let resultDate): - print(resultDate) // resultDate is Date - case .cancel: - ... - } - } - ``` - - If you want to get a date range you have to use `FastisRange` type: - - ```swift - let fastisView = FastisView(mode: .range) - fastisView.initialValue = FastisRange(from: Date(), to: Date()) // or .from(Date(), to: Date()) - fastisView.dismissHandler = { [weak self] action in - switch action { - case .done(let resultRange): - print(resultRange) // resultRange is FastisRange - case .cancel: - ... - } - } - ``` - */ - -public struct FastisView: UIViewControllerRepresentable { - - public var title: String? = nil - public var initialValue: Value? = nil - public var minimumDate: Date? = nil - public var maximumDate: Date? = nil - public var allowToChooseNilDate: Bool = false - public var allowDateRangeChanges: Bool = true - public var shortcuts: [FastisShortcut] = [] - public var dismissHandler: ((FastisController.DismissAction) -> Void)? = nil - - private var privateSelectMonthOnHeaderTap = false - private var privateCloseOnSelectionImmediately = false - - private let config: FastisConfig - - /// Initiate FastisView - /// - Parameter config: Configuration parameters - init(config: FastisConfig = .default) { - self.config = config - } - - public func makeUIViewController(context: Context) -> UINavigationController { - let fastisController = FastisController(config: self.config) - fastisController.title = self.title - fastisController.initialValue = self.initialValue - fastisController.minimumDate = self.minimumDate - fastisController.maximumDate = self.maximumDate - fastisController.allowToChooseNilDate = self.allowToChooseNilDate - fastisController.allowDateRangeChanges = self.allowDateRangeChanges - fastisController.shortcuts = self.shortcuts - fastisController.dismissHandler = self.dismissHandler - switch Value.mode { - case .single: - let singleController = fastisController as? FastisController - singleController?.closeOnSelectionImmediately = self.privateCloseOnSelectionImmediately - case .range: - let rangeController = fastisController as? FastisController - rangeController?.selectMonthOnHeaderTap = self.privateSelectMonthOnHeaderTap - } - return UINavigationController(rootViewController: fastisController) - } - - public func updateUIViewController( - _ uiViewController: UINavigationController, - context: UIViewControllerRepresentableContext - ) {} -} - -public extension FastisView where Value == FastisRange { - - /// Initiate FastisView - /// - Parameters: - /// - mode: Choose `.range` or `.single` mode - /// - config: Custom configuration parameters. Default value is equal to `FastisConfig.default` - init(mode: FastisModeRange, config: FastisConfig = .default) { - self.init(config: config) - } - - /** - Set this variable to `true` if you want to allow select date ranges by tapping on months - */ - var selectMonthOnHeaderTap: Bool { - get { - self.privateSelectMonthOnHeaderTap - } - set { - self.privateSelectMonthOnHeaderTap = newValue - } - } -} - -public extension FastisView where Value == Date { - - /// Initiate FastisView - /// - Parameters: - /// - mode: Choose .range or .single mode - /// - config: Custom configuration parameters. Default value is equal to `FastisConfig.default` - init(mode: FastisModeSingle, config: FastisConfig = .default) { - self.init(config: config) - } - - /** - Set this variable to `true` if you want to hide view of the selected date and close the controller right after the date is selected. - - Default value — `"False"` - */ - var closeOnSelectionImmediately: Bool { - get { - self.privateCloseOnSelectionImmediately - } - set { - self.privateCloseOnSelectionImmediately = newValue - } - } -}