diff --git a/Classes/HistoryListTableView.m b/Classes/HistoryListTableView.m index 5617e7895..5c72fc1ef 100644 --- a/Classes/HistoryListTableView.m +++ b/Classes/HistoryListTableView.m @@ -127,6 +127,7 @@ const bctbx_list_t *logs = linphone_core_get_call_logs(LC); self.sections = [NSMutableDictionary dictionary]; + NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"group.belledonne-communications.linphone.widget"]; while (logs != NULL) { LinphoneCallLog *log = (LinphoneCallLog *)logs->data; if (!missedFilter || linphone_call_log_get_status(log) == LinphoneCallMissed) { @@ -158,7 +159,27 @@ [self computeSections]; [super loadData]; - + + NSMutableDictionary *dictShare = [NSMutableDictionary dictionary]; + for (id day in self.sections.allKeys) { + for (id log in self.sections[day]) { + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; +// [dict setObject:[NSDate dateWithTimeIntervalSince1970:linphone_call_log_get_start_date([log pointerValue])] +// forKey:@"date"]; + [dict setObject:[NSString stringWithUTF8String:linphone_call_log_get_call_id([log pointerValue])] + forKey:@"id"]; + [dict setObject:[NSString stringWithUTF8String:linphone_address_as_string(linphone_call_log_get_remote_address([log pointerValue]))] + forKey:@"address"]; + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; + + NSString *stringFromDate = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:linphone_call_log_get_start_date([log pointerValue])]]; + [dictShare setObject:dict + forKey:stringFromDate]; + } + } + [mySharedDefaults setObject:dictShare forKey:@"logs"]; + if (IPAD) { if (![self selectFirstRow]) { HistoryDetailsView *view = VIEW(HistoryDetailsView); diff --git a/latestCallsWidget/TodayViewController.h b/latestCallsWidget/TodayViewController.h index 8b6f8a269..dde942af2 100644 --- a/latestCallsWidget/TodayViewController.h +++ b/latestCallsWidget/TodayViewController.h @@ -9,6 +9,13 @@ @interface TodayViewController : UIViewController @property (strong, nonatomic) IBOutletCollection(UIStackView) NSArray *stackViews; +@property (strong, nonatomic) NSMutableArray *contactsToDisplay; +@property (strong, nonatomic) NSMutableDictionary *logs; +@property (strong, nonatomic) NSMutableDictionary *imgs; +@property (strong, nonatomic) NSMutableArray *sortedDates; +@property (strong, nonatomic) NSMutableArray *logIds; +@property (strong, nonatomic) NSNumber *nbImgs; +@property (strong) dispatch_semaphore_t sem; - (IBAction)firstButtonTapped; - (IBAction)secondButtonTapped; diff --git a/latestCallsWidget/TodayViewController.m b/latestCallsWidget/TodayViewController.m index b8e2d92d8..548764a36 100644 --- a/latestCallsWidget/TodayViewController.m +++ b/latestCallsWidget/TodayViewController.m @@ -7,6 +7,10 @@ #import "TodayViewController.h" #import +#import "linphone/linphonecore.h" +#ifdef __IPHONE_9_0 +#import +#endif @interface TodayViewController () @@ -14,9 +18,120 @@ @implementation TodayViewController +- (instancetype) init { + printf("BONJOUR\n"); + return [super init]; +} + +- (void)loadData { + _contactsToDisplay = [NSMutableArray array]; + 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 = @[ + CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey, + CNContactFamilyNameKey, CNContactGivenNameKey, CNContactNicknameKey, + 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; + [self.imgs setObject:contact.imageData forKey:contact.givenName]; + printf("Ajout de l'image de %s\n", contact.givenName.UTF8String); + } + } + }]; + 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); + _nbImgs = [NSNumber numberWithInteger:_imgs.count]; + 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"]; + if ([_contactsToDisplay containsObject:[NSString stringWithUTF8String:linphone_address_as_string_uri_only(adr)]]) + continue; + [_contactsToDisplay addObject:[NSString stringWithUTF8String:linphone_address_as_string_uri_only(adr)]]; + [_logIds addObject:logId]; + NSString *displayName = [NSString stringWithUTF8String:linphone_address_get_display_name(adr)]; + UIStackView *stack = _stackViews[i]; + UIButton *button = stack.subviews[1]; + // making rounded image + UIImageView *imageView = button.imageView; + imageView.layer.cornerRadius = imageView.frame.size.height / 2; + imageView.clipsToBounds = YES; + UILabel *name = stack.subviews[2]; + if ([self.imgs.allKeys containsObject:displayName]) { + [button setImage:[UIImage imageWithData:[_imgs objectForKey:displayName]] forState:UIControlStateNormal]; + printf("Affichage de l'image de %s\n", displayName.UTF8String); + } + printf("Fin de l'affichage pour %s\n", displayName.UTF8String); + [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. + NSLog(@"------ LA VUE A CHARGE ------"); + _sem = dispatch_semaphore_create(0); + _nbImgs = [NSNumber numberWithInt:0]; + [self fetchContactsInBackGroundThread]; + [self loadData]; + [self draw]; } - (void)didReceiveMemoryWarning { @@ -30,7 +145,15 @@ // If an error is encountered, use NCUpdateResultFailed // If there's no update required, use NCUpdateResultNoData // If there's an update, use NCUpdateResultNewData - + NSLog(@"valeurs dans dict contacts : %i", (int)_imgs.count); + NSLog(@"valeurs de nbImgs : %i", (int)_nbImgs.integerValue); + /*if (_nbImgs.integerValue != _imgs.count) { + [self draw]; + completionHandler(NCUpdateResultNoData); + } else { + [self draw]; + completionHandler(NCUpdateResultNoData); + }*/ completionHandler(NCUpdateResultNewData); } @@ -44,18 +167,18 @@ } - (IBAction)firstButtonTapped { - [self launchOnHistoryDetailsWithId:@""]; + [self launchOnHistoryDetailsWithId:_logIds[0]]; } - (IBAction)secondButtonTapped { - [self launchOnHistoryDetailsWithId:@""]; + [self launchOnHistoryDetailsWithId:_logIds[1]]; } - (IBAction)thirdButtonTapped { - [self launchOnHistoryDetailsWithId:@""]; + [self launchOnHistoryDetailsWithId:_logIds[2]]; } - (IBAction)fourthButtonTapped { - [self launchOnHistoryDetailsWithId:@""]; + [self launchOnHistoryDetailsWithId:_logIds[3]]; } @end diff --git a/linphone.entitlements b/linphone.entitlements index 0a6451016..371282fab 100644 --- a/linphone.entitlements +++ b/linphone.entitlements @@ -7,6 +7,7 @@ com.apple.security.application-groups group.belledonne-communications.linphone.widget + group.belledonne-communications.linphone