From 63bf88ac6652f0da20ff55b15674e6759dffff27 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 25 Mar 2022 22:53:36 +1100 Subject: [PATCH] Adds a function to return a list of plugins which support a specified mixin --- lib/api.dart | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index 1574b6b6..4c326879 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -235,7 +235,28 @@ class InvenTreeAPI { // Returns True only if the server API version is new enough, and plugins are enabled bool pluginsEnabled() => apiVersion >= 34 && _pluginsEnabled; - List plugins = []; + // Cached list of plugins (refreshed when we connect to the server) + List _plugins = []; + + // Return a list of plugins enabled on the server + // Can optionally filter by a particular 'mixin' type + List getPlugins({String mixin = ""}) { + List plugins = []; + + for (var plugin in _plugins) { + // Do we wish to filter by a particular mixin? + if (mixin.isNotEmpty) { + if (!plugin.supportsMixin(mixin)) { + continue; + } + } + + plugins.add(plugin); + } + + // Return list of matching plugins + return plugins; + } // Getter for server version information String get version => _version; @@ -305,7 +326,7 @@ class InvenTreeAPI { _BASE_URL = address; // Clear the list of available plugins - plugins.clear(); + _plugins.clear(); print("Connecting to ${apiUrl} -> username=${username}"); @@ -486,7 +507,7 @@ class InvenTreeAPI { // The server does not support plugins, or they are not enabled if (!pluginsEnabled()) { - plugins.clear(); + _plugins.clear(); return; } @@ -497,12 +518,12 @@ class InvenTreeAPI { if (result is InvenTreePlugin) { if (result.active) { // Only add plugins that are active - plugins.add(result); + _plugins.add(result); } } } - print("Discovered ${plugins.length} active plugins!"); + print("Discovered ${_plugins.length} active plugins!"); }