Skip to content

Commit bdad390

Browse files
author
yixiang
committed
沙盒浏览器支持数据库表的本地预览
1 parent ac4dbfc commit bdad390

11 files changed

+400
-25
lines changed

iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ NS_ASSUME_NONNULL_BEGIN
1717
@property (nonatomic, copy) NSString *tableName;
1818

1919
- (NSArray *)tablesAtDB;
20+
- (NSArray *)dataAtTable;
2021

2122
@end
2223

iOS/DoraemonKit/Src/Core/Plugin/Sanbox/Util/DoraemonDBManager.m

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@interface DoraemonDBManager()
1212

13-
13+
@property (nonatomic, strong) NSMutableArray *dataArray;
1414

1515
@end
1616

@@ -56,6 +56,61 @@ - (NSArray *)tablesAtDB{
5656
return tableNameArray;
5757
}
5858

59+
//获取每一张表中的所有数据
60+
- (NSArray *)dataAtTable{
61+
sqlite3 *db = [self openDB];
62+
if (db == nil) {
63+
return nil;
64+
}
65+
//查询sqlite_master表
66+
NSString *sql = [NSString stringWithFormat:@"select * from %@",self.tableName];
67+
//执行sql
68+
char *errmsg = nil;
69+
sqlite3_exec(db, [sql UTF8String], selectCallback, nil, &errmsg);
70+
71+
//处理数据
72+
NSMutableArray *arrayM = [NSMutableArray arrayWithArray:self.dataArray];
73+
[self.dataArray removeAllObjects];
74+
75+
return arrayM;
76+
}
77+
78+
//查询回调
79+
int selectCallback(void *firstValue,int columnCount, char **columnValues, char **columnNames)
80+
{
81+
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
82+
for (int i = 0; i < columnCount; i++) {
83+
//获取当前的列表(字段名)
84+
char *columnName = columnNames[i];
85+
NSString *nameStr = nil;
86+
if (columnName == NULL) {
87+
nameStr = nil;
88+
}else{
89+
nameStr = [NSString stringWithUTF8String:columnName];
90+
}
91+
92+
//获取当前字段的值
93+
char *columnValue = columnValues[i];
94+
NSString *valueStr = nil;
95+
if (columnValue == NULL) {
96+
valueStr = nil;
97+
}else{
98+
valueStr = [NSString stringWithUTF8String:columnValue];
99+
}
100+
101+
[dict setValue:valueStr forKey:nameStr];
102+
}
103+
[[[DoraemonDBManager shareManager] dataArray] addObject:dict];
104+
return 0;
105+
}
106+
107+
#pragma mark - 懒加载
108+
- (NSMutableArray *)dataArray{
109+
if (_dataArray == nil) {
110+
_dataArray = [NSMutableArray array];
111+
}
112+
return _dataArray;
113+
}
59114

60115

