From a885fb5deec09bbbde2c9037b287588b654f08cf Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 7 Mar 2018 10:58:33 -0500 Subject: [PATCH] Fix first reminder too early, offset bugs. Schedule first reminder date relative to now, handle overflow // FREEBIE --- Signal/src/ViewControllers/DebugUI/DebugUIMisc.m | 6 ++++++ SignalServiceKit/src/Util/OWS2FAManager.h | 3 +++ SignalServiceKit/src/Util/OWS2FAManager.m | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m index 6efc8a089..5eb39600b 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m @@ -116,6 +116,12 @@ NS_ASSUME_NONNULL_BEGIN completion:nil]; }]]; + [items addObject:[OWSTableItem itemWithTitle:@"Reset 2FA Repetition Interval" + actionBlock:^() { + [OWS2FAManager.sharedManager setDefaultRepetitionInterval]; + }]]; + + #ifdef DEBUG [items addObject:[OWSTableItem subPageItemWithText:@"Share UIImage" actionBlock:^(UIViewController *viewController) { diff --git a/SignalServiceKit/src/Util/OWS2FAManager.h b/SignalServiceKit/src/Util/OWS2FAManager.h index 3eba7b085..950fc568c 100644 --- a/SignalServiceKit/src/Util/OWS2FAManager.h +++ b/SignalServiceKit/src/Util/OWS2FAManager.h @@ -33,6 +33,9 @@ typedef void (^OWS2FAFailure)(NSError *error); - (void)updateRepetitionIntervalWithWasSuccessful:(BOOL)wasSuccessful; +// used for testing +- (void)setDefaultRepetitionInterval; + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Util/OWS2FAManager.m b/SignalServiceKit/src/Util/OWS2FAManager.m index 17d20c0bb..5bc3b7521 100644 --- a/SignalServiceKit/src/Util/OWS2FAManager.m +++ b/SignalServiceKit/src/Util/OWS2FAManager.m @@ -95,6 +95,9 @@ const NSUInteger kDaySecs = kHourSecs * 24; [self.dbConnection setObject:pin forKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection]; + // Schedule next reminder relative to now + self.lastSuccessfulReminderDate = [NSDate new]; + [[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange object:nil userInfo:nil]; @@ -232,14 +235,15 @@ const NSUInteger kDaySecs = kHourSecs * 24; NSUInteger oldIndex = [allIntervals indexOfObjectPassingTest:^BOOL(NSNumber *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) { - return oldInterval >= (NSTimeInterval)obj.doubleValue; + return oldInterval <= (NSTimeInterval)obj.doubleValue; }]; NSUInteger newIndex; if (wasSuccessful) { newIndex = oldIndex + 1; } else { - newIndex = oldIndex - 1; + // prevent overflow + newIndex = oldIndex <= 0 ? 0 : oldIndex - 1; } // clamp to be valid @@ -249,6 +253,13 @@ const NSUInteger kDaySecs = kHourSecs * 24; return newInterval; } +- (void)setDefaultRepetitionInterval +{ + [self.dbConnection setDouble:self.defaultRepetitionInterval + forKey:kOWS2FAManager_RepetitionInterval + inCollection:kOWS2FAManager_Collection]; +} + @end NS_ASSUME_NONNULL_END