-
Notifications
You must be signed in to change notification settings - Fork 287
/
BHDownloadInlineButton.m
326 lines (263 loc) · 15.8 KB
/
BHDownloadInlineButton.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// BHDownloadInlineButton.m
// BHTwitter
//
// Created by BandarHelal on 09/04/2022.
//
#import "BHDownloadInlineButton.h"
#import "Colours/Colours.h"
#import "BHTBundle/BHTBundle.h"
@interface BHDownloadInlineButton () <BHDownloadDelegate>
@property (nonatomic, strong) JGProgressHUD *hud;
@end
@implementation BHDownloadInlineButton
static const NSString *KEY_HIT_TEST_EDGE_INSETS = @"HitTestEdgeInsets";
+ (CGSize)buttonImageSizeUsingViewModel:(id)arg1 options:(NSUInteger)arg2 overrideButtonSize:(CGSize)arg3 account:(id)arg4 {
return CGSizeZero;
}
- (void)statusDidUpdate:(id)arg1 options:(NSUInteger)arg2 displayTextOptions:(NSUInteger)arg3 animated:(BOOL)arg4 featureSwitches:(id)arg5 {
[self statusDidUpdate:arg1 options:arg2 displayTextOptions:arg3 animated:arg4];
}
- (void)statusDidUpdate:(id)arg1 options:(NSUInteger)arg2 displayTextOptions:(NSUInteger)arg3 animated:(BOOL)arg4 {
if ([self.delegate.delegate isKindOfClass:objc_getClass("T1SlideshowStatusView")]) {
[self setTintColor:UIColor.whiteColor];
} else if ([self.delegate.delegate isKindOfClass:objc_getClass("T1ImmersiveExploreCardView")]) {
[self setTintColor:UIColor.whiteColor];
} else if ([self.delegate.delegate isKindOfClass:objc_getClass("T1TwitterSwift.ImmersiveExploreCardViewHelper")]) {
[self setTintColor:UIColor.whiteColor];
} else {
[self setTintColor:[UIColor colorFromHexString:@"6D6E70"]];
}
}
- (instancetype)initWithOptions:(NSUInteger)arg1 overrideSize:(id)arg2 account:(id)arg3 {
self = [super initWithFrame:CGRectZero];
if (self != nil) {
[self setInlineActionType:131];
[self setTintColor:[UIColor colorFromHexString:@"6D6E70"]];
[self setImage:[UIImage systemImageNamed:@"arrow.down"] forState:UIControlStateNormal];
[self addTarget:self action:@selector(DownloadHandler:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (instancetype)initWithInlineActionType:(NSUInteger)arg1 options:(NSUInteger)arg2 overrideSize:(id)arg3 account:(id)arg4 {
self = [super initWithFrame:CGRectZero];
if (self != nil) {
[self setInlineActionType:arg1];
[self setTintColor:[UIColor colorFromHexString:@"6D6E70"]];
[self setImage:[UIImage systemImageNamed:@"arrow.down"] forState:UIControlStateNormal];
[self addTarget:self action:@selector(DownloadHandler:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (id)_t1_imageNamed:(id)arg1 fitSize:(CGSize)arg2 fillColor:(id)arg3 {
return nil;
}
- (void)DownloadHandler:(UIButton *)sender {
NSAttributedString *AttString = [[NSAttributedString alloc] initWithString:[[BHTBundle sharedBundle] localizedStringForKey:@"DOWNLOAD_MENU_TITLE"] attributes:@{
NSFontAttributeName: [[objc_getClass("TAEStandardFontGroup") sharedFontGroup] headline2BoldFont],
NSForegroundColorAttributeName: UIColor.labelColor
}];
TFNActiveTextItem *title = [[objc_getClass("TFNActiveTextItem") alloc] initWithTextModel:[[objc_getClass("TFNAttributedTextModel") alloc] initWithAttributedString:AttString] activeRanges:nil];
NSMutableArray *actions = [[NSMutableArray alloc] init];
[actions addObject:title];
NSMutableArray *innerActions = [[NSMutableArray alloc] init];
[innerActions addObject:title];
if ([self.delegate.delegate isKindOfClass:objc_getClass("T1SlideshowStatusView")]) {
T1SlideshowStatusView *selectedMedia = self.delegate.delegate;
for (TFSTwitterEntityMediaVideoVariant *video in selectedMedia.media.videoInfo.variants) {
if ([video.contentType isEqualToString:@"video/mp4"]) {
TFNActionItem *option = [objc_getClass("TFNActionItem") actionItemWithTitle:[BHTManager getVideoQuality:video.url] imageName:@"arrow_down_circle_stroke" action:^{
BHDownload *dwManager = [[BHDownload alloc] init];
[dwManager downloadFileWithURL:[NSURL URLWithString:video.url]];
[dwManager setDelegate:self];
if (![BHTManager DirectSave]) {
self.hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
self.hud.textLabel.text = [[BHTBundle sharedBundle] localizedStringForKey:@"PROGRESS_DOWNLOADING_STATUS_TITLE"];
[self.hud showInView:topMostController().view];
}
}];
[actions addObject:option];
}
if ([video.contentType isEqualToString:@"application/x-mpegURL"]) {
TFNActionItem *option = [objc_getClass("TFNActionItem") actionItemWithTitle:[[BHTBundle sharedBundle] localizedStringForKey:@"FFMPEG_DOWNLOAD_OPTION_TITLE"] imageName:@"arrow_down_circle_stroke" action:^{
self.hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
self.hud.textLabel.text = [[BHTBundle sharedBundle] localizedStringForKey:@"FETCHING_PROGRESS_TITLE"];
[self.hud showInView:topMostController().view];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
MediaInformation *mediaInfo = [BHTManager getM3U8Information:[NSURL URLWithString:video.url]];
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.hud dismiss];
TFNMenuSheetViewController *alert2 = [BHTManager newFFmpegDownloadSheet:mediaInfo downloadingURL:[NSURL URLWithString:video.url] progressView:self.hud];
[alert2 tfnPresentedCustomPresentFromViewController:topMostController() animated:YES completion:nil];
});
});
}];
[actions addObject:option];
}
}
} else {
if (self.delegate.viewModel.representedMediaEntities.count > 1) {
[self.delegate.viewModel.representedMediaEntities enumerateObjectsUsingBlock:^(TFSTwitterEntityMedia * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.mediaType == 2 || obj.mediaType == 3) {
TFNActionItem *option = [objc_getClass("TFNActionItem") actionItemWithTitle:[NSString stringWithFormat:@"Video %lu", (unsigned long)idx+1] imageName:@"arrow_down_circle_stroke" action:^{
for (TFSTwitterEntityMediaVideoVariant *video in obj.videoInfo.variants) {
if ([video.contentType isEqualToString:@"video/mp4"]) {
TFNActionItem *innerOption = [objc_getClass("TFNActionItem") actionItemWithTitle:[BHTManager getVideoQuality:video.url] imageName:@"arrow_down_circle_stroke" action:^{
BHDownload *dwManager = [[BHDownload alloc] init];
[dwManager downloadFileWithURL:[NSURL URLWithString:video.url]];
[dwManager setDelegate:self];
if (![BHTManager DirectSave]) {
self.hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
self.hud.textLabel.text = [[BHTBundle sharedBundle] localizedStringForKey:@"PROGRESS_DOWNLOADING_STATUS_TITLE"];
[self.hud showInView:topMostController().view];
}
}];
[innerActions addObject:innerOption];
}
if ([video.contentType isEqualToString:@"application/x-mpegURL"]) {
TFNActionItem *option = [objc_getClass("TFNActionItem") actionItemWithTitle:[[BHTBundle sharedBundle] localizedStringForKey:@"FFMPEG_DOWNLOAD_OPTION_TITLE"] imageName:@"arrow_down_circle_stroke" action:^{
self.hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
self.hud.textLabel.text = [[BHTBundle sharedBundle] localizedStringForKey:@"FETCHING_PROGRESS_TITLE"];
[self.hud showInView:topMostController().view];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
MediaInformation *mediaInfo = [BHTManager getM3U8Information:[NSURL URLWithString:video.url]];
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.hud dismiss];
TFNMenuSheetViewController *alert2 = [BHTManager newFFmpegDownloadSheet:mediaInfo downloadingURL:[NSURL URLWithString:video.url] progressView:self.hud];
[alert2 tfnPresentedCustomPresentFromViewController:topMostController() animated:YES completion:nil];
});
});
}];
[innerActions addObject:option];
}
}
TFNMenuSheetViewController *innerAlert = [[objc_getClass("TFNMenuSheetViewController") alloc] initWithActionItems:[NSArray arrayWithArray:innerActions]];
[innerAlert tfnPresentedCustomPresentFromViewController:topMostController() animated:YES completion:nil];
}];
[actions addObject:option];
}
}];
} else {
for (TFSTwitterEntityMediaVideoVariant *video in self.delegate.viewModel.representedMediaEntities.firstObject.videoInfo.variants) {
if ([video.contentType isEqualToString:@"video/mp4"]) {
TFNActionItem *option = [objc_getClass("TFNActionItem") actionItemWithTitle:[BHTManager getVideoQuality:video.url] imageName:@"arrow_down_circle_stroke" action:^{
BHDownload *dwManager = [[BHDownload alloc] init];
[dwManager downloadFileWithURL:[NSURL URLWithString:video.url]];
[dwManager setDelegate:self];
if (![BHTManager DirectSave]) {
self.hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
self.hud.textLabel.text = [[BHTBundle sharedBundle] localizedStringForKey:@"PROGRESS_DOWNLOADING_STATUS_TITLE"];
[self.hud showInView:topMostController().view];
}
}];
[actions addObject:option];
}
if ([video.contentType isEqualToString:@"application/x-mpegURL"]) {
TFNActionItem *option = [objc_getClass("TFNActionItem") actionItemWithTitle:[[BHTBundle sharedBundle] localizedStringForKey:@"FFMPEG_DOWNLOAD_OPTION_TITLE"] imageName:@"arrow_down_circle_stroke" action:^{
self.hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
self.hud.textLabel.text = [[BHTBundle sharedBundle] localizedStringForKey:@"FETCHING_PROGRESS_TITLE"];
[self.hud showInView:topMostController().view];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
MediaInformation *mediaInfo = [BHTManager getM3U8Information:[NSURL URLWithString:video.url]];
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.hud dismiss];
TFNMenuSheetViewController *alert2 = [BHTManager newFFmpegDownloadSheet:mediaInfo downloadingURL:[NSURL URLWithString:video.url] progressView:self.hud];
[alert2 tfnPresentedCustomPresentFromViewController:topMostController() animated:YES completion:nil];
});
});
}];
[actions addObject:option];
}
}
}
}
TFNMenuSheetViewController *alert = [[objc_getClass("TFNMenuSheetViewController") alloc] initWithActionItems:[NSArray arrayWithArray:actions]];
[alert tfnPresentedCustomPresentFromViewController:topMostController() animated:YES completion:nil];
}
- (void)setTouchInsets:(UIEdgeInsets)arg1 {
if ([self.delegate.delegate isKindOfClass:objc_getClass("T1StandardStatusInlineActionsViewAdapter")]) {
[self setImageEdgeInsets:arg1];
[self setHitTestEdgeInsets:arg1];
}
}
// https://stackoverflow.com/a/13067285
- (void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets {
NSValue *value = [NSValue value:&hitTestEdgeInsets withObjCType:@encode(UIEdgeInsets)];
objc_setAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIEdgeInsets)hitTestEdgeInsets {
NSValue *value = objc_getAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS);
if (value) {
UIEdgeInsets edgeInsets; [value getValue:&edgeInsets]; return edgeInsets;
} else {
return UIEdgeInsetsZero;
}
}
- (bool)enabled {
return true;
}
- (NSString *)actionSheetTitle {
return @"BHDownload";
}
- (NSUInteger)visibility {
return 1;
}
- (NSUInteger)alternateInlineActionType {
return 6;
}
- (NSUInteger)touchInsetPriority {
return 2;
}
- (double)extraWidth {
return 40;
}
- (CGFloat)trailingEdgeInset {
return 6;
}
- (bool)shouldShowCount {
return false;
}
- (NSUInteger)displayType {
return self->_displayType;
}
- (NSUInteger)inlineActionType {
return self->_inlineActionType;
}
- (T1StatusInlineActionsView *)delegate {
return self->_delegate;
}
- (id)buttonAnimator {
return self->_buttonAnimator;
}
- (id)viewModel {
return self->_viewModel;
}
- (void)downloadProgress:(float)progress {
self.hud.detailTextLabel.text = [BHTManager getDownloadingPersent:progress];
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) || !self.enabled || self.hidden) {
return [super pointInside:point withEvent:event];
}
CGRect relativeFrame = self.bounds;
CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.hitTestEdgeInsets);
return CGRectContainsPoint(hitFrame, point);
}
- (void)downloadDidFinish:(NSURL *)filePath Filename:(NSString *)fileName {
NSString *DocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *newFilePath = [[NSURL fileURLWithPath:DocPath] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", NSUUID.UUID.UUIDString]];
[manager moveItemAtURL:filePath toURL:newFilePath error:nil];
if (!([BHTManager DirectSave])) {
[self.hud dismiss];
[BHTManager showSaveVC:newFilePath];
} else {
[BHTManager save:newFilePath];
}
}
- (void)downloadDidFailureWithError:(NSError *)error {
if (error) {
[self.hud dismiss];
}
}
@end