IGListKit框架解析(一)

IGListKit框架解析(一)

Instagram在2016年年底發(fā)布了基于數(shù)據(jù)驅(qū)動的UICollectionView框架IGListKit。使用數(shù)據(jù)驅(qū)動去創(chuàng)造更為快速靈活的列表控件。以下是該框架的特點:

  • 數(shù)據(jù)驅(qū)動(數(shù)據(jù)改變 -> Diff算法 -> update界面)
  • 可重復(fù)單元和組件的更好體系結(jié)構(gòu)
  • 解耦的差異算法
  • 可以為數(shù)據(jù)模型自定義差異算法
  • 可擴(kuò)展的API

IGListKit架構(gòu)圖

3637572-bd1b004bc054287f.png

IGListKit的管理者--IGListAdapter

如上圖所示,該框架直接通過IGListAdapter對象去管理UICollectionView并封裝了UICollectionViewDelegate、UICollectionViewDataSource的相關(guān)實現(xiàn),同時Adapter和Controller互相持有,用戶只需要實現(xiàn)IGListAdapterDataSource就可以實現(xiàn)一個靈活的列表。

IGListAdapter的三個參數(shù):

  • IGListAdapterUpdater : 是一個實現(xiàn)了IGListUpdatingDelegate協(xié)議的對象,負(fù)責(zé)處理row和section的刷新。
  • Controller : Adapter和Controller互相持有的同時,也負(fù)責(zé)管理著SectionController。
  • WorkingRange : 可以為不可見范圍的section準(zhǔn)備內(nèi)容。

IGListAdapter是如何管理UICollectionView的呢?

我們在和IGListAdapter中尋找和UICollectionViewDelegate、UICollectionViewDataSource相關(guān)的方法,發(fā)現(xiàn)在IGListAdapter+UICollectionView中有相關(guān)實現(xiàn),以下是代碼:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sectionMap.objects.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    IGListSectionController * sectionController = [self sectionControllerForSection:section];
    IGAssert(sectionController != nil, @"Nil section controller for section %zi for item %@. Check your -diffIdentifier and -isEqual: implementations.",
            section, [self.sectionMap objectForSection:section]);
    const NSInteger numberOfItems = [sectionController numberOfItems];
    IGAssert(numberOfItems >= 0, @"Cannot return negative number of items %zi for section controller %@.", numberOfItems, sectionController);
    return numberOfItems;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    IGListSectionController *sectionController = [self sectionControllerForSection:indexPath.section];

    // flag that a cell is being dequeued in case it tries to access a cell in the process
    _isDequeuingCell = YES;
    UICollectionViewCell *cell = [sectionController cellForItemAtIndex:indexPath.item];
    _isDequeuingCell = NO;

    IGAssert(cell != nil, @"Returned a nil cell at indexPath <%@> from section controller: <%@>", indexPath, sectionController);

    // associate the section controller with the cell so that we know which section controller is using it
    [self mapView:cell toSectionController:sectionController];

    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    IGListSectionController *sectionController = [self sectionControllerForSection:indexPath.section];
    id <IGListSupplementaryViewSource> supplementarySource = [sectionController supplementaryViewSource];
    UICollectionReusableView *view = [supplementarySource viewForSupplementaryElementOfKind:kind atIndex:indexPath.item];
    IGAssert(view != nil, @"Returned a nil supplementary view at indexPath <%@> from section controller: <%@>, supplementary source: <%@>", indexPath, sectionController, supplementarySource);

    // associate the section controller with the cell so that we know which section controller is using it
    [self mapView:view toSectionController:sectionController];

    return view;
}

通過這段代碼,我們可以清晰地看到IGListAdapter通過管理著IGListSectionController去實現(xiàn)UICollectionViewDelegate和UICollectionViewDataSource的相關(guān)實現(xiàn),現(xiàn)在,我們?nèi)タ纯磶讉€關(guān)鍵性的方法的實現(xiàn)去探索IGListAdapter的結(jié)構(gòu)。

- (nullable IGListSectionController *)sectionControllerForSection:(NSInteger)section {
    IGAssertMainThread();
    
    return [self.sectionMap sectionControllerForSection:section];
}

- (NSInteger)sectionForSectionController:(IGListSectionController *)sectionController {
    IGAssertMainThread();
    IGParameterAssert(sectionController != nil);

    return [self.sectionMap sectionForSectionController:sectionController];
}

