Do not save changes when canceling contact updates

This commit is contained in:
REIS Benjamin 2016-10-21 10:38:26 +02:00
parent 04aa8fd62e
commit 44e9462dba
5 changed files with 114 additions and 5 deletions

View file

@ -30,6 +30,7 @@
}
@property(nonatomic, assign, setter=setContact:) Contact *contact;
@property(nonatomic) Contact *tmpContact;
@property(nonatomic, strong) IBOutlet ContactDetailsTableView *tableController;
@property(nonatomic, strong) IBOutlet UIToggleButton *editButton;
@property(nonatomic, strong) IBOutlet UIButton *backButton;

View file

@ -19,6 +19,7 @@
#import "ContactDetailsView.h"
#import "PhoneMainView.h"
#import "UIContactDetailsCell.h"
@implementation ContactDetailsView
@ -162,6 +163,12 @@
[_editButton setImage:[UIImage imageNamed:@"valid_disabled.png"]
forState:(UIControlStateDisabled | UIControlStateSelected)];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboards)];
[self.view addGestureRecognizer:tap];
}
- (void)viewWillAppear:(BOOL)animated {
@ -169,6 +176,7 @@
_editButton.hidden = ([ContactSelection getSelectionMode] != ContactSelectionModeEdit &&
[ContactSelection getSelectionMode] != ContactSelectionModeNone);
[_tableController.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];
self.tmpContact = NULL;
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(deviceOrientationDidChange:)
@ -193,6 +201,41 @@
- (void)viewWillDisappear:(BOOL)animated {
[_tableController.tableView removeObserver:self forKeyPath:@"contentSize"];
[super viewWillDisappear:animated];
if (self.tmpContact) {
_contact.firstName = _tmpContact.firstName.copy;
_contact.lastName = _tmpContact.lastName.copy;
while (_contact.sipAddresses.count > 0) {
[_contact removeSipAddressAtIndex:0];
}
NSInteger nbSipAd = 0;
while (_tmpContact.sipAddresses.count > nbSipAd) {
[_contact addSipAddress:_tmpContact.sipAddresses[nbSipAd]];
nbSipAd++;
}
while (_contact.phoneNumbers.count > 0) {
[_contact removePhoneNumberAtIndex:0];
}
NSInteger nbPhone = 0;
while (_tmpContact.phoneNumbers.count> nbPhone) {
[_contact addPhoneNumber:_tmpContact.phoneNumbers[nbPhone]];
nbPhone++;
}
while (_contact.emails.count > 0) {
[_contact removeEmailAtIndex:0];
}
NSInteger nbEmail = 0;
while (_tmpContact.emails.count> nbEmail) {
[_contact addEmail:_tmpContact.emails[nbEmail]];
nbEmail++;
}
self.tmpContact = NULL;
[self saveData];
}
}
#pragma mark - UICompositeViewDelegate Functions
@ -285,9 +328,41 @@ static UICompositeViewDescription *compositeDescription = nil;
#pragma mark - Action Functions
- (IBAction)onCancelClick:(id)event {
[self setEditing:FALSE];
[self resetData];
[self dismissKeyboards];
if (!_isAdding) {
_contact.firstName = _tmpContact.firstName.copy;
_contact.lastName = _tmpContact.lastName.copy;
while (_contact.sipAddresses.count > 0) {
[_contact removeSipAddressAtIndex:0];
}
NSInteger nbSipAd = 0;
while (_tmpContact.sipAddresses.count > nbSipAd) {
[_contact addSipAddress:_tmpContact.sipAddresses[nbSipAd]];
nbSipAd++;
}
while (_contact.phoneNumbers.count > 0) {
[_contact removePhoneNumberAtIndex:0];
}
NSInteger nbPhone = 0;
while (_tmpContact.phoneNumbers.count> nbPhone) {
[_contact addPhoneNumber:_tmpContact.phoneNumbers[nbPhone]];
nbPhone++;
}
while (_contact.emails.count > 0) {
[_contact removeEmailAtIndex:0];
}
NSInteger nbEmail = 0;
while (_tmpContact.emails.count> nbEmail) {
[_contact addEmail:_tmpContact.emails[nbEmail]];
nbEmail++;
}
[self saveData];
[self.tableController.tableView reloadData];
}
[self setEditing:FALSE];
if (IPAD) {
_emptyLabel.hidden = !_isAdding;
_avatarImage.hidden = !_emptyLabel.hidden;
@ -296,8 +371,14 @@ static UICompositeViewDescription *compositeDescription = nil;
} else {
if (_isAdding) {
[PhoneMainView.instance popCurrentView];
} else {
_avatarImage.hidden = FALSE;
_deleteButton.hidden = FALSE;
_editButton.hidden = FALSE;
}
}
self.tmpContact = NULL;
_isAdding = FALSE;
}
@ -315,7 +396,17 @@ static UICompositeViewDescription *compositeDescription = nil;
[self setEditing:FALSE];
[self saveData];
_isAdding = FALSE;
self.tmpContact = NULL;
_avatarImage.hidden = FALSE;
_deleteButton.hidden = FALSE;
_editButton.hidden = FALSE;
} else {
_tmpContact = [[Contact alloc] initWithPerson:ABPersonCreate()];
_tmpContact.firstName = _contact.firstName.copy;
_tmpContact.lastName = _contact.lastName.copy;
_tmpContact.sipAddresses = _contact.sipAddresses.copy;
_tmpContact.emails = _contact.emails.copy;
_tmpContact.phoneNumbers = _contact.phoneNumbers.copy;
[self setEditing:TRUE];
}
}
@ -327,7 +418,9 @@ static UICompositeViewDescription *compositeDescription = nil;
confirmMessage:nil
onCancelClick:nil
onConfirmationClick:^() {
[self setEditing:FALSE];
if (_tableController.isEditing) {
[self onCancelClick:sender];
}
[self removeContact];
}];
}
@ -339,6 +432,16 @@ static UICompositeViewDescription *compositeDescription = nil;
}
}
- (void)dismissKeyboards {
NSArray *cells = [self.tableController.tableView visibleCells];
for (UIContactDetailsCell *cell in cells) {
UIView * txt = cell.editTextfield;
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
#pragma mark - Image picker delegate
- (void)imagePickerDelegateImage:(UIImage *)image info:(NSDictionary *)info {

View file

@ -28,7 +28,6 @@
@private
OrderedDictionary *addressBookMap;
}
- (void)loadData;
- (void)loadSearchedData;

View file

@ -29,7 +29,6 @@
- (void)initContactsTableViewController {
addressBookMap = [[OrderedDictionary alloc] init];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(onAddressBookUpdate:)
name:kLinphoneAddressBookUpdate

View file

@ -117,4 +117,11 @@
forRowAtIndexPath:indexPath];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIView * txt = self.editTextfield;
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
@end