Skip to content

Commit

Permalink
fix: implementation scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Oct 23, 2023
1 parent 7241799 commit d45beca
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public class CapacitorUpdater {
public Activity activity;
public String PLUGIN_VERSION = "";
public String versionBuild = "";
public int periodCheckDelay = 300000;
public String versionCode = "";
public String versionOs = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class CapacitorUpdaterPlugin extends Plugin {

private Integer appReadyTimeout = 10000;
private Integer counterActivityCreate = 0;
private Integer periodCheckDelay = 0;
private Boolean autoDeleteFailed = true;
private Boolean autoDeletePrevious = true;
private Boolean autoUpdate = false;
Expand Down Expand Up @@ -171,10 +172,13 @@ public void notifyListeners(final String id, final JSObject res) {
this.getConfig().getString("statsUrl", statsUrlDefault);
this.implementation.channelUrl =
this.getConfig().getString("channelUrl", channelUrlDefault);
int userValue = this.getConfig().getInt("periodCheckDelay", 600);
int userValue = this.getConfig().getInt("periodCheckDelay", 0);

this.implementation.periodCheckDelay =
(userValue == 0 ? 600 : userValue) * 1000;
if (userValue >= 0 && userValue <= 600) {
this.periodCheckDelay = 600 * 1000;
} else if (userValue > 600) {
this.periodCheckDelay = userValue * 1000;
}

this.implementation.documentsDir = this.getContext().getFilesDir();
this.implementation.prefs = this.prefs;
Expand Down Expand Up @@ -763,6 +767,9 @@ public void current(final PluginCall call) {
}

public void checkForUpdateAfterDelay() {
if (this.periodCheckDelay == 0 || !this._isAutoUpdateEnabled()) {
return;
}
final Timer timer = new Timer();
timer.scheduleAtFixedRate(
new TimerTask() {
Expand Down Expand Up @@ -796,7 +803,7 @@ public void run() {
}
},
0,
CapacitorUpdaterPlugin.this.implementation.periodCheckDelay
this.periodCheckDelay
);
}

Expand Down
14 changes: 11 additions & 3 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
private var autoDeletePrevious = false
private var backgroundWork: DispatchWorkItem?
private var taskRunning = false
private var periodCheckDelay = 300
private var periodCheckDelay = 0
let semaphoreReady = DispatchSemaphore(value: 0)

override public func load() {
Expand All @@ -55,8 +55,13 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
appReadyTimeout = getConfig().getInt("appReadyTimeout", 10000)
implementation.timeout = Double(getConfig().getInt("responseTimeout", 20))
resetWhenUpdate = getConfig().getBoolean("resetWhenUpdate", true)
let periodCheckDelayValue = getConfig().getInt("periodCheckDelay", 600)
periodCheckDelay = (periodCheckDelayValue == 0) ? 600 : periodCheckDelayValue
let periodCheckDelayValue = getConfig().getInt("periodCheckDelay", 0)
if (periodCheckDelayValue >= 0 && periodCheckDelayValue > 600) {
periodCheckDelay = 600
}
else {
periodCheckDelay = periodCheckDelayValue
}

implementation.privateKey = getConfig().getString("privateKey", self.defaultPrivateKey)!
implementation.notifyDownload = notifyDownload
Expand Down Expand Up @@ -727,6 +732,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}

@objc func checkForUpdateAfterDelay() {
if (periodCheckDelay == 0 || !self._isAutoUpdateEnabled()) {
return
}
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(periodCheckDelay), repeats: true) { _ in
let url = URL(string: self.updateUrl)!
let res = self.implementation.getLatest(url: url)
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@
"prepublishOnly": "npm run build"
},
"devDependencies": {
"@capacitor/android": "^5.0.3",
"@capacitor/cli": "^5.0.3",
"@capacitor/core": "^5.0.3",
"@capacitor/android": "^5.5.0",
"@capacitor/cli": "^5.5.0",
"@capacitor/core": "^5.5.0",
"@capacitor/docgen": "^0.2.1",
"@capacitor/ios": "^5.0.3",
"@capacitor/ios": "^5.5.0",
"@ionic/eslint-config": "^0.3.0",
"@ionic/prettier-config": "^4.0.0",
"@ionic/swiftlint-config": "^1.1.2",
"@types/node": "^20.2.3",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.41.0",
"eslint-plugin-import": "^2.27.5",
"@types/node": "^20.8.7",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"eslint": "^8.52.0",
"eslint-plugin-import": "^2.29.0",
"prettier": "^2.8.8",
"prettier-plugin-java": "^2.1.0",
"rimraf": "^5.0.1",
"rollup": "^3.23.0",
"rimraf": "^5.0.5",
"rollup": "^3.29.4",
"swiftlint": "^1.0.2",
"typescript": "^5.0.4"
},
Expand All @@ -90,4 +90,4 @@
"src": "android"
}
}
}
}

0 comments on commit d45beca

Please sign in to comment.