From 16c1739bd46f8f721cfc0c7bdd5aa21813d531e4 Mon Sep 17 00:00:00 2001 From: hizumiaoba <56146205+hizumiaoba@users.noreply.github.com> Date: Thu, 16 Dec 2021 18:14:59 +0900 Subject: [PATCH] feat: add manualUpdate feature. BREAKING CHANGE: GUI Structure has been changed partly because of this. --- src/com/ranfa/languages/List_en_US.properties | 4 +- src/com/ranfa/languages/List_ja_JP.properties | 4 +- src/com/ranfa/lib/ManualUpdateThreadImpl.java | 59 +++++++++++++++++++++++++++ src/com/ranfa/main/DelesteRandomSelector.java | 25 ++++++++++-- src/com/ranfa/main/Messages.java | 4 +- src/test/LanguageTest.java | 4 ++ 6 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/com/ranfa/lib/ManualUpdateThreadImpl.java diff --git a/src/com/ranfa/languages/List_en_US.properties b/src/com/ranfa/languages/List_en_US.properties index c0ec3f6..2733d5b 100644 --- a/src/com/ranfa/languages/List_en_US.properties +++ b/src/com/ranfa/languages/List_en_US.properties @@ -25,4 +25,6 @@ MSGCompletePost:Complete to post. MSGTerminate:End MSGInternalYpdateNotDoneYet:Internal update in progress. Please wait a moment. MSGNarrowDownProcedure:How to select songs\r\n1.Select difficulty, attribute, and level.\r\n2.Click [Narrow down songs] button.\r\n3.Click [start!] button.\r\n4.Selected songs will be shown here!\r\nThe maximum number of selected songs: -MSGCurrentAlbumType:\nCurrent MASTER+ ALBUM type(based on simulation): \ No newline at end of file +MSGCurrentAlbumType:\nCurrent MASTER+ ALBUM type(based on simulation): +MSGManualUpdate:Manual
Update +MSGManualUpdateNotCompleteYet:Manual Update has not been finished yet. Please wait a moment. \ No newline at end of file diff --git a/src/com/ranfa/languages/List_ja_JP.properties b/src/com/ranfa/languages/List_ja_JP.properties index 009177a..f0ecea4 100644 --- a/src/com/ranfa/languages/List_ja_JP.properties +++ b/src/com/ranfa/languages/List_ja_JP.properties @@ -25,4 +25,6 @@ MSGCompletePost:\u6295\u7a3f\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002 MSGTerminate:\u7d42\u4e86 MSGInternalYpdateNotDoneYet:\u5185\u90e8\u66f4\u65b0\u51e6\u7406\u304c\u5b8c\u4e86\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u5c11\u3057\u5f85\u3063\u3066\u304b\u3089\u3084\u308a\u76f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002 MSGNarrowDownProcedure:\u697d\u66f2\u9078\u629e\u306e\u624b\u9806\r\n\uff11\uff0e\u96e3\u6613\u5ea6\u3001\u5c5e\u6027\u3001\u30ec\u30d9\u30eb\u3092\u9078\u629e\u3059\u308b\r\n\uff12\uff0e\u300c\u697d\u66f2\u53d6\u308a\u8fbc\u307f\u300d\u30dc\u30bf\u30f3\u3092\u62bc\u3059\uff01\r\n\uff13\uff0e\u300c\u958b\u59cb\u300d\u30dc\u30bf\u30f3\u3092\u62bc\u3059\uff01\r\n\uff14\uff0e\u9078\u629e\u3055\u308c\u305f\u697d\u66f2\u304c\u3053\u3053\u306b\u8868\u793a\u3055\u308c\u307e\u3059\uff01\r\n\u73fe\u5728\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u697d\u66f2\u9078\u629e\u306e\u6700\u5927\u6570\uff1a -MSGCurrentAlbumType:\n\u73fe\u5728\u306eMASTER+\u30a2\u30eb\u30d0\u30e0\u5468\u671f\uff08\u63a8\u5b9a\uff09\uff1a \ No newline at end of file +MSGCurrentAlbumType:\n\u73fe\u5728\u306eMASTER+\u30a2\u30eb\u30d0\u30e0\u5468\u671f\uff08\u63a8\u5b9a\uff09\uff1a +MSGManualUpdate:\u624b\u52d5\u66f4\u65b0 +MSGManualUpdateNotCompleteYet:\u624b\u52d5\u66f4\u65b0\u304c\u5b8c\u4e86\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u3082\u3046\u3057\u3070\u3089\u304f\u304a\u5f85\u3061\u304f\u3060\u3055\u3044\u3002 \ No newline at end of file diff --git a/src/com/ranfa/lib/ManualUpdateThreadImpl.java b/src/com/ranfa/lib/ManualUpdateThreadImpl.java new file mode 100644 index 0000000..8ff7efd --- /dev/null +++ b/src/com/ranfa/lib/ManualUpdateThreadImpl.java @@ -0,0 +1,59 @@ +package com.ranfa.lib; + +import java.util.ArrayList; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.function.BiConsumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ManualUpdateThreadImpl implements Runnable { + + //Declare flag + private static boolean flag = true; + + //Declare Executor service + private Executor executor = Executors.newWorkStealingPool(); + private BiConsumer, ArrayList> updateConsumer = (list1, list2) -> { + this.logger.info("Checking database updates..."); + if(list1.size() > list2.size()) { + long time = System.currentTimeMillis(); + this.logger.info("{} Update detected.", (list1.size() - list2.size())); + Scraping.writeToJson(list1); + this.logger.info("Update completed in {} ms", (System.currentTimeMillis() - time)); + this.logger.info("Updated database size: {}", list1.size()); + } else { + this.logger.info("database is up-to-date."); + } + }; + + //Declare logger + private Logger logger = LoggerFactory.getLogger(ManualUpdateThreadImpl.class); + + public ManualUpdateThreadImpl() { + this.logger.info("ManualUpdateThread is now available."); + } + + @Override + public void run() { + if (flag) { + flag = false; + } + this.logger.info("Checking database updates..."); + CompletableFuture> webData = CompletableFuture.supplyAsync(Scraping::getWholeData, this.executor); + CompletableFuture> localData = CompletableFuture.supplyAsync(Scraping::getFromJson, this.executor); + try { + this.updateConsumer.accept(webData.get(), localData.get()); + } catch (InterruptedException | ExecutionException e) { + this.logger.warn("Update failed.", e); + } + flag = true; + } + + public boolean getFlag() { + return flag; + } +} diff --git a/src/com/ranfa/main/DelesteRandomSelector.java b/src/com/ranfa/main/DelesteRandomSelector.java index 935842b..0feeb6d 100644 --- a/src/com/ranfa/main/DelesteRandomSelector.java +++ b/src/com/ranfa/main/DelesteRandomSelector.java @@ -34,6 +34,7 @@ import com.jgoodies.forms.layout.FormSpecs; import com.jgoodies.forms.layout.RowSpec; import com.ranfa.lib.CheckVersion; import com.ranfa.lib.EstimateAlbumTypeCycle; +import com.ranfa.lib.ManualUpdateThreadImpl; import com.ranfa.lib.Scraping; import com.ranfa.lib.SettingJSONProperty; import com.ranfa.lib.Settings; @@ -74,6 +75,9 @@ public class DelesteRandomSelector extends JFrame { private CompletableFuture albumTypeEstimateFuture = null; private String albumType = Messages.MSGAlbumTypeBeingCalculated.toString(); private Logger logger = LoggerFactory.getLogger(DelesteRandomSelector.class); + private ManualUpdateThreadImpl impl; + private Thread manualUpdateThread; + private JButton btnNewButton; /** * Launch the application. @@ -163,8 +167,8 @@ public class DelesteRandomSelector extends JFrame { } this.logger.debug("Version: {}", CheckVersion.getVersion()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.setBounds(100, 100, this.property.getWindowWidth(), this.property.getWindowHeight()); - // this.setBounds(100, 100, 640, 360); + // this.setBounds(100, 100, this.property.getWindowWidth(), this.property.getWindowHeight()); + this.setBounds(100, 100, 640, 360); this.contentPane = new JPanel(); this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); this.setContentPane(this.contentPane); @@ -269,6 +273,11 @@ public class DelesteRandomSelector extends JFrame { this.btnImport = new JButton(Messages.MSGUpdatingDatabase.toString()); this.btnImport.setEnabled(false); this.btnImport.addActionListener(e -> { + if(this.impl != null) { + if(!this.impl.getFlag()) { + JOptionPane.showMessageDialog(null, Messages.MSGManualUpdateNotCompleteYet.toString()); + } + } ArrayList fromJson = Scraping.getFromJson(); ArrayList specificlevelList = Scraping.getSpecificLevelSongs(fromJson, (Integer)DelesteRandomSelector.this.spinnerLevel.getValue(), DelesteRandomSelector.this.checkLessLv.isSelected(), DelesteRandomSelector.this.checkMoreLv.isSelected()); ArrayList specificDifficultyList = Scraping.getSpecificDifficultySongs(specificlevelList, DelesteRandomSelector.this.comboDifficultySelect.getSelectedItem().toString()); @@ -351,11 +360,21 @@ public class DelesteRandomSelector extends JFrame { DelesteRandomSelector.this.logger.info("seems to reject the permission.it should need try again."); } }); + + this.btnNewButton = new JButton(Messages.MSGManualUpdate.toString()); + this.btnNewButton.addActionListener(e -> { + this.impl = new ManualUpdateThreadImpl(); + this.manualUpdateThread = new Thread(this.impl); + this.manualUpdateThread.setName("ManualUpdate-thread"); + this.manualUpdateThread.setDaemon(false); + this.manualUpdateThread.start(); + }); + this.panelEast.add(this.btnNewButton, "1, 9"); this.panelEast.add(this.btnTwitterIntegration, "1, 11"); this.btnExit = new JButton(Messages.MSGTerminate.toString()); this.btnExit.addActionListener(e -> { - if(DelesteRandomSelector.this.softwareUpdateFuture.isDone() || DelesteRandomSelector.this.albumTypeEstimateFuture.isDone()) { + if(DelesteRandomSelector.this.softwareUpdateFuture.isDone() || DelesteRandomSelector.this.albumTypeEstimateFuture.isDone() || !this.impl.getFlag()) { DelesteRandomSelector.this.logger.info("Requested Exit by Button"); System.exit(0); } else { diff --git a/src/com/ranfa/main/Messages.java b/src/com/ranfa/main/Messages.java index bd34357..78cfedc 100644 --- a/src/com/ranfa/main/Messages.java +++ b/src/com/ranfa/main/Messages.java @@ -37,7 +37,9 @@ public enum Messages { MSGTerminate, MSGInternalYpdateNotDoneYet, MSGNarrowDownProcedure, - MSGCurrentAlbumType; + MSGCurrentAlbumType, + MSGManualUpdate, + MSGManualUpdateNotCompleteYet; @Override diff --git a/src/test/LanguageTest.java b/src/test/LanguageTest.java index 985fa7e..bbdcc87 100644 --- a/src/test/LanguageTest.java +++ b/src/test/LanguageTest.java @@ -50,6 +50,8 @@ public class LanguageTest { assertTrue(Messages.MSGInternalYpdateNotDoneYet.toString().equals("内部更新処理が完了していません。少し待ってからやり直してください。")); assertTrue(Messages.MSGNarrowDownProcedure.toString().equals("楽曲選択の手順\r\n1.難易度、属性、レベルを選択する\r\n2.「楽曲取り込み」ボタンを押す!\r\n3.「開始」ボタンを押す!\r\n4.選択された楽曲がここに表示されます!\r\n現在設定されている楽曲選択の最大数:")); assertTrue(Messages.MSGCurrentAlbumType.toString().equals("\n現在のMASTER+アルバム周期(推定):")); + assertTrue(Messages.MSGManualUpdate.toString().equals("手動更新")); + assertTrue(Messages.MSGManualUpdateNotCompleteYet.toString().equals("手動更新が完了していません。もうしばらくお待ちください。")); } @Test @@ -86,6 +88,8 @@ public class LanguageTest { assertTrue(Messages.MSGInternalYpdateNotDoneYet.toString().equals("Internal update in progress. Please wait a moment.")); assertTrue(Messages.MSGNarrowDownProcedure.toString().equals("How to select songs\r\n1.Select difficulty, attribute, and level.\r\n2.Click [Narrow down songs] button.\r\n3.Click [start!] button.\r\n4.Selected songs will be shown here!\r\nThe maximum number of selected songs:")); assertTrue(Messages.MSGCurrentAlbumType.toString().equals("\nCurrent MASTER+ ALBUM type(based on simulation):")); + assertTrue(Messages.MSGManualUpdate.toString().equals("Manual
Update")); + assertTrue(Messages.MSGManualUpdateNotCompleteYet.toString().equals("Manual Update has not been finished yet. Please wait a moment.")); } @After -- 2.11.0