VMDisplayViewController.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Copyright © 2021 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. private var memoryAlertOnce = false
  18. @objc public extension VMDisplayViewController {
  19. var runInBackground: Bool {
  20. boolForSetting("RunInBackground")
  21. }
  22. var disableIdleTimer: Bool {
  23. boolForSetting("DisableIdleTimer")
  24. }
  25. }
  26. // MARK: - View Loading
  27. public extension VMDisplayViewController {
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. }
  31. override func viewWillAppear(_ animated: Bool) {
  32. super.viewWillAppear(animated)
  33. navigationController?.setNavigationBarHidden(true, animated: animated)
  34. }
  35. override func viewWillDisappear(_ animated: Bool) {
  36. super.viewWillDisappear(animated)
  37. if let parent = parent {
  38. parent.setChildForHomeIndicatorAutoHidden(nil)
  39. parent.setChildViewControllerForPointerLock(nil)
  40. UIPress.pressResponderOverride = nil
  41. }
  42. }
  43. override func viewDidAppear(_ animated: Bool) {
  44. super.viewDidAppear(animated)
  45. if let parent = parent {
  46. parent.setChildForHomeIndicatorAutoHidden(self)
  47. parent.setChildViewControllerForPointerLock(self)
  48. UIPress.pressResponderOverride = self
  49. }
  50. #if !os(visionOS) && WITH_LOCATION_BACKGROUND
  51. if runInBackground {
  52. logger.info("Start location tracking to enable running in background")
  53. UTMLocationManager.sharedInstance().startUpdatingLocation()
  54. }
  55. #endif
  56. delegate.displayDidAppear()
  57. }
  58. }
  59. @objc extension VMDisplayViewController {
  60. func enterSuspended(isBusy busy: Bool) {
  61. if !busy {
  62. UIApplication.shared.isIdleTimerDisabled = false
  63. }
  64. }
  65. func enterLive() {
  66. UIApplication.shared.isIdleTimerDisabled = disableIdleTimer
  67. }
  68. }
  69. // MARK: Toolbar hiding
  70. public extension VMDisplayViewController {
  71. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  72. for touch in touches {
  73. if touch.type == .direct {
  74. delegate.displayDidAssertUserInteraction()
  75. break
  76. }
  77. }
  78. super.touchesBegan(touches, with: event)
  79. }
  80. }
  81. // MARK: Helper functions
  82. @objc public extension VMDisplayViewController {
  83. /*
  84. - (void)onDelay:(float)delay action:(void (^)(void))block {
  85. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*0.1), dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), block);
  86. }
  87. - (BOOL)boolForSetting:(NSString *)key {
  88. return [[NSUserDefaults standardUserDefaults] boolForKey:key];
  89. }
  90. - (NSInteger)integerForSetting:(NSString *)key {
  91. return [[NSUserDefaults standardUserDefaults] integerForKey:key];
  92. }
  93. */
  94. func onDelay(_ delay: Float, action: @escaping () -> Void) {
  95. DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + .milliseconds(100), execute: action)
  96. }
  97. func boolForSetting(_ key: String) -> Bool {
  98. return UserDefaults.standard.bool(forKey: key)
  99. }
  100. func integerForSetting(_ key: String) -> Int {
  101. return UserDefaults.standard.integer(forKey: key)
  102. }
  103. @discardableResult
  104. func debounce(_ delaySeconds: Int, context: Any? = nil, action: @escaping () -> Void) -> Any {
  105. if context != nil {
  106. let previous = context as! DispatchWorkItem
  107. previous.cancel()
  108. }
  109. let item = DispatchWorkItem(block: action)
  110. DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(delaySeconds), execute: item)
  111. return item
  112. }
  113. }