Skip to content

Commit cb343db

Browse files
committed
Migrated scroll observing code to worker.
1 parent 7710b2b commit cb343db

File tree

9 files changed

+238
-48
lines changed

9 files changed

+238
-48
lines changed

Example App/NativeUIKit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example App/NativeUIKit.xcodeproj/xcshareddata/xcschemes/watchOS Example.xcscheme

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,46 @@
5454
debugDocumentVersioning = "YES"
5555
debugServiceExtension = "internal"
5656
allowLocationSimulation = "YES">
57-
<BuildableProductRunnable
58-
runnableDebuggingMode = "0">
57+
<RemoteRunnable
58+
runnableDebuggingMode = "2"
59+
BundleIdentifier = "com.apple.Carousel"
60+
RemotePath = "/(null)">
5961
<BuildableReference
6062
BuildableIdentifier = "primary"
6163
BlueprintIdentifier = "F4C33E2F26C932C8001A28B1"
6264
BuildableName = "watchOS Example.app"
6365
BlueprintName = "watchOS Example"
6466
ReferencedContainer = "container:NativeUIKit.xcodeproj">
6567
</BuildableReference>
66-
</BuildableProductRunnable>
68+
</RemoteRunnable>
6769
</LaunchAction>
6870
<ProfileAction
6971
buildConfiguration = "Release"
7072
shouldUseLaunchSchemeArgsEnv = "YES"
7173
savedToolIdentifier = ""
7274
useCustomWorkingDirectory = "NO"
7375
debugDocumentVersioning = "YES">
74-
<BuildableProductRunnable
75-
runnableDebuggingMode = "0">
76+
<RemoteRunnable
77+
runnableDebuggingMode = "2"
78+
BundleIdentifier = "com.apple.Carousel"
79+
RemotePath = "/(null)">
7680
<BuildableReference
7781
BuildableIdentifier = "primary"
7882
BlueprintIdentifier = "F4C33E2F26C932C8001A28B1"
7983
BuildableName = "watchOS Example.app"
8084
BlueprintName = "watchOS Example"
8185
ReferencedContainer = "container:NativeUIKit.xcodeproj">
8286
</BuildableReference>
83-
</BuildableProductRunnable>
87+
</RemoteRunnable>
88+
<MacroExpansion>
89+
<BuildableReference
90+
BuildableIdentifier = "primary"
91+
BlueprintIdentifier = "F4C33E2F26C932C8001A28B1"
92+
BuildableName = "watchOS Example.app"
93+
BlueprintName = "watchOS Example"
94+
ReferencedContainer = "container:NativeUIKit.xcodeproj">
95+
</BuildableReference>
96+
</MacroExpansion>
8497
</ProfileAction>
8598
<AnalyzeAction
8699
buildConfiguration = "Debug">

Example App/iOS Example/Scenes/RootController.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import NativeUIKit
2525

