forked from callstack/react-native-bottom-tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabViewProvider.swift
More file actions
288 lines (246 loc) · 7.29 KB
/
TabViewProvider.swift
File metadata and controls
288 lines (246 loc) · 7.29 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import Foundation
import React
import SDWebImage
import SDWebImageSVGCoder
import SwiftUI
@objcMembers
public final class TabInfo: NSObject {
public let key: String
public let title: String
public let badge: String?
public let sfSymbol: String
public let activeTintColor: PlatformColor?
public let hidden: Bool
public let testID: String?
public let role: TabBarRole?
public init(
key: String,
title: String,
badge: String?,
sfSymbol: String,
activeTintColor: PlatformColor?,
hidden: Bool,
testID: String?,
role: String?
) {
self.key = key
self.title = title
self.badge = badge
self.sfSymbol = sfSymbol
self.activeTintColor = activeTintColor
self.hidden = hidden
self.testID = testID
self.role = TabBarRole(rawValue: role ?? "")
super.init()
}
}
@objc public protocol TabViewProviderDelegate {
func onPageSelected(key: String, reactTag: NSNumber?)
func onLongPress(key: String, reactTag: NSNumber?)
func onTabBarMeasured(height: Int, reactTag: NSNumber?)
func onLayout(size: CGSize, reactTag: NSNumber?)
}
@objc public class TabViewProvider: PlatformView {
private var delegate: TabViewProviderDelegate?
private var props = TabViewProps()
private var hostingController: PlatformHostingController<TabViewImpl>?
private var coalescingKey: UInt16 = 0
private var iconSize = CGSize(width: 27, height: 27)
@objc var onPageSelected: RCTDirectEventBlock?
@objc var onTabLongPress: RCTDirectEventBlock?
@objc var onTabBarMeasured: RCTDirectEventBlock?
@objc var onNativeLayout: RCTDirectEventBlock?
@objc public var icons: NSArray? {
didSet {
loadIcons(icons)
}
}
@objc public var children: [PlatformView] = [] {
didSet {
props.children = children
}
}
@objc public var sidebarAdaptable: Bool = false {
didSet {
props.sidebarAdaptable = sidebarAdaptable
}
}
@objc public var disablePageAnimations: Bool = false {
didSet {
props.disablePageAnimations = disablePageAnimations
}
}
@objc public var labeled: Bool = false {
didSet {
props.labeled = labeled
}
}
@objc public var selectedPage: NSString? {
didSet {
props.selectedPage = selectedPage as? String
}
}
@objc public var hapticFeedbackEnabled: Bool = false {
didSet {
props.hapticFeedbackEnabled = hapticFeedbackEnabled
}
}
@objc public var scrollEdgeAppearance: NSString? {
didSet {
props.scrollEdgeAppearance = scrollEdgeAppearance as? String
}
}
@objc public var minimizeBehavior: NSString? {
didSet {
props.minimizeBehavior = MinimizeBehavior(rawValue: minimizeBehavior as? String ?? "")
}
}
@objc public var translucent: Bool = true {
didSet {
props.translucent = translucent
}
}
@objc var items: NSArray? {
didSet {
props.items = parseTabData(from: items)
}
}
@objc public var barTintColor: PlatformColor? {
didSet {
props.barTintColor = barTintColor
}
}
@objc public var activeTintColor: PlatformColor? {
didSet {
props.activeTintColor = activeTintColor
}
}
@objc public var inactiveTintColor: PlatformColor? {
didSet {
props.inactiveTintColor = inactiveTintColor
}
}
@objc public var fontFamily: NSString? {
didSet {
props.fontFamily = fontFamily as? String
}
}
@objc public var fontWeight: NSString? {
didSet {
props.fontWeight = fontWeight as? String
}
}
@objc public var fontSize: NSNumber? {
didSet {
props.fontSize = fontSize as? Int
}
}
@objc public var tabBarHidden: Bool = false {
didSet {
props.tabBarHidden = tabBarHidden
}
}
// New arch specific properties
@objc public var itemsData: [TabInfo] = [] {
didSet {
props.items = itemsData
}
}
@objc public convenience init(delegate: TabViewProviderDelegate) {
self.init()
self.delegate = delegate
SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
}
override public func didUpdateReactSubviews() {
props.children = reactSubviews()
}
#if os(macOS)
override public func layout() {
super.layout()
setupView()
}
#else
override public func layoutSubviews() {
super.layoutSubviews()
setupView()
}
#endif
private func setupView() {
if self.hostingController != nil {
return
}
self.hostingController = PlatformHostingController(rootView: TabViewImpl(props: props) { key in
self.delegate?.onPageSelected(key: key, reactTag: self.reactTag)
} onLongPress: { key in
self.delegate?.onLongPress(key: key, reactTag: self.reactTag)
} onLayout: { size in
self.delegate?.onLayout(size: size, reactTag: self.reactTag)
} onTabBarMeasured: { height in
self.delegate?.onTabBarMeasured(height: height, reactTag: self.reactTag)
})
if let hostingController = self.hostingController, let parentViewController = reactViewController() {
parentViewController.addChild(hostingController)
addSubview(hostingController.view)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
hostingController.view.pinEdges(to: self)
#if !os(macOS)
hostingController.didMove(toParent: parentViewController)
#endif
}
}
private func loadIcons(_ icons: NSArray?) {
// TODO: Diff the arrays and update only changed items.
// Now if the user passes `unfocusedIcon` we update every item.
guard let imageSources = icons as? [RCTImageSource?] else { return }
for (index, imageSource) in imageSources.enumerated() {
guard let imageSource,
let url = imageSource.request.url else { continue }
let isSVG = url.pathExtension.lowercased() == "svg"
var options: SDWebImageOptions = [.continueInBackground,
.scaleDownLargeImages,
.avoidDecodeImage,
.highPriority]
if isSVG {
options.insert(.decodeFirstFrameOnly)
}
let context: [SDWebImageContextOption: Any]? = isSVG ? [
.imageThumbnailPixelSize: iconSize,
.imagePreserveAspectRatio: true
] : nil
SDWebImageManager.shared.loadImage(
with: url,
options: options,
context: context,
progress: nil
) { [weak self] image, _, _, _, _, _ in
guard let self else { return }
DispatchQueue.main.async {
if let image {
self.props.icons[index] = image.resizeImageTo(size: self.iconSize)
}
}
}
}
}
private func parseTabData(from array: NSArray?) -> [TabInfo] {
guard let array else { return [] }
var items: [TabInfo] = []
for value in array {
if let itemDict = value as? [String: Any] {
items.append(
TabInfo(
key: itemDict["key"] as? String ?? "",
title: itemDict["title"] as? String ?? "",
badge: itemDict["badge"] as? String,
sfSymbol: itemDict["sfSymbol"] as? String ?? "",
activeTintColor: RCTConvert.uiColor(itemDict["activeTintColor"] as? NSNumber),
hidden: itemDict["hidden"] as? Bool ?? false,
testID: itemDict["testID"] as? String ?? "",
role: itemDict["role"] as? String,
)
)
}
}
return items
}
}