Path: blob/main/Natives/DownloadProgressViewController.m
589 views
#import <dlfcn.h>1#import <objc/runtime.h>2#import "DownloadProgressViewController.h"3#import "WFWorkflowProgressView.h"45static void *CellProgressObserverContext = &CellProgressObserverContext;6static void *TotalProgressObserverContext = &TotalProgressObserverContext;78@interface DownloadProgressViewController ()9@property NSInteger fileListCount;10@end1112@implementation DownloadProgressViewController1314- (instancetype)initWithTask:(MinecraftResourceDownloadTask *)task {15self = [super init];16self.task = task;17return self;18}1920- (void)loadView {21[super loadView];22self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemClose target:self action:@selector(actionClose)];23self.tableView.allowsSelection = NO;2425// Load WFWorkflowProgressView26dlopen("/System/Library/PrivateFrameworks/WorkflowUIServices.framework/WorkflowUIServices", RTLD_GLOBAL);27}28- (void)viewDidAppear:(BOOL)animated {29[super viewDidAppear:animated];3031[self.task.textProgress addObserver:self32forKeyPath:@"fractionCompleted"33options:NSKeyValueObservingOptionInitial34context:TotalProgressObserverContext];35}3637- (void)viewDidDisappear:(BOOL)animated {38[super viewDidDisappear:animated];3940[self.task.textProgress removeObserver:self forKeyPath:@"fractionCompleted"];41}4243- (void)actionClose {44[self.navigationController dismissViewControllerAnimated:YES completion:nil];45}4647- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {48NSProgress *progress = object;49if (context == CellProgressObserverContext) {50UITableViewCell *cell = objc_getAssociatedObject(progress, @"cell");51if (!cell) return;52dispatch_async(dispatch_get_main_queue(), ^{53cell.detailTextLabel.text = progress.localizedAdditionalDescription;54WFWorkflowProgressView *progressView = (id)cell.accessoryView;55progressView.fractionCompleted = progress.fractionCompleted;56if (progress.finished) {57[progressView transitionCompletedLayerToVisible:YES animated:YES haptic:NO];58}59});60} else if (context == TotalProgressObserverContext) {61dispatch_async(dispatch_get_main_queue(), ^{62self.title = progress.localizedDescription;63if (self.fileListCount != self.task.fileList.count) {64[self.tableView reloadData];65}66self.fileListCount = self.task.fileList.count;67});68} else {69[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];70}71}7273- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section74{75return self.task.fileList.count;76}7778- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath79{80UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];8182if (cell == nil) {83cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];84WFWorkflowProgressView *progressView = [[NSClassFromString(@"WFWorkflowProgressView") alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];85progressView.resolvedTintColor = self.view.tintColor;86progressView.stopSize = 0;87cell.accessoryView = progressView;88}8990// Unset the last cell displaying the progress91NSProgress *lastProgress = objc_getAssociatedObject(cell, @"progress");92if (lastProgress) {93objc_setAssociatedObject(lastProgress, @"cell", nil, OBJC_ASSOCIATION_ASSIGN);94@try {95[lastProgress removeObserver:self forKeyPath:@"fractionCompleted"];96} @catch(id anException) {}97}9899NSProgress *progress = self.task.progressList[indexPath.row];100objc_setAssociatedObject(cell, @"progress", progress, OBJC_ASSOCIATION_ASSIGN);101objc_setAssociatedObject(progress, @"cell", cell, OBJC_ASSOCIATION_RETAIN_NONATOMIC);102[progress addObserver:self103forKeyPath:@"fractionCompleted"104options:NSKeyValueObservingOptionInitial105context:CellProgressObserverContext];106107WFWorkflowProgressView *progressView = (id)cell.accessoryView;108if (lastProgress.finished) {109[progressView reset];110}111progressView.fractionCompleted = progress.fractionCompleted;112[progressView transitionCompletedLayerToVisible:progress.finished animated:NO haptic:NO];113[progressView transitionRunningLayerToVisible:!progress.finished animated:NO];114115cell.textLabel.text = self.task.fileList[indexPath.row];116cell.detailTextLabel.text = progress.localizedAdditionalDescription;117return cell;118}119120@end121122123