Skip to content

Commit 8d73d5b

Browse files
committed
Added auth controllers. Fixed logic.
1 parent fc3c270 commit 8d73d5b

File tree

6 files changed

+214
-29
lines changed

6 files changed

+214
-29
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let package = Package(
1616
],
1717
dependencies: [
1818
.package(url: "https://github.com/ivanvorobei/SPAlert", .upToNextMajor(from: "4.2.0")),
19-
.package(url: "https://github.com/ivanvorobei/NativeUIKit", .upToNextMajor(from: "1.4.0")),
19+
.package(url: "https://github.com/ivanvorobei/NativeUIKit", .upToNextMajor(from: "1.4.1")),
2020
.package(url: "https://github.com/ivanvorobei/SPFirebase", .upToNextMajor(from: "1.0.6")),
2121
.package(url: "https://github.com/sparrowcode/SPSafeSymbols", .upToNextMajor(from: "1.0.5")),
2222
.package(url: "https://github.com/kean/Nuke", .upToNextMajor(from: "10.7.1"))

Sources/SPProfiling/Interface/Auth/AuthController.swift

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,33 @@ import NativeUIKit
2424
import SparrowKit
2525
import SPAlert
2626

27-
public class AuthController: NativeOnboardingFeaturesController {
27+
open class AuthController: NativeOnboardingFeaturesController {
2828

29-
private var completion: ()->Void
29+
public var completion: ((AuthController)->Void)?
3030

3131
// MARK: - Views
3232

33-
let actionToolbarView = NativeAppleAuthToolBarView()
33+
public let actionToolbarView = AuthToolBarView()
3434

3535
// MARK: - Init
3636

37-
init(title: String, description: String, completion: @escaping ()->Void) {
38-
self.completion = completion
37+
public init(title: String, description: String, completion: ((AuthController)->Void)? = nil) {
3938
super.init(
4039
iconImage: NativeAvatarView.generatePlaceholderImage(fontSize: 80, fontWeight: .medium),
4140
title: title,
4241
subtitle: description
4342
)
43+
self.completion = completion
4444
}
4545

46-
required init?(coder: NSCoder) {
46+
public required init?(coder: NSCoder) {
4747
fatalError("init(coder:) has not been implemented")
4848
}
4949

50+
deinit {
51+
NotificationCenter.default.removeObserver(self)
52+
}
53+
5054
// MARK: - Lifecycle
5155

5256
public override func viewDidLoad() {
@@ -56,16 +60,56 @@ public class AuthController: NativeOnboardingFeaturesController {
5660
navigationController.mimicrateToolBarView = actionToolbarView
5761
}
5862

59-
actionToolbarView.authButton.addTarget(self, action: #selector(self.tapSignInApple), for: .touchUpInside)
63+
actionToolbarView.authButton.addTarget(self, action: #selector(self.tapAppleSignIn), for: .touchUpInside)
64+
actionToolbarView.skipAuthButton.addTarget(self, action: #selector(self.tapContinueAnon), for: .touchUpInside)
65+
66+
NotificationCenter.default.addObserver(self, selector: #selector(self.updateSkipAuthButton), name: SPProfiling.didChangedAuthState, object: nil)
67+
68+
updateSkipAuthButton()
6069
}
6170

62-
@objc func tapSignInApple() {
71+
// MARK: - Actions
72+
73+
@objc func tapAppleSignIn() {
74+
self.actionToolbarView.setLoading(true)
6375
ProfileModel.signInApple(on: self) { error in
6476
if let error = error {
77+
self.actionToolbarView.setLoading(false)
6578
SPAlert.present(message: error.localizedDescription, haptic: .error)
6679
} else {
67-
self.completion()
80+
self.completion?(self)
81+
}
82+
}
83+
}
84+
85+
@objc func tapContinueAnon() {
86+
if ProfileModel.isAnonymous ?? false {
87+
self.completion?(self)
88+
} else {
89+
self.actionToolbarView.setLoading(true)
90+
ProfileModel.signInAnonymously() { error in
91+
if let error = error {
92+
self.actionToolbarView.setLoading(false)
93+
SPAlert.present(message: error.localizedDescription, haptic: .error)
94+
} else {
95+
self.completion?(self)
96+
}
6897
}
6998
}
7099
}
100+
101+
// MARK: - Private
102+
103+
@objc func updateSkipAuthButton() {
104+
let allowed: Bool = {
105+
if ProfileModel.isAnonymous != nil {
106+
// Any auth already isset.
107+
// Not allowed anonymous auth.
108+
return false
109+
} else {
110+
return true
111+
}
112+
}()
113+
actionToolbarView.skipAuthButton.isHidden = !allowed
114+
}
71115
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2022 Ivan Vorobei ([email protected])
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
import UIKit
23+
import NativeUIKit
24+
import SparrowKit
25+
import SPAlert
26+
27+
open class AuthOnboardingController: AuthController, NativeOnboardingChildInterface {
28+
29+
public weak var onboardingManagerDelegate: NativeOnboardingManagerDelegate?
30+
31+
// MARK: - Init
32+
33+
public init(title: String, description: String) {
34+
super.init(
35+
title: title,
36+
description: description
37+
)
38+
self.completion = { controller in
39+
self.onboardingManagerDelegate?.onboardingActionComplete(for: controller)
40+
}
41+
}
42+
43+
public required init?(coder: NSCoder) {
44+
fatalError("init(coder:) has not been implemented")
45+
}
46+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2021 Ivan Vorobei ([email protected])
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
import UIKit
23+
import SparrowKit
24+
import NativeUIKit
25+
import AuthenticationServices
26+
27+
open class AuthToolBarView: NativeMimicrateToolBarView {
28+
29+
// MARK: - Views
30+
31+
public let activityIndicatorView = UIActivityIndicatorView()
32+
33+
public let authButton = ASAuthorizationAppleIDButton().do {
34+
$0.roundCorners(radius: NativeLargeActionButton.defaultCornerRadius)
35+
$0.layer.masksToBounds = true
36+
}
37+
38+
public let skipAuthButton = SPDimmedButton().do {
39+
$0.setTitle(Texts.Auth.continue_anonymously)
40+
$0.applyDefaultAppearance(with: .tintedContent)
41+
$0.titleLabel?.font = UIFont.preferredFont(forTextStyle: .headline, addPoints: -1)
42+
}
43+
44+
// MARK: - Init
45+
46+
open override func commonInit() {
47+
super.commonInit()
48+
addSubview(activityIndicatorView)
49+
addSubview(authButton)
50+
addSubview(skipAuthButton)
51+
}
52+
53+
// MARK: - Actions
54+
55+
open func setLoading(_ state: Bool) {
56+
if state {
57+
activityIndicatorView.startAnimating()
58+
authButton.isHidden = true
59+
skipAuthButton.isHidden = true
60+
} else {
61+
activityIndicatorView.stopAnimating()
62+
authButton.isHidden = false
63+
skipAuthButton.isHidden = false
64+
}
65+
}
66+
67+
// MARK: - Layout
68+
69+
open override func layoutSubviews() {
70+
super.layoutSubviews()
71+
72+
let authButtonWidth = min(readableWidth, NativeLayout.Sizes.actionable_area_maximum_width)
73+
authButton.frame.setWidth(authButtonWidth)
74+
authButton.frame.setHeight(NativeLargeActionButton.defaultHeight)
75+
authButton.setXCenter()
76+
authButton.frame.origin.y = layoutMargins.top
77+
78+
skipAuthButton.setWidthAndFit(width: layoutWidth)
79+
skipAuthButton.frame.origin.y = authButton.frame.maxY + 12
80+
skipAuthButton.setXCenter()
81+
82+
let contentHeight: CGFloat = skipAuthButton.frame.maxY
83+
activityIndicatorView.setXCenter()
84+
activityIndicatorView.center.y = contentHeight / 2
85+
}
86+
87+
open override func sizeThatFits(_ size: CGSize) -> CGSize {
88+
layoutSubviews()
89+
if skipAuthButton.isHidden {
90+
return .init(width: size.width, height: authButton.frame.maxY + layoutMargins.bottom)
91+
} else {
92+
return .init(width: size.width, height: skipAuthButton.frame.maxY + layoutMargins.bottom)
93+
}
94+
}
95+
}

Sources/SPProfiling/Interface/Profile/Table/DiffableProfileItem.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// SOFTWARE.
2121

2222
import UIKit
23+
import SparrowKit
2324
import SPDiffable
2425
import NativeUIKit
2526

@@ -36,20 +37,13 @@ open class DiffableProfileItem: SPDiffableActionableItem {
3637
cellAuthSubtitle: String,
3738
cellProfileSubtitle: String,
3839
features: [NativeOnboardingFeatureView.FeatureModel],
39-
completion: @escaping ()->Void,
4040
presentOn controller: UIViewController
4141
) {
4242
self.cellAuthSubtitle = cellAuthSubtitle
4343
self.cellProfileSubtitle = cellProfileSubtitle
4444
super.init(id: Self.id, action: { item, indexPath in
4545
if ProfileModel.isAnonymous ?? true {
46-
ProfileModel.showAuth(
47-
title: authTitle,
48-
description: authDescription,
49-
features: features,
50-
completion: completion,
51-
on: controller
52-
)
46+
ProfileModel.showAuth(title: authTitle, description: authDescription, features: features, on: controller)
5347
} else {
5448
ProfileModel.showCurrentProfile(on: controller)
5549
}

Sources/SPProfiling/ProfileModelExtension.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,31 @@ extension ProfileModel {
101101
title: String,
102102
description: String,
103103
features: [NativeOnboardingFeatureView.FeatureModel],
104-
completion: @escaping ()->Void,
105-
on viewController: UIViewController
104+
on presentingController: UIViewController
106105
) {
107-
let controller = AuthController(title: title, description: description, completion: completion)
108-
controller.setFeatures(features)
109-
let navigationController = NativeNavigationController(rootViewController: controller)
110-
controller.navigationItem.rightBarButtonItem = controller.closeBarButtonItem
106+
let authController = AuthController(title: title, description: description, completion: { authController in
107+
guard ProfileModel.isAuthed else { return }
108+
authController.dismiss(animated: true) {
109+
if !(ProfileModel.isAnonymous ?? true) {
110+
ProfileModel.showCurrentProfile(on: presentingController)
111+
}
112+
}
113+
})
114+
authController.setFeatures(features)
115+
let navigationController = NativeNavigationController(rootViewController: authController)
116+
authController.navigationItem.rightBarButtonItem = authController.closeBarButtonItem
111117

112118
let horizontalMargin: CGFloat = NativeLayout.Spaces.Margins.modal_screen_horizontal
113-
controller.modalPresentationStyle = .formSheet
114-
controller.preferredContentSize = .init(width: 540, height: 620)
115-
controller.view.layoutMargins.left = horizontalMargin
116-
controller.view.layoutMargins.right = horizontalMargin
119+
authController.modalPresentationStyle = .formSheet
120+
authController.preferredContentSize = .init(width: 540, height: 620)
121+
authController.view.layoutMargins.left = horizontalMargin
122+
authController.view.layoutMargins.right = horizontalMargin
117123

118124
navigationController.inheritLayoutMarginsForNavigationBar = true
119125
navigationController.inheritLayoutMarginsForСhilds = true
120126
navigationController.viewDidLayoutSubviews()
121127

122-
viewController.present(navigationController)
128+
presentingController.present(navigationController)
123129
}
124130

125131

0 commit comments

Comments
 (0)