- (IGListSectionController *)sectionControllerForObject:(id)object {
    IGAssertMainThread();
    IGParameterAssert(object != nil);

    return [self.sectionMap sectionControllerForObject:object];
}

- (id)objectForSectionController:(IGListSectionController *)sectionController {
    IGAssertMainThread();
    IGParameterAssert(sectionController != nil);

    const NSInteger section = [self.sectionMap sectionForSectionController:sectionController];
    return [self.sectionMap objectForSection:section];
}

上述幾個方法,都是IGListAdapter+UICollectionView中實現(xiàn)UICollectionViewDelegate、UICollectionViewDataSource時經(jīng)常使用的方法,可以清晰地看到sectionMap是這些方法共同使用的屬性,sectionMap是IGListKit中自定義的Map類型(IGListSectionMap),通過IGListSectionMap的代碼,來一窺IGListAdapter實現(xiàn)管理者的方式。

@interface IGListSectionMap : NSObject <NSCopying>

- (instancetype)initWithMapTable:(NSMapTable *)mapTable NS_DESIGNATED_INITIALIZER;

/**
 The objects stored in the map.
 */
@property (nonatomic, strong, readonly) NSArray *objects;

/**
 Update the map with objects and the section controller counterparts.

 @param objects The objects in the collection.
 @param sectionControllers The section controllers that map to each object.
 */
- (void)updateWithObjects:(NSArray <id <NSObject>> *)objects sectionControllers:(NSArray <IGListSectionController *> *)sectionControllers;

/**
 Fetch a section controller given a section.

 @param section The section index of the section controller.

 @return A section controller.
 */
- (nullable IGListSectionController *)sectionControllerForSection:(NSInteger)section;

/**
 Fetch the object for a section

 @param section The section index of the object.

 @return The object corresponding to the section.
 */
- (nullable id)objectForSection:(NSInteger)section;

/**
 Fetch a section controller given an object. Can return nil.

 @param object The object that maps to a section controller.

 @return A section controller.
 */
- (nullable id)sectionControllerForObject:(id)object;

/**
 Look up the section index for a section controller.

 @param sectionController The list to look up.

 @return The section index of the given section controller if it exists, NSNotFound otherwise.
 */
- (NSInteger)sectionForSectionController:(id)sectionController;

/**
 Look up the section index for an object.

 @param object The object to look up.

 @return The section index of the given object if it exists, NSNotFound otherwise.
 */
- (NSInteger)sectionForObject:(id)object;

這是IGListSectionMap中主要的幾個實現(xiàn),也是經(jīng)常用到的幾個,我們再看看IGListSectionMap中的變量,很簡單。

@interface IGListSectionMap ()

// both of these maps allow fast lookups of objects, list objects, and indexes
@property (nonatomic, strong, readonly, nonnull) NSMapTable<id, IGListSectionController *> *objectToSectionControllerMap;
@property (nonatomic, strong, readonly, nonnull) NSMapTable<IGListSectionController *, NSNumber *> *sectionControllerToSectionMap;

@property (nonatomic, strong, nonnull) NSMutableArray *mObjects;

@end

通過這兩個變量的名稱以及注釋,也可以判斷出該結(jié)構(gòu)支持“雙向查找”。再看看更新方法,確認(rèn)一下是不是這么回事。

- (void)updateWithObjects:(NSArray *)objects sectionControllers:(NSArray *)sectionControllers {
    IGParameterAssert(objects.count == sectionControllers.count);

    [self reset];

    self.mObjects = [objects mutableCopy];

    id firstObject = objects.firstObject;
    id lastObject = objects.lastObject;

    [objects enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
        IGListSectionController *sectionController = sectionControllers[idx];

        // set the index of the list for easy reverse lookup
        [self.sectionControllerToSectionMap setObject:@(idx) forKey:sectionController];
        [self.objectToSectionControllerMap setObject:sectionController forKey:object];

        sectionController.isFirstSection = (object == firstObject);
        sectionController.isLastSection = (object == lastObject);
        sectionController.section = (NSInteger)idx;
    }];
}

由此可見,該結(jié)構(gòu)是很靈活的,在IGListSectionMap結(jié)構(gòu)的輔助下,IGListAdapter可以控制UICollectionView下層的IGListSectionController,通過IGListSectionController也通過IGListBindingSectionControllerDataSource去管理著UICollectionViewCell、ViewModel以及Cell的展示。

下一期,具體講一下IGlistKit中運(yùn)用的技巧、IGListBindingSectionController以及IGListKit的“靈魂”IGListDiff算法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容