JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "RecordingsListTableView.h"
#import "UIRecordingCell.h"
#import "LinphoneManager.h"
#import "PhoneMainView.h"
#import "Utils.h"
 
@implementation RecordingsListTableView
 
#pragma mark - Lifecycle Functions
 
- (void)initRecordingsTableViewController {
    recordings = [NSMutableDictionary dictionary];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    writablePath = [paths objectAtIndex:0];
    writablePath = [writablePath stringByAppendingString:@"/"];
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (![self selectFirstRow]) {
        //TODO: Make first cell expand to show player
    }
    [self loadData];
}
 
- (id)init {
    self = [super init];
    if (self) {
        [self initRecordingsTableViewController];
    }
    return self;
}
 
- (id)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];
    if (self) {
        [self initRecordingsTableViewController];
    }
    return self;
}
 
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeAllRecordings];
}
 
- (void)removeAllRecordings {
    for (NSInteger j = 0; j < [self.tableView numberOfSections]; ++j) {
        for (NSInteger i = 0; i < [self.tableView numberOfRowsInSection:j]; ++i) {
            [[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]] setRecording:nil];
        }
    }
}
 
 
- (void)loadData {
    LOGI(@"====>>>> Load recording list - Start");
    
    recordings = [NSMutableDictionary dictionary];
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:writablePath error:NULL];
    for (NSString *file in directoryContent) {
        if (![file hasPrefix:@"recording_"]) {
            continue;
        }
        NSArray *parsedName = [LinphoneUtils parseRecordingName:file];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"EEEE, MMM d, yyyy"];
        if ([parsedName count] < 2) {
            LOGW(@"Can not parse this recoding file: %@", file);
            continue;
        }
        NSString *dayPretty = [dateFormat stringFromDate:[parsedName objectAtIndex:1]];
        NSMutableArray *recOfDay = [recordings objectForKey:dayPretty];
        if (recOfDay) {
            // Loop through the object until a later object, then insert it right before
            int i;
            for (i = 0; i < [recOfDay count]; ++i) {
                NSString *fileAtIndex = [recOfDay objectAtIndex:i];
                NSArray *parsedNameAtIndex = [LinphoneUtils parseRecordingName:fileAtIndex];
                if ([[parsedName objectAtIndex:1] compare:[parsedNameAtIndex objectAtIndex:1]] == NSOrderedDescending) {
                    break;
                }
            }
            [recOfDay insertObject:[writablePath stringByAppendingString:file] atIndex:i];
        } else {
            recOfDay = [NSMutableArray arrayWithObjects:[writablePath stringByAppendingString:file], nil];
            [recordings setObject:recOfDay forKey:dayPretty];
        }
    }
    
    
    LOGI(@"====>>>> Load recording list - End");
    [super loadData];
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow];
    if (selectedRow && [selectedRow compare:indexPath] == NSOrderedSame) {
        return 150;
    } else {
        return 40;
    }
}
 
#pragma mark - UITableViewDataSource Functions
 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return nil;
}
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [recordings count];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sortedKey = [self getSortedKeys];
    return [(NSArray *)[recordings objectForKey:[sortedKey objectAtIndex:section]] count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *kCellId = NSStringFromClass(UIRecordingCell.class);
    UIRecordingCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    if (cell == nil) {
        cell = [[UIRecordingCell alloc] initWithIdentifier:kCellId];
    }
    NSString *date = [[self getSortedKeys] objectAtIndex:[indexPath section]];
    NSMutableArray *subAr = [recordings objectForKey:date];
    NSString *recordingPath = subAr[indexPath.row];
    [cell setRecording:recordingPath];
    [super accessoryForCell:cell atPath:indexPath];
    //accessoryForCell set it to gray but we don't want it
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell updateFrame];
    cell.contentView.userInteractionEnabled = false;
    return cell;
}
 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, tableView.sectionHeaderHeight);
    UIView *tempView = [[UIView alloc] initWithFrame:frame];
    if (@available(iOS 13, *)) {
        tempView.backgroundColor = [UIColor systemBackgroundColor];
    } else {
        tempView.backgroundColor = [UIColor whiteColor];
    }
    
    UILabel *tempLabel = [[UILabel alloc] initWithFrame:frame];
    tempLabel.backgroundColor = [UIColor clearColor];
    tempLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"color_A.png"]];
    tempLabel.text = [[self getSortedKeys] objectAtIndex:section];
    tempLabel.textAlignment = NSTextAlignmentCenter;
    tempLabel.font = [UIFont boldSystemFontOfSize:17];
    tempLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [tempView addSubview:tempLabel];
    
    return tempView;
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    if (![self isEditing]) {
        [tableView beginUpdates];
        [(UIRecordingCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] updateFrame];
        [tableView endUpdates];
    }
}
 
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [NSNotificationCenter.defaultCenter removeObserver:self];
        [tableView beginUpdates];
        
        
        NSString *date = [[self getSortedKeys] objectAtIndex:[indexPath section]];
        NSMutableArray *subAr = [recordings objectForKey:date];
        NSString *recordingPath = subAr[indexPath.row];
        [subAr removeObjectAtIndex:indexPath.row];
        if (subAr.count == 0) {
            [recordings removeObjectForKey:date];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                     withRowAnimation:UITableViewRowAnimationFade];
        }
        
        UIRecordingCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
        [cell setRecording:NULL];
        
        remove([recordingPath cStringUsingEncoding:NSUTF8StringEncoding]);
        
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
        
        [self loadData];
    }
}
 
- (void)removeSelectionUsing:(void (^)(NSIndexPath *))remover {
    [super removeSelectionUsing:^(NSIndexPath *indexPath) {
        [NSNotificationCenter.defaultCenter removeObserver:self];
        
        NSString *date = [[self getSortedKeys] objectAtIndex:[indexPath section]];
        NSMutableArray *subAr = [recordings objectForKey:date];
        NSString *recordingPath = subAr[indexPath.row];
        [subAr removeObjectAtIndex:indexPath.row];
        if (subAr.count == 0) {
            [recordings removeObjectForKey:date];
        }
        UIRecordingCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
        [cell setRecording:NULL];
        remove([recordingPath cStringUsingEncoding:NSUTF8StringEncoding]);
    }];
}
 
- (void)setSelected:(NSString *)filepath {
    NSArray *parsedName = [LinphoneUtils parseRecordingName:filepath];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EEEE, MMM d, yyyy"];
    NSString *dayPretty = [dateFormat stringFromDate:[parsedName objectAtIndex:1]];
    NSUInteger section;
    NSArray *keys = [recordings allKeys];
    for (section = 0; section < [keys count]; ++section) {
        if ([dayPretty isEqualToString:(NSString *)[keys objectAtIndex:section]]) {
            break;
        }
    }
    NSUInteger row;
    NSArray *recs = [recordings objectForKey:dayPretty];
    for (row = 0; row < [recs count]; ++row) {
        if ([filepath isEqualToString:(NSString *)[recs objectAtIndex:row]]) {
            break;
        }
    }
    NSUInteger indexes[] = {section, row};
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathWithIndexes:indexes length:2] animated:TRUE scrollPosition:UITableViewScrollPositionNone];
}
 
#pragma mark - Utilities
 
- (NSArray *)getSortedKeys {
    return [[recordings allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *day2, NSString *day1){
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"EEEE, MMM d, yyyy"];
        NSDate *date1 = [dateFormat dateFromString:day1];
        NSDate *date2 = [dateFormat dateFromString:day2];
        return [date1 compare:date2];
    }];
}
 
 
@end