Skip to content

Commit

Permalink
feat(ios): add keep-alive setting prop to list item (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Oct 15, 2024
1 parent 53c8a09 commit 92ea412
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,4 @@

@interface HippyNextBaseListItemView : HippyView <ViewAppearStateProtocol>

@property (nonatomic, strong) id type;
@property (nonatomic, assign) BOOL isSticky;


@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,27 @@
#import "HippyNextBaseListItemViewManager.h"
#import "HippyNextBaseListItemView.h"
#import "HippyShadowWaterfallItem.h"
#import "HippyNextShadowListItem.h"


@implementation HippyNextBaseListItemViewManager

HIPPY_EXPORT_MODULE(ListViewItem)

HIPPY_EXPORT_VIEW_PROPERTY(type, id)
HIPPY_EXPORT_VIEW_PROPERTY(isSticky, BOOL)
HIPPY_EXPORT_VIEW_PROPERTY(onAppear, HippyDirectEventBlock)
HIPPY_EXPORT_VIEW_PROPERTY(onDisappear, HippyDirectEventBlock)
HIPPY_EXPORT_VIEW_PROPERTY(onWillAppear, HippyDirectEventBlock)
HIPPY_EXPORT_VIEW_PROPERTY(onWillDisappear, HippyDirectEventBlock)
HIPPY_EXPORT_SHADOW_PROPERTY(sticky, BOOL)
HIPPY_EXPORT_SHADOW_PROPERTY(keepAlive, BOOL)

- (UIView *)view {
return [[HippyNextBaseListItemView alloc] init];
}

- (HippyShadowView *)shadowView {
return [[HippyShadowWaterfallItem alloc] init];
return [[HippyNextShadowListItem alloc] init];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#import "UIView+Hippy.h"
#import "UIView+Render.h"
#import "HippyShadowListView.h"
#import "HippyNextShadowListItem.h"

static NSString *const kCellIdentifier = @"HippyListCellIdentifier";
static NSString *const kSupplementaryIdentifier = @"HippySupplementaryIdentifier";
Expand All @@ -41,6 +42,7 @@
@interface HippyNextBaseListView () <HippyRefreshDelegate> {
BOOL _isInitialListReady;
NSArray<UICollectionViewCell *> *_previousVisibleCells;
NSMutableArray<UIView *> *_keepAliveCellViews; // cellViews that marked keep-Alive
}

@end
Expand Down Expand Up @@ -265,17 +267,28 @@ - (void)collectionView:(UICollectionView *)collectionView

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HippyNextBaseListViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
HippyShadowView *shadowView = [self.dataSource cellForIndexPath:indexPath];
HippyNextShadowListItem *shadowView = (HippyNextShadowListItem *)[self.dataSource cellForIndexPath:indexPath];

UIView *cellView = nil;
UIView *cachedVisibleCellView = [_cachedWeakCellViews objectForKey:shadowView.hippyTag];
if (cachedVisibleCellView) {
cellView = cachedVisibleCellView;
// Remove keep-Alive cellView if needed
if ([_keepAliveCellViews containsObject:cachedVisibleCellView] && shadowView.keepAlive == NO) {
[_keepAliveCellViews removeObject:cachedVisibleCellView];
}
HippyLogTrace(@"%@ 🟢 use cached visible cellView at {%ld - %ld} for %@",
self.hippyTag, indexPath.section, indexPath.row, shadowView.hippyTag);
} else {
cellView = [self.uiManager createViewForShadowListItem:shadowView];
[_cachedWeakCellViews setObject:cellView forKey:shadowView.hippyTag];
// Add keep-Alive cellView to cache if needed
if (shadowView.keepAlive) {
if (!_keepAliveCellViews) {
_keepAliveCellViews = [NSMutableArray array];
}
[_keepAliveCellViews addObject:cellView];
}
HippyLogTrace(@"%@ 🟡 create cellView at {%ld - %ld} for %@",
self.hippyTag, indexPath.section, indexPath.row, shadowView.hippyTag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
#import "HippyNextBaseListViewDataSource.h"
#import "HippyShadowView.h"
#import "HippyShadowListView.h"
#import "HippyNextShadowListItem.h"


static NSString * const kStickyCellPropKey = @"sticky";

@interface HippyNextBaseListViewDataSource () {
NSMutableArray *_shadowHeaderViews;
}
Expand All @@ -42,9 +41,8 @@ - (void)setDataSource:(NSArray<HippyShadowView *> *)dataSource containBannerView
NSMutableArray<HippyShadowView *> *shadowSectionCell = nil;
BOOL isFirstIndex = YES;
for (HippyShadowView *shadowView in dataSource) {
if ([self.itemViewName isEqualToString:shadowView.viewName]) {
NSNumber *sticky = shadowView.props[kStickyCellPropKey];
if ([sticky boolValue]) {
if ([shadowView isKindOfClass:HippyNextShadowListItem.class]) {
if (((HippyNextShadowListItem *)shadowView).isSticky) {
[shadowHeaders addObject:shadowView];
if (shadowSectionCell) {
[shadowCells addObject:shadowSectionCell];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#import "HippyShadowWaterfallItem.h"

NS_ASSUME_NONNULL_BEGIN

/// ListView item's shadow view
@interface HippyNextShadowListItem : HippyShadowWaterfallItem

/// Whether item is stick to the top
@property (nonatomic, assign, getter=isSticky) BOOL sticky;

/// Memory management property that sets item to exist for the lifetime of listView
@property (nonatomic, assign) BOOL keepAlive;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "HippyNextShadowListItem.h"

@implementation HippyNextShadowListItem

@end

0 comments on commit 92ea412

Please sign in to comment.