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>
## 0.5.1
* Resolved a crash on iOS which was caused by 2 observers on idleTimerDisabled.
## 0.5.0+2
* Fixed example app builds on macOS.
... ...
... ... @@ -21,8 +21,8 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
integration_test: 5ed24a436eb7ec17b6a13046e9bf7ca4a404e59e
wakelock: bfc7955c418d0db797614075aabbc58a39ab5107
wakelock: d0fc7c864128eac40eba1617cb5264d9c940b46f
PODFILE CHECKSUM: 8e679eca47255a8ca8067c4c67aab20e64cb974d
COCOAPODS: 1.10.0
COCOAPODS: 1.10.1
... ...
... ... @@ -2,6 +2,6 @@
<Workspace
version = "1.0">
<FileRef
location = "group:Runner.xcodeproj">
location = "self:">
</FileRef>
</Workspace>
... ...
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface IdleTimerDisabledObserver : NSObject
@property BOOL enable;
+ (IdleTimerDisabledObserver*)singleInstance;
- (void) beginObserving;
- (void) endObserving;
@end
NS_ASSUME_NONNULL_END
... ...
#import "IdleTimerDisabledObserver.h"
@interface IdleTimerDisabledObserver() {
int numObservers;
}
@end
static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathObserverContextApplicationIsIdleTimerDisabled;
@implementation IdleTimerDisabledObserver
+ (IdleTimerDisabledObserver*)singleInstance {
static IdleTimerDisabledObserver *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (void)beginObserving {
if (numObservers == 0) {
[UIApplication.sharedApplication addObserver:self
forKeyPath:@"idleTimerDisabled"
options:NSKeyValueObservingOptionNew
context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
}
numObservers ++;
}
- (void)endObserving {
numObservers --;
if (numObservers == 0) {
[UIApplication.sharedApplication removeObserver:self forKeyPath:@"idleTimerDisabled" context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == mKeyPathObserverContextApplicationIsIdleTimerDisabled) {
if (UIApplication.sharedApplication.idleTimerDisabled != self.enable) {
UIApplication.sharedApplication.idleTimerDisabled = self.enable;
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@end
... ...
#import "WakelockPlugin.h"
#import "IdleTimerDisabledObserver.h"
#import "messages.h"
static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathObserverContextApplicationIsIdleTimerDisabled;
@interface WakelockPlugin () <FLTWakelockApi>
... ... @@ -14,7 +14,7 @@ static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathO
WakelockPlugin* instance = [[WakelockPlugin alloc] init];
FLTWakelockApiSetup(registrar.messenger, instance);
[UIApplication.sharedApplication addObserver:instance forKeyPath:@"idleTimerDisabled" options:NSKeyValueObservingOptionNew context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
[[IdleTimerDisabledObserver singleInstance] beginObserving];
}
- (void)toggle:(FLTToggleMessage*)input error:(FlutterError**)error {
... ... @@ -36,18 +36,14 @@ static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathO
return result;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == mKeyPathObserverContextApplicationIsIdleTimerDisabled) {
if (UIApplication.sharedApplication.idleTimerDisabled != self.enable) {
UIApplication.sharedApplication.idleTimerDisabled = self.enable;
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)setEnable:(BOOL)enable {
[IdleTimerDisabledObserver singleInstance].enable = enable;
_enable = enable;
}
- (void)dealloc {
[UIApplication.sharedApplication removeObserver:self forKeyPath:@"idleTimerDisabled" context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
[[IdleTimerDisabledObserver singleInstance] endObserving];
}
@end
... ...
... ... @@ -2,7 +2,7 @@ name: wakelock
description: >-2
Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
Android, iOS, macOS, Windows, and web.
version: 0.5.0+2
version: 0.5.1
homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock
environment:
... ...