Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): add keep-alive setting prop to list item (experimental) #4066

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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";
@@ -41,6 +42,7 @@
@interface HippyNextBaseListView () <HippyRefreshDelegate> {
BOOL _isInitialListReady;
NSArray<UICollectionViewCell *> *_previousVisibleCells;
NSMutableArray<UIView *> *_keepAliveCellViews; // cellViews that marked keep-Alive
}

@end
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
@@ -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];
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
Loading