From 160585bf634f7f20fb2fb84899a0b6a2a24affe6 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Wed, 13 Sep 2023 21:45:53 +0800 Subject: [PATCH] feat: export and import single config --- .../nekomimi/nekogram/config/ConfigItem.java | 22 +++- .../config/cell/ConfigCellSelectBox.java | 4 + .../config/cell/ConfigCellTextCheck.java | 4 + .../config/cell/ConfigCellTextDetail.java | 4 + .../config/cell/ConfigCellTextInput.java | 4 + .../nekogram/helpers/SettingsHelper.java | 11 +- .../settings/BaseNekoXSettingsActivity.java | 106 ++++++++++++++++-- .../settings/NekoAccountSettingsActivity.java | 10 +- .../settings/NekoChatSettingsActivity.java | 10 +- .../NekoExperimentalSettingsActivity.java | 10 +- .../settings/NekoGeneralSettingsActivity.java | 10 +- 11 files changed, 146 insertions(+), 49 deletions(-) diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/ConfigItem.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/ConfigItem.java index 97b69a2259..164f678283 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/ConfigItem.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/ConfigItem.java @@ -167,4 +167,24 @@ public void saveConfig() { } } } -} \ No newline at end of file + + public Object checkConfigFromString(String value) { + try { + switch (type) { + case configTypeBool: + return Boolean.parseBoolean(value); + case configTypeInt: + return Integer.parseInt(value); + case configTypeString: + return value; + case configTypeLong: + return Long.parseLong(value); + case configTypeFloat: + return Float.parseFloat(value); + default: + return null; + } + } catch (Exception ignored) {} + return null; + } +} diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellSelectBox.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellSelectBox.java index 972a0a48a9..e15c79ec54 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellSelectBox.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellSelectBox.java @@ -54,6 +54,10 @@ public int getType() { return CellGroup.ITEM_TYPE_TEXT_SETTINGS_CELL; } + public ConfigItem getBindConfig() { + return this.bindConfig; + } + public String getKey() { return this.key; } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextCheck.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextCheck.java index 961fdbbdaf..4df7453855 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextCheck.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextCheck.java @@ -37,6 +37,10 @@ public int getType() { return CellGroup.ITEM_TYPE_TEXT_CHECK; } + public ConfigItem getBindConfig() { + return bindConfig; + } + public String getKey() { return bindConfig == null ? null : bindConfig.getKey(); } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextDetail.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextDetail.java index 8094644574..58bdfdfa6c 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextDetail.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextDetail.java @@ -27,6 +27,10 @@ public int getType() { return CellGroup.ITEM_TYPE_TEXT_DETAIL; } + public ConfigItem getBindConfig() { + return bindConfig; + } + public String getKey() { return bindConfig == null ? null : bindConfig.getKey(); } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextInput.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextInput.java index 80bd989edf..aae95b8bbb 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextInput.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/config/cell/ConfigCellTextInput.java @@ -51,6 +51,10 @@ public int getType() { return CellGroup.ITEM_TYPE_TEXT_SETTINGS_CELL; } + public ConfigItem getBindConfig() { + return bindConfig; + } + public String getKey() { return bindConfig == null ? null : bindConfig.getKey(); } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/SettingsHelper.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/SettingsHelper.java index 50f6655c6a..e2d8f3a78d 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/SettingsHelper.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/SettingsHelper.java @@ -67,6 +67,10 @@ public static void processDeepLink(Uri uri, Callback callback, Runnable unknown) if (TextUtils.isEmpty(row)) { row = uri.getQueryParameter("row"); } + var value = uri.getQueryParameter("v"); + if (TextUtils.isEmpty(value)) { + value = uri.getQueryParameter("value"); + } if (!TextUtils.isEmpty(row)) { var rowFinal = row; if (neko_fragment != null) { @@ -74,7 +78,12 @@ public static void processDeepLink(Uri uri, Callback callback, Runnable unknown) AndroidUtilities.runOnUIThread(() -> finalNeko_fragment.scrollToRow(rowFinal, unknown)); } else if (nekox_fragment != null) { BaseNekoXSettingsActivity finalNekoX_fragment = nekox_fragment; - AndroidUtilities.runOnUIThread(() -> finalNekoX_fragment.scrollToRow(rowFinal, unknown)); + if (!TextUtils.isEmpty(value)) { + String finalValue = value; + AndroidUtilities.runOnUIThread(() -> finalNekoX_fragment.importToRow(rowFinal, finalValue, unknown)); + } else { + AndroidUtilities.runOnUIThread(() -> finalNekoX_fragment.scrollToRow(rowFinal, unknown)); + } } } } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/BaseNekoXSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/BaseNekoXSettingsActivity.java index 110577e5e9..e6e784c0ca 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/BaseNekoXSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/BaseNekoXSettingsActivity.java @@ -1,14 +1,23 @@ package tw.nekomimi.nekogram.settings; +import android.content.Context; + import androidx.recyclerview.widget.LinearLayoutManager; import org.telegram.messenger.AndroidUtilities; +import org.telegram.messenger.LocaleController; +import org.telegram.messenger.R; +import org.telegram.ui.ActionBar.AlertDialog; import org.telegram.ui.ActionBar.BaseFragment; import org.telegram.ui.Components.BlurredRecyclerView; +import org.telegram.ui.Components.BulletinFactory; +import java.util.ArrayList; import java.util.HashMap; +import java.util.Locale; import tw.nekomimi.nekogram.config.CellGroup; +import tw.nekomimi.nekogram.config.ConfigItem; import tw.nekomimi.nekogram.config.cell.AbstractConfigCell; import tw.nekomimi.nekogram.config.cell.ConfigCellCustom; import tw.nekomimi.nekogram.config.cell.ConfigCellSelectBox; @@ -21,20 +30,21 @@ public class BaseNekoXSettingsActivity extends BaseFragment { protected LinearLayoutManager layoutManager; protected HashMap rowMap = new HashMap<>(20); protected HashMap rowMapReverse = new HashMap<>(20); + protected HashMap rowMapConfigReverse = new HashMap<>(20); protected void addRowsToMap(CellGroup cellGroup) { rowMap.clear(); rowMapReverse.clear(); + rowMapConfigReverse.clear(); String key; + ConfigItem config; for (int i = 0; i < cellGroup.rows.size(); i++) { + config = getBindConfig(cellGroup.rows.get(i)); key = getRowKey(cellGroup.rows.get(i)); - if (key != null) { - rowMap.put(key, i); - rowMapReverse.put(i, key); - } else { - rowMap.put(String.valueOf(i), i); - rowMapReverse.put(i, String.valueOf(i)); - } + if (key == null) key = String.valueOf(i); + rowMap.put(key, i); + rowMapReverse.put(i, key); + rowMapConfigReverse.put(i, config); } } @@ -45,6 +55,25 @@ protected String getRowKey(int position) { return String.valueOf(position); } + protected String getRowValue(int position) { + ConfigItem config = rowMapConfigReverse.get(position); + if (config != null) return config.String(); + return null; + } + + protected ConfigItem getBindConfig(AbstractConfigCell row) { + if (row instanceof ConfigCellTextCheck) { + return ((ConfigCellTextCheck) row).getBindConfig(); + } else if (row instanceof ConfigCellSelectBox) { + return ((ConfigCellSelectBox) row).getBindConfig(); + } else if (row instanceof ConfigCellTextDetail) { + return ((ConfigCellTextDetail) row).getBindConfig(); + } else if (row instanceof ConfigCellTextInput) { + return ((ConfigCellTextInput) row).getBindConfig(); + } + return null; + } + protected String getRowKey(AbstractConfigCell row) { if (row instanceof ConfigCellTextCheck) { return ((ConfigCellTextCheck) row).getKey(); @@ -60,15 +89,70 @@ protected String getRowKey(AbstractConfigCell row) { return null; } - public void scrollToRow(String key, Runnable unknown) { + protected void createLongClickDialog(Context context, BaseFragment fragment, String prefix, int position) { + String key = getRowKey(position); + String value = getRowValue(position); + ArrayList itemsArray = new ArrayList<>(); + itemsArray.add(LocaleController.getString("CopyLink", R.string.CopyLink)); + if (value != null) { + itemsArray.add(LocaleController.getString("BackupSettings", R.string.BackupSettings)); + } + CharSequence[] items = itemsArray.toArray(new CharSequence[0]); + showDialog(new AlertDialog.Builder(context) + .setItems( + items, + (dialogInterface, i) -> { + switch (i) { + case 0: + AndroidUtilities.addToClipboard(String.format(Locale.getDefault(), "https://%s/nasettings/%s?r=%s", getMessagesController().linkPrefix, prefix, key)); + BulletinFactory.of(fragment).createCopyLinkBulletin().show(); + break; + case 1: + AndroidUtilities.addToClipboard(String.format(Locale.getDefault(), "https://%s/nasettings/%s?r=%s&v=%s", getMessagesController().linkPrefix, prefix, key, value)); + BulletinFactory.of(fragment).createCopyLinkBulletin().show(); + break; + } + }) + .create()); + } + + public void importToRow(String key, String value, Runnable unknown) { int position = -1; try { position = Integer.parseInt(key); } catch (NumberFormatException exception) { - if (rowMap.containsKey(key)) { - //noinspection ConstantConditions - position = rowMap.get(key); + Integer temp = rowMap.get(key); + if (temp != null) position = temp; + } + ConfigItem config = rowMapConfigReverse.get(position); + Context context = getContext(); + if (context != null && config != null && position > -1 && listView != null && layoutManager != null && value != null) { + Object new_value = config.checkConfigFromString(value); + if (new_value == null) { + scrollToRow(key, unknown); + return; } + var builder = new AlertDialog.Builder(context); + builder.setTitle(LocaleController.getString("ImportSettings", R.string.ImportSettings)); + builder.setMessage(LocaleController.getString("ImportSettingsAlert", R.string.ImportSettingsAlert)); + builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); + builder.setPositiveButton(LocaleController.getString("Import", R.string.Import), (dialogInter, i) -> { + config.changed(new_value); + config.saveConfig(); + }); + showDialog(builder.create()); + } else { + scrollToRow(key, unknown); + } + } + + public void scrollToRow(String key, Runnable unknown) { + int position = -1; + try { + position = Integer.parseInt(key); + } catch (NumberFormatException exception) { + Integer temp = rowMap.get(key); + if (temp != null) position = temp; } if (position > -1 && listView != null && layoutManager != null) { int finalPosition = position; diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoAccountSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoAccountSettingsActivity.java index 8779e4457d..0fc9a5c318 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoAccountSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoAccountSettingsActivity.java @@ -197,16 +197,8 @@ public void onItemClick(int id) { }); listView.setOnItemLongClickListener((view, position, x, y) -> { var holder = listView.findViewHolderForAdapterPosition(position); - var key = getRowKey(position); if (holder != null && listAdapter.isEnabled(holder)) { - showDialog(new AlertDialog.Builder(context) - .setItems( - new CharSequence[]{LocaleController.getString("CopyLink", R.string.CopyLink)}, - (dialogInterface, i) -> { - AndroidUtilities.addToClipboard(String.format(Locale.getDefault(), "https://%s/nasettings/%s?r=%s", getMessagesController().linkPrefix, "account", key)); - BulletinFactory.of(NekoAccountSettingsActivity.this).createCopyLinkBulletin().show(); - }) - .create()); + createLongClickDialog(context, NekoAccountSettingsActivity.this, "account", position); return true; } return false; diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java index 81bd18be30..6d36608eea 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java @@ -336,16 +336,8 @@ public void onItemClick(int id) { addRowsToMap(cellGroup); listView.setOnItemLongClickListener((view, position, x, y) -> { var holder = listView.findViewHolderForAdapterPosition(position); - var key = getRowKey(position); if (holder != null && listAdapter.isEnabled(holder)) { - showDialog(new AlertDialog.Builder(context) - .setItems( - new CharSequence[]{LocaleController.getString("CopyLink", R.string.CopyLink)}, - (dialogInterface, i) -> { - AndroidUtilities.addToClipboard(String.format(Locale.getDefault(), "https://%s/nasettings/%s?r=%s", getMessagesController().linkPrefix, "chat", key)); - BulletinFactory.of(NekoChatSettingsActivity.this).createCopyLinkBulletin().show(); - }) - .create()); + createLongClickDialog(context, NekoChatSettingsActivity.this, "chat", position); return true; } return false; diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java index e3d8abf8d9..079fb05d81 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java @@ -218,16 +218,8 @@ public void onItemClick(int id) { addRowsToMap(cellGroup); listView.setOnItemLongClickListener((view, position, x, y) -> { var holder = listView.findViewHolderForAdapterPosition(position); - var key = getRowKey(position); if (holder != null && listAdapter.isEnabled(holder)) { - showDialog(new AlertDialog.Builder(context) - .setItems( - new CharSequence[]{LocaleController.getString("CopyLink", R.string.CopyLink)}, - (dialogInterface, i) -> { - AndroidUtilities.addToClipboard(String.format(Locale.getDefault(), "https://%s/nasettings/%s?r=%s", getMessagesController().linkPrefix, "experimental", key)); - BulletinFactory.of(NekoExperimentalSettingsActivity.this).createCopyLinkBulletin().show(); - }) - .create()); + createLongClickDialog(context, NekoExperimentalSettingsActivity.this, "experimental", position); return true; } return false; diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java index ac23c50b84..c7abd8e458 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java @@ -380,16 +380,8 @@ public void onItemClick(int id) { addRowsToMap(cellGroup); listView.setOnItemLongClickListener((view, position, x, y) -> { var holder = listView.findViewHolderForAdapterPosition(position); - var key = getRowKey(position); if (holder != null && listAdapter.isEnabled(holder)) { - showDialog(new AlertDialog.Builder(context) - .setItems( - new CharSequence[]{LocaleController.getString("CopyLink", R.string.CopyLink)}, - (dialogInterface, i) -> { - AndroidUtilities.addToClipboard(String.format(Locale.getDefault(), "https://%s/nasettings/%s?r=%s", getMessagesController().linkPrefix, "general", key)); - BulletinFactory.of(NekoGeneralSettingsActivity.this).createCopyLinkBulletin().show(); - }) - .create()); + createLongClickDialog(context, NekoGeneralSettingsActivity.this, "general", position); return true; } return false;