From b4c1e53c834f5a8d5065b677dad9618f9eaff8b6 Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Tue, 2 May 2023 15:04:52 +0200 Subject: [PATCH] trying to optimize a bit and improve looks --- .../xyz/thastertyn/Scrape/JecnaScrape.java | 9 ++- .../java/xyz/thastertyn/Scrape/Jidelna.java | 25 +++++++- .../xyz/thastertyn/Scrape/OmluvnyList.java | 20 ++++++- .../java/xyz/thastertyn/Scrape/Rozvrh.java | 20 ++++++- .../java/xyz/thastertyn/Scrape/Sdeleni.java | 19 ++++++- .../java/xyz/thastertyn/Scrape/Znamky.java | 7 ++- .../UserInterface/Content/JecnaContent.java | 15 ++++- .../UserInterface/Content/OmluvnyList.java | 25 ++++---- .../UserInterface/Content/Rozvrh.java | 2 +- .../UserInterface/Content/Sdeleni.java | 57 ++++++------------- .../UserInterface/Content/Znamky.java | 19 +------ .../UserInterface/Dialogs/OptionsDialog.java | 5 +- 12 files changed, 136 insertions(+), 87 deletions(-) diff --git a/src/main/java/xyz/thastertyn/Scrape/JecnaScrape.java b/src/main/java/xyz/thastertyn/Scrape/JecnaScrape.java index e38836d..637536b 100644 --- a/src/main/java/xyz/thastertyn/Scrape/JecnaScrape.java +++ b/src/main/java/xyz/thastertyn/Scrape/JecnaScrape.java @@ -1,10 +1,13 @@ package xyz.thastertyn.Scrape; import java.io.IOException; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; + +import xyz.thastertyn.Types.Choice; +import xyz.thastertyn.Types.Options; public abstract class JecnaScrape { - public abstract void download() throws SocketTimeoutException, UnknownHostException, IOException; + public abstract Options[] getOptions(); + public abstract void download(Choice choice) throws IOException; + public abstract void download() throws IOException; } diff --git a/src/main/java/xyz/thastertyn/Scrape/Jidelna.java b/src/main/java/xyz/thastertyn/Scrape/Jidelna.java index 7ca2285..d939b45 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Jidelna.java +++ b/src/main/java/xyz/thastertyn/Scrape/Jidelna.java @@ -1,5 +1,28 @@ package xyz.thastertyn.Scrape; -public class Jidelna { +import java.io.IOException; + +import xyz.thastertyn.Types.Choice; +import xyz.thastertyn.Types.Options; + +public class Jidelna extends JecnaScrape { + + @Override + public Options[] getOptions() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); + } + + @Override + public void download(Choice choice) throws IOException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'download'"); + } + + @Override + public void download() throws IOException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'download'"); + } } diff --git a/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java b/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java index d038091..884f5b7 100644 --- a/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java +++ b/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java @@ -1,7 +1,6 @@ package xyz.thastertyn.Scrape; import java.io.IOException; -import java.net.UnknownHostException; import java.util.ArrayList; import org.jsoup.nodes.Document; @@ -9,12 +8,15 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import xyz.thastertyn.Tuples.Pair; +import xyz.thastertyn.Types.Choice; +import xyz.thastertyn.Types.Options; -public class OmluvnyList { +public class OmluvnyList extends JecnaScrape { private ArrayList> data = new ArrayList<>(); - public void downloadOmluvnyList() throws UnknownHostException, IOException + @Override + public void download() throws IOException { Document omluvnyListDokumentHTML = Downloader.download("https://www.spsejecna.cz/absence/student").get(); @@ -39,4 +41,16 @@ public class OmluvnyList { { return (!data.isEmpty()) ? data.toString() : null; } + + @Override + public Options[] getOptions() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); + } + + @Override + public void download(Choice choice) throws IOException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'download'"); + } } diff --git a/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java b/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java index 1ea1a0e..57bca1e 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java +++ b/src/main/java/xyz/thastertyn/Scrape/Rozvrh.java @@ -9,10 +9,13 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import xyz.thastertyn.Types.Choice; +import xyz.thastertyn.Types.Options; + /** * Jeden radek v rozvrhu */ -public class Rozvrh { +public class Rozvrh extends JecnaScrape { private String[][] rozvrh = new String[5][10]; @@ -23,7 +26,8 @@ public class Rozvrh { * @throws UnknownHostException kdyz neni pripojeni k internetu * @throws IOException ostatni exceptiony nejsou dulezite, tak jsou zahrnuty v jednom */ - public void downloadRozvrh() throws UnknownHostException, IOException + @Override + public void download() throws IOException { Document rozvrhDokumentHTML = Downloader.download("https://www.spsejecna.cz/timetable/class").get(); @@ -71,4 +75,16 @@ public class Rozvrh { return s; } + + @Override + public Options[] getOptions() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); + } + + @Override + public void download(Choice choice) throws IOException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'download'"); + } } diff --git a/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java b/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java index fa3a4bf..640f43b 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java +++ b/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java @@ -8,11 +8,14 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -public class Sdeleni { +import xyz.thastertyn.Types.Choice; +import xyz.thastertyn.Types.Options; + +public class Sdeleni extends JecnaScrape { ArrayList sdeleniList = new ArrayList<>(); - public void downloadSdeleni() throws UnknownHostException, IOException + public void download() throws UnknownHostException, IOException { Document sdeleniDoc = Downloader.download("https://www.spsejecna.cz/user-student/record-list").get(); @@ -43,4 +46,16 @@ public class Sdeleni { { return sdeleniList; } + + @Override + public Options[] getOptions() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); + } + + @Override + public void download(Choice choice) throws IOException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'download'"); + } } diff --git a/src/main/java/xyz/thastertyn/Scrape/Znamky.java b/src/main/java/xyz/thastertyn/Scrape/Znamky.java index 6b260c6..d6f7e67 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Znamky.java +++ b/src/main/java/xyz/thastertyn/Scrape/Znamky.java @@ -16,7 +16,7 @@ import xyz.thastertyn.Types.Options; import xyz.thastertyn.Types.Predmet; import xyz.thastertyn.Types.Znamka; -public class Znamky { +public class Znamky extends JecnaScrape { private boolean wasDownloaded; @@ -27,12 +27,13 @@ public class Znamky { private Options schoolYearOptions = new Options("Skolni R."); private Options schoolHalfYearOptions = new Options("Pololeti"); - public void downloadZnamky() throws UnknownHostException, IOException + public void download() throws UnknownHostException, IOException { download("https://www.spsejecna.cz/score/student"); } - public void downloadZnamky(Choice choice) throws SocketTimeoutException, UnknownHostException, IOException + @Override + public void download(Choice choice) throws IOException { download(String.format( "https://www.spsejecna.cz/score/student?schoolYearId=%s&schoolYearHalfId=%s", diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java b/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java index 6cda6de..067be58 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java @@ -6,8 +6,10 @@ import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.WindowBasedTextGUI; +import xyz.thastertyn.Scrape.JecnaScrape; import xyz.thastertyn.Types.Choice; import xyz.thastertyn.UserInterface.UpdateListener; +import xyz.thastertyn.UserInterface.Dialogs.OptionsDialog; /** @@ -22,8 +24,19 @@ public abstract class JecnaContent { protected abstract void setGUI(); protected abstract void download(Choice choice) throws IOException; - public abstract void showOptions(final WindowBasedTextGUI textGUI) throws IOException; public abstract void downloadDefault() throws IOException; + protected JecnaScrape scraper; + + public void showOptions(WindowBasedTextGUI textGUI) throws IOException { + OptionsDialog d = new OptionsDialog(scraper.getOptions()); + + Choice c = d.showDialog(textGUI); + + if(c != null) + { + download(c); + } + } public Panel getPanel() { diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java b/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java index 1dd6913..add0ac4 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java @@ -1,7 +1,6 @@ package xyz.thastertyn.UserInterface.Content; import java.io.IOException; -import java.net.UnknownHostException; import java.util.ArrayList; import com.googlecode.lanterna.gui2.Label; @@ -26,26 +25,26 @@ public class OmluvnyList extends JecnaContent{ } @Override - public void downloadDefault() + public void downloadDefault() throws IOException { download(null); } @Override - protected void download(Choice choice) + protected void download(Choice choice) throws IOException { - try{ - omluvnyList.downloadOmluvnyList(); + omluvnyList = new xyz.thastertyn.Scrape.OmluvnyList(); + if(choice != null) + { + omluvnyList.download(choice); + setGUI(); + listener.updatePanel(); + }else{ + omluvnyList.download(); setGUI(); - - hasStarted = true; - }catch(UnknownHostException e) - { - // omluvnyPanel.addComponent(new Label("A connection error occurred")); - }catch(IOException e) - { - // omluvnyPanel.addComponent(new Label("An error occurred")); } + + super.hasStarted = true; } @Override diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java b/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java index a8e8200..05848ff 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/Rozvrh.java @@ -41,7 +41,7 @@ public class Rozvrh extends JecnaContent { protected void download(Choice choice) { try{ - rozvrh.downloadRozvrh(); + rozvrh.download(); setGUI(); hasStarted = true; diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java b/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java index 2098720..4077a73 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import com.googlecode.lanterna.gui2.GridLayout; import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Panel; -import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import xyz.thastertyn.Types.Choice; import xyz.thastertyn.UserInterface.UpdateListener; @@ -19,34 +18,36 @@ public class Sdeleni extends JecnaContent { private Label borderLabel = new Label("Sdeleni R."); private xyz.thastertyn.Scrape.Sdeleni sdeleni = new xyz.thastertyn.Scrape.Sdeleni(); - - private boolean hasStarted = false; - + public Sdeleni(UpdateListener listener) { + super.mainPanel = this.mainPanel; + super.borderLabel = this.borderLabel; + super.scraper = this.sdeleni; this.listener = listener; } @Override - public void downloadDefault() + public void downloadDefault() throws IOException { download(null); } @Override - protected void download(Choice choice) + protected void download(Choice choice) throws IOException { - try{ - sdeleni.downloadSdeleni(); - - hasStarted = true; - }catch(UnknownHostException e) + sdeleni = new xyz.thastertyn.Scrape.Sdeleni(); + if(choice != null) { - - }catch(IOException e) - { - + sdeleni.download(choice); + setGUI(); + listener.updatePanel(); + }else{ + sdeleni.download(); + setGUI(); } + + super.hasStarted = true; } @Override @@ -58,30 +59,6 @@ public class Sdeleni extends JecnaContent { ArrayList sdeleniList = sdeleni.getSdeleni(); - for(String s : sdeleniList) - { - sdeleniPanel.addComponent(new Label(s)); - } - } - - @Override - public Label getLabel() { - return borderLabel; - } - - @Override - public Panel getPanel() { - return sdeleniPanel; - } - - @Override - public boolean hasStarted() { - return hasStarted; - } - - @Override - public void showOptions(WindowBasedTextGUI textGUI) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); + sdeleniList.forEach(s -> sdeleniPanel.addComponent(new Label(s))); } } diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java b/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java index 5e9ad50..ed7f47d 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/Znamky.java @@ -13,13 +13,11 @@ import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.LayoutData; import com.googlecode.lanterna.gui2.LinearLayout; import com.googlecode.lanterna.gui2.Panel; -import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import xyz.thastertyn.Types.Choice; import xyz.thastertyn.Types.Predmet; import xyz.thastertyn.Types.Znamka; import xyz.thastertyn.UserInterface.UpdateListener; -import xyz.thastertyn.UserInterface.Dialogs.OptionsDialog; public class Znamky extends JecnaContent { @@ -53,6 +51,7 @@ public class Znamky extends JecnaContent { { super.mainPanel = this.mainPanel; super.borderLabel = this.borderLabel; + super.scraper = this.znamky; this.listener = listener; } @@ -68,11 +67,11 @@ public class Znamky extends JecnaContent { znamky = new xyz.thastertyn.Scrape.Znamky(); if(choice != null) { - znamky.downloadZnamky(choice); + znamky.download(choice); setGUI(); listener.updatePanel(); }else{ - znamky.downloadZnamky(); + znamky.download(); setGUI(); } @@ -148,16 +147,4 @@ public class Znamky extends JecnaContent { vysledneZnamky.addComponent(vysl); } } - - @Override - public void showOptions(WindowBasedTextGUI textGUI) throws IOException { - OptionsDialog d = new OptionsDialog(znamky.getOptions()); - - Choice c = d.showDialog(textGUI); - - if(c != null) - { - download(c); - } - } } \ No newline at end of file diff --git a/src/main/java/xyz/thastertyn/UserInterface/Dialogs/OptionsDialog.java b/src/main/java/xyz/thastertyn/UserInterface/Dialogs/OptionsDialog.java index c646cdd..41b2056 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Dialogs/OptionsDialog.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Dialogs/OptionsDialog.java @@ -3,7 +3,7 @@ package xyz.thastertyn.UserInterface.Dialogs; import java.util.ArrayList; import java.util.Arrays; import java.util.List; - +import java.util.stream.Collectors; import com.googlecode.lanterna.gui2.Button; import com.googlecode.lanterna.gui2.ComboBox; @@ -94,7 +94,8 @@ public class OptionsDialog extends DialogWindow { // User pressed Ok new Choice(boxs.stream() .map(box -> box.getSelectedItem().getValue()) - .toList()) + .collect(Collectors.toList())) + // Why did .toList() stop working? : // User pressed Cancel null;