cleaned code and fixed a bug where images were lost on receiving a message

This commit is contained in:
David Idmansour 2018-06-20 11:09:16 +02:00
parent 1d27eabdd4
commit b081df9394
7 changed files with 86 additions and 132 deletions

View file

@ -143,14 +143,26 @@ static int sorted_history_comparison(LinphoneChatRoom *to_insert, LinphoneChatRo
LinphoneChatRoom *cr = sorted->data;
const LinphoneAddress *address = linphone_chat_room_get_peer_address(cr);
NSString *display;
[dict setObject:[[NSString stringWithUTF8String:linphone_address_as_string_uri_only(address)] substringFromIndex:4] forKey:@"address"];
[dict setObject:[[NSString stringWithUTF8String:linphone_address_as_string_uri_only(address)] substringFromIndex:4]
forKey:@"address"];
if (linphone_chat_room_get_conference_address(cr))
display = [NSString stringWithUTF8String:linphone_chat_room_get_subject(cr)];
else
else {
display = [NSString stringWithUTF8String:linphone_address_get_display_name(address)?:linphone_address_get_username(address)];
[dict setObject:display forKey:@"display"];
[dict setObject:[NSNumber numberWithBool:linphone_chat_room_get_conference_address(cr)] forKey:@"nbParticipants"];
if ([FastAddressBook imageForAddress:address])
[dict setObject:UIImageJPEGRepresentation([UIImage resizeImage:[FastAddressBook imageForAddress:address]
withMaxWidth:200
andMaxHeight:200],
1)
forKey:@"img"];
}
[dict setObject:display
forKey:@"display"];
[dict setObject:[NSNumber numberWithBool:linphone_chat_room_get_conference_address(cr)]
forKey:@"nbParticipants"];
[addresses addObject:dict];
if (addresses.count >= 4) //send no more data than needed
break;
sorted = sorted->next;
}

View file

