1 // Copyright 2021-present 650 Industries. All rights reserved.
2 
3 import ABI49_0_0ExpoModulesCore
4 
5 public final class KeepAwakeModule: Module {
6   private var activeTags = Set<String>()
7 
definitionnull8   public func definition() -> ModuleDefinition {
9     Name("ExpoKeepAwake")
10 
11     AsyncFunction("activate") { (tag: String) -> Bool in
12       if self.activeTags.isEmpty {
13         setActivated(true)
14       }
15       self.activeTags.insert(tag)
16       return true
17     }
18 
19     AsyncFunction("deactivate") { (tag: String) -> Bool in
20       self.activeTags.remove(tag)
21       if self.activeTags.isEmpty {
22         setActivated(false)
23       }
24       return true
25     }
26 
27     AsyncFunction("isActivated") { () -> Bool in
28       return DispatchQueue.main.sync {
29         return UIApplication.shared.isIdleTimerDisabled
30       }
31     }
32 
33     OnAppEntersForeground {
34       if !self.activeTags.isEmpty {
35         setActivated(true)
36       }
37     }
38 
39     OnAppEntersBackground {
40       if !self.activeTags.isEmpty {
41         setActivated(false)
42       }
43     }
44   }
45 }
46 
setActivatednull47 private func setActivated(_ activated: Bool) {
48   DispatchQueue.main.async {
49     UIApplication.shared.isIdleTimerDisabled = activated
50   }
51 }
52