Skip to content

Commit

Permalink
Merge branch 'master' of github.com:didi/DoraemonKit
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiangboy committed Mar 23, 2020
2 parents dadee56 + e59592e commit eed1273
Show file tree
Hide file tree
Showing 20 changed files with 456 additions and 119 deletions.
7 changes: 6 additions & 1 deletion iOS/DoraemonKit/Resource/en.lproj/Doraemon.strings
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@
"断网" = "Broken";
"超时" = "Timeout";
"限速" = "Limit";
"超时时间" = "timeout";
"延时" = "Delay";
"延时时间" = "Delay time";
"请求限速" = "Request limit";
"响应限速" = "Response limit";
"数据预览为空" = "The data is previewed as empty";
Expand All @@ -249,8 +250,12 @@
"本地是否存在mock数据: %@" = "mock data exists locally: %@";
"存在" = "exist";
"不存在" = "inexistence";
"App当前处于健康体检状态,无法进行此操作" = "App is currently in the state of health check, so this operation cannot be carried out";

//DoraemonKitDemo
"图片测试" = "Image Test";
"Mock测试" = "Mock Test";
"Mock测试2" = "Mock Test 2";
"block方式加入插件" = "Add plugins from block";
"测试工具集" = "Test tools";
"视觉测试Demo" = "UI test Demo";
Expand Down
4 changes: 3 additions & 1 deletion iOS/DoraemonKit/Resource/zh-Hans.lproj/Doraemon.strings
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@
"断网" = "断网";
"超时" = "超时";
"限速" = "限速";
"超时时间" = "超时时间";
"延时" = "延时";
"延时时间" = "延时时间";
"请求限速" = "请求限速";
"响应限速" = "响应限速";
"数据预览为空" = "数据预览为空";
Expand All @@ -246,6 +247,7 @@
"创建人: %@\n" = "创建人: %@\n";
"存在" = "存在";
"不存在" = "不存在";
"App当前处于健康体检状态,无法进行此操作" = "App当前处于健康体检状态,无法进行此操作";

//DoraemonKitDemo
"block方式加入插件" = "block方式加入插件";
Expand Down
4 changes: 0 additions & 4 deletions iOS/DoraemonKit/Src/Core/Cache/DoraemonCacheManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@

- (BOOL)anrTrackSwitch;

- (void)saveShouldAutorotate:(BOOL)on;

- (BOOL)shouldAutorotate;

/// 历史记录
- (NSArray<NSString *> *)h5historicalRecord;
- (void)saveH5historicalRecordWithText:(NSString *)text;
Expand Down
11 changes: 0 additions & 11 deletions iOS/DoraemonKit/Src/Core/Cache/DoraemonCacheManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
static NSString * const kDoraemonAllTestKey = @"doraemon_allTest_window_key";
static NSString * const kDoraemonMockCacheKey = @"doraemon_mock_cache_key";
static NSString * const kDoraemonHealthStartKey = @"doraemon_health_start_key";
static NSString * const kDoraemonShouldAutorotate = @"doraemon_shouldAutorotate_key";

@interface DoraemonCacheManager()

Expand Down Expand Up @@ -210,16 +209,6 @@ - (BOOL)anrTrackSwitch {
return [_defaults boolForKey:kDoraemonANRTrackKey];
}

- (void)saveShouldAutorotate:(BOOL)on{
[_defaults setBool:on forKey:kDoraemonShouldAutorotate];
[_defaults synchronize];
}

- (BOOL)shouldAutorotate{
return [_defaults boolForKey:kDoraemonShouldAutorotate];
}


- (NSArray<NSString *> *)h5historicalRecord {
return [_defaults objectForKey:kDoraemonH5historicalRecord];
}
Expand Down
1 change: 1 addition & 0 deletions iOS/DoraemonKit/Src/Core/Define/DoraemonDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "DoraemonUtil.h"

#define DoKitVersion @"3.0.0"
#define kbChange(x) x * 1000

#define DoKit_OpenLog

Expand Down
2 changes: 1 addition & 1 deletion iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ - (void)initData{
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonSubThreadUICheckPlugin];
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonANRPlugin];
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonLargeImageFilter];
//[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonWeakNetworkPlugin];
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonWeakNetworkPlugin];
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonStartTimePlugin];
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonUIProfilePlugin];
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonTimeProfilePlugin];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#import "DoraemonNetworkInterceptor.h"
#import "DoraemonMockManager.h"
#import "DoraemonDefine.h"
#import "UIViewController+Doraemon.h"

static NSString * const kDoraemonProtocolKey = @"doraemon_protocol_key";

