Skip to content

Commit

Permalink
[iOS] Improve loading time for load miniapp from bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
rleojoseph committed Oct 5, 2023
1 parent 9118a3a commit 1d82703
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension WKUserContentController {
delegate: MiniAppCallbackDelegate,
hostAppMessageDelegate: MiniAppMessageDelegate,
adsDisplayer: MiniAppAdDisplayer?,
secureStorageDelegate: MiniAppSecureStorageDelegate,
secureStorageDelegate: MiniAppSecureStorageDelegate?,
miniAppId: String,
miniAppTitle: String,
miniAppManageDelegate: MiniAppManageDelegate
Expand All @@ -25,7 +25,7 @@ extension WKUserContentController {
delegate: delegate,
hostAppMessageDelegate: hostAppMessageDelegate,
adsDisplayer: adsDisplayer,
secureStorageDelegate: secureStorageDelegate,
secureStorageDelegate: secureStorageDelegate ?? nil,
miniAppManageDelegate: miniAppManageDelegate,
miniAppId: miniAppId,
miniAppTitle: miniAppTitle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class MiniAppScriptMessageHandler: NSObject, WKScriptMessageHandler {
delegate: MiniAppCallbackDelegate,
hostAppMessageDelegate: MiniAppMessageDelegate,
adsDisplayer: MiniAppAdDisplayer?,
secureStorageDelegate: MiniAppSecureStorageDelegate,
secureStorageDelegate: MiniAppSecureStorageDelegate?,
miniAppManageDelegate: MiniAppManageDelegate?,
miniAppId: String,
miniAppTitle: String
Expand Down
1 change: 1 addition & 0 deletions Sources/Classes/core/Utilities/MiniAppSDKUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MiniAppSDKUtility {
do {
try FileManager.default.unzipItem(at: filePath, to: FileManager.getMiniAppVersionDirectory(with: miniAppId, and: versionId), skipCRC32: true)
cacheVerifier.storeHash(for: miniAppId, version: versionId)
completionHandler?(.success(true))
return
} catch let err {
MiniAppLogger.e("Error unzipping archive", err)
Expand Down
10 changes: 8 additions & 2 deletions Sources/Classes/core/View/MiniAppParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public extension MiniAppViewParameters {
let appId: String
let version: String?
let queryParams: String?
var fromBundle: Bool? = false

/**
Initializes default parameters for MiniAppView
Expand All @@ -35,13 +36,15 @@ public extension MiniAppViewParameters {
type: MiniAppType,
appId: String,
version: String? = nil,
queryParams: String? = nil
queryParams: String? = nil,
fromBundle: Bool? = false
) {
self.config = config
self.type = type
self.appId = appId
self.version = version
self.queryParams = queryParams
self.fromBundle = fromBundle
}
}

Expand Down Expand Up @@ -80,6 +83,7 @@ public extension MiniAppViewParameters {
let type: MiniAppType
let info: MiniAppInfo
let queryParams: String?
var fromBundle: Bool? = false

/**
Initializes url parameters for MiniAppView
Expand All @@ -94,12 +98,14 @@ public extension MiniAppViewParameters {
config: MiniAppConfig,
type: MiniAppType,
info: MiniAppInfo,
queryParams: String? = nil
queryParams: String? = nil,
fromBundle: Bool? = false
) {
self.config = config
self.type = type
self.info = info
self.queryParams = queryParams
self.fromBundle = fromBundle
}
}
}
23 changes: 17 additions & 6 deletions Sources/Classes/core/View/MiniAppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,23 @@ public class MiniAppView: UIView, MiniAppViewable {
switch params {
case let .default(params):
self.type = params.type
self.miniAppHandler = MiniAppViewHandler(
config: params.config,
appId: params.appId,
version: params.version,
queryParams: params.queryParams
)
if params.fromBundle ?? false {
self.miniAppHandler = MiniAppViewHandler(
config: params.config,
appId: params.appId,
version: params.version,
queryParams: params.queryParams,
fromBundle: params.fromBundle
)
} else {
self.miniAppHandler = MiniAppViewHandler(
config: params.config,
appId: params.appId,
version: params.version,
queryParams: params.queryParams
)
}

case let .url(urlParams):
self.type = urlParams.type
self.miniAppHandler = MiniAppViewHandler(
Expand Down
92 changes: 63 additions & 29 deletions Sources/Classes/core/View/MiniAppViewHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class MiniAppViewHandler: NSObject {
internal var miniAppDownloader: MiniAppDownloaderInterface
internal var miniAppStatus: MiniAppStatus
internal var manifestDownloader: ManifestDownloader
internal var miniAppInfoFetcher: MiniAppInfoFetcherInterface
internal var miniAppInfoFetcher: MiniAppInfoFetcherInterface?
internal var miniAppManifestStorage: MAManifestStorage
internal var metaDataDownloader: MetaDataDownloader
internal var metaDataDownloader: MetaDataDownloader?
internal var miniAppPermissionStorage: MiniAppPermissionsStorage
internal var secureStorage: MiniAppSecureStorage
internal var secureStorage: MiniAppSecureStorage?

internal var projectId: String?

Expand Down Expand Up @@ -94,6 +94,44 @@ class MiniAppViewHandler: NSObject {
super.init()
}

init(
config: MiniAppConfig,
appId: String,
version: String? = nil,
queryParams: String? = nil,
fromBundle: Bool? = false
) {
manifestDownloader = ManifestDownloader()
miniAppStatus = MiniAppStatus()
miniAppManifestStorage = MAManifestStorage()
metaDataDownloader = MetaDataDownloader()
miniAppPermissionStorage = MiniAppPermissionsStorage()

miniAppClient = MiniAppClient(
baseUrl: config.config?.baseUrl,
rasProjectId: config.config?.rasProjectId,
subscriptionKey: config.config?.subscriptionKey,
hostAppVersion: config.config?.hostAppVersion,
isPreviewMode: config.config?.isPreviewMode
)

adsDisplayer = config.adsDisplayer

miniAppDownloader = MiniAppDownloader(
apiClient: miniAppClient,
manifestDownloader: manifestDownloader,
status: miniAppStatus
)

self.appId = appId
self.version = version
self.queryParams = queryParams
self.messageDelegate = config.messageDelegate
self.navigationDelegate = config.navigationDelegate

super.init()
}

init(
config: MiniAppConfig,
url: URL,
Expand Down Expand Up @@ -159,7 +197,7 @@ class MiniAppViewHandler: NSObject {
UIViewController.attemptRotationToDeviceOrientation()
webView?.configuration.userContentController.removeMessageHandler()
NotificationCenter.default.removeObserver(self)
secureStorage.unloadStorage()
secureStorage?.unloadStorage()
}

required init?(coder: NSCoder) { return nil }
Expand Down Expand Up @@ -298,28 +336,24 @@ class MiniAppViewHandler: NSObject {
saveManifestInfoForBundle(miniAppManifest: miniAppManifest)
if isValidMiniAppInfo(versionId: versionId) {
if isMiniAppAvailable(versionId: versionId) {
if miniAppDownloader.isCacheSecure(appId: appId, versionId: versionId) {
DispatchQueue.main.async {
let newWebView = MiniAppWebView(
DispatchQueue.main.async {
let newWebView = MiniAppWebView(
miniAppId: self.appId,
versionId: versionId,
queryParams: self.queryParams
)
self.webView = newWebView
do {
try self.loadWebView(
webView: newWebView,
miniAppId: self.appId,
versionId: versionId,
queryParams: self.queryParams
)
self.webView = newWebView
do {
try self.loadWebView(
webView: newWebView,
miniAppId: self.appId,
versionId: versionId,
queryParams: self.queryParams
)
} catch {
completion(.failure(.unknownError(domain: "", code: 0, description: "Internal error")))
}
completion(.success(newWebView))
} catch {
completion(.failure(.unknownError(domain: "", code: 0, description: "Internal error")))
}
} else {
completion(.failure(.miniAppCorrupted))
completion(.success(newWebView))
}
} else {
completion(.failure(.miniAppNotFound))
Expand Down Expand Up @@ -406,7 +440,7 @@ extension MiniAppViewHandler {
miniAppVersion: String? = nil,
completionHandler: @escaping (Result<MiniAppInfo, MASDKError>) -> Void
) {
return miniAppInfoFetcher.getInfo(
miniAppInfoFetcher?.getInfo(
miniAppId: miniAppId,
miniAppVersion: miniAppVersion,
apiClient: self.miniAppClient,
Expand Down Expand Up @@ -529,7 +563,7 @@ extension MiniAppViewHandler {
if version.isEmpty {
return completionHandler(.failure(.invalidVersionId))
}
metaDataDownloader.getMiniAppMetaInfo(
metaDataDownloader?.getMiniAppMetaInfo(
miniAppId: appId,
miniAppVersion: version,
apiClient: self.miniAppClient,
Expand Down Expand Up @@ -707,7 +741,7 @@ extension MiniAppViewHandler: WKNavigationDelegate {

private func notifySecureStorageStatus() {
if shouldAutoLoadSecureStorage {
secureStorage.loadStorage { success in
secureStorage?.loadStorage { success in
if success {
MiniAppSecureStorage.sendLoadStorageReady(miniAppId: self.appId, miniAppVersion: self.version ?? "")
} else {
Expand Down Expand Up @@ -853,23 +887,23 @@ extension MiniAppViewHandler: MiniAppCallbackDelegate {
// MARK: - MiniAppSecureStorageDelegate
extension MiniAppViewHandler: MiniAppSecureStorageDelegate {
func get(key: String) throws -> String? {
return try secureStorage.get(key: key)
return try secureStorage?.get(key: key)
}

func set(dict: [String: String], completion: ((Result<Bool, MiniAppSecureStorageError>) -> Void)?) {
return secureStorage.set(dict: dict, completion: completion)
secureStorage?.set(dict: dict, completion: completion)
}

func remove(keys: [String], completion: ((Result<Bool, MiniAppSecureStorageError>) -> Void)?) {
return secureStorage.remove(keys: keys, completion: completion)
secureStorage?.remove(keys: keys, completion: completion)
}

func size() -> MiniAppSecureStorageSize {
return secureStorage.size()
return secureStorage?.size() ?? MiniAppSecureStorageSize(used: 0, max: 0)
}

func clearSecureStorage() throws {
try secureStorage.clearSecureStorage()
try secureStorage?.clearSecureStorage()
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Classes/ui/MiniAppSUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public struct MiniAppSUIView: UIViewRepresentable {
var miniAppManifest: MiniAppManifest?

public init(params: MiniAppViewParameters.DefaultParams, fromCache: Bool = false, handler: MiniAppSUIViewHandler, fromBundle: Bool = false, miniAppManifest: MiniAppManifest? = nil) {
self.params = .default(params)
var miniAppParams = params
miniAppParams.fromBundle = fromBundle
self.params = .default(miniAppParams)
self.fromCache = fromCache
self.handler = handler
self.fromBundle = fromBundle
Expand Down

0 comments on commit 1d82703

Please sign in to comment.