forked from google/GoogleSignIn-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleSignInButton.swift
197 lines (182 loc) · 6.42 KB
/
GoogleSignInButton.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
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright 2022 Google LLC
*
* 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.
*/
#if !arch(arm) && !arch(i386)
import SwiftUI
import CoreGraphics
// MARK: - Sign-In Button
/// A Google Sign In button to be used in SwiftUI.
@available(iOS 13.0, macOS 10.15, *)
public struct GoogleSignInButton: View {
/// An object containing the styling information needed to create the button.
@ObservedObject public var viewModel: GoogleSignInButtonViewModel
private let action: () -> Void
private let fontLoaded: Bool
/// Creates an instance of the Google Sign-In button for use in SwiftUI
/// - parameter viewModel: An instance of `GoogleSignInButtonViewModel`
/// containing information on the button's scheme, style, and state.
/// Defaults to `GoogleSignInButtonViewModel` with its standard defaults.
/// - parameter action: A closure to use as the button's action upon press.
/// - seealso: Refer to `GoogleSignInButtonViewModel.swift` for its defaults.
public init(
viewModel: GoogleSignInButtonViewModel = GoogleSignInButtonViewModel(),
action: @escaping () -> Void
) {
self.viewModel = viewModel
self.action = action
self.fontLoaded = Font.loadCGFont()
}
/// A convenience initializer to create a Google Sign-In button in SwiftUI
/// with scheme, style, and state.
/// - parameter scheme: The `GoogleSignInButtonColorScheme` to use. Defaults
/// to `.light`.
/// - parameter style: The `GoogleSignInButtonStyle` to use. Defaults to
/// `.standard`.
/// - parameter state: The `GoogleSignInButtonState` to use. Defaults to
/// `.normal`.
/// - parameter action: A closure to use as the button's action upon press.
public init(
scheme: GoogleSignInButtonColorScheme = .light,
style: GoogleSignInButtonStyle = .standard,
state: GoogleSignInButtonState = .normal,
action: @escaping () -> Void
) {
let vm = GoogleSignInButtonViewModel(
scheme: scheme,
style: style,
state: state
)
self.init(viewModel: vm, action: action)
}
public var body: some View {
Button(action: action) {
switch viewModel.style {
case .icon:
ZStack {
RoundedRectangle(cornerRadius: googleCornerRadius)
.fill(viewModel.buttonStyle.colors.iconColor)
.border(viewModel.buttonStyle.colors.iconBorderColor)
Image.signInButtonImage
}
case .standard, .wide:
HStack(alignment: .center) {
ZStack {
RoundedRectangle(cornerRadius: googleCornerRadius)
.fill(
viewModel.state == .disabled ?
.clear : viewModel.buttonStyle.colors.iconColor
)
.frame(
width: iconWidth - iconPadding,
height: iconWidth - iconPadding
)
Image.signInButtonImage
}
.padding(.leading, 1)
Text(viewModel.style.buttonText)
.fixedSize()
.padding(.trailing, textPadding)
.frame(
width: viewModel.style.widthForButtonText,
height: buttonHeight,
alignment: .leading
)
Spacer()
}
}
}
.font(signInButtonFont)
.buttonStyle(viewModel.buttonStyle)
.disabled(viewModel.state == .disabled)
.simultaneousGesture(
DragGesture(minimumDistance: 0)
.onChanged { _ in
guard viewModel.state != .disabled,
viewModel.state != .pressed else { return }
viewModel.state = .pressed
}
.onEnded { _ in
guard viewModel.state != .disabled else { return }
viewModel.state = .normal
}
)
}
}
// MARK: - Google Icon Image
@available(iOS 13.0, macOS 10.15, *)
private extension Image {
static var signInButtonImage: Image {
guard let iconURL = Bundle.urlForGoogleResource(
name: googleImageName,
withExtension: "png"
) else {
fatalError("Unable to load Google icon image url: \(Image.Error.unableToLoadGoogleIcon(name: googleImageName))")
}
#if os(iOS) || targetEnvironment(macCatalyst)
guard let uiImage = UIImage(contentsOfFile: iconURL.path) else {
fatalError("Unable to load Google icon image url: \(Image.Error.unableToLoadGoogleIcon(name: googleImageName))")
}
return Image(uiImage: uiImage)
#elseif os(macOS)
guard let nsImage = NSImage(contentsOfFile: iconURL.path) else {
fatalError("Unable to load Google icon image url: \(Image.Error.unableToLoadGoogleIcon(name: googleImageName))")
}
return Image(nsImage: nsImage)
#else
fatalError("Unrecognized platform for SwiftUI sign in button image")
#endif
}
enum Error: Swift.Error {
case unableToLoadGoogleIcon(name: String)
}
}
// MARK: - Google Font
@available(iOS 13.0, macOS 10.15, *)
private extension GoogleSignInButton {
var signInButtonFont: Font {
guard fontLoaded else {
return .system(size: fontSize, weight: .bold)
}
return .custom(fontNameRobotoBold, size: fontSize)
}
}
@available(iOS 13.0, macOS 10.15, *)
private extension Font {
/// Load the font for the button.
/// - returns A `Bool` indicating whether or not the font was loaded.
static func loadCGFont() -> Bool {
// Check to see if the font has already been loaded
#if os(iOS) || targetEnvironment(macCatalyst)
if let _ = UIFont(name: fontNameRobotoBold, size: fontSize) {
return true
}
#elseif os(macOS)
if let _ = NSFont(name: fontNameRobotoBold, size: fontSize) {
return true
}
#else
fatalError("Unrecognized platform for SwiftUI sign in button font")
#endif
guard let fontURL = Bundle.urlForGoogleResource(
name: fontNameRobotoBold,
withExtension: "ttf"
), let dataProvider = CGDataProvider(filename: fontURL.path),
let newFont = CGFont(dataProvider) else {
return false
}
return CTFontManagerRegisterGraphicsFont(newFont, nil)
}
}
#endif // !arch(arm) && !arch(i386)