Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/PojavLauncher_iOS
Path: blob/main/Natives/DownloadProgressViewController.m
589 views
1
#import <dlfcn.h>
2
#import <objc/runtime.h>
3
#import "DownloadProgressViewController.h"
4
#import "WFWorkflowProgressView.h"
5
6
static void *CellProgressObserverContext = &CellProgressObserverContext;
7
static void *TotalProgressObserverContext = &TotalProgressObserverContext;
8
9
@interface DownloadProgressViewController ()
10
@property NSInteger fileListCount;
11
@end
12
13
@implementation DownloadProgressViewController
14
15
- (instancetype)initWithTask:(MinecraftResourceDownloadTask *)task {
16
self = [super init];
17
self.task = task;
18
return self;
19
}
20
21
- (void)loadView {
22
[super loadView];
23
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemClose target:self action:@selector(actionClose)];
24
self.tableView.allowsSelection = NO;
25
26
// Load WFWorkflowProgressView
27
dlopen("/System/Library/PrivateFrameworks/WorkflowUIServices.framework/WorkflowUIServices", RTLD_GLOBAL);
28
}
29
- (void)viewDidAppear:(BOOL)animated {
30
[super viewDidAppear:animated];
31
32
[self.task.textProgress addObserver:self
33
forKeyPath:@"fractionCompleted"
34
options:NSKeyValueObservingOptionInitial
35
context:TotalProgressObserverContext];
36
}
37
38
- (void)viewDidDisappear:(BOOL)animated {
39
[super viewDidDisappear:animated];
40
41
[self.task.textProgress removeObserver:self forKeyPath:@"fractionCompleted"];
42
}
43
44
- (void)actionClose {
45
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
46
}
47
48
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
49
NSProgress *progress = object;
50
if (context == CellProgressObserverContext) {
51
UITableViewCell *cell = objc_getAssociatedObject(progress, @"cell");
52
if (!cell) return;
53
dispatch_async(dispatch_get_main_queue(), ^{
54
cell.detailTextLabel.text = progress.localizedAdditionalDescription;
55
WFWorkflowProgressView *progressView = (id)cell.accessoryView;
56
progressView.fractionCompleted = progress.fractionCompleted;
57
if (progress.finished) {
58
[progressView transitionCompletedLayerToVisible:YES animated:YES haptic:NO];
59
}
60
});
61
} else if (context == TotalProgressObserverContext) {
62
dispatch_async(dispatch_get_main_queue(), ^{
63
self.title = progress.localizedDescription;
64
if (self.fileListCount != self.task.fileList.count) {
65
[self.tableView reloadData];
66
}
67
self.fileListCount = self.task.fileList.count;
68
});
69
} else {
70
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
71
}
72
}
73
74
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
75
{
76
return self.task.fileList.count;
77
}
78
79
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
80
{
81
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
82
83
if (cell == nil) {
84
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
85
WFWorkflowProgressView *progressView = [[NSClassFromString(@"WFWorkflowProgressView") alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
86
progressView.resolvedTintColor = self.view.tintColor;
87
progressView.stopSize = 0;
88
cell.accessoryView = progressView;
89
}
90
91
// Unset the last cell displaying the progress
92
NSProgress *lastProgress = objc_getAssociatedObject(cell, @"progress");
93
if (lastProgress) {
94
objc_setAssociatedObject(lastProgress, @"cell", nil, OBJC_ASSOCIATION_ASSIGN);
95
@try {
96
[lastProgress removeObserver:self forKeyPath:@"fractionCompleted"];
97
} @catch(id anException) {}
98
}
99
100
NSProgress *progress = self.task.progressList[indexPath.row];
101
objc_setAssociatedObject(cell, @"progress", progress, OBJC_ASSOCIATION_ASSIGN);
102
objc_setAssociatedObject(progress, @"cell", cell, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
103
[progress addObserver:self
104
forKeyPath:@"fractionCompleted"
105
options:NSKeyValueObservingOptionInitial
106
context:CellProgressObserverContext];
107
108
WFWorkflowProgressView *progressView = (id)cell.accessoryView;
109
if (lastProgress.finished) {
110
[progressView reset];
111
}
112
progressView.fractionCompleted = progress.fractionCompleted;
113
[progressView transitionCompletedLayerToVisible:progress.finished animated:NO haptic:NO];
114
[progressView transitionRunningLayerToVisible:!progress.finished animated:NO];
115
116
cell.textLabel.text = self.task.fileList[indexPath.row];
117
cell.detailTextLabel.text = progress.localizedAdditionalDescription;
118
return cell;
119
}
120
121
@end
122
123