Files
keyboard/keyBoard/Class/Home/VC/FunctionTest/KBSkinCenterVC.m

177 lines
7.8 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// KBSkinCenterVC.m
//
#import "KBSkinCenterVC.h"
#import "Masonry.h"
#import "KBNetworkManager.h"
#import "KBSkinManager.h"
#import "KBHUD.h"
#import "KBConfig.h"
#import "KBSkinService.h"
#import "KBSkinInstallBridge.h"
@interface KBSkinCell : UITableViewCell
@property (nonatomic, strong) UIButton *applyBtn;
@end
@implementation KBSkinCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
_applyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[_applyBtn setTitle:KBLocalized(@"Download & Apply") forState:UIControlStateNormal];
_applyBtn.layer.cornerRadius = 6; _applyBtn.layer.borderWidth = 1;
_applyBtn.layer.borderColor = [UIColor colorWithWhite:0.85 alpha:1].CGColor;
[self.contentView addSubview:_applyBtn];
[_applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.contentView).offset(-16);
make.centerY.equalTo(self.contentView);
make.width.mas_greaterThanOrEqualTo(110);
make.height.mas_equalTo(34);
}];
}
return self;
}
@end
@interface KBSkinCenterVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray<NSDictionary *> *skins; // 每个元素即一套皮肤的 JSON与后端约定格式一致
@property (nonatomic, strong) UIButton *resetButton;
@end
@implementation KBSkinCenterVC
- (void)viewDidLoad {
[super viewDidLoad];
// self.title = KBLocalized(@"Skin Center");
self.view.backgroundColor = [UIColor whiteColor];
self.skins = @[
@{
@"id": @"local本地",
@"name": KBLocalized(@"Pink skin"),
// 关键zip_url 写成 bundle:// 前缀 + 文件名
@"zip_url": @"bundle://fense.zip",
// 颜色你可以先随便写一套,或者继承默认
@"background": @"#F5FFE8",
@"key_bg": @"#FFFFFF",
@"key_text": @"#4A4A4A",
@"key_highlight": @"#D9F4C4",
@"accent": @"#A4D68A"
// 不写 key_icons代码会自动用本地那份映射表
},
@{
@"id": @"remote002",
@"name": KBLocalized(@"Remote skin"),
// 关键zip_url 写成 bundle:// 前缀 + 文件名
@"zip_url": @"http://gx.zhukeping.com/download/Christmas.zip",
// // 颜色你可以先随便写一套,或者继承默认
// @"background": @"#F5FFE8",
// @"key_bg": @"#FFFFFF",
// @"key_text": @"#4A4A4A",
// @"key_highlight": @"#D9F4C4",
// @"accent": @"#A4D68A"
// 不写 key_icons代码会自动用本地那份映射表
}
];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KB_NAV_TOTAL_HEIGHT, KB_SCREEN_WIDTH, KB_SCREEN_HEIGHT - KB_NAV_TOTAL_HEIGHT) style:UITableViewStyleInsetGrouped];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.delegate = self; self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
// 底部添加“恢复默认皮肤”测试按钮
UIButton *reset = [UIButton buttonWithType:UIButtonTypeSystem];
[reset setTitle:KBLocalized(@"Restore default skin") forState:UIControlStateNormal];
reset.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
reset.layer.cornerRadius = 8.0;
reset.layer.borderWidth = 1.0;
reset.layer.borderColor = [UIColor colorWithWhite:0.85 alpha:1.0].CGColor;
[reset addTarget:self action:@selector(onResetDefault:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:reset];
self.resetButton = reset;
[reset mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.right.equalTo(self.view).offset(-16);
make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-16);
make.height.mas_equalTo(44);
}];
// 让 tableView 的内容区域避免被按钮遮挡
UIEdgeInsets inset = self.tableView.contentInset;
inset.bottom += 44 + 24; // 按钮高度 + 上下间距
self.tableView.contentInset = inset;
self.tableView.scrollIndicatorInsets = inset;
[[KBNetworkManager shared] GET:@"https://www.apple.com"
parameters:nil
headers:nil
completion:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSLog(@"[Test] apple.com finished, error = %@", error);
}];
}
#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.skins.count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cid = @"skin.cell";
KBSkinCell *cell = [tableView dequeueReusableCellWithIdentifier:cid];
if (!cell) { cell = [[KBSkinCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cid]; }
NSDictionary *skin = self.skins[indexPath.row];
cell.textLabel.text = skin[@"name"]; cell.detailTextLabel.text = skin[@"id"];
[cell.applyBtn removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[cell.applyBtn addTarget:self action:@selector(onApplyBtn:) forControlEvents:UIControlEventTouchUpInside];
cell.applyBtn.tag = indexPath.row;
return cell;
}
- (void)onApplyBtn:(UIButton *)sender {
NSInteger idx = sender.tag;
if (idx < 0 || idx >= self.skins.count) return;
NSDictionary *skin = self.skins[idx];
if (!skin) return;
if (idx == 0) {
// 将需求固定到 002.zip本地写死皮肤 id便于键盘扩展识别并解压。
static NSString * const kKBBundleSkinId002 = @"bundle_skin_fense";
[KBSkinInstallBridge publishBundleSkinRequestWithId:kKBBundleSkinId002
name:@"" ?: kKBBundleSkinId002
zipName:@"fense.zip"
iconShortNames:nil];
[KBHUD showInfo:KBLocalized(@"Keyboard has been notified to unzip. Switch to the custom keyboard to apply.")];
}else if (idx == 1){
[[KBSkinService shared] applySkinWithJSON:skin
fromViewController:self
mode:KBSkinSourceModeRemoteZip
completion:nil];
}else if (idx == 2){
// [[KBSkinService shared] applySkinWithJSON:skin
// fromViewController:self
// mode:KBSkinSourceModeLocalBundleZip
// completion:nil];
static NSString * const kKBBundleSkinId002 = @"bundle_Christmas";
[KBSkinInstallBridge publishBundleSkinRequestWithId:kKBBundleSkinId002
name:@"" ?: kKBBundleSkinId002
zipName:@"Christmas.zip"
iconShortNames:nil];
[KBHUD showInfo:KBLocalized(@"Keyboard has been notified to unzip. Switch to the custom keyboard to apply.")];
}
}
- (void)onResetDefault:(UIButton *)sender {
// 不需要皮肤 JSON传空字典即可
[[KBSkinService shared] applySkinWithJSON:@{}
fromViewController:self
mode:KBSkinSourceModeResetToDefault
completion:nil];
}
@end