From 371975c06498e37db9f22b878c65766224f7029f Mon Sep 17 00:00:00 2001 From: mob41 Date: Wed, 1 Jul 2020 00:10:16 +0800 Subject: [PATCH] Add announcement checker and UI tweaks --- .../mob41/osumer/updater/Announcement.java | 24 ++++ .../osumer/updater/AnnouncementChecker.java | 121 ++++++++++++++++++ .../mob41/osumer/ui/MainController.java | 2 +- .../src/main/resources/view/RootLayout.fxml | 16 ++- 4 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 osumer-lib/src/main/java/com/github/mob41/osumer/updater/Announcement.java create mode 100644 osumer-lib/src/main/java/com/github/mob41/osumer/updater/AnnouncementChecker.java diff --git a/osumer-lib/src/main/java/com/github/mob41/osumer/updater/Announcement.java b/osumer-lib/src/main/java/com/github/mob41/osumer/updater/Announcement.java new file mode 100644 index 0000000..382760d --- /dev/null +++ b/osumer-lib/src/main/java/com/github/mob41/osumer/updater/Announcement.java @@ -0,0 +1,24 @@ +package com.github.mob41.osumer.updater; + +import java.util.Calendar; + +public class Announcement { + + private final Calendar time; + + private final String text; + + public Announcement(Calendar time, String text) { + this.time = time; + this.text = text; + } + + public Calendar getTime() { + return time; + } + + public String getText() { + return text; + } + +} diff --git a/osumer-lib/src/main/java/com/github/mob41/osumer/updater/AnnouncementChecker.java b/osumer-lib/src/main/java/com/github/mob41/osumer/updater/AnnouncementChecker.java new file mode 100644 index 0000000..65d67d6 --- /dev/null +++ b/osumer-lib/src/main/java/com/github/mob41/osumer/updater/AnnouncementChecker.java @@ -0,0 +1,121 @@ +package com.github.mob41.osumer.updater; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Calendar; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import com.github.mob41.osumer.debug.WithDumpException; + +public class AnnouncementChecker { + + private static final String ANNOUNCEMENT_LIST = "https://mob41.github.io/osumer-updater/announcements.json"; + + public Announcement[] getAnnouncements() throws WithDumpException{ + URL url = null; + + try { + url = new URL(ANNOUNCEMENT_LIST + "?update=" + Calendar.getInstance().getTimeInMillis()); + } catch (MalformedURLException e){ + throw new WithDumpException( + ANNOUNCEMENT_LIST, + "URL url = null;", + "new URL(VERSION_LIST);", + "URLConnection conn = url.openConnection();", + "", + false, e); + } + + String data = ""; + try { + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + conn.setRequestMethod("GET"); + conn.setRequestProperty("Connection", "close"); + conn.setUseCaches(false); + conn.setConnectTimeout(10000); + conn.setReadTimeout(10000); + + InputStream in = conn.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + String line; + while ((line = reader.readLine()) != null){ + data += line; + } + } catch (IOException e) { + throw new WithDumpException( + ANNOUNCEMENT_LIST, + "URL url = new URL(VERSION_LIST);", + "(lots of code) -- Connecting and fetch data", + "Data validating (isEmpty / null)", + "", + false, e); + } + + if (data == null || data.isEmpty()){ + throw new WithDumpException( + ANNOUNCEMENT_LIST, + "(lots of code) -- Connecting and fetch data", + "Data validating (isEmpty / null)", + "Create JSONObject", + "No data fetched. \"data\" is null/isEmpty", + false); + } + + JSONObject json = null; + try { + json = new JSONObject(data); + } catch (JSONException e){ + throw new WithDumpException( + ANNOUNCEMENT_LIST, + "Data validating (isEmpty / null)", + "Create JSONObject", + "JSONObject validating \"announcements\" parameter", + "Structure invalid", + false, e); + } + + if (json.isNull("announcements")){ + throw new WithDumpException( + ANNOUNCEMENT_LIST, + "Create JSONObject", + "JSONObject validating \"sources\" parameter", + "Parsing announcement json to objects", + "Structure invalid, missing \"announcements\" parameter", + false); + } + + JSONArray arr = json.getJSONArray("announcements"); + + Calendar cal; + JSONObject obj; + Announcement[] out = new Announcement[arr.length()]; + for (int i = 0; i < out.length; i++) { + obj = arr.getJSONObject(i); + if (obj.isNull("time") || obj.isNull("text")) { + throw new WithDumpException( + data, + "JSONObject validating \"sources\" parameter", + "Parsing announcement json to objects", + "Output array", + "Structure invalid, announcement json missing some parameters", + false); + } + cal = Calendar.getInstance(); + cal.setTimeInMillis(obj.getLong("time")); + out[i] = new Announcement(cal, obj.getString("text")); + } + + return out; + } + +} diff --git a/osumer-ui/src/main/java/com/github/mob41/osumer/ui/MainController.java b/osumer-ui/src/main/java/com/github/mob41/osumer/ui/MainController.java index 6a17450..98bce30 100644 --- a/osumer-ui/src/main/java/com/github/mob41/osumer/ui/MainController.java +++ b/osumer-ui/src/main/java/com/github/mob41/osumer/ui/MainController.java @@ -267,7 +267,7 @@ public void handle(ActionEvent event) { alert.setHeaderText("About"); alert.setContentText( "osumer2 (" + Osumer.getVersionString() + ")\n" + - "Copyright (c) mob41. 2016-2019\n\n" + + "Copyright (c) mob41. 2016-2020\n\n" + "osumer is an application that provides osu! players a\n" + "more comfortable and faster way to download beatmaps." ); diff --git a/osumer-ui/src/main/resources/view/RootLayout.fxml b/osumer-ui/src/main/resources/view/RootLayout.fxml index c2b724d..40642c2 100644 --- a/osumer-ui/src/main/resources/view/RootLayout.fxml +++ b/osumer-ui/src/main/resources/view/RootLayout.fxml @@ -67,7 +67,7 @@ - + @@ -100,7 +100,15 @@ - - @@ -222,6 +230,6 @@ -