Skip to content

Commit 2e55e77

Browse files
committed
Warn user before clearing database if unsaved observations
1 parent ebe271d commit 2e55e77

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

Mage/LoginTableViewController.m

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "OAuthViewController.h"
2626
#import "OAuthAuthentication.h"
2727
#import "UINextField.h"
28+
#import "MageOfflineObservationManager.h"
2829

2930
@interface LoginTableViewController ()
3031
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loginIndicator;
@@ -154,10 +155,33 @@ - (void) startLogin {
154155
- (void) verifyLogin {
155156
if (!self.allowLogin) return;
156157

157-
if (self.server.reachabilityManager.reachable && ([self userChanged])) {
158-
[MagicalRecord deleteAndSetupMageCoreDataStack];
158+
if (self.server.reachabilityManager.reachable && [self userChanged]) {
159+
if ([MageOfflineObservationManager offlineObservationCount] > 0) {
160+
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Loss of Unsaved Data"
161+
message:@"The previously logged in user has unsaved observations. Continuing with a new user will remove all previous data, including unsaved observations. Continue?"
162+
preferredStyle:UIAlertControllerStyleAlert];
163+
164+
__weak __typeof__(self) weakSelf = self;
165+
[alert addAction:[UIAlertAction actionWithTitle:@"Continue" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
166+
[MagicalRecord deleteAndSetupMageCoreDataStack];
167+
[weakSelf login];
168+
169+
}]];
170+
171+
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
172+
173+
[self presentViewController:alert animated:YES completion:nil];
174+
} else {
175+
[MagicalRecord deleteAndSetupMageCoreDataStack];
176+
[self login];
177+
}
178+
179+
} else {
180+
[self login];
159181
}
160-
182+
}
183+
184+
- (void) login {
161185
// setup authentication
162186
[self startLogin];
163187
NSUUID *deviceUUID = [DeviceUUID retrieveDeviceUUID];

0 commit comments

Comments
 (0)