diff --git a/Classes/Notifications/NoNewNotificationsSectionController.swift b/Classes/Notifications/NoNewNotificationsSectionController.swift index b98a47dba..ed274b171 100644 --- a/Classes/Notifications/NoNewNotificationsSectionController.swift +++ b/Classes/Notifications/NoNewNotificationsSectionController.swift @@ -10,9 +10,8 @@ import UIKit import IGListKit import Crashlytics -final class NoNewNotificationSectionController: ListSectionController { +final class NoNewNotificationSectionController: ListSwiftSectionController { - private let topInset: CGFloat private let layoutInsets: UIEdgeInsets private let client = NotificationEmptyMessageClient() @@ -23,8 +22,7 @@ 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 @@ -32,17 +30,23 @@ final class NoNewNotificationSectionController: ListSectionController { } } - 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 diff --git a/Classes/Notifications/NotificationsViewController.swift b/Classes/Notifications/NotificationsViewController.swift index 2b8788b6f..ce41c8679 100644 --- a/Classes/Notifications/NotificationsViewController.swift +++ b/Classes/Notifications/NotificationsViewController.swift @@ -12,7 +12,9 @@ import FlatCache final class NotificationsViewController: BaseListViewController2, BaseListViewController2DataSource, -ForegroundHandlerDelegate, FlatCacheListener { +ForegroundHandlerDelegate, +FlatCacheListener, +BaseListViewController2EmptyDataSource { private let modelController: NotificationModelController private let foreground = ForegroundHandler(threshold: 5 * 60) @@ -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 { @@ -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) { diff --git a/Classes/View Controllers/BaseListViewController2.swift b/Classes/View Controllers/BaseListViewController2.swift index ad9de6624..78122c4f1 100644 --- a/Classes/View Controllers/BaseListViewController2.swift +++ b/Classes/View Controllers/BaseListViewController2.swift @@ -13,18 +13,23 @@ protocol BaseListViewController2DataSource: class { func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] } +protocol BaseListViewController2EmptyDataSource: class { + func emptyModel(for adapter: ListSwiftAdapter) -> ListSwiftPair +} + class BaseListViewController2: 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 @@ -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()