@ -170,39 +170,59 @@
+ (void) saveDataToUserDefaults {
const bctbx_list_t *logs = linphone_core_get_call_logs(LC);
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"group.belledonne-communications.linphone.widget"];
NSMutableDictionary *dictShare = [NSMutableDictionary dictionary];
NSMutableDictionary *images = [NSMutableDictionary dictionary];
while (logs != NULL) {
NSMutableArray *logsShare = [NSMutableArray array];
NSMutableDictionary *tmpStoreDict = [NSMutableDictionary dictionary];
NSMutableArray *addedContacts = [NSMutableArray array];
while (logs) {
LinphoneCallLog *log = (LinphoneCallLog *)logs->data;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
const LinphoneAddress *address = linphone_call_log_get_remote_address(log);
// if contact is already to be display, skip
if ([addedContacts containsObject:[NSString stringWithUTF8String:linphone_address_as_string_uri_only(address)]]) {
logs = bctbx_list_next(logs);
continue;
}
// if null log id, skip
if (!linphone_call_log_get_call_id(log)) {
logs = bctbx_list_next(logs);
continue;
}
//FastAddressBook *fab = [LinphoneManager instance].fastAddressBook;
Contact * contact = [FastAddressBook getContactWithAddress:linphone_call_log_get_remote_address(log)];
if (contact && contact.avatar) {
UIImage *image = [UIImage resizeImage:contact.avatar withMaxWidth:200 andMaxHeight:200];
NSData *imageData = UIImageJPEGRepresentation(image, 0);
[images setObject:imageData
forKey:[[NSString stringWithUTF8String:linphone_address_as_string_uri_only(linphone_call_log_get_remote_address(log))] substringFromIndex:4]];
NSLog(@"bjr%@", [[NSString stringWithUTF8String:linphone_address_as_string_uri_only(linphone_call_log_get_remote_address(log))] substringFromIndex:4]);
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSString stringWithUTF8String:linphone_call_log_get_call_id(log)]
forKey:@"id"];
[dict setObject:[NSString stringWithUTF8String:linphone_address_as_string(linphone_call_log_get_remote_address(log))]
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)]];
[dictShare setObject:dict
forKey:stringFromDate];
[dict setObject:[NSString stringWithUTF8String:linphone_address_get_display_name(address)?:linphone_address_get_username(address)]
forKey:@"display"];
UIImage *avatar = [FastAddressBook imageForAddress:address];
if (avatar) {
UIImage *image = [UIImage resizeImage:avatar
withMaxWidth:200
andMaxHeight:200];
NSData *imageData = UIImageJPEGRepresentation(image, 1);
[dict setObject:imageData
forKey:@"img"];
}
[tmpStoreDict setObject:dict
forKey:[NSDate dateWithTimeIntervalSince1970:linphone_call_log_get_start_date(log)]];
[addedContacts addObject:[NSString stringWithUTF8String:linphone_address_as_string_uri_only(address)]];
logs = bctbx_list_next(logs);
}
[mySharedDefaults setObject:dictShare forKey:@"logs"];
[mySharedDefaults setObject:images forKey:@"imageData"];
NSArray *sortedDates = [[NSMutableArray alloc]
initWithArray:[tmpStoreDict.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSDate *d1, NSDate *d2) {
return [d2 compare:d1];
}]];
// sort logs array on date
for (NSDate *date in sortedDates) {
[logsShare addObject:[tmpStoreDict objectForKey:date]];
if (logsShare.count >= 4) //send no more data than needed
break;
}
[mySharedDefaults setObject:logsShare forKey:@"logs"];
}
- (void)computeSections {

View file

@ -1243,6 +1243,7 @@ static void linphone_iphone_popup_password_request(LinphoneCore *lc, LinphoneAut
}];
}
[ChatsListTableView saveDataToUserDefaults];
[HistoryListTableView saveDataToUserDefaults];
}
static void linphone_iphone_message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *message) {

View file

@ -9,11 +9,9 @@
@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 *imgs;
@property (strong, nonatomic) NSMutableArray *logIds;
@property (strong, nonatomic) NSMutableArray *displayNames;
- (IBAction)firstButtonTapped;
- (IBAction)secondButtonTapped;

View file

@ -7,10 +7,6 @@
#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import "linphone/linphonecore.h"
#ifdef __IPHONE_9_0
#import <Contacts/Contacts.h>
#endif
@interface TodayViewController () <NCWidgetProviding>
@ -19,97 +15,35 @@
@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];
[_imgs removeAllObjects];
_imgs = nil;
_imgs = [NSMutableDictionary dictionaryWithDictionary:[mySharedDefaults objectForKey:@"imageData"]];
NSDictionary *logsTmp = [mySharedDefaults objectForKey:@"logs"];
[_logs removeAllObjects];
for (NSString *dateStr in logsTmp.allKeys) {
NSDictionary *log = [logsTmp objectForKey:dateStr];
NSDate *date = [formatter dateFromString:dateStr];
[dates addObject:date];
[_logs setObject:log forKey:date];
[_logIds removeAllObjects];
[_displayNames removeAllObjects];
NSMutableArray *logs = [NSMutableArray arrayWithArray:[mySharedDefaults objectForKey:@"logs"]];
for (NSDictionary *dict in logs) {
[_logIds addObject:[dict objectForKey:@"id"]];
[_displayNames addObject:[dict objectForKey:@"display"]];
[_imgs addObject:[dict objectForKey:@"img"]?:[NSNull null]];
}
[_sortedDates removeAllObjects];
_sortedDates = nil;
_sortedDates = [[NSMutableArray alloc]
initWithArray:[dates sortedArrayUsingComparator:^NSComparisonResult(NSDate *d1, NSDate *d2) {
return [d2 compare:d1]; // reverse order
}]];
}
- (UIImage *)resizeImage:(UIImage *)image
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float maxHeight = 200.0;
float maxWidth = 200.0;
float imgRatio = actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
float compressionQuality = 1;
if (actualHeight > maxHeight || actualWidth > maxWidth)
{
if(imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
} else if(imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
} else {
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();
return [UIImage imageWithData:imageData];
}
- (void)draw {
[_contactsToDisplay removeAllObjects];
[_logIds removeAllObjects];
int i = 0, j = 0;
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))?:linphone_address_get_username(adr)];
int i = 0;
for (i = 0 ; i < 4 && i < _logIds.count ; i++) {
UIStackView *stack = _stackViews[i];
UIButton *button = stack.subviews[0];
UILabel *name = stack.subviews[1];
if ([self.imgs.allKeys containsObject:address]) {
NSData *imgData = [_imgs objectForKey:address];
// UIImage *image = [UIImage imageWithData:imgData];
// image = [self resizeImage:image];
// NSData *data = UIImageJPEGRepresentation(image, 0);
[button setImage:[UIImage imageWithData:imgData] forState:UIControlStateNormal];
NSLog(@"Size of Image(bytes):%d", (int)[imgData length]);
if (_imgs[i] != [NSNull null]) {
[button setImage:[UIImage imageWithData:_imgs[i]] forState:UIControlStateNormal];
}
[stack setAlpha:1];
button.enabled = YES;
[name setText:displayName];
i++;
[name setText:_displayNames[i]];
}
while (i < _stackViews.count) {
while (i < 4) {
UIStackView *stack = _stackViews[i];
UIButton *button = stack.subviews[1];
UIButton *button = stack.subviews[0];
[stack setAlpha:0];
button.enabled = NO;
i++;
@ -127,19 +61,14 @@
imageView.clipsToBounds = YES;
}
_logIds = [NSMutableArray array];
_logs = [NSMutableDictionary dictionary];
_sortedDates = [NSMutableArray array];
_contactsToDisplay = [NSMutableArray array];
_imgs = [NSMutableDictionary dictionary];
_imgs = [NSMutableArray array];
_displayNames = [NSMutableArray array];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[_imgs removeAllObjects];
[_logs removeAllObjects];
[_sortedDates removeAllObjects];
[_contactsToDisplay removeAllObjects];
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {

View file

@ -9,11 +9,10 @@
@interface TodayViewController : UIViewController
@property (strong, nonatomic) IBOutletCollection(UIStackView) NSArray *stackViews;
@property (strong, nonatomic) NSMutableArray *chatrooms;
@property (strong, nonatomic) NSMutableArray *addresses;
@property (strong, nonatomic) NSMutableArray *displayNames;
@property (strong, nonatomic) NSMutableDictionary *imgs;
@property (strong, nonatomic) NSMutableArray *isConf;
@property (strong, nonatomic) NSMutableArray *imgs;
- (IBAction)firstButtonTapped;
- (IBAction)secondButtonTapped;

View file

@ -7,7 +7,6 @@
#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import "linphone/linphonecore.h"
@interface TodayViewController () <NCWidgetProviding>
@ -17,17 +16,16 @@
- (void)loadData {
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"group.belledonne-communications.linphone.widget"];
[_chatrooms removeAllObjects];
[_imgs removeAllObjects];
[_addresses removeAllObjects];
[_displayNames removeAllObjects];
[_isConf removeAllObjects];
_imgs = [NSMutableDictionary dictionaryWithDictionary:[mySharedDefaults objectForKey:@"imageData"]];
_chatrooms = [NSMutableArray arrayWithArray:[mySharedDefaults objectForKey:@"chatrooms"]];
for (NSDictionary *dict in _chatrooms) {
NSMutableArray *chatrooms = [NSMutableArray arrayWithArray:[mySharedDefaults objectForKey:@"chatrooms"]];
for (NSDictionary *dict in chatrooms) {
[_addresses addObject:[dict objectForKey:@"address"]];
[_displayNames addObject:[dict objectForKey:@"display"]];
[_isConf addObject:(NSNumber *)[dict objectForKey:@"nbParticipants"]];
[_imgs addObject:[dict objectForKey:@"img"]?:[NSNull null]];
}
}
@ -37,9 +35,8 @@
UIStackView *stack = _stackViews[i];
UIButton *button = stack.subviews[0];
UILabel *name = stack.subviews[1];
if ([self.imgs.allKeys containsObject:_addresses[i]]) {
NSData *imgData = [_imgs objectForKey:_addresses[i]];
[button setImage:[UIImage imageWithData:imgData] forState:UIControlStateNormal];
if (_imgs[i] != [NSNull null]) {
[button setImage:[UIImage imageWithData:_imgs[i]] forState:UIControlStateNormal];
} else if (((NSNumber *)_isConf[i]).boolValue) {
[button setImage:[UIImage imageNamed:@"chat_group_avatar.png"] forState:UIControlStateNormal];
}
@ -68,10 +65,9 @@
imageView.clipsToBounds = YES;
}
_chatrooms = [NSMutableArray array];
_addresses = [NSMutableArray array];
_displayNames = [NSMutableArray array];
_imgs = [NSMutableDictionary dictionary];
_imgs = [NSMutableArray array];
_isConf = [NSMutableArray array];
}
@ -79,7 +75,6 @@
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[_imgs removeAllObjects];
[_chatrooms removeAllObjects];
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {