Bug fix
This commit is contained in:
parent
5d97bedf3b
commit
5e79450255
|
@ -75,7 +75,7 @@
|
||||||
function getJson()
|
function getJson()
|
||||||
{
|
{
|
||||||
// get json
|
// get json
|
||||||
webObject.setText(JSON.stringfy(editor.get()))
|
webObject.setText(JSON.stringify(editor.get()))
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -2,6 +2,14 @@ package com.zane.smapiinstaller.dto;
|
||||||
|
|
||||||
import android.webkit.JavascriptInterface;
|
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.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
@ -16,6 +24,7 @@ public class WebViewObject {
|
||||||
private String language;
|
private String language;
|
||||||
private boolean editable;
|
private boolean editable;
|
||||||
private int height;
|
private int height;
|
||||||
|
private Consumer<String> setterCallback;
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public String getText() {
|
public String getText() {
|
||||||
|
@ -25,6 +34,9 @@ public class WebViewObject {
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public void setText(String text) {
|
public void setText(String text) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
|
if(setterCallback != null) {
|
||||||
|
setterCallback.accept(text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
|
|
|
@ -77,7 +77,7 @@ public class ConfigEditFragment extends Fragment {
|
||||||
CommonLogic.doOnNonNull(this.getContext(), (context -> {
|
CommonLogic.doOnNonNull(this.getContext(), (context -> {
|
||||||
int height = (int) (binding.scrollView.getMeasuredHeight() / context.getResources().getDisplayMetrics().density * 0.95);
|
int height = (int) (binding.scrollView.getMeasuredHeight() / context.getResources().getDisplayMetrics().density * 0.95);
|
||||||
String lang = LanguagesManager.getAppLanguage(context).getCountry();
|
String lang = LanguagesManager.getAppLanguage(context).getCountry();
|
||||||
switch (lang){
|
switch (lang) {
|
||||||
case "zh":
|
case "zh":
|
||||||
lang = "zh-CN";
|
lang = "zh-CN";
|
||||||
break;
|
break;
|
||||||
|
@ -91,10 +91,10 @@ public class ConfigEditFragment extends Fragment {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (editable) {
|
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");
|
binding.editTextConfigWebview.addJavascriptInterface(webObject, "webObject");
|
||||||
} else {
|
} 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");
|
binding.editTextConfigWebview.addJavascriptInterface(webObject, "webObject");
|
||||||
}
|
}
|
||||||
String assetText = FileUtils.getAssetText(context, "jsoneditor/editor.html");
|
String assetText = FileUtils.getAssetText(context, "jsoneditor/editor.html");
|
||||||
|
@ -138,21 +138,23 @@ public class ConfigEditFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onConfigSave() {
|
private void onConfigSave() {
|
||||||
|
binding.editTextConfigWebview.loadUrl("javascript:getJson()");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configSave(String config) {
|
||||||
try {
|
try {
|
||||||
if(webObject != null) {
|
JsonUtil.checkJson(config);
|
||||||
binding.editTextConfigWebview.loadUrl("javascript:getJson()");
|
FileOutputStream outputStream = new FileOutputStream(configPath);
|
||||||
JsonUtil.checkJson(webObject.getText());
|
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
|
||||||
FileOutputStream outputStream = new FileOutputStream(configPath);
|
outputStreamWriter.write(config);
|
||||||
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
|
outputStreamWriter.flush();
|
||||||
outputStreamWriter.write(webObject.getText());
|
|
||||||
outputStreamWriter.flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
DialogUtils.showAlertDialog(getView(), R.string.error, e.getLocalizedMessage());
|
DialogUtils.showAlertDialog(getView(), R.string.error, e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void onConfigCancel() {
|
private void onConfigCancel() {
|
||||||
CommonLogic.doOnNonNull(getView(), view -> Navigation.findNavController(view).popBackStack());
|
CommonLogic.doOnNonNull(getView(), view -> Navigation.findNavController(view).popBackStack());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue