Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit c9f5254

Browse files
authored
move background handling into feed for better state control (#2285)
1 parent e4f6e20 commit c9f5254

File tree

7 files changed

+76
-82
lines changed

7 files changed

+76
-82
lines changed

Classes/Issues/IssuesViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ final class IssuesViewController:
505505
emptyView.delegate = self
506506
emptyView.button.isHidden = false
507507
return emptyView
508-
case .loading, .loadingNext:
508+
case .loading, .loadingNext, .initial:
509509
return nil
510510
}
511511
}

Classes/Notifications/NotificationsViewController.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ import Squawk
1313

1414
final class NotificationsViewController: BaseListViewController2<Int>,
1515
BaseListViewController2DataSource,
16-
ForegroundHandlerDelegate,
1716
FlatCacheListener,
1817
TabNavRootViewControllerType,
19-
BaseListViewController2EmptyDataSource
20-
{
18+
BaseListViewController2EmptyDataSource {
2119

2220
private let modelController: NotificationModelController
23-
private let foreground = ForegroundHandler(threshold: 5 * 60)
2421
private let inboxType: InboxType
2522
private var notificationIDs = [String]()
2623

@@ -32,11 +29,13 @@ BaseListViewController2EmptyDataSource
3229
self.modelController = modelController
3330
self.inboxType = inboxType
3431

35-
super.init(emptyErrorMessage: NSLocalizedString("Cannot load your inbox.", comment: ""))
32+
super.init(
33+
emptyErrorMessage: NSLocalizedString("Cannot load your inbox.", comment: ""),
34+
backgroundThreshold: 5 * 60
35+
)
3636

3737
self.dataSource = self
3838
self.emptyDataSource = self
39-
self.foreground.delegate = self
4039

4140
switch inboxType {
4241
case .all: title = NSLocalizedString("All", comment: "")
@@ -283,12 +282,6 @@ BaseListViewController2EmptyDataSource
283282
})
284283
}
285284

286-
// MARK: ForegroundHandlerDelegate
287-
288-
func didForeground(handler: ForegroundHandler) {
289-
feed.refreshHead()
290-
}
291-
292285
// MARK: FlatCacheListener
293286

