From 5e79450255de9f14fd8ec756a53777ac703e6470 Mon Sep 17 00:00:00 2001 From: ZaneYork Date: Mon, 19 Oct 2020 10:33:52 +0800 Subject: [PATCH] Bug fix --- app/src/main/assets/jsoneditor/editor.html | 2 +- .../smapiinstaller/dto/WebViewObject.java | 12 ++++++++++ .../ui/config/ConfigEditFragment.java | 24 ++++++++++--------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/src/main/assets/jsoneditor/editor.html b/app/src/main/assets/jsoneditor/editor.html index 572d5cd..7329d62 100644 --- a/app/src/main/assets/jsoneditor/editor.html +++ b/app/src/main/assets/jsoneditor/editor.html @@ -75,7 +75,7 @@ function getJson() { // get json - webObject.setText(JSON.stringfy(editor.get())) + webObject.setText(JSON.stringify(editor.get())) } diff --git a/app/src/main/java/com/zane/smapiinstaller/dto/WebViewObject.java b/app/src/main/java/com/zane/smapiinstaller/dto/WebViewObject.java index 2587e8b..0f41f07 100644 --- a/app/src/main/java/com/zane/smapiinstaller/dto/WebViewObject.java +++ b/app/src/main/java/com/zane/smapiinstaller/dto/WebViewObject.java @@ -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 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 diff --git a/app/src/main/java/com/zane/smapiinstaller/ui/config/ConfigEditFragment.java b/app/src/main/java/com/zane/smapiinstaller/ui/config/ConfigEditFragment.java index 662337a..284b79b 100644 --- a/app/src/main/java/com/zane/smapiinstaller/ui/config/ConfigEditFragment.java +++ b/app/src/main/java/com/zane/smapiinstaller/ui/config/ConfigEditFragment.java @@ -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()); }