-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordingsViewController.mm
422 lines (358 loc) · 14.5 KB
/
RecordingsViewController.mm
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import "AppDelegate.h"
#import "DropboxUploader.h"
#import "RecordingInfo.h"
#import "RecordingsViewController.h"
#import "UserSettings.h"
@interface RecordingsViewController ()
- (void)configureCell:(UITableViewCell*)cell withRecordingInfo:(RecordingInfo*)recordingInfo;
- (RecordingInfo*)nextToUpload;
@end
@implementation RecordingsViewController
#pragma mark -
#pragma mark View lifecycle
- (id)initWithCoder:(NSCoder*)decoder
{
LOG(@"RecordingsViewController.initWithCoder");
if (self = [super initWithCoder:decoder]) {
appDelegate = nil;
managedObjectModel = nil;
managedObjectContext = nil;
persistentStoreCoordinator = nil;
fetchedResultsController = nil;
activeRecording = nil;
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateFromSettings) userInfo:nil repeats:NO];
[self nextToUpload];
}
return self;
}
- (void)dealloc
{
[uploader release];
[super dealloc];
}
- (void)viewDidLoad {
LOG(@"RecordingsViewController.viewDidLoad");
appDelegate = static_cast<AppDelegate*>([[UIApplication sharedApplication] delegate]);
self.title = NSLocalizedString(@"Recordings", @"Recordings view title");
self.tableView.allowsSelection = NO;
[super viewDidLoad];
}
- (void)viewDidUnload
{
LOG(@"RecordingsViewController.viewDidUnload");
appDelegate = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
if ([self.tableView numberOfRowsInSection:0] > 0) {
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
else {
self.navigationItem.rightBarButtonItem = nil;
}
[super viewWillAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)updateFromSettings
{
//
// Only carry around a DropboxUploader if configured to use Dropbox.
//
DBSession* dropboxSession = [DBSession sharedSession];
if ([[NSUserDefaults standardUserDefaults] boolForKey:kSettingsCloudStorageEnableKey] == YES &&
[dropboxSession isLinked] == YES) {
if (uploader == nil) {
uploader = [[DropboxUploader createWithSession:dropboxSession] retain];
uploader.monitor = self;
}
}
else {
if (uploader != nil) {
[uploader release];
uploader = nil;
}
}
}
- (void)readyToUpload
{
RecordingInfo* recordingInfo = [self nextToUpload];
if (recordingInfo != nil) {
uploader.uploadingFile = recordingInfo;
}
}
- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
if (appDelegate != nil) {
[self.tableView beginUpdates];
}
}
- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath
forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
if (appDelegate == nil) return;
UITableView* tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
[self configureCell:[tableView cellForRowAtIndexPath:newIndexPath] withRecordingInfo:anObject];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
if (self.tableView.editing == NO) {
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] withRecordingInfo:anObject];
}
break;
}
}
- (void)controller:(NSFetchedResultsController*)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if (appDelegate == nil) return;
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller
{
if (appDelegate != nil) {
[self.tableView endUpdates];
}
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString* kCellIdentifier = @"RecordingInfoCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kCellIdentifier] autorelease];
}
RecordingInfo* recording = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self configureCell:cell withRecordingInfo:recording];
return cell;
}
- (void)configureCell:(UITableViewCell*)cell withRecordingInfo:(RecordingInfo*)recording
{
cell.textLabel.text = recording.name;
NSString* status;
if (recording.uploaded == YES) {
status = NSLocalizedString(@"uploaded", @"Status tag for uploaded files");
}
else if (recording.uploading == YES) {
status = NSLocalizedString(@"uploading", @"Status tag for uploading files");
}
else if (recording == activeRecording) {
status = NSLocalizedString(@"recording", @"Status tag for active recording file");
}
else if (recording.progress < 0.0) {
if (recording.progress == -1001.0) {
status = NSLocalizedString(@"missing", @"File to upload is missing");
}
else {
status = NSLocalizedString(@"failed", @"Failed to upload file");
}
}
else {
status = NSLocalizedString(@"not uploaded", @"Status tag for files not uploaded");
}
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", recording.size, status];
//
// If this RecordingInfo object is being uploaded, show an activity indicator.
//
if (recording.uploaded == NO && recording.uploading == YES) {
UIProgressView* accessoryView = (UIProgressView*)[cell accessoryView];
if (accessoryView == nil) {
accessoryView = [[[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault] autorelease];
CGRect bounds = accessoryView.bounds;
bounds.size.width = 100;
accessoryView.bounds = bounds;
[cell setAccessoryView:accessoryView];
}
[accessoryView setProgress:recording.progress];
}
else {
[cell setAccessoryView:nil];
}
}
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
RecordingInfo* recordingInfo = [fetchedResultsController objectAtIndexPath:indexPath];
LOG(@"deleting file '%@'", recordingInfo.filePath);
[appDelegate recordingDeleted:recordingInfo];
if (uploader != nil && uploader.uploadingFile == recordingInfo)
[uploader cancelUpload];
NSError* error;
if ([[NSFileManager defaultManager] removeItemAtPath:recordingInfo.filePath error:&error] == NO) {
LOG(@"failed to remove file at '%@' - %@, %@", recordingInfo.filePath, error, [error userInfo]);
}
[managedObjectContext deleteObject:recordingInfo];
if (![managedObjectContext save:&error]) {
// Update to handle the error appropriately.
LOG(@"Unresolved error %@, %@", error, [error userInfo]);
}
if ([tableView numberOfRowsInSection:0] == 0) {
[self setEditing:NO animated:YES];
self.navigationItem.rightBarButtonItem = nil;
}
}
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LOG(@"RecordingsViewController.didSelectRow: %d", [indexPath indexAtPosition:1]);
}
#pragma mark -
#pragma mark CoreData
- (NSManagedObjectModel*)managedObjectModel
{
if (managedObjectModel != nil) return managedObjectModel;
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;
}
- (NSPersistentStoreCoordinator*)persistentStoreCoordinator
{
if (persistentStoreCoordinator != nil) return persistentStoreCoordinator;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* storePath = [basePath stringByAppendingPathComponent: @"Recordings.sqlite"];
NSURL* storeUrl = [NSURL fileURLWithPath:storePath];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:self.managedObjectModel];
NSError* error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error]) {
// Update to handle the error appropriately.
LOG(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
return persistentStoreCoordinator;
}
- (NSManagedObjectContext*)managedObjectContext
{
if (managedObjectContext != nil) return managedObjectContext;
NSPersistentStoreCoordinator* coordinator = self.persistentStoreCoordinator;
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
- (NSFetchedResultsController*)fetchedResultsController
{
if (fetchedResultsController != nil) return fetchedResultsController;
NSFetchRequest*fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RecordingInfo"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor* nameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:nameDescriptor]];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:@"RecordingInfo"];
fetchedResultsController.delegate = self;
NSError* error;
if (![self.fetchedResultsController performFetch:&error]) {
LOG(@"unresolved error %@, %@", error, [error userInfo]);
}
return fetchedResultsController;
}
- (RecordingInfo*)startRecording
{
RecordingInfo* recording = [NSEntityDescription insertNewObjectForEntityForName:@"RecordingInfo"
inManagedObjectContext:self.managedObjectContext];
[recording initialize];
activeRecording = recording;
return recording;
}
- (void)stopRecording
{
if (activeRecording != nil) {
RecordingInfo* recording = activeRecording;
activeRecording = nil;
[recording finalizeSize];
[self saveContext];
if (uploader != nil && uploader.uploadingFile == nil) {
RecordingInfo* recording = [self nextToUpload];
uploader.uploadingFile = recording;
}
}
}
- (void)saveContext
{
NSError* error;
if (managedObjectContext != nil && [managedObjectContext hasChanges] && [managedObjectContext save:&error] != YES) {
LOG(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
- (RecordingInfo*)nextToUpload
{
NSArray* recordings = [self.fetchedResultsController fetchedObjects];
UInt32 count = [recordings count];
UInt32 pending = 0;
RecordingInfo* next = nil;
RecordingInfo* retry = nil;
for (int index = 0; index < count; ++index) {
RecordingInfo* recording = [recordings objectAtIndex:index];
if (recording.uploaded == NO && recording != activeRecording) {
++pending;
if (recording.progress >= 0.0) {
if (next == nil) {
LOG(@"nextToUpload: %@", recording.name);
recording.progress = 0.0;
next = recording;
}
}
else {
if (retry == nil) {
LOG(@"nextToUpload: retrying %@", recording.name);
retry = recording;
}
}
}
}
if (pending) {
tabItem.badgeValue = [NSString stringWithFormat:@"%ld", pending, nil];
}
else {
tabItem.badgeValue = nil;
}
if (next) return next;
return retry;
}
@end