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 482e8ec commit 160585b
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 49 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,21 @@ 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> 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);
}
}

Expand All @@ -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();
Expand All @@ -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<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 = 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;
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 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 160585b

Please sign in to comment.