/
ViewConfiguration.swift
105 lines (84 loc) · 3.67 KB
/
ViewConfiguration.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// ViewConfiguration.swift
//
//
// Created by Guillaume Coquard on 26/03/24.
//
import SwiftUI
#if os(iOS)
import UIKit
#endif
public struct ViewConfiguration<Root> where Root: Representable.Represented {
public typealias ViewType = Root
public typealias Context = Bridge<ViewType>.Context
public typealias ConfigurationMoment = CycleMoment
public typealias ConfigurationTask = (ViewType?, Context?) -> (ViewType?, Context?)
static public func Passthrough(_ object: ViewType?, _ context: Context?) -> (ViewType?, Context?) { (object, nil) }
private(set) public var moment: ConfigurationMoment = .all
private(set) public var task: ConfigurationTask = Self.Passthrough
public init() {}
public init(_ configuration: Self) {
self.moment = configuration.moment
self.task = configuration.task
}
public init(_ configurations: [Self]) {
let configuration = configurations.reduce(self, { prev, next in prev + next })
self.moment = configuration.moment
self.task = configuration.task
}
public init(_ configurations: Self...) {
let configuration = configurations.reduce(self, { prev, next in prev + next })
self.moment = configuration.moment
self.task = configuration.task
}
public init(_ moment: ConfigurationMoment = .all, _ task: @escaping ConfigurationTask = Self.Passthrough) {
self.moment = moment
self.task = task
}
public init(_ moment: ConfigurationMoment = .all, tasks: [ConfigurationTask]) {
self.moment = moment
self.task = tasks.reduce({ (view: ViewType?, context: Context?) in (view, context) }) { prev, next in { (view: ViewType?, context: Context?) in next( prev(view, context).0, context ) }
}
}
public init(_ moment: ConfigurationMoment = .all, tasks: ConfigurationTask...) {
self.moment = moment
self.task = tasks.reduce({ (view: ViewType?, context: Context?) in (view, context) }) { prev, next in { (view: ViewType?, context: Context?) in next( prev( view, context ).0, context ) }
}
}
public init(_ moment: ConfigurationMoment = .all, from configuration: Self) {
self.moment = moment
self.task = configuration.task
}
public init(_ moment: ConfigurationMoment = .all, from configurations: [Self]) {
self.moment = moment
self.task = configurations.reduce(.init(), +).task
}
public init(_ moment: ConfigurationMoment = .all, from configurations: Self...) {
self.moment = moment
self.task = configurations.reduce(.init(), +).task
}
static public func +(lhs: Self, rhs: Self) -> Self {
let moment = lhs.moment.intersection(rhs.moment)
return if moment.isEmpty {
Self()
} else {
Self(moment, { (view: ViewType?, context: Context?) in rhs.task( lhs.task( view, context ).0, context) })
}
}
static public func +(lhs: Self, rhs: @escaping ConfigurationTask) -> Self {
Self(lhs.moment, { (view: ViewType?, context: Context?) in rhs( lhs.task( view, context ).0, context) })
}
static public func +(lhs: @escaping ConfigurationTask, rhs: Self) -> Self {
Self(rhs.moment, { (view: ViewType?, context: Context?) in rhs.task( lhs( view, context ).0, context) })
}
public func precedes(_ configurations: Self...) -> Self {
self + configurations.reduce(self, +)
}
public func succeeds(_ configurations: Self...) -> Self {
configurations.reduce(.init(), +) + self
}
@discardableResult
public func callAsFunction(_ view: ViewType?, _ context: Context?) -> (ViewType?, Context?) {
self.task( view, context )
}
}