Skip to content

Commit

Permalink
fix: current and disable reset in ios for now
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Nov 25, 2021
1 parent d618887 commit 89f22e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
public class CapacitorUpdater {
String TAG = "CapacitorUpdater";
private Context context;
private String lastPathHot = "";
private String basePathHot = "versions";
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
Expand Down Expand Up @@ -183,17 +182,19 @@ public Boolean set(String version) {
File destIndex = new File(destHot.getPath() + "/index.html");
Log.i(TAG, "set File : " + destHot.getPath());
if (destHot.exists() && destIndex.exists()) {
lastPathHot = destHot.getPath();
String lastPathHot = editor.getString("lastPathHot", "");
editor.putString("serverBasePath", lastPathHot);
editor.commit();
return true;
}
return false;
}
public String getLastPathHot() {
return lastPathHot;
return editor.getString("lastPathHot", "");
}
public Void reset() {
lastPathHot = "";
editor.putString("lastPathHot", "");
editor.putString("serverBasePath", "");
editor.commit();
}
}
20 changes: 12 additions & 8 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Just

extension URL {
var isDirectory: Bool {
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
}
var exist: Bool {
return FileManager().fileExists(atPath: self.path)
Expand All @@ -27,6 +27,7 @@ extension URL {

// Persistent path /var/mobile/Containers/Data/Application/8C0C07BE-0FD3-4FD4-B7DF-90A88E12B8C3/Library/NoCloud/ionic_built_snapshots/FOLDER
// Hot Reload path /var/mobile/Containers/Data/Application/8C0C07BE-0FD3-4FD4-B7DF-90A88E12B8C3/Documents/FOLDER
// Normal /private/var/containers/Bundle/Application/8C0C07BE-0FD3-4FD4-B7DF-90A88E12B8C3/App.app/public

private func prepareFolder(source: URL) {
if (!FileManager.default.fileExists(atPath: source.path)) {
Expand Down Expand Up @@ -100,7 +101,7 @@ extension URL {
saveDownloadedPersist(content: r.content, version: version)
return version
} else {
print("Error downloading zip file", r.error)
print("Error downloading zip file", r.error ?? "unknow")
}
return nil
}
Expand Down Expand Up @@ -135,20 +136,23 @@ extension URL {
let destHotPersist = libraryUrl.appendingPathComponent(basePathPersist).appendingPathComponent(version)
let indexPersist = destHot.appendingPathComponent("index.html")
if (destHot.isDirectory && destHotPersist.isDirectory && indexHot.exist && indexPersist.exist) {
lastPathHot = destHot.path
lastPathPersist = destHotPersist.path
UserDefaults.standard.set(destHot.path, forKey: "lastPathHot")
UserDefaults.standard.set(destHotPersist.path, forKey: "lastPathPersist")
return true
}
return false
}

@objc public func getLastPathHot() -> String {
return lastPathHot
return UserDefaults.standard.string(forKey: "lastPathHot") ?? ""
}

@objc public func getLastPathPersist() -> String {
return lastPathPersist
return UserDefaults.standard.string(forKey: "lastPathPersist") ?? ""
}

@objc public func reset() {
lastPathHot = ""
lastPathPersist = ""
UserDefaults.standard.set("", forKey: "lastPathHot")
UserDefaults.standard.set("", forKey: "lastPathPersist")
}
}
36 changes: 21 additions & 15 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import Capacitor
@objc(CapacitorUpdaterPlugin)
public class CapacitorUpdaterPlugin: CAPPlugin {
private let implementation = CapacitorUpdater()
private var lastPath = ""



@objc func download(_ call: CAPPluginCall) {
let url = URL(string: call.getString("url") ?? "")

Expand All @@ -27,6 +25,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
@objc func set(_ call: CAPPluginCall) {
let version = call.getString("version") ?? ""
let res = implementation.set(version: version)
let defaults = UserDefaults.standard

if (res) {
guard let bridge = self.bridge else { return call.reject("bridge missing") }
Expand All @@ -35,9 +34,11 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
let pathHot = implementation.getLastPathHot()
let pathPersist = implementation.getLastPathPersist()
if (pathHot != "" && pathPersist != "") {
vc.setServerBasePath(path: pathHot)
let defaults = UserDefaults.standard
if (defaults.string(forKey: "serverBasePathOriginal") == nil) {
defaults.set(vc.getServerBasePath(), forKey: "serverBasePathOriginal")
}
defaults.set(String(pathPersist.suffix(10)), forKey: "serverBasePath")
vc.setServerBasePath(path: pathHot)
return call.resolve()
} else {
return call.reject("cannot set " + version)
Expand Down Expand Up @@ -67,16 +68,21 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}

@objc func reset(_ call: CAPPluginCall) {
guard let bridge = self.bridge else { return call.reject("bridge missing") }

if let vc = bridge.viewController as? CAPBridgeViewController {
implementation.reset()
vc.setServerBasePath(path: "")
let defaults = UserDefaults.standard
defaults.set(String(""), forKey: "serverBasePath")
return call.resolve()
}
call.reject("Update failed, viewController missing")
// guard let bridge = self.bridge else { return call.reject("bridge missing") }
//
// if let vc = bridge.viewController as? CAPBridgeViewController {
// let defaults = UserDefaults.standard
// let serverBasePathOriginal = defaults.string(forKey: "serverBasePathOriginal") ?? ""
// print("RESET", serverBasePathOriginal, implementation.getLastPathHot())
// implementation.reset()
// vc.setServerBasePath(path: serverBasePathOriginal)
// defaults.set(serverBasePathOriginal, forKey: "serverBasePath")
// DispatchQueue.main.async {
// bridge.webView?.reload()
// }
// return call.resolve()
// }
call.reject("Reset failed, not implemented")
}

@objc func current(_ call: CAPPluginCall) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "capacitor-updater",
"version": "1.0.15",
"version": "1.0.16",
"license": "AGPL-3.0-only",
"description": "Download app update from url",
"main": "dist/plugin.cjs.js",
Expand Down

0 comments on commit 89f22e6

Please sign in to comment.