1
This commit is contained in:
@@ -151,19 +151,67 @@ static NSString * const kMySkinCellId = @"kMySkinCellId";
|
||||
|
||||
- (void)onDelete {
|
||||
// 根据选中项删除
|
||||
NSArray<NSIndexPath *> *selected = [[self.collectionView indexPathsForSelectedItems] sortedArrayUsingSelector:@selector(compare:)];
|
||||
if (selected.count == 0) return;
|
||||
NSArray<NSIndexPath *> *selectedIndexPaths = [[self.collectionView indexPathsForSelectedItems] sortedArrayUsingSelector:@selector(compare:)];
|
||||
if (selectedIndexPaths.count == 0) return;
|
||||
|
||||
// 批量更新,先改数据,再删 UI
|
||||
[self.collectionView performBatchUpdates:^{
|
||||
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
|
||||
for (NSIndexPath *ip in selected) { [set addIndex:ip.item]; }
|
||||
[self.data removeObjectsAtIndexes:set];
|
||||
[self.collectionView deleteItemsAtIndexPaths:selected];
|
||||
} completion:^(BOOL finished) {
|
||||
[self updateBottomUI];
|
||||
// 根据当前数据源显隐空视图(删除到 0 条时应显示空态)
|
||||
[self.collectionView kb_endLoadingForEmpty];
|
||||
NSMutableArray<NSNumber *> *themeIds = [NSMutableArray arrayWithCapacity:selectedIndexPaths.count];
|
||||
for (NSIndexPath *ip in selectedIndexPaths) {
|
||||
if (ip.item >= self.data.count) { continue; }
|
||||
KBMyTheme *theme = self.data[ip.item];
|
||||
id themeIdValue = theme.themeId;
|
||||
NSNumber *numberId = nil;
|
||||
if ([themeIdValue isKindOfClass:[NSNumber class]]) {
|
||||
numberId = (NSNumber *)themeIdValue;
|
||||
} else if ([themeIdValue isKindOfClass:[NSString class]]) {
|
||||
NSString *idString = (NSString *)themeIdValue;
|
||||
if (idString.length > 0) {
|
||||
static NSCharacterSet *nonDigitSet;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
nonDigitSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
|
||||
});
|
||||
if ([idString rangeOfCharacterFromSet:nonDigitSet].location == NSNotFound) {
|
||||
numberId = @([idString longLongValue]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (numberId) {
|
||||
[themeIds addObject:numberId];
|
||||
}
|
||||
}
|
||||
|
||||
if (themeIds.count == 0) {
|
||||
[KBHUD showInfo:KBLocalized(@"Invalid parameter")];
|
||||
return;
|
||||
}
|
||||
|
||||
[KBHUD show];
|
||||
KBWeakSelf
|
||||
[self.viewModel deletePurchasedThemesWithThemeIds:themeIds
|
||||
completion:^(BOOL success, NSError * _Nullable error) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[KBHUD dismiss];
|
||||
if (!success) {
|
||||
NSString *msg = error.localizedDescription ?: KBLocalized(@"Network error");
|
||||
[KBHUD showInfo:msg];
|
||||
return;
|
||||
}
|
||||
// 批量更新,先改数据,再删 UI
|
||||
[weakSelf.collectionView performBatchUpdates:^{
|
||||
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
|
||||
for (NSIndexPath *ip in selectedIndexPaths) {
|
||||
if (ip.item < weakSelf.data.count) {
|
||||
[set addIndex:ip.item];
|
||||
}
|
||||
}
|
||||
[weakSelf.data removeObjectsAtIndexes:set];
|
||||
[weakSelf.collectionView deleteItemsAtIndexPaths:selectedIndexPaths];
|
||||
} completion:^(BOOL finished) {
|
||||
[weakSelf updateBottomUI];
|
||||
// 根据当前数据源显隐空视图(删除到 0 条时应显示空态)
|
||||
[weakSelf.collectionView kb_endLoadingForEmpty];
|
||||
}];
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user