Skip to main content

Embed SDK SwfitUI

//
// ARMODUtility.swift
// ARMODStore
//
// Created by Phantomsxr.com on 2021/5/2.
//

import Foundation


class ARMODUtility {
public static var armod = ARMOD()

static func getStatusBarHeight() -> CGFloat {
var heightToReturn: CGFloat = 0.0
for window in UIApplication.shared.windows {
if let height = window.windowScene?.statusBarManager?.statusBarFrame.height, height > heightToReturn {
heightToReturn = height
}
}
print(heightToReturn)
return heightToReturn
}

static func initARMOD(){

let configJson = convertPlistToJson(plistName: "AppConfig")
armod.registerAPIforNativeCalls(ARMODCallback())
armod.initARMOD(configJson)
armod.setUIInterfaceOrientation(.portrait)
armod.loadAndShowView()

}
static func launchARById(id:String) {
initARMOD()
armod.fetchProject(id)
}

static func launchARScan() {
initARMOD()
armod.fetchProjectByImage()
}

static func convertPlistToJson(plistName:String) -> String {
if let url = Bundle.main.url(forResource:plistName, withExtension: "plist") {
do {
let data = try Data(contentsOf:url)
let dict = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String:Any]
let jsonData = try JSONSerialization.data(withJSONObject: dict , options: .prettyPrinted)
return String(data: jsonData, encoding: String.Encoding.utf8) ?? ""
} catch {
print(error)
}
}
return ""
}
}

//
// ARMODCallback.swift
// ARMOD Store
//
// Created by phantomsxr.com on 2021/2/15.
//

import Foundation
import UIKit
import SwiftUI

class ARMODCallback: UIResponder,NativeCallsProtocol {
func onARMODExit() {

}

func packageSizeMoreThanPresetSize(_ currentSize: Float, preset presetSize: Float) {
showAlert(title: "Tips", buttonTitle: "Continue", msg: String(format: "Need to download %.2f", currentSize),completionHandler: {
ARMODUtility.armod.continueToDownloadARExperience()})
}

fileprivate let loadingView = UIHostingController(rootView: LoadingView())
public static let arView = UIHostingController(rootView: ARView(statusBarHeight: ARMODUtility.getStatusBarHeight()))

func throwException(_ message: String!, errorCode code: Int32) {
print(String(format: "[ERROR]:%@,code:%d", message,code))
showAlert(title: "ERROR", buttonTitle: "Ok", msg: String(format: "[ERROR]:%@,code:%d", message,code),completionHandler: {
ARMODUtility.armod.unloadAndHideView()})
}


func addLoadingOverlay() {
let controller = ARMODUtility.armod.getController()
let view = controller?.view

view!.addSubview(loadingView.view)
controller?.addChild(loadingView)
loadingView.didMove(toParent: controller)
loadingView.view.backgroundColor = UIColor.clear
loadingView.view.translatesAutoresizingMaskIntoConstraints = false
loadingView.view.topAnchor.constraint(equalTo: view!.topAnchor).isActive = true
loadingView.view.bottomAnchor.constraint(equalTo: view!.bottomAnchor).isActive = true
loadingView.view.leftAnchor.constraint(equalTo: view!.leftAnchor).isActive = true
loadingView.view.rightAnchor.constraint(equalTo: view!.rightAnchor).isActive = true

view!.addSubview(ARMODCallback.arView.view)
controller?.addChild(ARMODCallback.arView)
ARMODCallback.arView.didMove(toParent: controller)
ARMODCallback.arView.view.backgroundColor = UIColor.clear
ARMODCallback.arView.view.translatesAutoresizingMaskIntoConstraints = false
ARMODCallback.arView.view.topAnchor.constraint(equalTo: view!.topAnchor).isActive = true
ARMODCallback.arView.view.bottomAnchor.constraint(equalTo: view!.bottomAnchor).isActive = true
ARMODCallback.arView.view.leftAnchor.constraint(equalTo: view!.leftAnchor).isActive = true
ARMODCallback.arView.view.rightAnchor.constraint(equalTo: view!.rightAnchor).isActive = true

}


func updateLoadingProgress(_ progress: Float) {
}

func removeLoadingOverlay() {
loadingView.removeFromParent()
loadingView.dismiss(animated: true, completion: nil)
loadingView.view.removeFromSuperview()
}

func deviceNotSupport() {
print("Device not support")
}

func sdkInitialized() {
print("SDK Initialized")

}

func openBuilt(inBrowser url: String!) {
print("")
}

func recognitionStart() {

}

func recognitionComplete() {
print("")
}

func tryAcquireInformation(_ opTag: String!) -> String! {
return ""
}


//https://stackoverflow.com/a/60562200
@objc private func showAlert(title:String,buttonTitle:String,msg:String,completionHandler:@escaping()->Void){
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: buttonTitle, style: .cancel) { (action) in
completionHandler()
}
alert.addAction(cancelAction)
DispatchQueue.main.async {
ARMODUtility.armod.getController()?.present(alert, animated: true, completion: nil)
}
}
}