Skip to content

Commit

Permalink
Changed ITMViewController to have static variables instead of the cre…
Browse files Browse the repository at this point in the history
…ateApplication function. Added ITMSwiftUI.
  • Loading branch information
toddsouthenbentley committed Aug 23, 2021
1 parent 83833c4 commit 95f8a56
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Sources/ITwinMobile/ITMApplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class ITMApplication: NSObject, WKUIDelegate, WKNavigationDelegate {
private var reachabilityObserver: Any?
public static var logger = ITMLogger()

override public init() {
required public override init() {
webView = type(of: self).createEmptyWebView()
webViewLogger = type(of: self).createWebViewLogger(webView)
itmMessenger = type(of: self).createITMMessenger(webView)
Expand Down
30 changes: 30 additions & 0 deletions Sources/ITwinMobile/ITMSwiftUI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//---------------------------------------------------------------------------------------
//
// $Source: FieldiOS/App/Utils/ITMApplication.swift $
//
// $Copyright: (c) 2021 Bentley Systems, Incorporated. All rights reserved. $
//
//---------------------------------------------------------------------------------------

import SwiftUI

@available(iOS 13.0, *)
public struct ITMSwiftUIWebView: UIViewControllerRepresentable {
public func makeUIViewController(context: Context) -> ITMViewController {
return ITMViewController()
}

public func updateUIViewController(_ uiViewController: ITMViewController, context: Context) {
//intentionally doing nothing here
}
}

@available(iOS 13.0, *)
public struct ITMSwiftUIContentView: View {
// wihtout this empty init, things wouldn't compile
public init() {
}
public var body: some View {
ITMSwiftUIWebView()
}
}
46 changes: 29 additions & 17 deletions Sources/ITwinMobile/ITMViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,30 @@ import UIKit
import WebKit

open class ITMViewController: UIViewController {
let application: ITMApplication
public static var applicationType = ITMApplication.self
public static var autoLoadWebApplication = true
public static var delayedAutoLoad = false
public let application: ITMApplication
private var iTwinMobile: ITwinMobile?
private var loadedOnce = false
private var willEnterForegroundObserver: Any? = nil

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
application = type(of: self).createApplication()
application = ITMViewController.applicationType.init()
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

required public init?(coder: NSCoder) {
application = type(of: self).createApplication()
application = ITMViewController.applicationType.init()
super.init(coder: coder)
}

deinit {
if let willEnterForegroundObserver = willEnterForegroundObserver {
NotificationCenter.default.removeObserver(willEnterForegroundObserver)
}
}

open class func createApplication() -> ITMApplication {
return ITMApplication()
}

open override func viewWillAppear(_ animated: Bool) {
iTwinMobile = ITwinMobile(viewController: self, itmMessenger: application.itmMessenger)
super.viewWillAppear(animated)
Expand All @@ -46,19 +45,32 @@ open class ITMViewController: UIViewController {
super.viewWillDisappear(animated)
}

open override func loadView() {
let webView = application.webView
view = webView
}

public func loadWebApplication() {
if !self.loadedOnce {
self.application.loadBackend(true)
// Due to a bug in iModelJS, loadFrontend must be executed after the initial willEnterForegroundNotification.
Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in
self.application.loadFrontend();
}
self.loadedOnce = true
}
}

open override func viewDidLoad() {
willEnterForegroundObserver = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { _ in
if !self.loadedOnce {
self.application.loadBackend(true)
// Due to a bug in iModelJS, loadFrontend must be executed after the initial willEnterForegroundNotification.
Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in
self.application.loadFrontend();
if ITMViewController.autoLoadWebApplication {
if ITMViewController.delayedAutoLoad {
willEnterForegroundObserver = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { _ in
self.loadWebApplication()
}
self.loadedOnce = true
} else {
loadWebApplication()
}
}
super.viewDidLoad()
let webView = application.webView
view = webView
}
}

0 comments on commit 95f8a56

Please sign in to comment.