From 1b9fef13d671108d846c8048a4533454c1ba2cf9 Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Tue, 9 May 2023 15:10:15 +0200 Subject: [PATCH] Fixed Sdeleni bug caused by emoji --- .../thastertyn/Login/LocalCredentials.java | 17 +++++++ .../xyz/thastertyn/Login/LoginController.java | 14 ++++- .../xyz/thastertyn/Scrape/OmluvnyList.java | 51 +++++++++++++++++-- .../java/xyz/thastertyn/Scrape/Sdeleni.java | 7 ++- .../xyz/thastertyn/Types/OmluvnyList.java | 24 ++++++++- .../UserInterface/Content/JecnaContent.java | 9 +++- .../UserInterface/Content/OmluvnyList.java | 7 ++- .../UserInterface/Content/Sdeleni.java | 24 ++++++++- .../UserInterface/Dialogs/EscapeDialog.java | 13 +++-- .../thastertyn/UserInterface/MainWindow.java | 5 +- .../UserInterface/WindowSwitchListener.java | 42 +++++++++------ 11 files changed, 171 insertions(+), 42 deletions(-) diff --git a/src/main/java/xyz/thastertyn/Login/LocalCredentials.java b/src/main/java/xyz/thastertyn/Login/LocalCredentials.java index 9178b16..a8ed630 100644 --- a/src/main/java/xyz/thastertyn/Login/LocalCredentials.java +++ b/src/main/java/xyz/thastertyn/Login/LocalCredentials.java @@ -17,6 +17,8 @@ public class LocalCredentials { * */ + private static LocalCredentials localCredentials = new LocalCredentials(); + private String path; private File credentialsFile; @@ -109,6 +111,7 @@ public class LocalCredentials { writer.append("user=" + credentials[0]); writer.append("\n"); writer.append("pass=" + credentials[1]); + writer.append("\n"); writer.close(); } catch (IOException e) { @@ -118,5 +121,19 @@ public class LocalCredentials { return true; } + + public boolean deleteCredentials() + { + credentialsFile.delete(); + return true; + } + + private LocalCredentials() {} + + public static LocalCredentials getInstance() + { + return localCredentials; + } + } diff --git a/src/main/java/xyz/thastertyn/Login/LoginController.java b/src/main/java/xyz/thastertyn/Login/LoginController.java index 1613061..6a32de8 100644 --- a/src/main/java/xyz/thastertyn/Login/LoginController.java +++ b/src/main/java/xyz/thastertyn/Login/LoginController.java @@ -16,7 +16,7 @@ public class LoginController { private WindowBasedTextGUI textGUI; private xyz.thastertyn.Login.CredentialsInput dialog; - private LocalCredentials localCredentials = new LocalCredentials(); + private LocalCredentials localCredentials = LocalCredentials.getInstance(); private xyz.thastertyn.Login.Login login = new xyz.thastertyn.Login.Login(); public LoginController(WindowBasedTextGUI textGUI) @@ -45,6 +45,18 @@ public class LoginController { loginUsingCredentials(credentials); } + public void loginUsingGui() + { + dialog = new CredentialsInput(); + Triplet data = dialog.showDialog(textGUI); // Failed to get credentials to log in, get them from user + String[] credentials = new String[] {data.getValue0(), data.getValue1()}; + + if(data.getValue2()) + { + localCredentials.saveCredentials(credentials); + } + } + private void loginUsingCredentials(String[] credentials) { try { diff --git a/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java b/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java index 3d21313..846dd9b 100644 --- a/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java +++ b/src/main/java/xyz/thastertyn/Scrape/OmluvnyList.java @@ -1,20 +1,23 @@ package xyz.thastertyn.Scrape; import java.io.IOException; +import java.time.LocalDate; +import java.time.Month; import java.util.ArrayList; +import java.util.Arrays; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import xyz.thastertyn.Tuples.Pair; import xyz.thastertyn.Types.Choice; import xyz.thastertyn.Types.Option; import xyz.thastertyn.Types.Options; public class OmluvnyList extends JecnaScrape { - private ArrayList> data; + private ArrayList data; + private Choice currentChoice; private Options schoolYearOptions; @@ -26,6 +29,9 @@ public class OmluvnyList extends JecnaScrape { @Override public void download(Choice choice) throws IOException { + + currentChoice = choice; + download(String.format( "https://www.spsejecna.cz/absence/student?schoolYearId=%s", choice.getChoices().get(0))); @@ -42,9 +48,13 @@ public class OmluvnyList extends JecnaScrape { for(Element e : omluvneListy) { String date = e.child(0).text(); + + String text = e.child(1).text(); - data.add(new Pair(date, text)); + data.add( + new xyz.thastertyn.Types.OmluvnyList( + parseDate(date), text)); } Elements options = omluvnyListDokumentHTML.select("form.listConfigure").select("select[id=schoolYearId]").select("option"); @@ -53,6 +63,39 @@ public class OmluvnyList extends JecnaScrape { { schoolYearOptions.addOption(new Option(e.text(), e.attr("value"))); } + + currentChoice = new Choice(Arrays.asList(schoolYearOptions.getOptions().get(0).getValue())); + } + + private LocalDate parseDate(String text) + { + int year = 0, month = 0, day = 0; + String[] split = text.split("\\."); + + month = Integer.parseInt(split[0]); + day = Integer.parseInt(split[1]); + + if(currentChoice == null) + { + // Pick the current year + int currYear = LocalDate.now().getYear(); + int currMonth = LocalDate.now().getMonthValue(); + + if(month > currMonth && currMonth < 8) + { + year = currYear; + }else if(month < currMonth && currMonth > 8) + { + year = currYear + 1; + }else if(month < currMonth && currMonth < 8) + { + year = currYear; + } + }else{ + year = Integer.parseInt(currentChoice.getChoices().get(0)); + } + + return LocalDate.of(year, Month.of(month), day); } @Override @@ -60,7 +103,7 @@ public class OmluvnyList extends JecnaScrape { return new Options[] {schoolYearOptions}; } - public ArrayList> getData() + public ArrayList getData() { return data; } diff --git a/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java b/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java index 03b4784..4f50ab4 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java +++ b/src/main/java/xyz/thastertyn/Scrape/Sdeleni.java @@ -33,9 +33,9 @@ public class Sdeleni extends JecnaScrape { if(isPositive) { - label = "✅" + " " + label; + label = "✓" + "\u00a0" + label; }else{ - label = "❌" + " " + label; + label = "✗" + "\u00a0" + label; } sdeleniList.add(label); @@ -49,11 +49,10 @@ public class Sdeleni extends JecnaScrape { @Override public Options[] getOptions() { - throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); + return null; } @Override public void download(Choice choice) throws IOException { - throw new UnsupportedOperationException("Unimplemented method 'download'"); } } diff --git a/src/main/java/xyz/thastertyn/Types/OmluvnyList.java b/src/main/java/xyz/thastertyn/Types/OmluvnyList.java index 373671b..5b17ea7 100644 --- a/src/main/java/xyz/thastertyn/Types/OmluvnyList.java +++ b/src/main/java/xyz/thastertyn/Types/OmluvnyList.java @@ -1,13 +1,33 @@ package xyz.thastertyn.Types; +import java.time.LocalDate; + public class OmluvnyList { - private String datum; + private LocalDate datum; private String popis; - public OmluvnyList(String datum, String popis) + public OmluvnyList(LocalDate datum, String popis) { this.datum = datum; this.popis = popis; } + + public LocalDate getDatum() { + return datum; + } + + public void setDatum(LocalDate datum) { + this.datum = datum; + } + + public String getPopis() { + return popis; + } + + public void setPopis(String popis) { + this.popis = popis; + } + + } diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java b/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java index e760330..7418b86 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/JecnaContent.java @@ -40,8 +40,7 @@ public abstract class JecnaContent { }else{ scraper.download(); setGUI(); - listener.updatePanel(); - } + } hasStarted = true; } @@ -52,6 +51,12 @@ public abstract class JecnaContent { } public void showOptions(WindowBasedTextGUI textGUI) throws IOException { + + if(scraper.getOptions() == null) + { + return; + } + OptionsDialog dialog = new OptionsDialog(scraper.getOptions()); Choice choice = dialog.showDialog(textGUI); diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java b/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java index e981f3c..238644e 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/OmluvnyList.java @@ -8,7 +8,6 @@ import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.LinearLayout; import com.googlecode.lanterna.gui2.Panel; -import xyz.thastertyn.Tuples.Pair; import xyz.thastertyn.UserInterface.UpdateListener; public class OmluvnyList extends JecnaContent{ @@ -28,15 +27,15 @@ public class OmluvnyList extends JecnaContent{ protected void setGUI() { this.mainPanel.removeAllComponents(); - ArrayList> a = omluvnyList.getData(); + ArrayList a = omluvnyList.getData(); Panel content = new Panel() .setLayoutManager(new LinearLayout(Direction.VERTICAL)) .addTo(mainPanel); - for(Pair p : a) + for(xyz.thastertyn.Types.OmluvnyList omluvnyList : a) { - content.addComponent(new Label(p.getValue0() + " - " + p.getValue1())); + content.addComponent(new Label(omluvnyList.getDatum() + " - " + omluvnyList.getPopis())); } } } diff --git a/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java b/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java index 166af7c..595d7ff 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Content/Sdeleni.java @@ -2,8 +2,11 @@ package xyz.thastertyn.UserInterface.Content; import java.util.ArrayList; +import com.googlecode.lanterna.TextColor.ANSI; +import com.googlecode.lanterna.gui2.Direction; import com.googlecode.lanterna.gui2.GridLayout; import com.googlecode.lanterna.gui2.Label; +import com.googlecode.lanterna.gui2.LinearLayout; import com.googlecode.lanterna.gui2.Panel; import xyz.thastertyn.UserInterface.UpdateListener; @@ -29,6 +32,25 @@ public class Sdeleni extends JecnaContent { ArrayList sdeleniList = sdeleni.getSdeleni(); - sdeleniList.forEach(sdeleni -> mainPanel.addComponent(new Label(sdeleni))); + for(String sdeleni : sdeleniList) + { + Panel row = new Panel().setLayoutManager(new LinearLayout(Direction.HORIZONTAL)); + + String[] split = sdeleni.split("\u00a0"); + + Label check = new Label(split[0]); + Label text = new Label(split[1]); + + if(split[0].equals("✓")) + { + check.setForegroundColor(ANSI.GREEN); + }else{ + check.setForegroundColor(ANSI.RED); + } + + row.addComponent(check) + .addComponent(text) + .addTo(mainPanel); + } } } diff --git a/src/main/java/xyz/thastertyn/UserInterface/Dialogs/EscapeDialog.java b/src/main/java/xyz/thastertyn/UserInterface/Dialogs/EscapeDialog.java index 0466e43..0ccd1dc 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/Dialogs/EscapeDialog.java +++ b/src/main/java/xyz/thastertyn/UserInterface/Dialogs/EscapeDialog.java @@ -11,6 +11,9 @@ import com.googlecode.lanterna.gui2.Window; import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.dialogs.DialogWindow; +import xyz.thastertyn.Login.LocalCredentials; +import xyz.thastertyn.Login.LoginController; + public class EscapeDialog extends DialogWindow { public EscapeDialog() @@ -42,16 +45,16 @@ public class EscapeDialog extends DialogWindow { public void onDeleteLogin() { - close(); - - // Call login manager to delete login + LocalCredentials.getInstance().deleteCredentials(); + LoginController controller = new LoginController(getTextGUI()); + controller.loginUsingGui(); } public void onLogout() { close(); - - // Go back to square one and start logging in again + LoginController controller = new LoginController(getTextGUI()); + controller.loginUsingGui(); } public void onExit() diff --git a/src/main/java/xyz/thastertyn/UserInterface/MainWindow.java b/src/main/java/xyz/thastertyn/UserInterface/MainWindow.java index 206aad2..e9344e6 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/MainWindow.java +++ b/src/main/java/xyz/thastertyn/UserInterface/MainWindow.java @@ -37,7 +37,7 @@ public class MainWindow { screen = new TerminalScreen(terminal); screen.startScreen(); - + // Create window to hold the panel window = new BasicWindow(); window.setHints(Arrays.asList(Window.Hint.CENTERED)); @@ -52,14 +52,13 @@ public class MainWindow { window.setComponent(mainPanel); - Panel content = new Panel(); mainPanel.addComponent(content.withBorder(Borders.singleLine("Jecnak"))); LoginController controller = new LoginController(textGUI); controller.login(); - window.addWindowListener(new WindowSwitchListener(content, textGUI)); + window.addWindowListener(new WindowSwitchListener(content, textGUI, screen)); textGUI.addWindowAndWait(window); diff --git a/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java b/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java index f969a5f..2faba0b 100644 --- a/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java +++ b/src/main/java/xyz/thastertyn/UserInterface/WindowSwitchListener.java @@ -17,6 +17,8 @@ import com.googlecode.lanterna.gui2.dialogs.MessageDialog; import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton; import com.googlecode.lanterna.input.KeyStroke; import com.googlecode.lanterna.input.KeyType; +import com.googlecode.lanterna.screen.Screen; +import com.googlecode.lanterna.screen.Screen.RefreshType; import xyz.thastertyn.UserInterface.Content.JecnaContent; import xyz.thastertyn.UserInterface.Content.OmluvnyList; @@ -40,15 +42,17 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { private Panel tabsPanel = new Panel() .setLayoutManager(new LinearLayout(Direction.HORIZONTAL)); private Panel holderPanel = new Panel(); + private Screen screen; int current = contents.length - 1; - public WindowSwitchListener(Panel holder, WindowBasedTextGUI textGUI) + public WindowSwitchListener(Panel holder, WindowBasedTextGUI textGUI, Screen screen) { this.textGUI = textGUI; + this.screen = screen; holder.addComponent(tabsPanel); holder.addComponent(holderPanel.withBorder(Borders.singleLine())); - + for(int i = 0; i < contents.length; i++) { this.tabs[i] = contents[i].getLabel(); @@ -90,6 +94,11 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { setPanelPreferedSize(); updateLabels(); + try { + screen.refresh(RefreshType.COMPLETE); + } catch (IOException e) { + e.printStackTrace(); + } } private void checkIfDownloaded() @@ -108,25 +117,26 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { private void setPanelPreferedSize() { int tabColumns = tabsPanel - .getSize() - .getColumns(); + .getSize() + .getColumns(); int currentColumns = contents[current] - .getPanel() - .getSize() - .getColumns(); + .getPanel() + .getSize() + .getColumns(); if(currentColumns < tabColumns) { holderPanel.addComponent( - contents[current].getPanel() + contents[current] + .getPanel() .setPreferredSize( new TerminalSize( - tabColumns - 2, + tabColumns, contents[current].getPanel().getPreferredSize().getRows()))); return; } - + holderPanel.addComponent(contents[current].getPanel()); } @@ -160,12 +170,10 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { switch(type) { case Tab: - if(keyStroke.isShiftDown()) - { - previous(); - }else{ - next(); - } + next(); + break; + case ReverseTab: + previous(); break; case Character: if(keyStroke.getCharacter() == ' ') @@ -184,6 +192,7 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { @Override public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) { + hasBeenHandled.set(true); } @Override @@ -193,6 +202,7 @@ public class WindowSwitchListener implements WindowListener, UpdateListener { @Override public void onMoved(Window window, TerminalPosition oldPosition, TerminalPosition newPosition) { + window.invalidate(); } @Override