@@ -17,39 +17,26 @@ class BlogDashboardViewModel {
1717 private let quickLinks = [ " Quick Links " ]
1818 private let posts = [ " Posts " ]
1919
20- typealias DataSource = UICollectionViewDiffableDataSource < Section , String >
21- typealias Snapshot = NSDiffableDataSourceSnapshot < Section , String >
2220 typealias QuickLinksHostCell = HostCollectionViewCell < QuickLinksView >
2321
2422 private let managedObjectContext : NSManagedObjectContext
2523 private let blog : Blog
2624
27- private lazy var service : DashboardServiceRemote = {
28- let api = WordPressComRestApi . defaultApi ( in: managedObjectContext,
29- localeKey: WordPressComRestApi . LocaleKeyV2)
30-
31- return DashboardServiceRemote ( wordPressComRestApi: api)
25+ private lazy var service : BlogDashboardService = {
26+ return BlogDashboardService ( managedObjectContext: managedObjectContext)
3227 } ( )
3328
34- private lazy var dataSource : DataSource ? = {
29+ private lazy var dataSource : DashboardDataSource ? = {
3530 guard let viewController = viewController else {
3631 return nil
3732 }
3833
39- return DataSource ( collectionView: viewController. collectionView) { [ unowned self] collectionView, indexPath, identifier in
40- switch identifier {
41- case self . quickLinks. first:
42- let cell = collectionView. dequeueReusableCell ( withReuseIdentifier: QuickLinksHostCell . defaultReuseID, for: indexPath) as? QuickLinksHostCell
43- cell? . hostedView = QuickLinksView ( title: self . quickLinks [ indexPath. item] )
44- return cell
45- case self . posts. first:
46- let cell = collectionView. dequeueReusableCell ( withReuseIdentifier: DashboardPostsCardCell . defaultReuseID, for: indexPath) as? DashboardPostsCardCell
47- cell? . configure ( viewController, blog: blog)
48- return cell
49- default :
50- break
51- }
52- return UICollectionViewCell ( )
34+ return DashboardDataSource ( collectionView: viewController. collectionView) { [ unowned self] collectionView, indexPath, identifier in
35+
36+ let cellType = identifier. id. cell
37+ let cell = collectionView. dequeueReusableCell ( withReuseIdentifier: cellType. defaultReuseID, for: indexPath)
38+ return cell
39+
5340 }
5441 } ( )
5542
@@ -68,15 +55,9 @@ class BlogDashboardViewModel {
6855 viewController? . showLoading ( )
6956 applySnapshotForInitialData ( )
7057
71- let cardsToFetch : [ String ] = DashboardCard . allCases
72- . filter { $0. isRemote }
73- . map { $0. rawValue }
74-
75- service. fetch ( cards: cardsToFetch, forBlogID: dotComID, success: { [ weak self] _ in
58+ service. fetch ( wpComID: dotComID, completion: { [ weak self] snapshot in
7659 self ? . viewController? . stopLoading ( )
77- self ? . applySnapshotWithMockedData ( )
78- } , failure: { _ in
79-
60+ self ? . apply ( snapshot: snapshot)
8061 } )
8162 }
8263}
@@ -87,15 +68,11 @@ private extension BlogDashboardViewModel {
8768 // This is necessary when using an IntrinsicCollectionView
8869 // Otherwise, the collection view will never update its height
8970 func applySnapshotForInitialData( ) {
90- let snapshot = Snapshot ( )
71+ let snapshot = DashboardSnapshot ( )
9172 dataSource? . apply ( snapshot, animatingDifferences: false )
9273 }
9374
94- func applySnapshotWithMockedData( ) {
95- var snapshot = Snapshot ( )
96- snapshot. appendSections ( Section . allCases)
97- snapshot. appendItems ( quickLinks, toSection: Section . quickLinks)
98- snapshot. appendItems ( posts, toSection: Section . posts)
75+ func apply( snapshot: DashboardSnapshot ) {
9976 dataSource? . apply ( snapshot, animatingDifferences: false )
10077 }
10178}
0 commit comments