Expand Down Expand Up @@ -81,13 +80,31 @@ + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request{
NSString *urlString = [NSString stringWithFormat:@"https://mock.dokit.cn/api/app/scene/%@",sceneId];
DoKitLog(@"MOCK URL == %@",urlString);
mutableReqeust = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
dispatch_async(dispatch_get_main_queue(), ^{
[DoraemonToastUtil showToastBlack:[NSString stringWithFormat:@"mock url = %@",request.URL.absoluteURL] inView:[UIViewController rootViewControllerForKeyWindow].view];
});
}
return [mutableReqeust copy];
}

- (void)handleFromSelect{
if(DoraemonWeakNetwork_Break == [[DoraemonNetworkInterceptor shareInstance].weakDelegate weakNetSelecte]){
DoKitLog(@"yd Break Net");
}else if(DoraemonWeakNetwork_Delay == [[DoraemonNetworkInterceptor shareInstance].weakDelegate weakNetSelecte]){
DoKitLog(@"yd Delay Net");//此处有dispatch_get_main_queue,无法使用switch
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([[DoraemonNetworkInterceptor shareInstance].weakDelegate delayTime] * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.task resume];
});
}else if(DoraemonWeakNetwork_WeakSpeed == [[DoraemonNetworkInterceptor shareInstance].weakDelegate weakNetSelecte]){
DoKitLog(@"yd WeakUpFlow Net");
if (@available(iOS 9.0, *)) {
[[DoraemonNetworkInterceptor shareInstance].weakDelegate handleWeak:[self.request.URL dataRepresentation] isDown:NO];
} else {
// Fallback on earlier versions
}
[self.task resume];
}else{
[self.task resume];
}
}

