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

Commit 8452d73

Browse files
authored
Display empty notification cell (#1884)
1 parent 0858ae1 commit 8452d73

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

Diff for: Classes/Notifications/NoNewNotificationsSectionController.swift

+19-15
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import UIKit
1010
import IGListKit
1111
import Crashlytics
1212

13-
final class NoNewNotificationSectionController: ListSectionController {
13+
final class NoNewNotificationSectionController: ListSwiftSectionController<String> {
1414

15-
private let topInset: CGFloat
1615
private let layoutInsets: UIEdgeInsets
1716
private let client = NotificationEmptyMessageClient()
1817

@@ -23,26 +22,31 @@ final class NoNewNotificationSectionController: ListSectionController {
2322
}
2423
private var state: State = .loading
2524

26-
init(topInset: CGFloat, layoutInsets: UIEdgeInsets) {
27-
self.topInset = topInset
25+
init(layoutInsets: UIEdgeInsets) {
2826
self.layoutInsets = layoutInsets
2927
super.init()
3028
client.fetch { [weak self] (result) in
3129
self?.handleFinished(result)
3230
}
3331
}
3432

35-
override func sizeForItem(at index: Int) -> CGSize {
36-
guard let size = collectionContext?.containerSize
37-
else { fatalError("Missing context") }
38-
return CGSize(width: size.width, height: size.height - topInset - layoutInsets.top - layoutInsets.bottom)
39-
}
40-
41-
override func cellForItem(at index: Int) -> UICollectionViewCell {
42-
guard let cell = collectionContext?.dequeueReusableCell(of: NoNewNotificationsCell.self, for: self, at: index) as? NoNewNotificationsCell
43-
else { fatalError("Missing context or cell is wrong type") }
44-
configure(cell)
45-
return cell
33+
override func createBinders(from value: String) -> [ListBinder] {
34+
return [
35+
binder(
36+
value,
37+
cellType: ListCellType.class(NoNewNotificationsCell.self),
38+
size: { [layoutInsets] in
39+
return CGSize(
40+
width: $0.collection.containerSize.width,
41+
height: $0.collection.containerSize.height - layoutInsets.top - layoutInsets.bottom
42+
)
43+
},
44+
configure: { [weak self] in
45+
// TODO accessing the value seems to be required for this to compile
46+
print($1.value)
47+
self?.configure($0)
48+
})
49+
]
4650
}
4751

4852
// MARK: Private API

Diff for: Classes/Notifications/NotificationsViewController.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import FlatCache
1212

1313
final class NotificationsViewController: BaseListViewController2<Int>,
1414
BaseListViewController2DataSource,
15-
ForegroundHandlerDelegate, FlatCacheListener {
15+
ForegroundHandlerDelegate,
16+
FlatCacheListener,
17+
BaseListViewController2EmptyDataSource {
1618

1719
private let modelController: NotificationModelController
1820
private let foreground = ForegroundHandler(threshold: 5 * 60)
@@ -30,6 +32,7 @@ ForegroundHandlerDelegate, FlatCacheListener {
3032
super.init(emptyErrorMessage: NSLocalizedString("Cannot load your inbox.", comment: ""))
3133

3234
self.dataSource = self
35+
self.emptyDataSource = self
3336
self.foreground.delegate = self
3437

3538
switch inboxType {
@@ -268,6 +271,15 @@ ForegroundHandlerDelegate, FlatCacheListener {
268271
}
269272
}
270273

274+
// MARK: BaseListViewController2EmptyDataSource
275+
276+
func emptyModel(for adapter: ListSwiftAdapter) -> ListSwiftPair {
277+
let layoutInsets = view.safeAreaInsets
278+
return ListSwiftPair.pair("empty-notification-value", {
279+
return NoNewNotificationSectionController(layoutInsets: layoutInsets)
280+
})
281+
}
282+
271283
// MARK: ForegroundHandlerDelegate
272284

273285
func didForeground(handler: ForegroundHandler) {

Diff for: Classes/View Controllers/BaseListViewController2.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ protocol BaseListViewController2DataSource: class {
1313
func models(adapter: ListSwiftAdapter) -> [ListSwiftPair]
1414
}
1515

16+
protocol BaseListViewController2EmptyDataSource: class {
17+
func emptyModel(for adapter: ListSwiftAdapter) -> ListSwiftPair
18+
}
19+
1620
class BaseListViewController2<PageType: CustomStringConvertible>: UIViewController,
1721
ListSwiftAdapterDataSource,
1822
FeedDelegate,
1923
LoadMoreSectionController2Delegate {
2024

2125
private let emptyErrorMessage: String
26+
2227
public weak var dataSource: BaseListViewController2DataSource?
28+
public weak var emptyDataSource: BaseListViewController2EmptyDataSource?
2329

2430
public private(set) lazy var feed: Feed = { Feed(viewController: self, delegate: self) }()
2531
private var page: PageType?
2632
private var hasError = false
27-
private let emptyKey: ListDiffable = "emptyKey" as ListDiffable
2833

2934
init(emptyErrorMessage: String) {
3035
self.emptyErrorMessage = emptyErrorMessage
@@ -118,6 +123,12 @@ LoadMoreSectionController2Delegate {
118123
return []
119124
}
120125

126+
if let emptyDataSource = self.emptyDataSource,
127+
hasNoObjects,
128+
feed.status == .idle {
129+
return [emptyDataSource.emptyModel(for: adapter)]
130+
}
131+
121132
if let page = self.page?.description {
122133
let pagePair = ListSwiftPair.pair(page) { [weak self] in
123134
let controller = LoadMoreSectionController2()

0 commit comments

Comments
 (0)