61116
@end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// DoraemonDBCell.h
3+
// AFNetworking
4+
//
5+
// Created by yixiang on 2019/4/1.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
#import "DoraemonDBRowView.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface DoraemonDBCell : UITableViewCell
14+
15+
@property (nonatomic, strong) DoraemonDBRowView *rowView;
16+
17+
- (void)renderCellWithArray:(NSArray *)array;
18+
19+
@end
20+
21+
NS_ASSUME_NONNULL_END
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// DoraemonDBCell.m
3+
// AFNetworking
4+
//
5+
// Created by yixiang on 2019/4/1.
6+
//
7+
8+
#import "DoraemonDBCell.h"
9+
#import "DoraemonDBRowView.h"
10+
11+
@interface DoraemonDBCell()
12+
13+
@end
14+
15+
@implementation DoraemonDBCell
16+
17+
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
18+
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
19+
if (self) {
20+
_rowView = [[DoraemonDBRowView alloc] init];
21+
[self.contentView addSubview:_rowView];
22+
}
23+
return self;
24+
}
25+
26+
- (void)layoutSubviews{
27+
[super layoutSubviews];
28+
_rowView.frame = self.contentView.bounds;
29+
}
30+
31+
- (void)renderCellWithArray:(NSArray *)array{
32+
_rowView.dataArray = array;
33+
}
34+
35+
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// DoraemonDBRowView.h
3+
// AFNetworking
4+
//
5+
// Created by yixiang on 2019/4/1.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
@class DoraemonDBRowView;
10+
11+
typedef NS_ENUM(NSInteger, DoraemonDBRowViewType) {
12+
DoraemonDBRowViewTypeForTitle = 0,
13+
DoraemonDBRowViewTypeForOne = 1,
14+
DoraemonDBRowViewTypeForTwo = 2
15+
16+
};
17+
18+
@protocol DoraemonDBRowViewTypeDelegate <NSObject>
19+
20+
- (void)rowView:(DoraemonDBRowView *)rowView didLabelTaped:(UILabel *)label;
21+
22+
@end
23+
24+
25+
@interface DoraemonDBRowView : UIView
26+
27+
@property(nonatomic, copy) NSArray *dataArray;
28+
29+
@property(nonatomic, assign) DoraemonDBRowViewType type;
30+
31+
@property(nonatomic, weak) id<DoraemonDBRowViewTypeDelegate> delegate;
32+
33+
@end
34+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// DoraemonDBRowView.m
3+
// AFNetworking
4+
//
5+
// Created by yixiang on 2019/4/1.
6+
//
7+
8+
#import "DoraemonDBRowView.h"
9+
#import "DoraemonDefine.h"
10+
11+
@implementation DoraemonDBRowView
12+
13+
- (instancetype)initWithFrame:(CGRect)frame{
14+
if (self = [super initWithFrame:frame]) {
15+
16+
}
17+
return self;
18+
}
19+
20+
- (void)setDataArray:(NSArray *)dataArray{
21+
_dataArray = dataArray;
22+
for (UIView *sub in self.subviews) {
23+
[sub removeFromSuperview];
24+
}
25+
for (int i = 0; i < self.dataArray.count; i++) {
26+
NSString *content = self.dataArray[i];
27+
UILabel *label = [[UILabel alloc] init];
28+
UIColor *color = [UIColor doraemon_colorWithString:@"#dcdcdc"];
29+
if (self.type == DoraemonDBRowViewTypeForOne) {
30+
color = [UIColor doraemon_colorWithString:@"#e6e6e6"];
31+
}
32+
if (self.type == DoraemonDBRowViewTypeForTwo) {
33+
color = [UIColor doraemon_colorWithString:@"#ebebeb"];
34+
}
35+
label.backgroundColor = color;
36+
label.text = content;
37+
label.textAlignment = NSTextAlignmentCenter;
38+
label.tag = i;
39+
label.userInteractionEnabled = YES;
40+
[label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLabel:)]];
41+
[self addSubview:label];
42+
}
43+
}
44+
45+
- (void)layoutSubviews{
46+
[super layoutSubviews];
47+
48+
for (UIView *subView in self.subviews) {
49+
if ([subView isKindOfClass:UILabel.class]) {
50+
CGFloat width = (self.bounds.size.width - (self.dataArray.count - 1)) / self.dataArray.count;
51+
subView.frame = CGRectMake(subView.tag * (width + 1), 0, width, self.bounds.size.height);
52+
}
53+
}
54+
}
55+
56+
- (void)tapLabel:(UITapGestureRecognizer *)tap{
57+
UILabel *label = (UILabel *)tap.view;
58+
if ([self.delegate respondsToSelector:@selector(rowView:didLabelTaped:)]) {
59+
[self.delegate rowView:self didLabelTaped:label];
60+
}
61+
}
62+
63+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// DoraemonDBShowView.h
3+
// AFNetworking
4+
//
5+
// Created by yixiang on 2019/4/1.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface DoraemonDBShowView : UIView
13+
14+
- (void)showText:(NSString *)text;
15+
16+
@end
17+
18+
NS_ASSUME_NONNULL_END
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// DoraemonDBShowView.m
3+
// AFNetworking
4+
//
5+
// Created by yixiang on 2019/4/1.
6+
//
7+
8+
#import "DoraemonDBShowView.h"
9+
#import "DoraemonDefine.h"
10+
11+
@interface DoraemonDBShowView()
12+
13+
@property (nonatomic, strong) UILabel *displayLabel;
14+
15+
@end
16+
17+
@implementation DoraemonDBShowView
18+
19+
- (instancetype)initWithFrame:(CGRect)frame{
20+
self = [super initWithFrame:frame];
21+
if (self) {
22+
_displayLabel = [[UILabel alloc] init];
23+
_displayLabel.numberOfLines = 0;
24+
_displayLabel.textAlignment = NSTextAlignmentCenter;
25+
_displayLabel.backgroundColor = [UIColor doraemon_colorWithString:@"#AAAAAA"];
26+
[self addSubview:_displayLabel];
27+
}
28+
return self;
29+
}
30+
31+
- (void)showText:(NSString *)text{
32+
_displayLabel.frame = CGRectMake(self.doraemon_width/2-150/2, self.doraemon_height/2-100/2, 150, 100);
33+
__weak typeof(self) weakSelf = self;
34+
[UIView animateWithDuration:0.25 animations:^{
35+
__strong __typeof(weakSelf) strongSelf = weakSelf;
36+
strongSelf.displayLabel.frame = CGRectMake(self.doraemon_width/2-300/2, self.doraemon_height/2-200/2, 300, 200);
37+
} completion:^(BOOL finished) {
38+
__strong __typeof(weakSelf) strongSelf = weakSelf;
39+
strongSelf.displayLabel.text = text;
40+
}];
41+
}
42+
43+
@end

0 commit comments

Comments
 (0)