Skip to content

Commit

Permalink
fix: delete self issue
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Aug 15, 2022
1 parent db17ac5 commit 30d10cd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,21 @@ public void reset() {
this.reset(false);
}

public void setSuccess(final BundleInfo bundle) {
public void setSuccess(final BundleInfo bundle, Boolean autoDeletePrevious) {
this.setBundleStatus(bundle.getId(), BundleStatus.SUCCESS);
final BundleInfo fallback = this.getFallbackBundle();
Log.d(CapacitorUpdater.TAG, "Fallback bundle is: " + fallback);
if(autoDeletePrevious && !fallback.isBuiltin()) {
Log.i(CapacitorUpdater.TAG, "Version successfully loaded: " + bundle.getVersionName());
try {
final Boolean res = this.delete(fallback.getId());
if (res) {
Log.i(CapacitorUpdater.TAG, "Deleted previous bundle: " + fallback.getVersionName());
}
} catch (final IOException e) {
Log.e(CapacitorUpdater.TAG, "Failed to delete previous bundle: " + fallback.getVersionName(), e);
}
}
this.setFallbackBundle(bundle);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ public void current(final PluginCall call) {
@PluginMethod
public void notifyAppReady(final PluginCall call) {
try {
Log.i(CapacitorUpdater.TAG, "Current bundle loaded successfully. ['notifyAppReady()' was called]");
final BundleInfo bundle = this.implementation.getCurrentBundle();
this.implementation.setSuccess(bundle);
this.implementation.setSuccess(bundle, this.autoDeletePrevious);
Log.i(CapacitorUpdater.TAG, "Current bundle loaded successfully. ['notifyAppReady()' was called] " + bundle.toString());
call.resolve();
}
catch(final Exception e) {
Expand Down Expand Up @@ -627,13 +627,11 @@ public void onActivityStopped(@NonNull final Activity activity) {
private void checkRevert() {
// Automatically roll back to fallback version if notifyAppReady has not been called yet
final BundleInfo current = this.implementation.getCurrentBundle();
final BundleInfo fallback = this.implementation.getFallbackBundle();

if(current.isBuiltin()) {
Log.i(CapacitorUpdater.TAG, "Built-in bundle is active. Nothing to do.");
return;
}
Log.d(CapacitorUpdater.TAG, "Fallback bundle is: " + fallback);
Log.d(CapacitorUpdater.TAG, "Current bundle is: " + current);

if(BundleStatus.SUCCESS != current.getStatus()) {
Expand All @@ -658,16 +656,6 @@ private void checkRevert() {
}
} else {
Log.i(CapacitorUpdater.TAG, "notifyAppReady was called. This is fine: " + current.getId());
if(this.autoDeletePrevious && !fallback.isBuiltin()) {
try {
final Boolean res = this.implementation.delete(fallback.getId());
if (res) {
Log.i(CapacitorUpdater.TAG, "Deleted previous bundle: " + fallback.getVersionName());
}
} catch (final IOException e) {
Log.e(CapacitorUpdater.TAG, "Failed to delete previous bundle: " + fallback.getVersionName(), e);
}
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,19 @@ extension CustomError: LocalizedError {
}
}

public func setSuccess(bundle: BundleInfo) {
public func setSuccess(bundle: BundleInfo, autoDeletePrevious: Bool) {
self.setBundleStatus(id: bundle.getId(), status: BundleStatus.SUCCESS)
let fallback: BundleInfo = self.getFallbackBundle()
print("\(self.TAG) Fallback bundle is: \(fallback.toString())")
if(autoDeletePrevious) {
print("\(self.TAG) Version successfully loaded: \(bundle.toString())")
let res = self.delete(id: fallback.getId())
if (res) {
print("\(self.TAG) Deleted previous bundle: \(fallback.toString())")
} else {
print("\(self.TAG) Failed to delete previous bundle: \(fallback.toString())")
}
}
self.setFallbackBundle(fallback: bundle)
}

Expand Down
15 changes: 2 additions & 13 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}

@objc func notifyAppReady(_ call: CAPPluginCall) {
print("\(self.implementation.TAG) Current bundle loaded successfully. ['notifyAppReady()' was called]")
let version = self.implementation.getCurrentBundle()
self.implementation.setSuccess(bundle: version)
self.implementation.setSuccess(bundle: version, autoDeletePrevious: self.autoDeletePrevious)
print("\(self.implementation.TAG) Current bundle loaded successfully. ['notifyAppReady()' was called] \(version.toString())")
call.resolve()
}

Expand Down Expand Up @@ -323,13 +323,11 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
func checkRevert() {
// Automatically roll back to fallback version if notifyAppReady has not been called yet
let current: BundleInfo = self.implementation.getCurrentBundle()
let fallback: BundleInfo = self.implementation.getFallbackBundle()
if(current.isBuiltin()) {
print("\(self.implementation.TAG) Built-in bundle is active. Nothing to do.")
return
}

print("\(self.implementation.TAG) Fallback bundle is: \(fallback.toString())")
print("\(self.implementation.TAG) Current bundle is: \(current.toString())")

if(BundleStatus.SUCCESS.localizedString != current.getStatus()) {
Expand All @@ -352,15 +350,6 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}
} else {
print("\(self.implementation.TAG) notifyAppReady was called. This is fine: \(current.toString())")
if(self.autoDeletePrevious) {
print("\(self.implementation.TAG) Version successfully loaded: \(current.toString())")
let res = self.implementation.delete(id: fallback.getId())
if (res) {
print("\(self.implementation.TAG) Deleted previous bundle: \(fallback.toString())")
} else {
print("\(self.implementation.TAG) Failed to delete previous bundle: \(fallback.toString())")
}
}
}
}

Expand Down

0 comments on commit 30d10cd

Please sign in to comment.