294287
func flatCacheDidUpdate(cache: FlatCache, update: FlatCache.Update) {

Classes/PullRequestReviews/PullRequestReviewCommentsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ final class PullRequestReviewCommentsViewController: MessageViewController,
173173
emptyView.delegate = self
174174
emptyView.button.isHidden = false
175175
return emptyView
176-
case .loadingNext:
176+
case .loadingNext, .initial:
177177
return nil
178178
case .loading:
179179
return EmptyLoadingView()

Classes/Systems/Feed.swift

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protocol FeedDelegate: class {
1717
final class Feed: NSObject, UIScrollViewDelegate {
1818

1919
enum Status {
20+
case initial
2021
case idle
2122
case loading
2223
case loadingNext
@@ -25,31 +26,54 @@ final class Feed: NSObject, UIScrollViewDelegate {
2526
let swiftAdapter: ListSwiftAdapter
2627
let collectionView: UICollectionView
2728

28-
public private(set) var status: Status = .idle
29+
public private(set) var status: Status = .initial
2930
private weak var delegate: FeedDelegate?
3031
private let feedRefresh = FeedRefresh()
3132
private let managesLayout: Bool
3233
private let loadingView = EmptyLoadingView()
3334

35+
private let backgroundThreshold: CFTimeInterval?
36+
private var backgroundTime: CFTimeInterval?
37+
3438
init(
3539
viewController: UIViewController,
3640
delegate: FeedDelegate,
3741
collectionView: UICollectionView? = nil,
38-
managesLayout: Bool = true
42+
managesLayout: Bool = true,
43+
backgroundThreshold: CFTimeInterval? = nil
3944
) {
4045
self.swiftAdapter = ListSwiftAdapter(viewController: viewController)
4146
self.delegate = delegate
4247
self.managesLayout = managesLayout
4348
self.collectionView = collectionView
4449
?? UICollectionView(frame: .zero, collectionViewLayout: ListCollectionViewLayout.basic())
50+
self.backgroundThreshold = backgroundThreshold
51+
4552
super.init()
53+
4654
self.adapter.scrollViewDelegate = self
4755

4856
self.collectionView.alwaysBounceVertical = true
4957
self.collectionView.backgroundColor = Styles.Colors.background
5058
self.collectionView.refreshControl = feedRefresh.refreshControl
5159
self.collectionView.keyboardDismissMode = .onDrag
5260
feedRefresh.refreshControl.addTarget(self, action: #selector(Feed.onRefresh(sender:)), for: .valueChanged)
61+
62+
if backgroundThreshold != nil {
63+
let center = NotificationCenter.default
64+
center.addObserver(
65+
self,
66+
selector: #selector(didBecomeActive),
67+
name: .UIApplicationDidBecomeActive,
68+
object: nil
69+
)
70+
center.addObserver(
71+
self,
72+
selector: #selector(willResignActive),
73+
name: .UIApplicationWillResignActive,
74+
object: nil
75+
)
76+
}
5377
}
5478

5579
// MARK: Public API
@@ -122,7 +146,7 @@ final class Feed: NSObject, UIScrollViewDelegate {
122146
// MARK: Private API
123147

124148
private func refresh() {
125-
guard status == .idle else { return }
149+
guard status == .idle || status == .initial else { return }
126150
status = .loading
127151
delegate?.loadFromNetwork(feed: self)
128152
}
@@ -150,4 +174,37 @@ final class Feed: NSObject, UIScrollViewDelegate {
150174
}
151175
}
152176

177+
// MARK: Notifications
178+
179+
@objc func willResignActive() {
180+
backgroundTime = CACurrentMediaTime()
181+
}
182+
183+
@objc func didBecomeActive() {
184+
defer { backgroundTime = nil }
185+
186+
let refresh: (Bool) -> Void = { head in
187+
// there seems to be a bug where refreshing while still becoming active messes up the iOS 11 header and
188+
// refresh control. put an artificial delay to let the system cool down?
189+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
190+
if head {
191+
self.refreshHead()
192+
} else {
193+
self.refresh()
194+
}
195+
})
196+
}
197+
198+
// if foreground and haven't done an initial load, refresh
199+
// can occur when process is still alive after a background fetch
200+
if status == .initial {
201+
// dont refresh head since spinner will still be shown
202+
refresh(false)
203+
} else if let backgroundTime = self.backgroundTime,
204+
let backgroundThreshold = self.backgroundThreshold,
205+
CACurrentMediaTime() - backgroundTime > backgroundThreshold {
206+
refresh(true)
207+
}
208+
}
209+
153210
}

Classes/Systems/ForegroundHandler.swift

Lines changed: 0 additions & 58 deletions
This file was deleted.

Classes/View Controllers/BaseListViewController2.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@ ListSwiftAdapterEmptyViewSource,
2525
EmptyViewDelegate {
2626

2727
private let emptyErrorMessage: String
28+
private let backgroundThreshold: CFTimeInterval?
2829

2930
public weak var dataSource: BaseListViewController2DataSource?
3031
public weak var emptyDataSource: BaseListViewController2EmptyDataSource?
3132

32-
public private(set) lazy var feed: Feed = { Feed(viewController: self, delegate: self) }()
33+
public private(set) lazy var feed: Feed = { Feed(
34+
viewController: self,
35+
delegate: self,
36+
backgroundThreshold: backgroundThreshold
37+
) }()
3338
private var page: PageType?
3439
private var hasError = false
3540

36-
init(emptyErrorMessage: String) {
41+
init(emptyErrorMessage: String, backgroundThreshold: CFTimeInterval? = nil) {
3742
self.emptyErrorMessage = emptyErrorMessage
43+
self.backgroundThreshold = backgroundThreshold
3844
super.init(nibName: nil, bundle: nil)
3945
}
4046

Freetime.xcodeproj/project.pbxproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@
383383
29DB264A1FCA10A800C3D0C9 /* GithubHighlighting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29DB26491FCA10A800C3D0C9 /* GithubHighlighting.swift */; };
384384
29DB5D152132227F00E408D9 /* InboxZeroLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29DB5D142132227F00E408D9 /* InboxZeroLoader.swift */; };
385385
29DB5D192133418300E408D9 /* LocalNotificationsCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29DB5D182133418200E408D9 /* LocalNotificationsCache.swift */; };
386-
29EB1EEF1F425E5100A200B4 /* ForegroundHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29EB1EEE1F425E5100A200B4 /* ForegroundHandler.swift */; };
387386
29EDFE7C1F65C580005BCCEB /* SplitViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29EDFE7B1F65C580005BCCEB /* SplitViewTests.swift */; };
388387
29EDFE821F661562005BCCEB /* RepositoryReadmeModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29EDFE811F661562005BCCEB /* RepositoryReadmeModel.swift */; };
389388
29EDFE841F661776005BCCEB /* RepositoryReadmeSectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29EDFE831F661776005BCCEB /* RepositoryReadmeSectionController.swift */; };
@@ -926,7 +925,6 @@
926925
29DB26491FCA10A800C3D0C9 /* GithubHighlighting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GithubHighlighting.swift; sourceTree = "<group>"; };
927926
29DB5D142132227F00E408D9 /* InboxZeroLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InboxZeroLoader.swift; sourceTree = "<group>"; };
928927
29DB5D182133418200E408D9 /* LocalNotificationsCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalNotificationsCache.swift; sourceTree = "<group>"; };
929-
29EB1EEE1F425E5100A200B4 /* ForegroundHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForegroundHandler.swift; sourceTree = "<group>"; };
930928
29EDFE7B1F65C580005BCCEB /* SplitViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SplitViewTests.swift; sourceTree = "<group>"; };
931929
29EDFE811F661562005BCCEB /* RepositoryReadmeModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepositoryReadmeModel.swift; sourceTree = "<group>"; };
932930
29EDFE831F661776005BCCEB /* RepositoryReadmeSectionController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepositoryReadmeSectionController.swift; sourceTree = "<group>"; };
@@ -1601,7 +1599,6 @@
16011599
297AE84E1EC0D58A00B44A1F /* Info.plist */,
16021600
DC60C6D41F983DF800241271 /* IssueLabelCellTests.swift */,
16031601
29A476B11ED24D99005D0953 /* IssueTests.swift */,
1604-
BDB6AA752165B8EA009BB73C /* SwitchBranches.swift */,
16051602
29C2950D1EC7B43B00D46CD2 /* ListKitTestCase.swift */,
16061603
29C295091EC7AFA500D46CD2 /* ListTestKit.swift */,
16071604
2977D8BE215AE12D0073F737 /* LocalNotificationCacheTests.swift */,
@@ -1615,6 +1612,7 @@
16151612
DC60C6D21F983BB900241271 /* SignatureTests.swift */,
16161613
45A9D0381F9D3D6A00FD5AEF /* Snapshot Tests */,
16171614
29EDFE7B1F65C580005BCCEB /* SplitViewTests.swift */,
1615+
BDB6AA752165B8EA009BB73C /* SwitchBranches.swift */,
16181616
295F52A61EF1B9D2000B53CF /* Test.md */,
16191617
);
16201618
path = FreetimeTests;
@@ -1694,7 +1692,6 @@
16941692
DC78570F1F97F546009BADDA /* Debouncer.swift */,
16951693
29C167791ECA14F700439D62 /* Feed.swift */,
16961694
54AD5E8D1F24D953004A4BD6 /* FeedSelectionProviding.swift */,
1697-
29EB1EEE1F425E5100A200B4 /* ForegroundHandler.swift */,
16981695
29C167731ECA0DBB00439D62 /* GithubAPIDateFormatter.swift */,
16991696
29C0E7061ECBC6C50051D756 /* GithubClient.swift */,
17001697
2981A8A31EFE9FC700E25EF1 /* GithubEmoji.swift */,
@@ -2787,7 +2784,6 @@
27872784
291929421F3EA8CD0012067B /* File.swift in Sources */,
27882785
299E86491EFD9DBB00E5FE70 /* FlexController.m in Sources */,
27892786
29F3A18620CBF99E00645CB7 /* NotificationModelController.swift in Sources */,
2790-
29EB1EEF1F425E5100A200B4 /* ForegroundHandler.swift in Sources */,
27912787
29C167741ECA0DBB00439D62 /* GithubAPIDateFormatter.swift in Sources */,
27922788
290CA770216AE91300DE04F8 /* UITabBarController+SelectType.swift in Sources */,
27932789
294434E11FB1F2DA00050C06 /* BookmarkNavigationController.swift in Sources */,

0 commit comments

Comments
 (0)