This commit is contained in:
ZaneYork 2020-10-19 10:33:52 +08:00
parent 5d97bedf3b
commit 5e79450255
3 changed files with 26 additions and 12 deletions

View File

@ -75,7 +75,7 @@
function getJson()
{
// get json
webObject.setText(JSON.stringfy(editor.get()))
webObject.setText(JSON.stringify(editor.get()))
}
</script>
</body>

View File

@ -2,6 +2,14 @@ package com.zane.smapiinstaller.dto;
import android.webkit.JavascriptInterface;
import com.zane.smapiinstaller.R;
import com.zane.smapiinstaller.utils.DialogUtils;
import com.zane.smapiinstaller.utils.JsonUtil;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.function.Consumer;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@ -16,6 +24,7 @@ public class WebViewObject {
private String language;
private boolean editable;
private int height;
private Consumer<String> setterCallback;
@JavascriptInterface
public String getText() {
@ -25,6 +34,9 @@ public class WebViewObject {
@JavascriptInterface
public void setText(String text) {
this.text = text;
if(setterCallback != null) {
setterCallback.accept(text);
}
}
@JavascriptInterface

View File

@ -77,7 +77,7 @@ public class ConfigEditFragment extends Fragment {
CommonLogic.doOnNonNull(this.getContext(), (context -> {
int height = (int) (binding.scrollView.getMeasuredHeight() / context.getResources().getDisplayMetrics().density * 0.95);
String lang = LanguagesManager.getAppLanguage(context).getCountry();
switch (lang){
switch (lang) {
case "zh":
lang = "zh-CN";
break;
@ -91,10 +91,10 @@ public class ConfigEditFragment extends Fragment {
break;
}
if (editable) {
webObject = new WebViewObject(fileText, "code", lang, true, height);
webObject = new WebViewObject(fileText, "tree", lang, true, height, this::configSave);
binding.editTextConfigWebview.addJavascriptInterface(webObject, "webObject");
} else {
webObject = new WebViewObject(fileText, "text-plain", lang, false, height);
webObject = new WebViewObject(fileText, "text-plain", lang, false, height, null);
binding.editTextConfigWebview.addJavascriptInterface(webObject, "webObject");
}
String assetText = FileUtils.getAssetText(context, "jsoneditor/editor.html");
@ -138,21 +138,23 @@ public class ConfigEditFragment extends Fragment {
}
private void onConfigSave() {
binding.editTextConfigWebview.loadUrl("javascript:getJson()");
}
private void configSave(String config) {
try {
if(webObject != null) {
binding.editTextConfigWebview.loadUrl("javascript:getJson()");
JsonUtil.checkJson(webObject.getText());
FileOutputStream outputStream = new FileOutputStream(configPath);
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
outputStreamWriter.write(webObject.getText());
outputStreamWriter.flush();
}
JsonUtil.checkJson(config);
FileOutputStream outputStream = new FileOutputStream(configPath);
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
outputStreamWriter.write(config);
outputStreamWriter.flush();
}
} catch (Exception e) {
DialogUtils.showAlertDialog(getView(), R.string.error, e.getLocalizedMessage());
}
}
private void onConfigCancel() {
CommonLogic.doOnNonNull(getView(), view -> Navigation.findNavController(view).popBackStack());
}