You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by cx...@apache.org on 2017/03/07 07:55:49 UTC

[3/8] incubator-weex git commit: * [ios] support loadmore event in recycler

* [ios] support loadmore event in recycler


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/95873da2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/95873da2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/95873da2

Branch: refs/heads/0.11-dev
Commit: 95873da2ea520d4b491131b81883e637e701aec8
Parents: 9c6a4a4
Author: \u9690\u98ce <cx...@apache.org>
Authored: Tue Mar 7 11:21:40 2017 +0800
Committer: \u9690\u98ce <cx...@apache.org>
Committed: Tue Mar 7 11:21:40 2017 +0800

----------------------------------------------------------------------
 examples/vue/components/waterfall.vue           |  8 ++-
 .../Component/Recycler/WXRecyclerComponent.m    | 61 +++++++++++++++++---
 .../Recycler/WXRecyclerUpdateController.h       |  2 +
 .../Recycler/WXRecyclerUpdateController.m       | 10 +++-
 4 files changed, 69 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/95873da2/examples/vue/components/waterfall.vue
----------------------------------------------------------------------
diff --git a/examples/vue/components/waterfall.vue b/examples/vue/components/waterfall.vue
index 7bf42f9..0fa64a4 100644
--- a/examples/vue/components/waterfall.vue
+++ b/examples/vue/components/waterfall.vue
@@ -1,9 +1,9 @@
 <template>
-  <waterfall class="page"
+  <waterfall class="page" ref="waterfall"
   v-bind:style="{padding:padding}"
   :column-width="columnWidth" :column-count="columnCount" :column-gap="columnGap"
   :show-scrollbar="showScrollbar" :scrollable="scrollable"
-  @scroll="recylerScroll"
+  @scroll="recylerScroll" @loadmore="loadmore" loadmoreoffset=3000
   >
     <refresh class="refresh" @refresh="onrefresh" @pullingdown="onpullingdown" :display="refreshing ? 'show' : 'hide'">
       <loading-indicator class="indicator"></loading-indicator>
@@ -326,6 +326,10 @@
       recylerScroll: function(e) {
         this.contentOffset = e.contentOffset.y
       },
+      loadmore: function(e) {
+        console.log('receive loadmore event')
+        // this.$refs.waterfall.resetLoadmore()
+      },
       showOrRemoveHeader: function() {
         this.showHeader = !this.showHeader
       },

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/95873da2/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
index 7dffe8c..d78cd57 100644
--- a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerComponent.m
@@ -79,6 +79,7 @@ typedef enum : NSUInteger {
     UICollectionViewLayout *_collectionViewlayout;
     
     UIEdgeInsets _padding;
+    NSUInteger _previousLoadMoreCellNumber;
 }
 
 - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
