-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #48 FastisView for presentation in SwiftUI
- Loading branch information
1 parent
debd688
commit 2c1723c
Showing
5 changed files
with
324 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// 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<MainView> { | ||
|
||
init() { | ||
super.init(rootView: MainView()) | ||
} | ||
|
||
@MainActor required dynamic init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// 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() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// | ||
// 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<Value: FastisValue>: 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<Value>] = [] | ||
public var dismissHandler: ((FastisController<Value>.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<Value>(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<Date> | ||
singleController?.closeOnSelectionImmediately = self.privateCloseOnSelectionImmediately | ||
case .range: | ||
let rangeController = fastisController as? FastisController<FastisRange> | ||
rangeController?.selectMonthOnHeaderTap = self.privateSelectMonthOnHeaderTap | ||
} | ||
return UINavigationController(rootViewController: fastisController) | ||
} | ||
|
||
public func updateUIViewController( | ||
_ uiViewController: UINavigationController, | ||
context: UIViewControllerRepresentableContext<FastisView> | ||
) {} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} |