Files
keyboard/keyBoard/Class/Me/VC/MySkinVC.m

290 lines
12 KiB
Mathematica
Raw Normal View History

2025-11-08 21:44:41 +08:00
//
// MySkinVC.m
// keyBoard
//
#import "MySkinVC.h"
#import <Masonry/Masonry.h>
#import "UIColor+Extension.h"
2025-11-09 21:41:35 +08:00
#import <MJRefresh/MJRefresh.h>
#import "UIScrollView+KBEmptyView.h" // LYEmptyView
2025-11-08 21:44:41 +08:00
#import "MySkinCell.h"
static NSString * const kMySkinCellId = @"kMySkinCellId";
@interface MySkinVC () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView; //
@property (nonatomic, strong) UIView *bottomView; //
@property (nonatomic, strong) UILabel *selectedLabel; //
@property (nonatomic, strong) UIButton *deleteButton; //
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *data; //
2025-11-09 21:41:35 +08:00
@property (nonatomic, assign) NSInteger loadCount; // /
2025-11-08 21:44:41 +08:00
@property (nonatomic, assign, getter=isEditingMode) BOOL editingMode; //
@end
@implementation MySkinVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"My Skin"; //
// Editor/Cancel
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Editor" style:UIBarButtonItemStylePlain target:self action:@selector(onToggleEdit)];
2025-11-09 21:41:35 +08:00
// +
self.data = [NSMutableArray array];
2025-11-08 21:44:41 +08:00
//
[self.view addSubview:self.collectionView];
[self.view addSubview:self.bottomView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view.mas_bottom); // bottomView
}];
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
make.height.mas_equalTo(64);
}];
2025-11-09 21:41:35 +08:00
// LYEmptyView +
__weak typeof(self) weakSelf = self;
[self.collectionView kb_makeDefaultEmptyViewWithImage:nil
title:@"暂无皮肤"
detail:@"下拉刷新试试"
buttonTitle:@"重试"
tapHandler:nil
buttonHandler:^{ [weakSelf.collectionView.mj_header beginRefreshing]; }];
[self.collectionView kb_setLYAutoShowEnabled:NO]; //
//
[self.collectionView kb_endLoadingForEmpty];
// +
self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(fetchData)];
//
[self.collectionView.mj_header beginRefreshing];
2025-11-08 21:44:41 +08:00
//
self.bottomView.hidden = YES;
}
2025-11-09 21:41:35 +08:00
#pragma mark - Data
- (void)fetchData {
// 1.0s
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.loadCount += 1;
// /
[self.data removeAllObjects];
if (self.loadCount % 2 == 0) {
for (int i = 0; i < 8; i++) {
[self.data addObject:@{ @"title": @"Dopamine" }];
}
}
[self.collectionView reloadData];
[self.collectionView kb_endLoadingForEmpty]; // / emptyView
[self.collectionView.mj_header endRefreshing];
});
}
2025-11-08 21:44:41 +08:00
#pragma mark - Actions
- (void)onToggleEdit {
self.editingMode = !self.editingMode;
//
self.navigationItem.rightBarButtonItem.title = self.isEditingMode ? @"Cancel" : @"Editor";
//
self.bottomView.hidden = !self.isEditingMode;
// /退
self.collectionView.allowsMultipleSelection = self.isEditingMode;
// cell/ markView
for (UICollectionViewCell *c in self.collectionView.visibleCells) {
if (![c isKindOfClass:MySkinCell.class]) continue;
MySkinCell *cell = (MySkinCell *)c;
cell.editing = self.isEditingMode;
NSIndexPath *ip = [self.collectionView indexPathForCell:cell];
BOOL selected = [[self.collectionView indexPathsForSelectedItems] containsObject:ip];
[cell updateSelected:selected];
}
// reload offscreen cell
[self.collectionView reloadData];
//
for (NSIndexPath *ip in [self.collectionView indexPathsForSelectedItems]) {
[self.collectionView deselectItemAtIndexPath:ip animated:NO];
}
[self updateBottomUI];
}
- (void)onDelete {
//
NSArray<NSIndexPath *> *selected = [[self.collectionView indexPathsForSelectedItems] sortedArrayUsingSelector:@selector(compare:)];
if (selected.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];
2025-11-09 21:41:35 +08:00
// 0
[self.collectionView kb_endLoadingForEmpty];
2025-11-08 21:44:41 +08:00
}];
}
- (void)updateBottomUI {
NSInteger count = self.collectionView.indexPathsForSelectedItems.count;
self.selectedLabel.text = [NSString stringWithFormat:@"Selected: %ld Skins", (long)count];
BOOL enable = count > 0;
self.deleteButton.enabled = enable;
if (enable) {
self.deleteButton.backgroundColor = [UIColor colorWithHex:0x02BEAC];
[self.deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.deleteButton.layer.borderColor = [UIColor colorWithHex:0x02BEAC].CGColor;
} else {
self.deleteButton.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
[self.deleteButton setTitleColor:[UIColor colorWithHex:0xC8C8C8] forState:UIControlStateNormal];
self.deleteButton.layer.borderColor = [UIColor colorWithHex:0xE6E6E6].CGColor;
}
}
#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MySkinCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMySkinCellId forIndexPath:indexPath];
NSDictionary *d = self.data[indexPath.item];
[cell configWithTitle:d[@"title"] image:nil];
cell.editing = self.isEditingMode; //
//
BOOL selected = [[collectionView indexPathsForSelectedItems] containsObject:indexPath];
[cell updateSelected:selected];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (!self.isEditingMode) {
//
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
return;
}
MySkinCell *cell = (MySkinCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell updateSelected:YES];
[self updateBottomUI];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
if (!self.isEditingMode) return;
MySkinCell *cell = (MySkinCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell updateSelected:NO];
[self updateBottomUI];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (![cell isKindOfClass:MySkinCell.class]) return;
MySkinCell *c = (MySkinCell *)cell;
c.editing = self.isEditingMode; // cell
BOOL selected = [[collectionView indexPathsForSelectedItems] containsObject:indexPath];
[c updateSelected:selected];
}
#pragma mark - Lazy
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
CGFloat inset = 16;
CGFloat spacing = 12;
CGFloat W = UIScreen.mainScreen.bounds.size.width;
CGFloat itemW = floor((W - inset * 2 - spacing) / 2.0);
CGFloat itemH = itemW * 0.82f; //
layout.itemSize = CGSizeMake(itemW, itemH);
layout.minimumInteritemSpacing = spacing;
layout.minimumLineSpacing = spacing;
layout.sectionInset = UIEdgeInsetsMake(12, inset, 12, inset);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.allowsMultipleSelection = NO; //
[_collectionView registerClass:MySkinCell.class forCellWithReuseIdentifier:kMySkinCellId];
}
return _collectionView;
}
- (UIView *)bottomView {
if (!_bottomView) {
_bottomView = [UIView new];
_bottomView.backgroundColor = [UIColor whiteColor];
_bottomView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.06].CGColor;
_bottomView.layer.shadowOpacity = 1;
_bottomView.layer.shadowOffset = CGSizeMake(0, -2);
[_bottomView addSubview:self.selectedLabel];
[_bottomView addSubview:self.deleteButton];
[self.selectedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_bottomView).offset(16);
make.centerY.equalTo(_bottomView);
}];
[self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_bottomView).offset(-16);
make.centerY.equalTo(_bottomView);
make.width.mas_equalTo(92);
make.height.mas_equalTo(36);
}];
}
return _bottomView;
}
- (UILabel *)selectedLabel {
if (!_selectedLabel) {
_selectedLabel = [UILabel new];
_selectedLabel.textColor = [UIColor colorWithHex:0x666666];
_selectedLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
_selectedLabel.text = @"Selected: 0 Skins";
}
return _selectedLabel;
}
- (UIButton *)deleteButton {
if (!_deleteButton) {
_deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_deleteButton setTitle:@"Delete" forState:UIControlStateNormal];
_deleteButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
_deleteButton.layer.cornerRadius = 18;
_deleteButton.layer.borderWidth = 1;
_deleteButton.clipsToBounds = YES;
[_deleteButton addTarget:self action:@selector(onDelete) forControlEvents:UIControlEventTouchUpInside];
//
_deleteButton.enabled = NO;
_deleteButton.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
[_deleteButton setTitleColor:[UIColor colorWithHex:0xC8C8C8] forState:UIControlStateNormal];
_deleteButton.layer.borderColor = [UIColor colorWithHex:0xE6E6E6].CGColor;
}
return _deleteButton;
}
@end