- (void)startLoading{
NSMutableURLRequest * recursiveRequest;
NSMutableArray * calculatedModes;
Expand All @@ -114,8 +131,11 @@ - (void)startLoading{
self.startTime = [[NSDate date] timeIntervalSince1970];
self.task = [[[self class] sharedDemux] dataTaskWithRequest:recursiveRequest delegate:self modes:self.modes];
assert(self.task != nil);

[self.task resume];
if([DoraemonNetworkInterceptor shareInstance].weakDelegate){
[self handleFromSelect];
}else{
[self.task resume];
}
}

- (void)stopLoading{
Expand All @@ -133,23 +153,6 @@ - (void)stopLoading{
}
}

- (void)handleWeak:(NSData *)data{
NSUInteger count = 0;
NSData *limitData = nil;
while (true) {
limitData = [[DoraemonNetworkInterceptor shareInstance].weakDelegate doraemonNSURLProtocolWeak:data count:count];
if(limitData.length > 0){
[self.data appendData:limitData];
[self.client URLProtocol:self didLoadData:limitData];
}
if([[DoraemonNetworkInterceptor shareInstance].weakDelegate endWeak:limitData]){
return ;
}
count++;
}
}


#pragma mark - NSURLSessionDelegate
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
assert([NSThread currentThread] == self.clientThread);
Expand All @@ -160,8 +163,11 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
assert([NSThread currentThread] == self.clientThread);
if ([DoraemonNetworkInterceptor shareInstance].weakDelegate && [[DoraemonNetworkInterceptor shareInstance].weakDelegate shouldWeak]) {
[self handleWeak:data];
if ([DoraemonNetworkInterceptor shareInstance].weakDelegate) {
if(DoraemonWeakNetwork_WeakSpeed == [[DoraemonNetworkInterceptor shareInstance].weakDelegate weakNetSelecte]){
DoKitLog(@"yd WeakDownFlow Net");
[[DoraemonNetworkInterceptor shareInstance].weakDelegate handleWeak:data isDown:YES];
}
}
[self.data appendData:data];
[self.client URLProtocol:self didLoadData:data];
Expand All @@ -173,6 +179,11 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
self.error = error;
[self.client URLProtocol:self didFailWithError:error];
}else{
if(DoraemonWeakNetwork_OutTime == [[DoraemonNetworkInterceptor shareInstance].weakDelegate weakNetSelecte]){
DoKitLog(@"yd Outtime Net");
[self.client URLProtocol:self didFailWithError:[[NSError alloc] initWithCoder:[[NSCoder alloc] init]]];
return ;
}
[self.client URLProtocolDidFinishLoading:self];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,24 @@ NS_ASSUME_NONNULL_BEGIN

@protocol DoraemonNetworkWeakDelegate <NSObject>

- (BOOL)shouldWeak;

- (NSData *)doraemonNSURLProtocolWeak:(NSData *)data count:(NSInteger)times;

- (BOOL)endWeak:(NSData *)data;
typedef NS_ENUM(NSUInteger, DoraemonWeakNetType) {
#pragma mark - 弱网选项对应
// 断网
DoraemonWeakNetwork_Break,
// 超时
DoraemonWeakNetwork_OutTime,
// 限网
DoraemonWeakNetwork_WeakSpeed,
//延时
DoraemonWeakNetwork_Delay
};

- (NSInteger)weakNetSelecte;

- (NSUInteger)delayTime;

- (void)handleWeak:(NSData *)data isDown:(BOOL)is;
//- (NSData *)doraemonNSURLProtocolWeak:(NSData *)data count:(NSInteger)times;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import "DoraemonNetFlowDataSource.h"
#import "DoraemonWeakNetworkWindow.h"

@implementation DoraemonNetFlowDataSource{
dispatch_semaphore_t semaphore;
Expand All @@ -32,6 +33,7 @@ - (instancetype)init{
- (void)addHttpModel:(DoraemonNetFlowHttpModel *)httpModel{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
[_httpModelArray insertObject:httpModel atIndex:0];
[[DoraemonWeakNetworkWindow shareInstance] updateFlowValue:httpModel.uploadFlow downFlow:httpModel.downFlow fromWeak:NO];
dispatch_semaphore_signal(semaphore);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#import "DoraemonCellSwitch.h"
#import "DoraemonWeakNetworkDetailView.h"
#import "DoraemonDefine.h"
#import "DoraemonToastUtil.h"
#import "DoraemonCacheManager.h"
#import "DoraemonWeakNetworkWindow.h"

@interface DoraemonWeakNetworkViewController()
@interface DoraemonWeakNetworkViewController()<DoraemonWeakNetworkWindowDelegate>

@property (nonatomic, strong) DoraemonCellSwitch *weakSwitchView;
@property (nonatomic, strong) DoraemonWeakNetworkDetailView *detail;
Expand All @@ -29,7 +32,7 @@ - (void)viewDidLoad {
[_weakSwitchView.switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[_weakSwitchView needDownLine];
[self.view addSubview:_weakSwitchView];

[DoraemonWeakNetworkWindow shareInstance].delegate = self;
_detail = [[DoraemonWeakNetworkDetailView alloc] initWithFrame:CGRectMake(0, _weakSwitchView.doraemon_bottom , self.view.doraemon_width, self.view.doraemon_height - _weakSwitchView.doraemon_bottom)];
_detail.hidden = ![DoraemonWeakNetworkManager shareInstance].shouldWeak;
[self.view addSubview:_detail];
Expand All @@ -41,12 +44,27 @@ - (BOOL)needBigTitleView{

- (void)switchAction:(id)sender{
UISwitch *switchButton = (UISwitch*)sender;
if([[DoraemonCacheManager sharedInstance] healthStart]){
switchButton.on = NO;
[DoraemonToastUtil showToastBlack:DoraemonLocalizedString(@"App当前处于健康体检状态,无法进行此操作") inView:self.view];
return ;
}
[DoraemonWeakNetworkManager shareInstance].shouldWeak = [switchButton isOn];

[[DoraemonWeakNetworkManager shareInstance] canInterceptNetFlow:[switchButton isOn]];
_detail.hidden = ![switchButton isOn];
[DoraemonWeakNetworkWindow shareInstance].hidden = _detail.hidden;
if([switchButton isOn]){
[[DoraemonWeakNetworkManager shareInstance] startRecord];
}else{
[[DoraemonWeakNetworkManager shareInstance] endRecord];
}
}


#pragma mark - DoraemonWeakNetworkWindowDelegate
- (void)doraemonWeakNetworkWindowClosed {
_weakSwitchView.switchView.on = NO;
_detail.hidden = YES;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ @interface DoraemonWeakNetworkHandle()
@implementation DoraemonWeakNetworkHandle

- (NSData *)weakFlow:(NSData *)data count:(NSInteger)times size:(NSInteger)weakSize{

if(data.length < weakSize){
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -16,17 +15,14 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) NSUInteger selecte;
@property (nonatomic, assign) NSUInteger upFlowSpeed;
@property (nonatomic, assign) NSUInteger downFlowSpeed;
@property (nonatomic, assign) NSUInteger outTime;

@property (nonatomic, assign) NSUInteger delayTime;


+ (DoraemonWeakNetworkManager *)shareInstance;

- (void)changeWeakSize:(NSInteger)size;

- (void)canInterceptNetFlow:(BOOL)enable;

- (void)selectWeakItemChange:(NSUInteger)select sleepTime:(CGFloat)time weakSize:(NSUInteger)size;
- (void)startRecord;
- (void)endRecord;

@end

Expand Down
Loading

0 comments on commit eed1273

Please sign in to comment.