LiYang
Committed by GitHub

fix: Avoid changes in values unexpected (#64)

* fix: Avoid changes in values unexpected

* Do not check stable

* Attempt to fix integration test check

* Add version

* Remove extra pub get

Co-authored-by: 李阳 <yang.li@plaso.me>
Co-authored-by: creativecreatorormaybenot <creativecreatorormaybenot@gmail.com>
@@ -24,7 +24,8 @@ jobs: @@ -24,7 +24,8 @@ jobs:
24 os: 24 os:
25 - ubuntu-latest 25 - ubuntu-latest
26 channel: 26 channel:
27 - - 'stable' 27 + # Do not check stable until NNBD is on stable.
  28 + #- 'stable'
28 - 'beta' 29 - 'beta'
29 - 'dev' 30 - 'dev'
30 package: 31 package:
@@ -55,7 +56,8 @@ jobs: @@ -55,7 +56,8 @@ jobs:
55 device: 56 device:
56 - 'iPhone 11 Pro Max (14.0)' 57 - 'iPhone 11 Pro Max (14.0)'
57 channel: 58 channel:
58 - - 'stable' 59 + # Do not check stable until NNBD is on stable.
  60 + #- 'stable'
59 - 'beta' 61 - 'beta'
60 - 'dev' 62 - 'dev'
61 package: 63 package:
@@ -3,6 +3,7 @@ description: >-2 @@ -3,6 +3,7 @@ description: >-2
3 Example app demonstrating how to use the wakelock plugin. It also includes integration tests for 3 Example app demonstrating how to use the wakelock plugin. It also includes integration tests for
4 testing the plugin in an integrated example. 4 testing the plugin in an integrated example.
5 publish_to: 'none' 5 publish_to: 'none'
  6 +version: 1.0.0+1
6 7
7 environment: 8 environment:
8 sdk: '>=2.12.0-0 <3.0.0' 9 sdk: '>=2.12.0-0 <3.0.0'
1 #import "WakelockPlugin.h" 1 #import "WakelockPlugin.h"
2 #import "messages.h" 2 #import "messages.h"
3 3
  4 +static void * mKeyPathObserverContextApplicationIsIdleTimerDisabled = &mKeyPathObserverContextApplicationIsIdleTimerDisabled;
  5 +
4 @interface WakelockPlugin () <FLTWakelockApi> 6 @interface WakelockPlugin () <FLTWakelockApi>
  7 +
  8 +@property (nonatomic, assign) BOOL enable;
  9 +
5 @end 10 @end
6 11
7 @implementation WakelockPlugin 12 @implementation WakelockPlugin
8 + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { 13 + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
9 WakelockPlugin* instance = [[WakelockPlugin alloc] init]; 14 WakelockPlugin* instance = [[WakelockPlugin alloc] init];
10 FLTWakelockApiSetup(registrar.messenger, instance); 15 FLTWakelockApiSetup(registrar.messenger, instance);
  16 +
  17 + [UIApplication.sharedApplication addObserver:instance forKeyPath:@"idleTimerDisabled" options:NSKeyValueObservingOptionNew context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
11 } 18 }
12 19
13 - (void)toggle:(FLTToggleMessage*)input error:(FlutterError**)error { 20 - (void)toggle:(FLTToggleMessage*)input error:(FlutterError**)error {
14 NSNumber *enable = input.enable; 21 NSNumber *enable = input.enable;
  22 + self.enable = enable.boolValue;
15 NSNumber *enabled = [NSNumber numberWithBool:[[UIApplication sharedApplication] isIdleTimerDisabled]]; 23 NSNumber *enabled = [NSNumber numberWithBool:[[UIApplication sharedApplication] isIdleTimerDisabled]];
16 24
17 if (![enable isEqualToNumber:enabled]) { 25 if (![enable isEqualToNumber:enabled]) {
@@ -28,4 +36,18 @@ @@ -28,4 +36,18 @@
28 return result; 36 return result;
29 } 37 }
30 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 + }
  47 +}
  48 +
  49 +- (void)dealloc {
  50 + [UIApplication.sharedApplication removeObserver:self forKeyPath:@"idleTimerDisabled" context:mKeyPathObserverContextApplicationIsIdleTimerDisabled];
  51 +}
  52 +
31 @end 53 @end