-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTDeviceAdapter.swift
More file actions
170 lines (132 loc) · 5.04 KB
/
STDeviceAdapter.swift
File metadata and controls
170 lines (132 loc) · 5.04 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
//
// STDeviceAdapter.swift
// STBaseProject
//
// Created by 寒江孤影 on 2019/03/16.
//
import UIKit
import Foundation
public struct STBarHeightsConfiguration {
public var navigationBarRegularHeight: CGFloat = 64.0
public var navigationBarSafeAreaHeight: CGFloat = 88.0
public var navigationBarContainerHeight: CGFloat = 50.0
public var tabBarRegularHeight: CGFloat = 49.0
public var tabBarSafeAreaHeight: CGFloat = 83.0
public init() {}
}
public final class STDeviceAdapter {
public static let shared = STDeviceAdapter()
public private(set) var designSize = CGSize.zero
public private(set) var barHeights = STBarHeightsConfiguration()
public private(set) var fontFamily = STFontFamilyConfig.system
private init() {}
public func configure(designSize: CGSize) {
self.designSize = designSize
}
public func configureNavigationBar(regularHeight: CGFloat, safeAreaHeight: CGFloat, containerHeight: CGFloat = 50) {
self.barHeights.navigationBarRegularHeight = regularHeight
self.barHeights.navigationBarSafeAreaHeight = safeAreaHeight
self.barHeights.navigationBarContainerHeight = containerHeight
}
public func configureTabBar(regularHeight: CGFloat, safeAreaHeight: CGFloat) {
self.barHeights.tabBarRegularHeight = regularHeight
self.barHeights.tabBarSafeAreaHeight = safeAreaHeight
}
public func applyBarHeights(_ configuration: STBarHeightsConfiguration) {
self.barHeights = configuration
}
public func configureFontFamily(_ config: STFontFamilyConfig) {
self.fontFamily = config
}
public static var widthScale: CGFloat {
let designSize = shared.designSize
guard designSize != .zero else { return 1.0 }
return screenWidth / designSize.width
}
public static var heightScale: CGFloat {
let designSize = shared.designSize
guard designSize != .zero else { return 1.0 }
return screenHeight / designSize.height
}
public static func scaledValue(_ value: CGFloat) -> CGFloat {
self.scaled(value, multiplier: widthScale)
}
public static func scaledHeightValue(_ value: CGFloat) -> CGFloat {
self.scaled(value, multiplier: heightScale)
}
public static func scaledWidth(_ value: CGFloat) -> CGFloat {
self.scaledValue(value)
}
public static func scaledHeight(_ value: CGFloat) -> CGFloat {
self.scaledHeightValue(value)
}
public static func scaledFontSize(_ value: CGFloat) -> CGFloat {
self.scaledValue(value)
}
public static func scaledSpacing(_ value: CGFloat) -> CGFloat {
self.scaledValue(value)
}
public static var screenWidth: CGFloat {
UIScreen.main.bounds.width
}
public static var screenHeight: CGFloat {
UIScreen.main.bounds.height
}
public static var screenSize: CGSize {
UIScreen.main.bounds.size
}
public static var safeAreaInsets: UIEdgeInsets {
self.currentKeyWindow?.safeAreaInsets ?? .zero
}
public static var isNotchScreen: Bool {
UIDevice.current.userInterfaceIdiom == .phone && safeAreaInsets.top > 20
}
public static var navigationBarHeight: CGFloat {
self.isNotchScreen
? self.shared.barHeights.navigationBarSafeAreaHeight
: self.shared.barHeights.navigationBarRegularHeight
}
public static var navigationBarContainerHeight: CGFloat {
self.shared.barHeights.navigationBarContainerHeight
}
public static var tabBarHeight: CGFloat {
self.isNotchScreen
? self.shared.barHeights.tabBarSafeAreaHeight
: self.shared.barHeights.tabBarRegularHeight
}
public static var bottomSafeAreaHeight: CGFloat {
self.safeAreaInsets.bottom
}
public static var safeTabBarHeight: CGFloat {
self.tabBarHeight + self.bottomSafeAreaHeight
}
public static var statusBarHeight: CGFloat {
self.currentKeyWindow?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
}
public static var contentHeight: CGFloat {
self.screenHeight - self.navigationBarHeight - self.statusBarHeight
}
public static var contentHeightWithTabBar: CGFloat {
self.screenHeight - self.navigationBarHeight - self.statusBarHeight - self.tabBarHeight
}
public static var isLandscape: Bool {
UIDevice.current.orientation.isLandscape
}
public static var isPortrait: Bool {
UIDevice.current.orientation.isPortrait
}
public static var orientation: UIDeviceOrientation {
UIDevice.current.orientation
}
private static var currentKeyWindow: UIWindow? {
return UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap(\.windows)
.first(where: \.isKeyWindow)
}
private static func scaled(_ value: CGFloat, multiplier: CGFloat) -> CGFloat {
let result = value * multiplier
let scale = UIScreen.main.scale
return (result * scale).rounded(.up) / scale
}
}