Skip to content

Commit

Permalink
feat: export and import single config
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Sep 13, 2023
1 parent 993996c commit a931893
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,24 @@ public void saveConfig() {
}
}
}
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,23 @@ 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) {
BaseNekoSettingsActivity finalNeko_fragment = neko_fragment;
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));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}

Expand All @@ -45,6 +58,25 @@ protected String getRowKey(int position) {
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();
Expand All @@ -60,15 +92,71 @@ 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<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) {
if (rowMap.containsKey(key)) {
//noinspection ConstantConditions
position = rowMap.get(key);
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) {
Integer temp = rowMap.get(key);
if (temp != null) position = temp;
}
if (position > -1 && listView != null && layoutManager != null) {
int finalPosition = position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -226,7 +218,8 @@ public void onResume() {
}
}

private void updateRows() {
@Override
protected void updateRows() {
rowCount = 0;

accountRow = rowCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -381,7 +373,8 @@ public void onResume() {
}
}

private void updateRows() {
@Override
protected void updateRows() {
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -318,7 +310,8 @@ public void onResume() {
}
}

private void updateRows() {
@Override
protected void updateRows() {
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity {
public boolean onFragmentCreate() {
super.onFragmentCreate();

updateRows(true);
updateRows();

return true;
}
Expand Down Expand Up @@ -355,7 +355,7 @@ public void onItemClick(int id) {
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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -655,8 +647,9 @@ public void onResume() {
}
}

private void updateRows(boolean notify) {
if (notify && listAdapter != null) {
@Override
protected void updateRows() {
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
}
Expand Down

0 comments on commit a931893

Please sign in to comment.