Skip to content

Commit

Permalink
Add FLEXMITMDataSource, refactor MITMVC
Browse files Browse the repository at this point in the history
  • Loading branch information
NSExceptional committed Oct 6, 2021
1 parent 5acb330 commit afeff1b
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 116 deletions.
34 changes: 34 additions & 0 deletions Classes/Network/FLEXMITMDataSource.h
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
102 changes: 102 additions & 0 deletions Classes/Network/FLEXMITMDataSource.m
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
1 change: 1 addition & 0 deletions Classes/Network/FLEXNetworkMITMViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "FLEXTableViewController.h"
#import "FLEXGlobalsEntry.h"

/// The main screen for the network observer, which displays a list of network transactions.
@interface FLEXNetworkMITMViewController : FLEXTableViewController <FLEXGlobalsEntry>

@end
Loading

0 comments on commit afeff1b

Please sign in to comment.