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 97b69a225..164f67828 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 class ConfigItem { } } } -} \ 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 972a0a48a..e15c79ec5 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 class ConfigCellSelectBox extends AbstractConfigCell { 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 961fdbbda..4df745385 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 class ConfigCellTextCheck extends AbstractConfigCell { 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 809464457..58bdfdfa6 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 class ConfigCellTextDetail extends AbstractConfigCell { 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 80bd989ed..aae95b8bb 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 class ConfigCellTextInput extends AbstractConfigCell { 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 50f6655c6..e2d8f3a78 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 class SettingsHelper { 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 class SettingsHelper { 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 110577e5e..39ea08de6 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,24 @@ public class BaseNekoXSettingsActivity extends BaseFragment { protected LinearLayoutManager layoutManager; protected HashMap rowMap = new HashMap<>(20); protected HashMap rowMapReverse = new HashMap<>(20); + protected HashMap rowConfigMapReverse = new HashMap<>(20); + + protected void updateRows() { + } protected void addRowsToMap(CellGroup cellGroup) { rowMap.clear(); rowMapReverse.clear(); + rowConfigMapReverse.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); + rowConfigMapReverse.put(i, config); } } @@ -45,6 +58,25 @@ public class BaseNekoXSettingsActivity extends BaseFragment { return String.valueOf(position); } + protected String getRowValue(int position) { + ConfigItem config = rowConfigMapReverse.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 +92,71 @@ public class BaseNekoXSettingsActivity extends BaseFragment { return null; } + 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) { + Integer temp = rowMap.get(key); + if (temp != null) position = temp; + } + ConfigItem config = rowConfigMapReverse.get(position); + Context context = getParentActivity(); + if (context != null && config != 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(); + updateRows(); + }); + builder.show(); + } else { + scrollToRow(key, unknown); + } + } + public void scrollToRow(String key, 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; } 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 8779e4457..db143260d 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 class NekoAccountSettingsActivity extends BaseNekoXSettingsActivity { }); 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; @@ -226,7 +218,8 @@ public class NekoAccountSettingsActivity extends BaseNekoXSettingsActivity { } } - private void updateRows() { + @Override + protected void updateRows() { rowCount = 0; accountRow = rowCount++; 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 81bd18be3..537ffc285 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 class NekoChatSettingsActivity extends BaseNekoXSettingsActivity implemen 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; @@ -381,7 +373,8 @@ public class NekoChatSettingsActivity extends BaseNekoXSettingsActivity implemen } } - private void updateRows() { + @Override + protected void updateRows() { if (listAdapter != null) { listAdapter.notifyDataSetChanged(); } 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 e3d8abf8d..6986e85ed 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 class NekoExperimentalSettingsActivity extends BaseNekoXSettingsActivity 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; @@ -318,7 +310,8 @@ public class NekoExperimentalSettingsActivity extends BaseNekoXSettingsActivity } } - private void updateRows() { + @Override + protected void updateRows() { if (listAdapter != null) { listAdapter.notifyDataSetChanged(); } 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 ac23c50b8..f1c2e0f1e 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java @@ -244,7 +244,7 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity { public boolean onFragmentCreate() { super.onFragmentCreate(); - updateRows(true); + updateRows(); return true; } @@ -355,7 +355,7 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity { boolean needReset = NekoConfig.translationProvider.Int() - 1 != i && (NekoConfig.translationProvider.Int() == 1 || i == 0); NekoConfig.translationProvider.setConfigInt(i + 1); if (needReset) { - updateRows(true); + updateRows(); } else { listAdapter.notifyItemChanged(position); } @@ -380,16 +380,8 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity { 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; @@ -655,8 +647,9 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity { } } - private void updateRows(boolean notify) { - if (notify && listAdapter != null) { + @Override + protected void updateRows() { + if (listAdapter != null) { listAdapter.notifyDataSetChanged(); } }