-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBackupsBrowserTableViewController.m
185 lines (137 loc) · 6.57 KB
/
BackupsBrowserTableViewController.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
//
// BackupsBrowserTableViewController.m
// Strongbox
//
// Created by Mark on 27/09/2019.
// Copyright © 2014-2021 Mark McGuill. All rights reserved.
//
#import "BackupsBrowserTableViewController.h"
#import "BackupsManager.h"
#import "Utils.h"
#import "Alerts.h"
#import "LocalDeviceStorageProvider.h"
#import "NSDate+Extensions.h"
#import "Serializator.h"
#import "DatabasePreferences.h"
@interface BackupsBrowserTableViewController ()
@property NSArray<BackupItem*>* items;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonShowAll;
@property BOOL showAll;
@end
@implementation BackupsBrowserTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableFooterView = UIView.new;
self.barButtonShowAll.enabled = self.metadata == nil;
self.barButtonShowAll.tintColor = self.metadata == nil ? nil : UIColor.clearColor;
[self internalRefresh];
}
- (void)refresh {
dispatch_async(dispatch_get_main_queue(), ^{
[self internalRefresh];
});
}
- (void)internalRefresh {
self.items = [BackupsManager.sharedInstance getAvailableBackups:self.metadata all:self.showAll];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"backupItemCell" forIndexPath:indexPath];
BackupItem* item = self.items[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"backups_backup_modified_title_fmt", @"Modified %@"), item.modDate.friendlyDateTimeString];
if ( !self.metadata ) {
NSString* recoveredNickname = [self tryDetermineNickName:item];
cell.textLabel.text = recoveredNickname;
}
cell.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"backups_backup_taken_at_subtitle_fmt", @"%@ (Backup taken %@)"), friendlyFileSizeString(item.fileSize.unsignedIntegerValue), item.backupCreatedDate.friendlyDateTimeString];
cell.imageView.image = [UIImage imageNamed:@"file"];
return cell;
}
- (NSString*)tryDetermineNickName:(BackupItem*)item {
NSArray *components = item.url.pathComponents;
if ( components && components.count > 1 ) {
NSString* secondLast = components[components.count - 2];
DatabasePreferences* metadata = [DatabasePreferences fromUuid:secondLast];
if ( metadata ) {
return metadata.nickName;
}
else {
return item.url.lastPathComponent;
}
}
else {
return @"Unknown";
}
}
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
BackupItem* item = self.items[indexPath.row];
UITableViewRowAction *removeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:NSLocalizedString(@"generic_remove", @"Remove")
handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[Alerts yesNo:self
title:NSLocalizedString(@"generic_are_you_sure", @"Are you sure?")
message:NSLocalizedString(@"backup_vc_are_you_sure_remove", @"Are you sure you want to remove this backup?")
action:^(BOOL response) {
if(response) {
[BackupsManager.sharedInstance deleteBackup:item];
[self refresh];
}
}];
}];
UITableViewRowAction *addLocalAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:NSLocalizedString(@"backups_add_local", @"Add as Local Database")
handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self createAsLocalDatabase:item];
}];
addLocalAction.backgroundColor = [UIColor systemTealColor];
return @[removeAction,
addLocalAction];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)createAsLocalDatabase:(BackupItem*)item {
NSError* error;
NSData* data = [NSData dataWithContentsOfURL:item.url options:kNilOptions error:&error];
if (!data) {
[Alerts error:self title:NSLocalizedString(@"generic_error", @"Error") error:error];
return;
}
NSDictionary* attr = [NSFileManager.defaultManager attributesOfItemAtPath:item.url.path error:&error];
if (!attr || error) {
[Alerts error:self title:NSLocalizedString(@"generic_error", @"Error") error:error];
return;
}
NSDate* modDate = attr.fileModificationDate;
NSString* origNickName = self.metadata ? self.metadata.nickName : [self tryDetermineNickName:item];
NSString* nickName = [NSString stringWithFormat:@"Restored Backup of %@", origNickName];
NSString* extension = [Serializator getLikelyFileExtension:data];
[LocalDeviceStorageProvider.sharedInstance create:nickName
fileName:[NSString stringWithFormat:@"%@.%@", origNickName, extension]
data:data
modDate:modDate
completion:^(DatabasePreferences * _Nonnull metadata, NSError * _Nonnull error) {
if(error || !metadata) {
[Alerts error:self title:NSLocalizedString(@"generic_error", @"Error") error:error];
return;
}
if ( ![metadata addWithDuplicateCheck:data initialCacheModDate:modDate error:&error] ) {
[Alerts error:self error:error];
}
else {
[Alerts info:self
title:NSLocalizedString(@"generic_done", @"Done")
message:NSLocalizedString(@"backup_vc_backup_added_to_databases", @"This backup has been added to your databases")
completion:^{
[self.navigationController popToRootViewControllerAnimated:YES];
}];
}
}];
}
- (IBAction)onButtonAll:(id)sender {
}
@end