Sebastian Roth
Committed by GitHub

Prevent double-observers for the idleTimerDisabled value change (#108)

* Add a additional class to wrap observers for the sharedApplication.idleTimerDisabled

* CL & version

* Align wording with other entries

Co-authored-by: creativecreatorormaybenot <creativecreatorormaybenot@gmail.com>
  1 +## 0.5.1
  2 +
  3 +* Resolved a crash on iOS which was caused by 2 observers on idleTimerDisabled.
  4 +
1 ## 0.5.0+2 5 ## 0.5.0+2
2 6
3 * Fixed example app builds on macOS. 7 * Fixed example app builds on macOS.
@@ -21,8 +21,8 @@ EXTERNAL SOURCES: @@ -21,8 +21,8 @@ EXTERNAL SOURCES:
21 SPEC CHECKSUMS: 21 SPEC CHECKSUMS:
22 Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c 22 Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
23 integration_test: 5ed24a436eb7ec17b6a13046e9bf7ca4a404e59e 23 integration_test: 5ed24a436eb7ec17b6a13046e9bf7ca4a404e59e
24 - wakelock: bfc7955c418d0db797614075aabbc58a39ab5107 24 + wakelock: d0fc7c864128eac40eba1617cb5264d9c940b46f
25 25
26 PODFILE CHECKSUM: 8e679eca47255a8ca8067c4c67aab20e64cb974d 26 PODFILE CHECKSUM: 8e679eca47255a8ca8067c4c67aab20e64cb974d
27 27
28 -COCOAPODS: 1.10.0 28 +COCOAPODS: 1.10.1
@@ -2,6 +2,6 @@ @@ -2,6 +2,6 @@
2 <Workspace 2 <Workspace
3 version = "1.0"> 3 version = "1.0">
4 <FileRef 4 <FileRef
5 - location = "group:Runner.xcodeproj"> 5 + location = "self:">
6 </FileRef> 6 </FileRef>
7 </Workspace> 7 </Workspace>
  1 +#import <Foundation/Foundation.h>
  2 +
  3 +NS_ASSUME_NONNULL_BEGIN
  4 +
  5 +@interface IdleTimerDisabledObserver : NSObject
  6 +
  7 +@property BOOL enable;
  8 +
  9 ++ (IdleTimerDisabledObserver*)singleInstance;
  10 +
  11 +- (void) beginObserving;
  12 +
  13 +- (void) endObserving;
  14 +
  15 +@end
  16 +
  17 +NS_ASSUME_NONNULL_END
  1 +#import "IdleTimerDisabledObserver.h"
  2 +
  3 +@interface IdleTimerDisabledObserver() {
  4 + int numObservers;
  5 +}
  6 +
  7 +@end
  8 +
  9 +static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathObserverContextApplicationIsIdleTimerDisabled;
  10 +
  11 +@implementation IdleTimerDisabledObserver
  12 +
  13 ++ (IdleTimerDisabledObserver*)singleInstance {
  14 + static IdleTimerDisabledObserver *instance = nil;
  15 + static dispatch_once_t onceToken;
  16 + dispatch_once(&onceToken, ^{
  17 + instance = [[self alloc] init];
  18 + });
  19 + return instance;
  20 +}
  21 +
  22 +- (void)beginObserving {
  23 + if (numObservers == 0) {
  24 + [UIApplication.sharedApplication addObserver:self
  25 + forKeyPath:@"idleTimerDisabled"
  26 + options:NSKeyValueObservingOptionNew
  27 + context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
  28 + }
  29 +
  30 + numObservers ++;
  31 +}
  32 +
  33 +- (void)endObserving {
  34 + numObservers --;
  35 +
  36 + if (numObservers == 0) {
  37 + [UIApplication.sharedApplication removeObserver:self forKeyPath:@"idleTimerDisabled" context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
  38 + }
  39 +}
  40 +
  41 +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  42 + if (context == mKeyPathObserverContextApplicationIsIdleTimerDisabled) {
  43 + if (UIApplication.sharedApplication.idleTimerDisabled != self.enable) {
  44 + UIApplication.sharedApplication.idleTimerDisabled = self.enable;
  45 + }
  46 + } else {
  47 + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  48 + }
  49 +}
  50 +
  51 +
  52 +@end
1 #import "WakelockPlugin.h" 1 #import "WakelockPlugin.h"
  2 +#import "IdleTimerDisabledObserver.h"
2 #import "messages.h" 3 #import "messages.h"
3 4
4 -static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathObserverContextApplicationIsIdleTimerDisabled;  
5 5
6 @interface WakelockPlugin () <FLTWakelockApi> 6 @interface WakelockPlugin () <FLTWakelockApi>
7 7
@@ -14,7 +14,7 @@ static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathO @@ -14,7 +14,7 @@ static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathO
14 WakelockPlugin* instance = [[WakelockPlugin alloc] init]; 14 WakelockPlugin* instance = [[WakelockPlugin alloc] init];
15 FLTWakelockApiSetup(registrar.messenger, instance); 15 FLTWakelockApiSetup(registrar.messenger, instance);
16 16
17 - [UIApplication.sharedApplication addObserver:instance forKeyPath:@"idleTimerDisabled" options:NSKeyValueObservingOptionNew context:mKeyPathObserverContextApplicationIsIdleTimerDisabled]; 17 + [[IdleTimerDisabledObserver singleInstance] beginObserving];
18 } 18 }
19 19
20 - (void)toggle:(FLTToggleMessage*)input error:(FlutterError**)error { 20 - (void)toggle:(FLTToggleMessage*)input error:(FlutterError**)error {
@@ -36,18 +36,14 @@ static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathO @@ -36,18 +36,14 @@ static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathO
36 return result; 36 return result;
37 } 37 }
38 38
39 -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {  
40 - if (context == mKeyPathObserverContextApplicationIsIdleTimerDisabled) {  
41 - if (UIApplication.sharedApplication.idleTimerDisabled != self.enable) {  
42 - UIApplication.sharedApplication.idleTimerDisabled = self.enable;  
43 - }  
44 - } else {  
45 - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];  
46 - } 39 +- (void)setEnable:(BOOL)enable {
  40 + [IdleTimerDisabledObserver singleInstance].enable = enable;
  41 +
  42 + _enable = enable;
47 } 43 }
48 44
49 - (void)dealloc { 45 - (void)dealloc {
50 - [UIApplication.sharedApplication removeObserver:self forKeyPath:@"idleTimerDisabled" context:mKeyPathObserverContextApplicationIsIdleTimerDisabled]; 46 + [[IdleTimerDisabledObserver singleInstance] endObserving];
51 } 47 }
52 48
53 @end 49 @end
@@ -2,7 +2,7 @@ name: wakelock @@ -2,7 +2,7 @@ name: wakelock
2 description: >-2 2 description: >-2
3 Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on 3 Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
4 Android, iOS, macOS, Windows, and web. 4 Android, iOS, macOS, Windows, and web.
5 -version: 0.5.0+2 5 +version: 0.5.1
6 homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock 6 homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock
7 7
8 environment: 8 environment: