123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //
- // Copyright © 2021 osy. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- import SwiftUI
- private var memoryAlertOnce = false
- @objc public extension VMDisplayViewController {
- var runInBackground: Bool {
- boolForSetting("RunInBackground")
- }
-
- var disableIdleTimer: Bool {
- boolForSetting("DisableIdleTimer")
- }
- }
- // MARK: - View Loading
- public extension VMDisplayViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- navigationController?.setNavigationBarHidden(true, animated: animated)
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- if let parent = parent {
- parent.setChildForHomeIndicatorAutoHidden(nil)
- parent.setChildViewControllerForPointerLock(nil)
- UIPress.pressResponderOverride = nil
- }
- }
-
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- if let parent = parent {
- parent.setChildForHomeIndicatorAutoHidden(self)
- parent.setChildViewControllerForPointerLock(self)
- UIPress.pressResponderOverride = self
- }
- if runInBackground {
- logger.info("Start location tracking to enable running in background")
- UTMLocationManager.sharedInstance().startUpdatingLocation()
- }
- delegate.displayDidAppear()
- }
- }
- @objc extension VMDisplayViewController {
- func enterSuspended(isBusy busy: Bool) {
- if !busy {
- UIApplication.shared.isIdleTimerDisabled = false
- }
- }
-
- func enterLive() {
- UIApplication.shared.isIdleTimerDisabled = disableIdleTimer
- }
-
- private func suspend() {
- // dummy function for selector
- }
-
- func terminateApplication() {
- DispatchQueue.main.async { [self] in
- // animate to home screen
- let app = UIApplication.shared
- app.performSelector(onMainThread: #selector(suspend), with: nil, waitUntilDone: true)
-
- // wait 2 seconds while app is going background
- Thread.sleep(forTimeInterval: 2)
-
- // exit app when app is in background
- exit(0);
- }
- }
- }
- // MARK: Toolbar hiding
- public extension VMDisplayViewController {
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- for touch in touches {
- if touch.type == .direct {
- delegate.displayDidAssertUserInteraction()
- break
- }
- }
- super.touchesBegan(touches, with: event)
- }
- }
- // MARK: Helper functions
- @objc public extension VMDisplayViewController {
- /*
- - (void)onDelay:(float)delay action:(void (^)(void))block {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*0.1), dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), block);
- }
- - (BOOL)boolForSetting:(NSString *)key {
- return [[NSUserDefaults standardUserDefaults] boolForKey:key];
- }
- - (NSInteger)integerForSetting:(NSString *)key {
- return [[NSUserDefaults standardUserDefaults] integerForKey:key];
- }
- */
- func onDelay(_ delay: Float, action: @escaping () -> Void) {
- DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + .milliseconds(100), execute: action)
- }
-
- func boolForSetting(_ key: String) -> Bool {
- return UserDefaults.standard.bool(forKey: key)
- }
-
- func integerForSetting(_ key: String) -> Int {
- return UserDefaults.standard.integer(forKey: key)
- }
- }
|