Change when plugins are disabled on reload and listing reloads

This commit is contained in:
Kelvin 2025-09-08 19:00:41 +02:00
commit a9bb900994
2 changed files with 44 additions and 22 deletions

View file

@ -177,16 +177,11 @@ class StatePlatform {
}
withContext(Dispatchers.IO) {
var toDisables = mutableListOf<IPlatformClient>();
var enabled: Array<String>;
synchronized(_clientsLock) {
for(e in _enabledClients) {
try {
e.disable();
onSourceDisabled.emit(e);
}
catch(ex: Throwable) {
UIDialogs.appToast(ToastView.Toast("If this happens often, please inform the developers on Github", false, null, "Plugin [${e.name}] failed to disable"));
}
toDisables.add(e);
}
_enabledClients.clear();
@ -236,6 +231,18 @@ class StatePlatform {
}
}
selectClients(*enabled);
for(toDisable in toDisables) {
launch(Dispatchers.IO) {
try {
toDisable.disable();
onSourceDisabled.emit(toDisable);
}
catch(ex: Throwable) {
Logger.e(TAG, "FAILED TO DISABLE CLIENT [${toDisable?.name}] AFTER UpdateAvailableClients", ex);
}
}
}
};
}
@ -348,11 +355,11 @@ class StatePlatform {
StateApp.instance.handleCaptchaException(c, ex);
}
var toDisable: IPlatformClient? = null;
synchronized(_clientsLock) {
if (_enabledClients.contains(client)) {
_enabledClients.remove(client);
client.disable();
onSourceDisabled.emit(client);
toDisable = client;
newClient.initialize();
_enabledClients.add(newClient);
}
@ -360,6 +367,18 @@ class StatePlatform {
_availableClients.removeIf { it.id == id };
_availableClients.add(newClient);
}
if(toDisable != null) {
launch(Dispatchers.IO) {
try {
toDisable?.disable();
onSourceDisabled.emit(client);
}
catch (ex: Throwable) {
Logger.e(TAG, "FAILED TO DISABLE CLIENT [${toDisable?.name}] AFTER RELOAD", ex);
}
}
}
afterReload?.invoke();
return@withContext newClient;
};

View file

@ -402,18 +402,25 @@ class StatePlugins {
}
val icon = config.absoluteIconUrl?.let { absIconUrl ->
withContext(Dispatchers.Main) {
it.setText("Saving plugin...");
it.setProgress(0.75);
}
val iconResp = client.get(absIconUrl);
if(iconResp.isOk)
return@let iconResp.body?.byteStream()?.use { it.readBytes() };
return@let null;
}
withContext(Dispatchers.Main) {
it.setText("Saving plugin...");
it.setProgress(0.75);
}
val installEx = StatePlugins.instance.createPlugin(config, script, icon, reinstall);
if(installEx != null)
throw installEx;
withContext(Dispatchers.Main) {
it.setText("Reloading available plugins...");
it.setProgress(0.9);
}
StatePlatform.instance.updateAvailableClients(context);
withContext(Dispatchers.Main) {
@ -522,10 +529,8 @@ class StatePlugins {
if(id == StateDeveloper.DEV_ID)
throw IllegalStateException("Attempted to retrieve a persistent developer plugin, this is not allowed");
synchronized(_plugins) {
return _plugins.findItem { it.config.id == id };
}
}
fun getPlugins(): List<SourcePluginDescriptor> {
return _plugins.getItems();
}
@ -533,14 +538,12 @@ class StatePlugins {
fun deletePlugin(id: String) {
synchronized(_pluginScripts) {
synchronized(_plugins) {
_pluginScripts.deleteFile(id);
val plugins = _plugins.findItems { it.config.id == id };
for(plugin in plugins)
_plugins.delete(plugin);
}
}
}
fun createPlugin(config: SourcePluginConfig, script: String, icon: ByteArray? = null, reinstall: Boolean = false, flags: List<String> = listOf()) : Throwable? {
try {
if(config.id == StateDeveloper.DEV_ID)