Skip to content

Commit 8e0a5b7

Browse files
refactor(onboarding): scrollview ♻️ (#355)
* refactor(onboarding): scrollview ♻️ * refactor(helpers): add missing function ♻️
1 parent e6ece12 commit 8e0a5b7

6 files changed

Lines changed: 95 additions & 31 deletions

File tree

waosSwift.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
BF00138B237427670044C01A /* Eureka.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF00138A237427660044C01A /* Eureka.swift */; };
11+
BF01C99B2621935A00143475 /* UIScrollView+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF01C99A2621935A00143475 /* UIScrollView+Rx.swift */; };
12+
BF01C9A12621937800143475 /* UIScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF01C9A02621937800143475 /* UIScrollView.swift */; };
1113
BF058D772282BCAC00E2A077 /* AuthSigninController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF058D762282BCAC00E2A077 /* AuthSigninController.swift */; };
1214
BF058D792282BCB600E2A077 /* AuthFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF058D782282BCB600E2A077 /* AuthFlow.swift */; };
1315
BF058D7B2282BCC500E2A077 /* AuthSigninReactor.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF058D7A2282BCC500E2A077 /* AuthSigninReactor.swift */; };
@@ -209,6 +211,8 @@
209211

210212
/* Begin PBXFileReference section */
211213
BF00138A237427660044C01A /* Eureka.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Eureka.swift; sourceTree = "<group>"; };
214+
BF01C99A2621935A00143475 /* UIScrollView+Rx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIScrollView+Rx.swift"; sourceTree = "<group>"; };
215+
BF01C9A02621937800143475 /* UIScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIScrollView.swift; sourceTree = "<group>"; };
212216
BF05418D2361964F008628CF /* waosSwift.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = waosSwift.entitlements; sourceTree = "<group>"; };
213217
BF058D762282BCAC00E2A077 /* AuthSigninController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSigninController.swift; sourceTree = "<group>"; };
214218
BF058D782282BCB600E2A077 /* AuthFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthFlow.swift; sourceTree = "<group>"; };
@@ -755,8 +759,8 @@
755759
children = (
756760
BF4A2F88225C9AFF0001B4CE /* app */,
757761
BF4A2F4F225B7C200001B4CE /* core */,
758-
BF058D712282BC5300E2A077 /* auth */,
759762
BF4A2F5C225B7C200001B4CE /* onBoarding */,
763+
BF058D712282BC5300E2A077 /* auth */,
760764
BF4A2F77225BB65E0001B4CE /* tasks */,
761765
BF4A2F78225BB6690001B4CE /* secondController */,
762766
BF10C81B25F78B8C00327730 /* users */,
@@ -878,6 +882,7 @@
878882
BF1D704C245F561100F8EA36 /* Data.swift */,
879883
BFBBE1AD2566F4D50067510D /* UIButton.swift */,
880884
BFA1E4582587C9FF009210B5 /* UINavigationController.swift */,
885+
BF01C9A02621937800143475 /* UIScrollView.swift */,
881886
);
882887
path = Extensions;
883888
sourceTree = "<group>";
@@ -1076,6 +1081,7 @@
10761081
BF62ADA623E45ECC000D5225 /* UIAlertController+Rx.swift */,
10771082
BFBA338B2403FBB500CF9299 /* UIImageView+Kingfisher.swift */,
10781083
BF1211B926038C3800C57F1F /* SwiftSpinner+Rx.swift */,
1084+
BF01C99A2621935A00143475 /* UIScrollView+Rx.swift */,
10791085
);
10801086
path = Rx;
10811087
sourceTree = "<group>";
@@ -1396,6 +1402,7 @@
13961402
BF4A2F87225C8F160001B4CE /* UILocalizations.swift in Sources */,
13971403
BF058D7E2282BCDD00E2A077 /* AuthService.swift in Sources */,
13981404
BF9A3674228DFDAE00EE2AB8 /* TasksResponses.swift in Sources */,
1405+
BF01C9A12621937800143475 /* UIScrollView.swift in Sources */,
13991406
BF8C6727227C43E80012B5A8 /* Array+SectionModel.swift in Sources */,
14001407
BF71BD9322A12A2E002CF9DE /* Validations.swift in Sources */,
14011408
BF4A2F71225BB2CD0001B4CE /* CoreFlow.swift in Sources */,
@@ -1439,6 +1446,7 @@
14391446
BF7975042531E8B700B92B0D /* UITextField.swift in Sources */,
14401447
BF8C672C2280177E0012B5A8 /* UICollectionView+ReusableKit.swift in Sources */,
14411448
BFA92D2F237C050C006A3B8D /* CoreFormController.swift in Sources */,
1449+
BF01C99B2621935A00143475 /* UIScrollView+Rx.swift in Sources */,
14421450
BF5BA0F0251206FA005DCDDD /* ASAuthorizationControllerProxy.swift in Sources */,
14431451
BFCAA248250A7CE40065FB69 /* AuthForgotController.swift in Sources */,
14441452
BF00138B237427670044C01A /* Eureka.swift in Sources */,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Dependencies
3+
*/
4+
5+
import UIKit
6+
7+
/**
8+
* Dependencies
9+
*/
10+
11+
extension UIScrollView {
12+
13+
func isBottom(toleranceHeight: CGFloat) -> Bool {
14+
return contentOffset.y > contentSize.height - frame.height + contentInset.bottom - toleranceHeight
15+
}
16+
17+
func isNeedScroll() -> Bool {
18+
return (contentSize.width > self.frame.width) ||
19+
(contentSize.height > self.frame.height)
20+
}
21+
22+
func scrollToBottom(animation: Bool) {
23+
scrollToBottom(offset: 0, animation: animation)
24+
}
25+
26+
func scrollToBottom(offset: CGFloat, animation: Bool) {
27+
UIView.animate(withDuration: animation ? 0.25 : 0) {
28+
self.contentOffset = CGPoint(x: self.contentOffset.x,
29+
y: self.contentSize.height - self.frame.size.height + self.contentInset.bottom + offset)
30+
}
31+
}
32+
33+
var remaining: CGPoint {
34+
let horizontal = self.contentSize.width - self.frame.width - self.contentOffset.x
35+
let vertical = self.contentSize.height - self.frame.height - self.contentOffset.y
36+
return CGPoint(x: horizontal, y: vertical)
37+
}
38+
39+
func setCurrentPage(_ page: Int, animated: Bool) {
40+
var rect = bounds
41+
rect.origin.x = rect.width * CGFloat(page)
42+
rect.origin.y = 0
43+
scrollRectToVisible(rect, animated: animated)
44+
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Dependencies
3+
*/
4+
import UIKit
5+
import RxCocoa
6+
import RxSwift
7+
8+
/**
9+
* Dependencies
10+
*/
11+
12+
extension Reactive where Base: UIScrollView {
13+
14+
var currentPage: Observable<Int> {
15+
return didEndDecelerating.map({
16+
let pageWidth = self.base.frame.width
17+
let pageHorizontal = floor((self.base.contentOffset.x - pageWidth / 2) / pageWidth) + 1
18+
let pageHeight = self.base.frame.height
19+
let pageVertical = floor((self.base.contentOffset.y - pageHeight / 2) / pageHeight) + 1
20+
return Int(pageHorizontal != 0 ? pageHorizontal : pageVertical != 0 ? pageVertical : 0 )
21+
})
22+
}
23+
24+
var isReachedBottom: ControlEvent<Void> {
25+
let source = self.contentOffset
26+
.filter { [weak base = self.base] _ in
27+
guard let base = base else { return false }
28+
return base.isBottom(toleranceHeight: base.frame.height / 2)
29+
}
30+
.map { _ in Void() }
31+
return ControlEvent(events: source)
32+
}
33+
34+
}

waosSwift/modules/auth/controllers/AuthForgotController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ final class AuthForgotController: CoreController, View, Stepper {
8787
self.view.addSubview(self.labelSuccess)
8888
// config
8989
self.view.backgroundColor = Metric.primary
90+
self.navigationController?.clear()
9091
}
9192

9293
override func setupConstraints() {

waosSwift/modules/auth/controllers/AuthSignupController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ final class AuthSignUpController: CoreController, View, Stepper {
108108
self.view.addSubview(self.labelErrors)
109109
// config
110110
self.view.backgroundColor = Metric.primary
111+
self.navigationController?.clear()
111112
}
112113

113114
override func setupConstraints() {

waosSwift/modules/onBoarding/controllers/OnBoardingController.swift

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,14 @@ private extension OnboardingController {
115115
// MARK: views (View -> View)
116116

117117
func bindView(_ reactor: OnboardingReactor) {
118-
pageControl.rx.controlEvent(.valueChanged)
118+
self.pageControl.rx.controlEvent(.valueChanged)
119119
.subscribe(onNext: { [weak self] in
120-
guard let currentPage = self?.pageControl.currentPage else {
121-
return
122-
}
123-
self?.scrollView.setCurrentPage(currentPage, animated: true)
124-
})
125-
.disposed(by: self.disposeBag)
126-
127-
scrollView.rx.currentPage
128-
.subscribe(onNext: { [weak self] in
129-
self?.pageControl.currentPage = $0
120+
self?.scrollView.setCurrentPage(self?.pageControl.currentPage ?? 0, animated: true)
130121
})
131122
.disposed(by: self.disposeBag)
123+
self.scrollView.rx.currentPage
124+
.bind(to: self.pageControl.rx.currentPage)
125+
.disposed(by: disposeBag)
132126
}
133127

134128
// MARK: actions (View -> Reactor)
@@ -167,22 +161,3 @@ private extension OnboardingController {
167161
.disposed(by: self.disposeBag)
168162
}
169163
}
170-
171-
extension Reactive where Base: UIScrollView {
172-
var currentPage: Observable<Int> {
173-
return didEndDecelerating.map({
174-
let pageWidth = self.base.frame.width
175-
let page = floor((self.base.contentOffset.x - pageWidth / 2) / pageWidth) + 1
176-
return Int(page)
177-
})
178-
}
179-
}
180-
181-
extension UIScrollView {
182-
func setCurrentPage(_ page: Int, animated: Bool) {
183-
var rect = bounds
184-
rect.origin.x = rect.width * CGFloat(page)
185-
rect.origin.y = 0
186-
scrollRectToVisible(rect, animated: animated)
187-
}
188-
}

0 commit comments

Comments
 (0)