Fixed Sdeleni bug caused by emoji

This commit is contained in:
Thastertyn 2023-05-09 15:10:15 +02:00
parent 88aec67833
commit 1b9fef13d6
11 changed files with 171 additions and 42 deletions

View File

@ -17,6 +17,8 @@ public class LocalCredentials {
* </ul> * </ul>
*/ */
private static LocalCredentials localCredentials = new LocalCredentials();
private String path; private String path;
private File credentialsFile; private File credentialsFile;
@ -109,6 +111,7 @@ public class LocalCredentials {
writer.append("user=" + credentials[0]); writer.append("user=" + credentials[0]);
writer.append("\n"); writer.append("\n");
writer.append("pass=" + credentials[1]); writer.append("pass=" + credentials[1]);
writer.append("\n");
writer.close(); writer.close();
} catch (IOException e) { } catch (IOException e) {
@ -118,5 +121,19 @@ public class LocalCredentials {
return true; return true;
} }
public boolean deleteCredentials()
{
credentialsFile.delete();
return true;
}
private LocalCredentials() {}
public static LocalCredentials getInstance()
{
return localCredentials;
}
} }

View File

@ -16,7 +16,7 @@ public class LoginController {
private WindowBasedTextGUI textGUI; private WindowBasedTextGUI textGUI;
private xyz.thastertyn.Login.CredentialsInput dialog; 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(); private xyz.thastertyn.Login.Login login = new xyz.thastertyn.Login.Login();
public LoginController(WindowBasedTextGUI textGUI) public LoginController(WindowBasedTextGUI textGUI)
@ -45,6 +45,18 @@ public class LoginController {
loginUsingCredentials(credentials); loginUsingCredentials(credentials);
} }
public void loginUsingGui()
{
dialog = new CredentialsInput();
Triplet<String, String, Boolean> 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) private void loginUsingCredentials(String[] credentials)
{ {
try { try {

View File

@ -1,20 +1,23 @@
package xyz.thastertyn.Scrape; package xyz.thastertyn.Scrape;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import xyz.thastertyn.Tuples.Pair;
import xyz.thastertyn.Types.Choice; import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Option; import xyz.thastertyn.Types.Option;
import xyz.thastertyn.Types.Options; import xyz.thastertyn.Types.Options;
public class OmluvnyList extends JecnaScrape { public class OmluvnyList extends JecnaScrape {
private ArrayList<Pair<String, String>> data; private ArrayList<xyz.thastertyn.Types.OmluvnyList> data;
private Choice currentChoice;
private Options schoolYearOptions; private Options schoolYearOptions;
@ -26,6 +29,9 @@ public class OmluvnyList extends JecnaScrape {
@Override @Override
public void download(Choice choice) throws IOException { public void download(Choice choice) throws IOException {
currentChoice = choice;
download(String.format( download(String.format(
"https://www.spsejecna.cz/absence/student?schoolYearId=%s", "https://www.spsejecna.cz/absence/student?schoolYearId=%s",
choice.getChoices().get(0))); choice.getChoices().get(0)));
@ -42,9 +48,13 @@ public class OmluvnyList extends JecnaScrape {
for(Element e : omluvneListy) for(Element e : omluvneListy)
{ {
String date = e.child(0).text(); String date = e.child(0).text();
String text = e.child(1).text(); String text = e.child(1).text();
data.add(new Pair<String, String>(date, text)); data.add(
new xyz.thastertyn.Types.OmluvnyList(
parseDate(date), text));
} }
Elements options = omluvnyListDokumentHTML.select("form.listConfigure").select("select[id=schoolYearId]").select("option"); 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"))); 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 @Override
@ -60,7 +103,7 @@ public class OmluvnyList extends JecnaScrape {
return new Options[] {schoolYearOptions}; return new Options[] {schoolYearOptions};
} }
public ArrayList<Pair<String, String>> getData() public ArrayList<xyz.thastertyn.Types.OmluvnyList> getData()
{ {
return data; return data;
} }

View File

@ -33,9 +33,9 @@ public class Sdeleni extends JecnaScrape {
if(isPositive) if(isPositive)
{ {
label = "" + " " + label; label = "" + "\u00a0" + label;
}else{ }else{
label = "" + " " + label; label = "" + "\u00a0" + label;
} }
sdeleniList.add(label); sdeleniList.add(label);
@ -49,11 +49,10 @@ public class Sdeleni extends JecnaScrape {
@Override @Override
public Options[] getOptions() { public Options[] getOptions() {
throw new UnsupportedOperationException("Unimplemented method 'getOptions'"); return null;
} }
@Override @Override
public void download(Choice choice) throws IOException { public void download(Choice choice) throws IOException {
throw new UnsupportedOperationException("Unimplemented method 'download'");
} }
} }

View File

@ -1,13 +1,33 @@
package xyz.thastertyn.Types; package xyz.thastertyn.Types;
import java.time.LocalDate;
public class OmluvnyList { public class OmluvnyList {
private String datum; private LocalDate datum;
private String popis; private String popis;
public OmluvnyList(String datum, String popis) public OmluvnyList(LocalDate datum, String popis)
{ {
this.datum = datum; this.datum = datum;
this.popis = popis; 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;
}
} }

View File

@ -40,7 +40,6 @@ public abstract class JecnaContent {
}else{ }else{
scraper.download(); scraper.download();
setGUI(); setGUI();
listener.updatePanel();
} }
hasStarted = true; hasStarted = true;
@ -52,6 +51,12 @@ public abstract class JecnaContent {
} }
public void showOptions(WindowBasedTextGUI textGUI) throws IOException { public void showOptions(WindowBasedTextGUI textGUI) throws IOException {
if(scraper.getOptions() == null)
{
return;
}
OptionsDialog dialog = new OptionsDialog(scraper.getOptions()); OptionsDialog dialog = new OptionsDialog(scraper.getOptions());
Choice choice = dialog.showDialog(textGUI); Choice choice = dialog.showDialog(textGUI);

View File

@ -8,7 +8,6 @@ import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.LinearLayout; import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.Panel;
import xyz.thastertyn.Tuples.Pair;
import xyz.thastertyn.UserInterface.UpdateListener; import xyz.thastertyn.UserInterface.UpdateListener;
public class OmluvnyList extends JecnaContent{ public class OmluvnyList extends JecnaContent{
@ -28,15 +27,15 @@ public class OmluvnyList extends JecnaContent{
protected void setGUI() protected void setGUI()
{ {
this.mainPanel.removeAllComponents(); this.mainPanel.removeAllComponents();
ArrayList<Pair<String, String>> a = omluvnyList.getData(); ArrayList<xyz.thastertyn.Types.OmluvnyList> a = omluvnyList.getData();
Panel content = new Panel() Panel content = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL)) .setLayoutManager(new LinearLayout(Direction.VERTICAL))
.addTo(mainPanel); .addTo(mainPanel);
for(Pair<String, String> 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()));
} }
} }
} }

View File

@ -2,8 +2,11 @@ package xyz.thastertyn.UserInterface.Content;
import java.util.ArrayList; 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.GridLayout;
import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.Panel;
import xyz.thastertyn.UserInterface.UpdateListener; import xyz.thastertyn.UserInterface.UpdateListener;
@ -29,6 +32,25 @@ public class Sdeleni extends JecnaContent {
ArrayList<String> sdeleniList = sdeleni.getSdeleni(); ArrayList<String> 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);
}
} }
} }

View File

@ -11,6 +11,9 @@ import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow; import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
import xyz.thastertyn.Login.LocalCredentials;
import xyz.thastertyn.Login.LoginController;
public class EscapeDialog extends DialogWindow { public class EscapeDialog extends DialogWindow {
public EscapeDialog() public EscapeDialog()
@ -42,16 +45,16 @@ public class EscapeDialog extends DialogWindow {
public void onDeleteLogin() public void onDeleteLogin()
{ {
close(); LocalCredentials.getInstance().deleteCredentials();
LoginController controller = new LoginController(getTextGUI());
// Call login manager to delete login controller.loginUsingGui();
} }
public void onLogout() public void onLogout()
{ {
close(); close();
LoginController controller = new LoginController(getTextGUI());
// Go back to square one and start logging in again controller.loginUsingGui();
} }
public void onExit() public void onExit()

View File

@ -52,14 +52,13 @@ public class MainWindow {
window.setComponent(mainPanel); window.setComponent(mainPanel);
Panel content = new Panel(); Panel content = new Panel();
mainPanel.addComponent(content.withBorder(Borders.singleLine("Jecnak"))); mainPanel.addComponent(content.withBorder(Borders.singleLine("Jecnak")));
LoginController controller = new LoginController(textGUI); LoginController controller = new LoginController(textGUI);
controller.login(); controller.login();
window.addWindowListener(new WindowSwitchListener(content, textGUI)); window.addWindowListener(new WindowSwitchListener(content, textGUI, screen));
textGUI.addWindowAndWait(window); textGUI.addWindowAndWait(window);

View File

@ -17,6 +17,8 @@ import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton; import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;
import com.googlecode.lanterna.input.KeyStroke; import com.googlecode.lanterna.input.KeyStroke;
import com.googlecode.lanterna.input.KeyType; 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.JecnaContent;
import xyz.thastertyn.UserInterface.Content.OmluvnyList; import xyz.thastertyn.UserInterface.Content.OmluvnyList;
@ -40,12 +42,14 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
private Panel tabsPanel = new Panel() private Panel tabsPanel = new Panel()
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL)); .setLayoutManager(new LinearLayout(Direction.HORIZONTAL));
private Panel holderPanel = new Panel(); private Panel holderPanel = new Panel();
private Screen screen;
int current = contents.length - 1; int current = contents.length - 1;
public WindowSwitchListener(Panel holder, WindowBasedTextGUI textGUI) public WindowSwitchListener(Panel holder, WindowBasedTextGUI textGUI, Screen screen)
{ {
this.textGUI = textGUI; this.textGUI = textGUI;
this.screen = screen;
holder.addComponent(tabsPanel); holder.addComponent(tabsPanel);
holder.addComponent(holderPanel.withBorder(Borders.singleLine())); holder.addComponent(holderPanel.withBorder(Borders.singleLine()));
@ -90,6 +94,11 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
setPanelPreferedSize(); setPanelPreferedSize();
updateLabels(); updateLabels();
try {
screen.refresh(RefreshType.COMPLETE);
} catch (IOException e) {
e.printStackTrace();
}
} }
private void checkIfDownloaded() private void checkIfDownloaded()
@ -108,21 +117,22 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
private void setPanelPreferedSize() private void setPanelPreferedSize()
{ {
int tabColumns = tabsPanel int tabColumns = tabsPanel
.getSize() .getSize()
.getColumns(); .getColumns();
int currentColumns = contents[current] int currentColumns = contents[current]
.getPanel() .getPanel()
.getSize() .getSize()
.getColumns(); .getColumns();
if(currentColumns < tabColumns) if(currentColumns < tabColumns)
{ {
holderPanel.addComponent( holderPanel.addComponent(
contents[current].getPanel() contents[current]
.getPanel()
.setPreferredSize( .setPreferredSize(
new TerminalSize( new TerminalSize(
tabColumns - 2, tabColumns,
contents[current].getPanel().getPreferredSize().getRows()))); contents[current].getPanel().getPreferredSize().getRows())));
return; return;
} }
@ -160,12 +170,10 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
switch(type) switch(type)
{ {
case Tab: case Tab:
if(keyStroke.isShiftDown()) next();
{ break;
previous(); case ReverseTab:
}else{ previous();
next();
}
break; break;
case Character: case Character:
if(keyStroke.getCharacter() == ' ') if(keyStroke.getCharacter() == ' ')
@ -184,6 +192,7 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
@Override @Override
public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) { public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) {
hasBeenHandled.set(true);
} }
@Override @Override
@ -193,6 +202,7 @@ public class WindowSwitchListener implements WindowListener, UpdateListener {
@Override @Override
public void onMoved(Window window, TerminalPosition oldPosition, TerminalPosition newPosition) { public void onMoved(Window window, TerminalPosition oldPosition, TerminalPosition newPosition) {
window.invalidate();
} }
@Override @Override