@@ -228,7 +229,6 @@ typedef enum : NSUInteger {
 
 - (void)_insertSubcomponent:(WXComponent *)subcomponent atIndex:(NSInteger)index
 {
-    // TODO: refresh loading fixed
     if ([subcomponent isKindOfClass:[WXCellComponent class]]) {
         ((WXCellComponent *)subcomponent).delegate = self;
     } else if ([subcomponent isKindOfClass:[WXHeaderComponent class]]) {
@@ -273,6 +273,11 @@ typedef enum : NSUInteger {
     
 }
 
+- (void)updateController:(WXRecyclerUpdateController *)controller willCrashWithException:(NSException *)exception oldData:(NSArray<WXSectionDataController *> *)oldData newData:(NSArray<WXSectionDataController *> *)newData
+{
+    WXLogError(@"recycler update did occur an exception:%@, oldData:%@, newData:%@", exception, oldData, newData);
+}
+
 #pragma mark - UICollectionViewDataSource
 
 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
@@ -449,6 +454,46 @@ typedef enum : NSUInteger {
     }
 }
 
+#pragma mark - Load More Event
+
+- (void)setLoadmoreretry:(NSUInteger)loadmoreretry
+{
+    if (loadmoreretry != self.loadmoreretry) {
+        _previousLoadMoreCellNumber = 0;
+    }
+    
+    [super setLoadmoreretry:loadmoreretry];
+}
+
+- (void)loadMore
+{
+    [super loadMore];
+    
+    _previousLoadMoreCellNumber = [self totalNumberOfCells];
+}
+
+- (BOOL)isNeedLoadMore
+{
+    BOOL superNeedLoadMore = [super isNeedLoadMore];
+    return superNeedLoadMore && _previousLoadMoreCellNumber != [self totalNumberOfCells];
+}
+
+- (NSUInteger)totalNumberOfCells
+{
+    NSUInteger cellNumber = 0;
+    NSUInteger sectionCount = [_collectionView numberOfSections];
+    for (int section = 0; section < sectionCount; section ++) {
+        cellNumber += [_collectionView numberOfItemsInSection:section];
+    }
+    
+    return cellNumber;
+}
+
+- (void)resetLoadmore{
+    [super resetLoadmore];
+    _previousLoadMoreCellNumber = 0;
+}
+
 #pragma makrk - private
 
 - (float)_floatValueForColumnGap:(WXLength *)gap
@@ -499,7 +544,7 @@ typedef enum : NSUInteger {
         WXComponent* component = components[i];
         
         if ([component isKindOfClass:[WXHeaderComponent class]]) {
-            if (i != 0) {
+            if (i != 0 && (currentSection.headerComponent || currentSection.cellComponents.count > 0)) {
                 currentSection.cellComponents = [cellArray copy];
                 [sectionArray addObject:currentSection];
                 currentSection = [WXSectionDataController new];
@@ -511,12 +556,14 @@ typedef enum : NSUInteger {
             [cellArray addObject:(WXCellComponent *)component];
         } else if ([component isKindOfClass:[WXFooterComponent class]]) {
             currentSection.footerComponent = component;
+        } else {
+            continue;
         }
-        
-        if (i == components.count - 1 && cellArray.count > 0) {
-            currentSection.cellComponents = [cellArray copy];
-            [sectionArray addObject:currentSection];
-        }
+    }
+    
+    if (cellArray.count > 0) {
+        currentSection.cellComponents = [cellArray copy];
+        [sectionArray addObject:currentSection];
     }
     
     return sectionArray;

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/95873da2/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.h b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.h
index e376c19..41e6e08 100644
--- a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.h
+++ b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.h
@@ -16,6 +16,8 @@
 
 - (void)updateController:(WXRecyclerUpdateController *)controller didPerformUpdateWithFinished:(BOOL)finished;
 
+- (void)updateController:(WXRecyclerUpdateController *)controller willCrashWithException:(NSException *)exception oldData:(NSArray<WXSectionDataController *> *)oldData newData:(NSArray<WXSectionDataController *> *)newData;
+
 @end
 
 @interface WXRecyclerUpdateController : NSObject

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/95873da2/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.m b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.m
index 9fb0f59..98d9ec5 100644
--- a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.m
+++ b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXRecyclerUpdateController.m
@@ -132,6 +132,7 @@
     }
     
     void (^updates)() = [^{
+        [self.delegate updateController:self willPerformUpdateWithNewData:newData];
         [UIView setAnimationsEnabled:NO];
         WXLogDebug(@"UICollectionView update:%@", diffResult);
         [self applyUpdate:diffResult toCollectionView:self.collectionView];
@@ -151,10 +152,13 @@
         return;
     }
     
-    [self.delegate updateController:self willPerformUpdateWithNewData:newData];
-    
     WXLogDebug(@"Diff result:%@", diffResult);
-    [collectionView performBatchUpdates:updates completion:completion];
+    @try {
+        [collectionView performBatchUpdates:updates completion:completion];
+    } @catch (NSException *exception) {
+        [self.delegate updateController:self willCrashWithException:exception oldData:oldData newData:newData];
+        @throw exception;
+    }
 }
 
 - (void)cleanup