linphone-iphone/latestCallsWidget/TodayViewController.m

173 lines
6.6 KiB
Objective-C

//
// TodayViewController.m
// latestCallsWidget
//
// Created by David Idmansour on 06/06/2018.
//
#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import "linphone/linphonecore.h"
#ifdef __IPHONE_9_0
#import <Contacts/Contacts.h>
#endif
@interface TodayViewController () <NCWidgetProviding>
@end
@implementation TodayViewController
- (void)loadData {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"group.belledonne-communications.linphone.widget"];
NSMutableArray *dates = [NSMutableArray array];
NSDictionary *logsTmp = [mySharedDefaults objectForKey:@"logs"];
_logs = [NSMutableDictionary dictionary];
for (NSString *dateStr in logsTmp.allKeys) {
NSDictionary *log = [logsTmp objectForKey:dateStr];
NSDate *date = [formatter dateFromString:dateStr];
[dates addObject:date];
[_logs setObject:log forKey:date];
}
_sortedDates = [[NSMutableArray alloc]
initWithArray:[dates sortedArrayUsingComparator:^NSComparisonResult(NSDate *d1, NSDate *d2) {
return [d2 compare:d1]; // reverse order
}]];
}
- (void) fetchContactsInBackGroundThread{
_imgs = [NSMutableDictionary dictionary];
CNContactStore *store = [[CNContactStore alloc] init];
CNEntityType entityType = CNEntityTypeContacts;
[store requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError *_Nullable error) {
BOOL success = FALSE;
if(granted){
//LOGD(@"CNContactStore authorization granted");
NSError *contactError;
CNContactStore* store = [[CNContactStore alloc] init];
[store containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers:@[ store.defaultContainerIdentifier]] error:&contactError];
NSArray *keysToFetch = @[
CNContactImageDataAvailableKey, CNContactInstantMessageAddressesKey,
CNContactIdentifierKey, CNContactImageDataKey
];
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
success = [store enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact *__nonnull contact, BOOL *__nonnull stop) {
if (contactError) {
NSLog(@"error fetching contacts %@",
contactError);
} else {
if (contact.imageDataAvailable) {
NSArray *addresses = contact.instantMessageAddresses;
NSString *address = @"";
for (CNLabeledValue *v in addresses) {
CNInstantMessageAddress *addressValue = v.value;
if ([addressValue.service isEqualToString:@"SIP"]) {
address = addressValue.username;
[self.imgs setObject:contact.imageData
forKey:address];
}
}
}
}
}];
dispatch_semaphore_signal(self.sem);
}
}];
}
- (void)draw {
_contactsToDisplay = [NSMutableArray array];
_logIds = [NSMutableArray array];
int i = 0, j = 0;
dispatch_semaphore_wait(_sem, DISPATCH_TIME_FOREVER);
while (i < _stackViews.count && j < _sortedDates.count) {
NSDate *date = _sortedDates[j++];
NSString *address = [[_logs objectForKey:date] objectForKey:@"address"];
LinphoneAddress *adr = linphone_address_new([address UTF8String]);
NSString *logId = [[_logs objectForKey:date] objectForKey:@"id"];
address = [[NSString stringWithUTF8String:linphone_address_as_string_uri_only(adr)] substringFromIndex:4];
if ([_contactsToDisplay containsObject:address])
continue;
[_contactsToDisplay addObject:address];
[_logIds addObject:logId];
NSString *displayName = [NSString stringWithUTF8String:linphone_address_get_display_name(adr)];
UIStackView *stack = _stackViews[i];
UIButton *button = stack.subviews[0];
// making rounded image
UIImageView *imageView = button.imageView;
imageView.layer.cornerRadius = imageView.frame.size.height / 2;
imageView.clipsToBounds = YES;
UILabel *name = stack.subviews[1];
if ([self.imgs.allKeys containsObject:address])
[button setImage:[UIImage imageWithData:[_imgs objectForKey:address]] forState:UIControlStateNormal];
[stack setAlpha:1];
button.enabled = YES;
[name setText:displayName];
i++;
}
while (i < _stackViews.count) {
UIStackView *stack = _stackViews[i];
UIButton *button = stack.subviews[1];
[stack setAlpha:0];
button.enabled = NO;
i++;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_sem = dispatch_semaphore_create(0);
[self fetchContactsInBackGroundThread];
[self loadData];
[self draw];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData
[self fetchContactsInBackGroundThread];
[self loadData];
[self draw];
completionHandler(NCUpdateResultNewData);
}
- (void)launchAppWithURL:(NSURL *) url {
[self.extensionContext openURL:url
completionHandler:nil];
}
- (void)launchOnHistoryDetailsWithId:(NSString *)logId {
[self launchAppWithURL:[NSURL URLWithString:[@"linphone-widget://call_log/show?" stringByAppendingString:logId]]];
}
- (IBAction)firstButtonTapped {
[self launchOnHistoryDetailsWithId:_logIds[0]];
}
- (IBAction)secondButtonTapped {
[self launchOnHistoryDetailsWithId:_logIds[1]];
}
- (IBAction)thirdButtonTapped {
[self launchOnHistoryDetailsWithId:_logIds[2]];
}
- (IBAction)fourthButtonTapped {
[self launchOnHistoryDetailsWithId:_logIds[3]];
}
@end