Add plugin dependent settings items to plugin item

This commit is contained in:
Vitaliy 2020-03-13 13:09:01 +02:00
parent a5777b9ca2
commit 27a5daf2db
2 changed files with 52 additions and 20 deletions

View file

@ -156,7 +156,6 @@ public class SettingsHelper {
public abstract static class SettingsItem {
private SettingsItemType type;
private String pluginDependentId;
boolean shouldReplace = false;
@ -183,11 +182,6 @@ public class SettingsHelper {
@NonNull
public abstract String getFileName();
@Nullable
public String getPluginDependentId() {
return pluginDependentId;
}
public boolean shouldReadOnCollecting() {
return false;
}
@ -209,9 +203,6 @@ public class SettingsHelper {
}
void readFromJson(@NonNull JSONObject json) throws JSONException {
if (json.has("pluginId")) {
pluginDependentId = json.getString("pluginId");
}
}
void writeToJson(@NonNull JSONObject json) throws JSONException {
@ -256,6 +247,7 @@ public class SettingsHelper {
private OsmandApplication app;
private CustomOsmandPlugin plugin;
private List<SettingsItem> pluginItems;
PluginSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
super(SettingsItemType.PLUGIN, json);
@ -284,6 +276,10 @@ public class SettingsHelper {
return plugin;
}
public List<SettingsItem> getPluginItems() {
return pluginItems;
}
@Override
void readFromJson(@NonNull JSONObject json) throws JSONException {
String pluginId = json.getString("pluginId");
@ -1666,23 +1662,45 @@ public class SettingsHelper {
SettingsItemsFactory(OsmandApplication app, String jsonStr) throws IllegalArgumentException, JSONException {
this.app = app;
JSONObject json = new JSONObject(jsonStr);
collectItems(new JSONObject(jsonStr));
}
private void collectItems(JSONObject json) throws IllegalArgumentException, JSONException {
JSONArray itemsJson = json.getJSONArray("items");
Map<String, List<SettingsItem>> pluginItems = new HashMap<>();
for (int i = 0; i < itemsJson.length(); i++) {
JSONObject itemJson = itemsJson.getJSONObject(i);
SettingsItem item;
SettingsItem item = null;
try {
item = createItem(itemJson);
if (item != null) {
items.add(item);
}
} catch (IllegalArgumentException e) {
LOG.error("Error creating item from json: " + itemJson, e);
}
if (item != null) {
if (itemJson.has("pluginId") && item.type != SettingsItemType.PLUGIN) {
String pluginId = itemJson.getString("pluginId");
List<SettingsItem> items = pluginItems.get(pluginId);
if (items != null) {
items.add(item);
} else {
items = new ArrayList<>();
items.add(item);
pluginItems.put(pluginId, items);
}
} else {
items.add(item);
}
}
}
if (items.size() == 0) {
throw new IllegalArgumentException("No items");
}
for (SettingsItem item : items) {
if (item instanceof PluginSettingsItem) {
PluginSettingsItem pluginSettingsItem = ((PluginSettingsItem) item);
pluginSettingsItem.pluginItems = pluginItems.get(pluginSettingsItem.getName());
}
}
}
@NonNull

View file

@ -787,17 +787,31 @@ public class ImportHelper {
progress.dismiss();
}
if (succeed) {
boolean pluginImport = false;
List<SettingsHelper.SettingsItem> pluginIndependentItems = new ArrayList<>();
List<SettingsHelper.PluginSettingsItem> pluginSettingsItems = new ArrayList<>();
for (SettingsHelper.SettingsItem item : items) {
if (item instanceof SettingsHelper.PluginSettingsItem) {
pluginImport = true;
CustomOsmandPlugin plugin = ((SettingsHelper.PluginSettingsItem) item).getPlugin();
OsmandPlugin.addCustomPlugin(app, activity, plugin);
pluginSettingsItems.add((SettingsHelper.PluginSettingsItem) item);
} else {
pluginIndependentItems.add(item);
}
}
if (!pluginImport) {
for (SettingsHelper.PluginSettingsItem pluginItem : pluginSettingsItems) {
final CustomOsmandPlugin plugin = pluginItem.getPlugin();
OsmandPlugin.addCustomPlugin(app, activity, plugin);
if (!Algorithms.isEmpty(pluginItem.getPluginItems())) {
app.getSettingsHelper().importSettings(file, pluginItem.getPluginItems(), "", 1, new SettingsHelper.SettingsImportListener() {
@Override
public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List<SettingsHelper.SettingsItem> items) {
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, plugin.getName()));
}
});
}
}
if (!pluginIndependentItems.isEmpty()) {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
ImportSettingsFragment.showInstance(fragmentManager, items, file);
ImportSettingsFragment.showInstance(fragmentManager, pluginIndependentItems, file);
}
} else if (empty) {
app.showShortToastMessage(app.getString(R.string.file_import_error, name, app.getString(R.string.shared_string_unexpected_error)));