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

Commit

Permalink
Display empty notification cell (#1884)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnystrom authored Jun 25, 2018
1 parent 0858ae1 commit 8452d73
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
34 changes: 19 additions & 15 deletions Classes/Notifications/NoNewNotificationsSectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import UIKit
import IGListKit
import Crashlytics

final class NoNewNotificationSectionController: ListSectionController {
final class NoNewNotificationSectionController: ListSwiftSectionController<String> {

private let topInset: CGFloat
private let layoutInsets: UIEdgeInsets
private let client = NotificationEmptyMessageClient()

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

init(topInset: CGFloat, layoutInsets: UIEdgeInsets) {
self.topInset = topInset
init(layoutInsets: UIEdgeInsets) {
self.layoutInsets = layoutInsets
super.init()
client.fetch { [weak self] (result) in
self?.handleFinished(result)
}
}

override func sizeForItem(at index: Int) -> CGSize {
guard let size = collectionContext?.containerSize
else { fatalError("Missing context") }
return CGSize(width: size.width, height: size.height - topInset - layoutInsets.top - layoutInsets.bottom)
}

override func cellForItem(at index: Int) -> UICollectionViewCell {
guard let cell = collectionContext?.dequeueReusableCell(of: NoNewNotificationsCell.self, for: self, at: index) as? NoNewNotificationsCell
else { fatalError("Missing context or cell is wrong type") }
configure(cell)
return cell
override func createBinders(from value: String) -> [ListBinder] {
return [
binder(
value,
cellType: ListCellType.class(NoNewNotificationsCell.self),
size: { [layoutInsets] in
return CGSize(
width: $0.collection.containerSize.width,
height: $0.collection.containerSize.height - layoutInsets.top - layoutInsets.bottom
)
},
configure: { [weak self] in
// TODO accessing the value seems to be required for this to compile
print($1.value)
self?.configure($0)
})
]
}

// MARK: Private API
Expand Down
14 changes: 13 additions & 1 deletion Classes/Notifications/NotificationsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import FlatCache

final class NotificationsViewController: BaseListViewController2<Int>,
BaseListViewController2DataSource,
ForegroundHandlerDelegate, FlatCacheListener {
ForegroundHandlerDelegate,
FlatCacheListener,
BaseListViewController2EmptyDataSource {

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

self.dataSource = self
self.emptyDataSource = self
self.foreground.delegate = self

switch inboxType {
Expand Down Expand Up @@ -268,6 +271,15 @@ ForegroundHandlerDelegate, FlatCacheListener {
}
}

// MARK: BaseListViewController2EmptyDataSource

func emptyModel(for adapter: ListSwiftAdapter) -> ListSwiftPair {
let layoutInsets = view.safeAreaInsets
return ListSwiftPair.pair("empty-notification-value", {
return NoNewNotificationSectionController(layoutInsets: layoutInsets)
})
}

// MARK: ForegroundHandlerDelegate

func didForeground(handler: ForegroundHandler) {
Expand Down
13 changes: 12 additions & 1 deletion Classes/View Controllers/BaseListViewController2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ protocol BaseListViewController2DataSource: class {
func models(adapter: ListSwiftAdapter) -> [ListSwiftPair]
}

protocol BaseListViewController2EmptyDataSource: class {
func emptyModel(for adapter: ListSwiftAdapter) -> ListSwiftPair
}

class BaseListViewController2<PageType: CustomStringConvertible>: UIViewController,
ListSwiftAdapterDataSource,
FeedDelegate,
LoadMoreSectionController2Delegate {

private let emptyErrorMessage: String

public weak var dataSource: BaseListViewController2DataSource?
public weak var emptyDataSource: BaseListViewController2EmptyDataSource?

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

init(emptyErrorMessage: String) {
self.emptyErrorMessage = emptyErrorMessage
Expand Down Expand Up @@ -118,6 +123,12 @@ LoadMoreSectionController2Delegate {
return []
}

if let emptyDataSource = self.emptyDataSource,
hasNoObjects,
feed.status == .idle {
return [emptyDataSource.emptyModel(for: adapter)]
}

if let page = self.page?.description {
let pagePair = ListSwiftPair.pair(page) { [weak self] in
let controller = LoadMoreSectionController2()
Expand Down

0 comments on commit 8452d73

Please sign in to comment.