From 1c7c28f03215f03e97387d7e6b45ae752c396dcb Mon Sep 17 00:00:00 2001
From: HunterHuang0X7C7 <1125607007@qq.com>
Date: 星期六, 12 八月 2023 16:58:15 +0800
Subject: [PATCH] 去掉无用引用,优化退出卡死和窗口泄露,优化开启后启动报错

---
 src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java |   85 +++++++++++++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 27 deletions(-)

diff --git a/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java b/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
index d99e4b2..2e34353 100644
--- a/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
+++ b/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
@@ -114,10 +114,10 @@
     }
 
 
-    @Override
+    /*@Override
     protected void finalize() throws Throwable {
         super.finalize();
-    }
+    }*/
 
     private void init() {
         mCompositeDisposable = new CompositeDisposable();
@@ -219,13 +219,14 @@
                                     setInputText(tvInputTargetDbName, cb.getTargetDbName());
                                     setInputText(tvInputFreq, cb.getFreqValue());
                                     cbSelectFreqUnit.getSelectionModel().select(cb.getFreqUnit());
+
+                                    //榛樿鍚姩鍚屾鍔熻兘
+                                    if (cb.isStartSync()) {
+                                        startSync();    //鍚姩鍚屾鍔熻兘
+                                    }
                                 }
                             });
 
-                            //榛樿鍚姩鍚屾鍔熻兘
-                            if (cb.isStartSync()) {
-                                startSync();    //鍚姩鍚屾鍔熻兘
-                            }
                         } else if (BuildType.equals(BuildTypeEnum.DEBUG)) {
                             try {
                                 String srcHost = ConfigProperties.SRC_HOST;
@@ -338,24 +339,54 @@
 
     //鐐瑰嚮锛氶��鍑�
     private void clickExit() {
-        if (isSyncing()) {
-            stopSync();//鍋滄鍚屾
-        }
-        //绯荤粺鎵樼洏
-        SystemTray tray = SystemTray.getSystemTray();
-        if (tray != null) {
-            TrayIcon[] icons = tray.getTrayIcons();
-            for (TrayIcon icon : icons) {
-                if (icon == null) continue;
-                //閫�鍑轰箣鍓嶅厛绉婚櫎绯荤粺鎵樼洏鍥炬爣
-                if (APPID.equals(icon.getActionCommand())) {
-                    tray.remove(icon);
-                }
-            }
-        }
-        //閫�鍑�
-        Platform.exit();
+        addSubscribe(Observable.just(true)
+                .subscribeOn(Schedulers.newThread())
+                .observeOn(Schedulers.io())
+                .doOnNext(new Consumer<Object>() {
+                    @Override
+                    public void accept(Object o) throws Exception {
+                        if (isSyncing()) {
+                            stopSync();//鍋滄鍚屾
+                        }
+                    }
+                })
+                .doOnNext(new Consumer<Object>() {
+                    @Override
+                    public void accept(Object o) throws Exception {
+
+                        //妫�鏌ョ郴缁熸槸鍚︽敮鎸佹墭鐩�
+                        if (SystemTray.isSupported()) {
+                            //绯荤粺鎵樼洏
+                            SystemTray tray = SystemTray.getSystemTray();
+                            if (tray != null) {
+                                TrayIcon[] icons = tray.getTrayIcons();
+                                for (TrayIcon icon : icons) {
+                                    if (icon == null) continue;
+                                    //閫�鍑轰箣鍓嶅厛绉婚櫎绯荤粺鎵樼洏鍥炬爣
+                                    if (APPID.equals(icon.getActionCommand())) {
+                                        tray.remove(icon);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                })
+                .subscribe(new Consumer<Boolean>() {
+                    @Override
+                    public void accept(Boolean aBoolean) throws Exception {
+
+                        TrayUtil.getInstance().exitApp();//閫�鍑�
+                    }
+                }, new Consumer<Throwable>() {
+                    @Override
+                    public void accept(Throwable e) throws Exception {
+                        e.printStackTrace();
+
+                        TrayUtil.getInstance().exitApp();//閫�鍑�
+                    }
+                }));
     }
+
 
     //鐐瑰嚮锛氬叧浜�
     private void clickAbout() {
@@ -394,7 +425,7 @@
                                 , targetFreq, freqSelect, startSync);
 
                         //鍐欏叆鏂囦欢
-                        String path = PathUtil.projectPath + "\\config.json";
+                        String path = PathUtil.getConfigFilePath("config.json");
                         String content = JsonUtil.toJson(cb);
 
                         //浣跨敤 BufferedWriter 鍐欐枃浠�
@@ -461,7 +492,7 @@
     //浠庢湰鍦伴厤缃枃浠朵腑鑾峰彇閰嶇疆淇℃伅
     private ConfigBean getSaveConfig() {
         try {
-            String json = FileUtil.bufferedReader(PathUtil.projectPath + "\\config.json");
+            String json = FileUtil.bufferedReader(PathUtil.getConfigFilePath("config.json"));
             return JsonUtil.fromJson(json, ConfigBean.class);
         } catch (Exception e) {
             e.printStackTrace();
@@ -567,7 +598,7 @@
         operate.setAlignment(Pos.CENTER_RIGHT);
         operate.setSpacing(8);
 //        operate.setStyle("-fx-background-color: #F0F0F0");
-        operate.setPadding(new Insets(12, 20, 6, 20));
+        operate.setPadding(new Insets(12, 20, 12, 20));
         operate.getChildren().addAll(defButton, cancelButton);
 
         VBox vBox = new VBox();
@@ -858,7 +889,7 @@
                         }
                         cb.setStartSync(startSync);
 
-                        String path = PathUtil.projectPath + "\\config.json";
+                        String path = PathUtil.getConfigFilePath("config.json");
                         String content = JsonUtil.toJson(cb);
 
                         //浣跨敤 BufferedWriter 鍐欐枃浠�

--
Gitblit v1.9.1