2626
class RootController: NativeScrollController {
2727

28+
override init() {
29+
super.init(navigationScrollBehavior: .hidable)
30+
}
31+
32+
required init?(coder: NSCoder) {
33+
fatalError("init(coder:) has not been implemented")
34+
}
35+
2836
let actionToolBarView = NativeLargeActionToolBarView().do {
2937
$0.actionButton.setTitle("Action Button")
3038
$0.footerLabel.text = "Here footer text and it provided my developer and maybe some lines even."

Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ let package = Package(
1414
],
1515
dependencies: [
1616
.package(name: "SparrowKit", url: "https://github.com/ivanvorobei/SparrowKit", .upToNextMajor(from: "3.2.6")),
17-
.package(name: "SPPerspective", url: "https://github.com/ivanvorobei/SPPerspective", .upToNextMajor(from: "1.3.6"))
17+
.package(name: "SPPerspective", url: "https://github.com/ivanvorobei/SPPerspective", .upToNextMajor(from: "1.3.6")),
18+
.package(name: "SPDiffable", url: "https://github.com/ivanvorobei/SPDiffable", .upToNextMajor(from: "1.4.2"))
1819
],
1920
targets: [
2021
.target(
2122
name: "NativeUIKit",
2223
dependencies: [
2324
.product(name: "SparrowKit", package: "SparrowKit"),
24-
.product(name: "SPPerspective", package: "SPPerspective")
25+
.product(name: "SPPerspective", package: "SPPerspective"),
26+
.product(name: "SPDiffable", package: "SPDiffable")
2527
],
2628
swiftSettings: [
2729
.define("NATIVEUIKIT_SPM")
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
#if canImport(UIKit) && (os(iOS))
23+
import UIKit
24+
import SparrowKit
25+
import SPDiffable
26+
27+
@available(iOS 13.0, *)
28+
open class NativeHeaderTableController: SPDiffableTableController {
29+
30+
// MARK: - Data
31+
32+
private var scrollWorker: NativeNavigationScrollWorker!
33+
34+
// MARK: - Init
35+
36+
init(headerView: UIView, navigationScrollBehavior: NativeNavigationScrollBehavior) {
37+
super.init(style: .insetGrouped)
38+
scrollWorker = NativeNavigationScrollWorker(scrollView: self.tableView, scrollBehavior: navigationScrollBehavior)
39+
tableView.tableHeaderView = HeaderContainerView(contentView: headerView)
40+
}
41+
42+
public required init?(coder: NSCoder) {
43+
fatalError("init(coder:) has not been implemented")
44+
}
45+
46+
// MARK: - Layout
47+
48+
open override func viewDidLayoutSubviews() {
49+
super.viewDidLayoutSubviews()
50+
if let headerView = tableView.tableHeaderView as? HeaderContainerView {
51+
headerView.setWidthAndFit(width: view.frame.width)
52+
if cachedHeaderHeight != headerView.frame.height {
53+
cachedHeaderHeight = headerView.frame.height
54+
DispatchQueue.main.async { [weak self] in
55+
guard let self = self else { return }
56+
guard let snapshot = self.diffableDataSource?.snapshot() else { return }
57+
self.diffableDataSource?.apply(snapshot)
58+
}
59+
}
60+
}
61+
}
62+
63+
private var cachedHeaderHeight: CGFloat? = nil
64+
65+
// MARK: - UIScrollViewDelegate
66+
67+
open override func scrollViewDidScroll(_ scrollView: UIScrollView) {
68+
if scrollView == self.tableView {
69+
scrollWorker.scrollViewDidScroll()
70+
}
71+
}
72+
73+
// MARK: - Views
74+
75+
class HeaderContainerView: SPView {
76+
77+
let contentView: UIView
78+
79+
init(contentView: UIView) {
80+
self.contentView = contentView
81+
super.init()
82+
}
83+
84+
required init?(coder aDecoder: NSCoder) {
85+
fatalError("init(coder:) has not been implemented")
86+
}
87+
88+
override func commonInit() {
89+
super.commonInit()
90+
insetsLayoutMarginsFromSafeArea = false
91+
layoutMargins = .zero
92+
addSubview(contentView)
93+
}
94+
95+
override func layoutSubviews() {
96+
super.layoutSubviews()
97+
contentView.setWidthAndFit(width: frame.width)
98+
contentView.frame.origin.x = .zero
99+
contentView.frame.origin.y = .zero
100+
}
101+
102+
override func sizeThatFits(_ size: CGSize) -> CGSize {
103+
frame.setWidth(size.width)
104+
layoutSubviews()
105+
return .init(width: size.width, height: contentView.frame.maxY)
106+
}
107+
}
108+
}
109+
#endif

Sources/NativeUIKit/Controllers/NativeScrollController.swift

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,18 @@ open class NativeScrollController: SPScrollController {
3030

3131
// MARK: - Data
3232

33-
/**
34-
NativeUIKit: Scroll Behavior for navigation bar.
35-
*/
36-
public let navigationScrollBehavior: NavigationScrollBehavior
37-
38-
/**
39-
NativeUIKit: Set height for change navigation appearance in this range.
40-
*/
41-
open var heightForChangeNavigationAppearance: CGFloat = .zero {
42-
didSet {
43-
scrollView.contentInset.top = heightForChangeNavigationAppearance
44-
scrollView.contentInset.bottom = NativeLayout.Spaces.Scroll.bottom_inset_reach_end
45-
scrollView.delegate?.scrollViewDidScroll?(self.scrollView)
46-
}
47-
}
33+
private var scrollWorker: NativeNavigationScrollWorker!
4834

4935
// MARK: - Init
5036

5137
public override init() {
52-
self.navigationScrollBehavior = .default
5338
super.init()
39+
scrollWorker = NativeNavigationScrollWorker(scrollView: self.scrollView, scrollBehavior: .default)
5440
}
5541

56-
public init(navigationScrollBehavior: NavigationScrollBehavior) {
57-
self.navigationScrollBehavior = navigationScrollBehavior
42+
public init(navigationScrollBehavior: NativeNavigationScrollBehavior) {
5843
super.init()
44+
scrollWorker = NativeNavigationScrollWorker(scrollView: self.scrollView, scrollBehavior: navigationScrollBehavior)
5945
}
6046

6147
public required init?(coder: NSCoder) {
@@ -65,33 +51,13 @@ open class NativeScrollController: SPScrollController {
6551
open override func commonInit() {
6652
super.commonInit()
6753
scrollView.delegate = self
68-
switch self.navigationScrollBehavior {
69-
case .default:
70-
break
71-
case .hidable:
72-
heightForChangeNavigationAppearance = 16
73-
}
74-
}
75-
76-
// MARK: - Models
77-
78-
public enum NavigationScrollBehavior {
79-
80-
case `default`
81-
case hidable
8254
}
8355

8456
// MARK: - UIScrollViewDelegate
8557

8658
open func scrollViewDidScroll(_ scrollView: UIScrollView) {
87-
switch navigationScrollBehavior {
88-
case .default:
89-
break
90-
case .hidable:
91-
let contentOffsetY = scrollView.safeAreaInsets.top + scrollView.contentInset.top + scrollView.contentOffset.y
92-
let spaceToNavigationBottom = scrollView.contentInset.top
93-
let progress = (heightForChangeNavigationAppearance - (spaceToNavigationBottom - contentOffsetY)) / heightForChangeNavigationAppearance
94-
navigationController?.navigationBar.setBackgroundAlpha(progress)
59+
if scrollView == self.scrollView {
60+
scrollWorker.scrollViewDidScroll()
9561
}
9662
}
9763
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
#if canImport(UIKit) && (os(iOS))
23+
import UIKit
24+
import SparrowKit
25+
26+
open class NativeNavigationScrollWorker: NSObject {
27+
28+
private weak var scrollView: UIScrollView?
29+
private let scrollBehavior: NativeNavigationScrollBehavior
30+
private let heightForChangeNavigationAppearance: CGFloat = 16
31+
32+
init(scrollView: UIScrollView, scrollBehavior: NativeNavigationScrollBehavior) {
33+
self.scrollView = scrollView
34+
self.scrollBehavior = scrollBehavior
35+
super.init()
36+
scrollView.contentInset.top = heightForChangeNavigationAppearance
37+
scrollView.contentInset.bottom = NativeLayout.Spaces.Scroll.bottom_inset_reach_end
38+
}
39+
40+
public func scrollViewDidScroll() {
41+
switch scrollBehavior {
42+
case .default:
43+
break
44+
case .hidable:
45+
guard let scrollView = self.scrollView else { return }
46+
let contentOffsetY = scrollView.safeAreaInsets.top + scrollView.contentInset.top + scrollView.contentOffset.y
47+
let spaceToNavigationBottom = scrollView.contentInset.top
48+
let progress = (heightForChangeNavigationAppearance - (spaceToNavigationBottom - contentOffsetY)) / heightForChangeNavigationAppearance
49+
scrollView.viewController?.navigationController?.navigationBar.setBackgroundAlpha(progress)
50+
}
51+
}
52+
}
53+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
#if canImport(UIKit) && (os(iOS))
23+
import UIKit
24+
25+
public enum NativeNavigationScrollBehavior {
26+
27+
case `default`
28+
case hidable
29+
}
30+
#endif

0 commit comments

Comments
 (0)