Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Commit

Permalink
Add announcement checker and UI tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
a1aw committed Jun 30, 2020
1 parent b36925d commit 371975c
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
Expand Down
16 changes: 12 additions & 4 deletions osumer-ui/src/main/resources/view/RootLayout.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<tabs>
<Tab text="Quick Start">
<content>
<VBox prefHeight="200.0" prefWidth="100.0">
<VBox prefHeight="200.0" prefWidth="600.0">
<children>
<VBox minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="600.0">
<children>
Expand Down Expand Up @@ -100,7 +100,15 @@
</Button>
</right>
</BorderPane>
<Label prefWidth="568.0" text="Specify a beatmap link or ID to start downloading. (e.g. s320118 or b714001 or https://osu.ppy.sh/s/320118/)" wrapText="true">
<Label prefHeight="34.0" prefWidth="600.0" text="Specify a beatmap link or ID to start downloading." wrapText="true">
<VBox.margin>
<Insets left="16.0" right="16.0" />
</VBox.margin>
</Label>
<Label layoutX="26.0" layoutY="51.0" prefHeight="34.0" prefWidth="600.0" text="(e.g. s320118 or b714001 or https://osu.ppy.sh/s/320118/)" wrapText="true">
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets bottom="16.0" left="16.0" right="16.0" />
</VBox.margin>
Expand Down Expand Up @@ -212,7 +220,7 @@
</Label>
</children>
</FlowPane>
<Label fx:id="updateText" alignment="CENTER_RIGHT" prefHeight="17.0" prefWidth="296.0" text="Checking for updates..." GridPane.columnIndex="1" />
<Label fx:id="updateText" alignment="CENTER_RIGHT" prefHeight="17.0" prefWidth="296.0" text="Checking for updates..." wrapText="true" GridPane.columnIndex="1" />
</children>
<BorderPane.margin>
<Insets bottom="8.0" left="8.0" right="8.0" top="8.0" />
Expand All @@ -222,6 +230,6 @@
</BorderPane>
</center>
<bottom>
<Label fx:id="announcementLabel" text="Loading announcements..." BorderPane.alignment="CENTER" />
<Label fx:id="announcementLabel" text="Loading announcements..." wrapText="true" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>

0 comments on commit 371975c

Please sign in to comment.