Skip to content

Commit a1f1a3e

Browse files
authored
Add defaults for easier button instantiation (google#118)
* Add defaults for easier button instantiation
1 parent b44618c commit a1f1a3e

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

GoogleSignInSwift/Sources/GoogleSignInButton.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,49 @@ import CoreGraphics
2222
/// A Google Sign In button to be used in SwiftUI.
2323
@available(iOS 13.0, macOS 10.15, *)
2424
public struct GoogleSignInButton: View {
25-
@ObservedObject var viewModel: GoogleSignInButtonViewModel
25+
/// An object containing the styling information needed to create the button.
26+
@ObservedObject public var viewModel: GoogleSignInButtonViewModel
2627
private let action: () -> Void
2728
private let fontLoaded: Bool
2829

2930
/// Creates an instance of the Google Sign-In button for use in SwiftUI
3031
/// - parameter viewModel: An instance of `GoogleSignInButtonViewModel`
31-
/// containing information on the button's scheme, style, and state.
32+
/// containing information on the button's scheme, style, and state.
33+
/// Defaults to `GoogleSignInButtonViewModel` with its standard defaults.
3234
/// - parameter action: A closure to use as the button's action upon press.
35+
/// - seealso: Refer to `GoogleSignInButtonViewModel.swift` for its defaults.
3336
public init(
34-
viewModel: GoogleSignInButtonViewModel,
37+
viewModel: GoogleSignInButtonViewModel = GoogleSignInButtonViewModel(),
3538
action: @escaping () -> Void
3639
) {
3740
self.viewModel = viewModel
3841
self.action = action
3942
self.fontLoaded = Font.loadCGFont()
4043
}
4144

45+
/// A convenience initializer to create a Google Sign-In button in SwiftUI
46+
/// with scheme, style, and state.
47+
/// - parameter scheme: The `GoogleSignInButtonColorScheme` to use. Defaults
48+
/// to `.light`.
49+
/// - parameter style: The `GoogleSignInButtonStyle` to use. Defaults to
50+
/// `.standard`.
51+
/// - parameter state: The `GoogleSignInButtonState` to use. Defaults to
52+
/// `.normal`.
53+
/// - parameter action: A closure to use as the button's action upon press.
54+
public init(
55+
scheme: GoogleSignInButtonColorScheme = .light,
56+
style: GoogleSignInButtonStyle = .standard,
57+
state: GoogleSignInButtonState = .normal,
58+
action: @escaping () -> Void
59+
) {
60+
let vm = GoogleSignInButtonViewModel(
61+
scheme: scheme,
62+
style: style,
63+
state: state
64+
)
65+
self.init(viewModel: vm, action: action)
66+
}
67+
4268
public var body: some View {
4369
Button(action: action) {
4470
switch viewModel.style {

GoogleSignInSwift/Sources/GoogleSignInButtonViewModel.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ public class GoogleSignInButtonViewModel: ObservableObject {
3131
}
3232

3333
/// Creates instances of the SwiftUI sign-in button.
34-
/// - parameter scheme: An instance of `GoogleSignInButtonColorScheme`.
35-
/// - parameter style: An instance of `GoogleSignInButtonStyle`.
34+
/// - parameter scheme: An instance of `GoogleSignInButtonColorScheme`. Defaults to
35+
/// `.light`.
36+
/// - parameter style: An instance of `GoogleSignInButtonStyle`. Defaults to
37+
/// `.standard`.
3638
/// - parameter state: An instance of `GoogleSignInButtonState`. Defaults to
3739
/// `.normal`.
3840
@available(iOS 13.0, macOS 10.15, *)
3941
public init(
40-
scheme: GoogleSignInButtonColorScheme,
41-
style: GoogleSignInButtonStyle,
42+
scheme: GoogleSignInButtonColorScheme = .light,
43+
style: GoogleSignInButtonStyle = .standard,
4244
state: GoogleSignInButtonState = .normal) {
4345
self.scheme = scheme
4446
self.style = style

Samples/Swift/DaysUntilBirthday/Shared/Views/SignInView.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ struct SignInView: View {
2323
var body: some View {
2424
VStack {
2525
HStack {
26-
let buttonViewModel = GoogleSignInButtonViewModel(
27-
scheme: .light,
28-
style: .standard
29-
)
30-
GoogleSignInButton(
31-
viewModel: buttonViewModel,
32-
action: authViewModel.signIn
33-
)
26+
GoogleSignInButton(action: authViewModel.signIn)
3427
.accessibility(hint: Text("Sign in with Google button."))
3528
.padding()
3629
}

0 commit comments

Comments
 (0)