-
Notifications
You must be signed in to change notification settings - Fork 83
/
GridConfiguration.swift
185 lines (160 loc) · 7.03 KB
/
GridConfiguration.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// GridConfiguration.swift
// Proton
//
// Created by Rajdeep Kwatra on 6/6/2022.
// Copyright © 2022 Rajdeep Kwatra. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
/// Defines configuration for Columns
class GridColumnDimension {
var isCollapsed: Bool
var width: GridColumnWidth
let collapsedWidth: CGFloat
/// Instantiates dimension for Grid Columns
/// - Parameters:
/// - width: Default column width
/// - isCollapsed: Determines if column is collapsed
/// - collapsedWidth: Default width for collapsed column.
init(width: GridColumnWidth, isCollapsed: Bool = false, collapsedWidth: CGFloat) {
self.isCollapsed = isCollapsed
self.width = width
self.collapsedWidth = collapsedWidth
}
func value(basedOn total: CGFloat, viewportWidth: CGFloat) -> CGFloat {
guard !isCollapsed else { return collapsedWidth }
return width.value(basedOn: total, viewportWidth: viewportWidth)
}
}
/// Defines how Grid Column width should be calculated
public enum GridColumnWidth {
public enum ConstrainedWidth {
case absolute(CGFloat)
case viewport(padding: CGFloat)
}
/// Defines a fixed with for column
/// - Parameter : `CGFloat` value for width.
/// - min: Closure providing minimum value for column. If fixed value is less than min, min is used.
/// - max: Closure providing maximum value for column. If fixed value is more than max, max is used.
case fixed(CGFloat, min: (() -> ConstrainedWidth)? = nil, max: (() -> ConstrainedWidth)? = nil)
/// Defines a fixed with for column
/// - Parameters :
/// - : `CGFloat` value for percentage of available width.
/// - min: Closure providing minimum value for column. If computed fractional value is less than min, min is used.
/// - max: Closure providing maximum value for column. If computed fractional value is more than max, max is used.
/// - Note: Percentage is calculated based on total available width for GridView, typically, width of containing `EditorView`
case fractional(CGFloat, min: (() -> ConstrainedWidth)? = nil, max: (() -> ConstrainedWidth)? = nil)
/// Defines width based on available viewport.
/// - Parameter padding: Padding for adjusting width with respect to viewport. Positive values decreases column width from viewport width and negative
/// increases column width by padding over viewport width,
case viewport(padding: CGFloat)
func value(basedOn total: CGFloat, viewportWidth: CGFloat) -> CGFloat {
switch self {
case let .fixed(fixedValue, minVal, maxVal):
return getConstrainedWidth(originalValue: fixedValue,
viewportWidth: viewportWidth,
minVal: minVal,
maxVal: maxVal)
case let .fractional(value, minVal, maxVal):
let fractionalValue = value * total
return getConstrainedWidth(originalValue: fractionalValue,
viewportWidth: viewportWidth,
minVal: minVal,
maxVal: maxVal)
case let .viewport(padding):
return viewportWidth - padding
}
}
private func getConstrainedWidth(originalValue: CGFloat,
viewportWidth: CGFloat,
minVal: (() -> ConstrainedWidth)?,
maxVal: (() -> ConstrainedWidth)?) -> CGFloat {
if let minVal = minVal?() {
switch minVal {
case .absolute(let value):
return max(value, originalValue)
case .viewport(let padding):
return max(viewportWidth - padding, originalValue)
}
}
if let maxVal = maxVal?() {
switch maxVal {
case .absolute(let value):
return min(value, originalValue)
case .viewport(let padding):
return min(viewportWidth - padding, originalValue)
}
}
return originalValue
}
}
public struct GridColumnConfiguration {
public let width: GridColumnWidth
public let style: GridCellStyle
public init(width: GridColumnWidth, style: GridCellStyle = .init()) {
self.width = width
self.style = style
}
}
public struct GridRowConfiguration {
public let initialHeight: CGFloat
public let style: GridCellStyle
public init(initialHeight: CGFloat, style: GridCellStyle = .init()) {
self.initialHeight = initialHeight
self.style = style
}
}
public struct GradientColors {
public let primary: UIColor
public let secondary: UIColor
public init(primary: UIColor, secondary: UIColor) {
self.primary = primary
self.secondary = secondary
}
}
public struct GridConfiguration {
public let style: GridStyle
public let boundsLimitShadowColors: GradientColors
public let columnsConfiguration: [GridColumnConfiguration]
public let rowsConfiguration: [GridRowConfiguration]
public let collapsedColumnWidth: CGFloat
public let collapsedRowHeight: CGFloat
/// Ignores optimization to initialize editor within the cell. With optimization, the editor is not initialized until the cell is ready to be rendered on the UI thereby
/// not incurring any overheads when creating attributedText containing a `GridView` in an attachment. Defaults to `false`.
public let ignoresOptimizedInit: Bool
public init(columnsConfiguration: [GridColumnConfiguration],
rowsConfiguration: [GridRowConfiguration],
style: GridStyle = .default,
boundsLimitShadowColors: GradientColors = GradientColors(primary: .black, secondary: .white),
collapsedColumnWidth: CGFloat = 2,
collapsedRowHeight: CGFloat = 2,
ignoresOptimizedInit: Bool = false
) {
self.columnsConfiguration = columnsConfiguration
self.rowsConfiguration = rowsConfiguration
self.style = style
self.boundsLimitShadowColors = boundsLimitShadowColors
self.collapsedColumnWidth = collapsedColumnWidth
self.collapsedRowHeight = collapsedRowHeight
self.ignoresOptimizedInit = ignoresOptimizedInit
}
public var numberOfColumns: Int {
columnsConfiguration.count
}
public var numberOfRows: Int {
rowsConfiguration.count
}
}