-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FLEXMITMDataSource, refactor MITMVC
- Loading branch information
1 parent
5acb330
commit afeff1b
Showing
5 changed files
with
305 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// FLEXMITMDataSource.h | ||
// FLEX | ||
// | ||
// Created by Tanner Bennett on 8/22/21. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface FLEXMITMDataSource<__covariant TransactionType> : NSObject | ||
|
||
+ (instancetype)dataSourceWithProvider:(NSArray<TransactionType> *(^)())future; | ||
|
||
@property (nonatomic, readonly) NSArray<TransactionType> *transactions; | ||
@property (nonatomic, readonly) NSArray<TransactionType> *allTransactions; | ||
/// Equal to \c allTransactions if not filtered | ||
@property (nonatomic, readonly) NSArray<TransactionType> *filteredTransactions; | ||
|
||
/// Use this instead of either of the other two as it updates based on whether we have a filter or not | ||
@property (nonatomic) NSInteger bytesReceived; | ||
@property (nonatomic) NSInteger totalBytesReceived; | ||
/// Equal to \c totalBytesReceived if not filtered | ||
@property (nonatomic) NSInteger filteredBytesReceived; | ||
|
||
- (void)reloadByteCounts; | ||
- (void)reloadData:(void (^_Nullable)(FLEXMITMDataSource *dataSource))completion; | ||
- (void)filter:(NSString *)searchString completion:(void(^_Nullable)(FLEXMITMDataSource *dataSource))completion; | ||
|
||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// FLEXMITMDataSource.m | ||
// FLEX | ||
// | ||
// Created by Tanner Bennett on 8/22/21. | ||
// | ||
|
||
#import "FLEXMITMDataSource.h" | ||
#import "FLEXNetworkTransaction.h" | ||
#import "FLEXUtility.h" | ||
|
||
@interface FLEXMITMDataSource () | ||
@property (nonatomic, readonly) NSArray *(^dataProvider)(); | ||
@property (nonatomic) NSString *filterString; | ||
@end | ||
|
||
@implementation FLEXMITMDataSource | ||
|
||
+ (instancetype)dataSourceWithProvider:(NSArray<id> *(^)())future { | ||
FLEXMITMDataSource *ds = [self new]; | ||
ds->_dataProvider = future; | ||
[ds reloadData:nil]; | ||
|
||
return ds; | ||
} | ||
|
||
- (NSArray *)transactions { | ||
return _filteredTransactions; | ||
} | ||
|
||
- (NSInteger)bytesReceived { | ||
return _filteredBytesReceived; | ||
} | ||
|
||
- (void)reloadByteCounts { | ||
[self updateBytesReceived]; | ||
[self updateFilteredBytesReceived]; | ||
} | ||
|
||
- (void)reloadData:(void (^)(FLEXMITMDataSource *dataSource))completion { | ||
self.allTransactions = self.dataProvider(); | ||
[self filter:self.filterString completion:completion]; | ||
} | ||
|
||
- (void)filter:(NSString *)searchString completion:(void (^)(FLEXMITMDataSource *dataSource))completion { | ||
self.filterString = searchString; | ||
|
||
if (!searchString.length) { | ||
self.filteredTransactions = self.allTransactions; | ||
if (completion) completion(self); | ||
} else { | ||
[self onBackgroundQueue:^NSArray *{ | ||
return [self.allTransactions flex_filtered:^BOOL(FLEXNetworkTransaction *entry, NSUInteger idx) { | ||
return [entry.request.URL.absoluteString localizedCaseInsensitiveContainsString:searchString]; | ||
}]; | ||
} thenOnMainQueue:^(NSArray *filteredNetworkTransactions) { | ||
if ([self.filterString isEqual:searchString]) { | ||
self.filteredTransactions = filteredNetworkTransactions; | ||
if (completion) completion(self); | ||
} | ||
}]; | ||
} | ||
} | ||
|
||
- (void)setAllTransactions:(NSArray *)transactions { | ||
_allTransactions = transactions; | ||
[self updateBytesReceived]; | ||
} | ||
|
||
- (void)setFilteredTransactions:(NSArray *)filteredTransactions { | ||
_filteredTransactions = filteredTransactions; | ||
[self updateFilteredBytesReceived]; | ||
} | ||
|
||
- (void)updateBytesReceived { | ||
NSInteger bytesReceived = 0; | ||
for (FLEXNetworkTransaction *transaction in self.transactions) { | ||
bytesReceived += transaction.receivedDataLength; | ||
} | ||
|
||
self.bytesReceived = bytesReceived; | ||
} | ||
|
||
- (void)updateFilteredBytesReceived { | ||
NSInteger filteredBytesReceived = 0; | ||
for (FLEXNetworkTransaction *transaction in self.filteredTransactions) { | ||
filteredBytesReceived += transaction.receivedDataLength; | ||
} | ||
|
||
self.filteredBytesReceived = filteredBytesReceived; | ||
} | ||
|
||
- (void)onBackgroundQueue:(NSArray *(^)(void))backgroundBlock thenOnMainQueue:(void(^)(NSArray *))mainBlock { | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | ||
NSArray *items = backgroundBlock(); | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
mainBlock(items); | ||
}); | ||
}); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.