feat: export and import single config

This commit is contained in:
xtaodada 2023-09-13 23:05:11 +08:00
parent 482e8ecc70
commit 45be1ea41b
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
11 changed files with 161 additions and 56 deletions

View File

@ -167,4 +167,24 @@ public class ConfigItem {
}
}
}
}
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;
}
}

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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));
}
}
}
}

View File

@ -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<String, Integer> rowMap = new HashMap<>(20);
protected HashMap<Integer, String> rowMapReverse = new HashMap<>(20);
protected HashMap<Integer, ConfigItem> 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<CharSequence> 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;

View File

@ -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++;

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}
}