Hopefully on right path to choices, also switched to english

This commit is contained in:
Thastertyn 2023-04-25 21:57:35 +02:00
parent a633fa6490
commit 06183dd59c
11 changed files with 238 additions and 121 deletions

View File

@ -1,9 +1,5 @@
package xyz.thastertyn;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.plaf.FontUIResource;
import xyz.thastertyn.UserInterface.MainWindow;
/**

View File

@ -65,7 +65,7 @@ public class Login {
lastCheck = start;
}
/*public void loginJidelna() throws UnknownHostException, IOException
public void loginJidelna(String user, String pass) throws UnknownHostException, IOException
{
//#region JSESSIONID
Connection.Response jidelna = Jsoup.connect("https://objednavky.jidelnasokolska.cz/")
@ -94,13 +94,13 @@ public class Login {
//#endregion
//#region Login
Connection.Response jidelnaLogin = Jsoup.connect("https://objednavky.jidelnasokolska.cz/j_spring_security_check")
Jsoup.connect("https://objednavky.jidelnasokolska.cz/j_spring_security_check")
.header("Connection", "keep-alive")
.header("Content-Type", "application/x-www-form-urlencoded")
.cookie("XSRF-TOKEN", XSRF_TOKEN)
.cookie("JSESSIONID", jidelnaJSESSIONID)
.data("j_username", Credentials.user)
.data("j_password", Credentials.pass)
.data("j_username", user)
.data("j_password", pass)
.data("terminal", "false")
.data("type", "web")
.data("_csrf", XSRF_TOKEN)
@ -108,5 +108,5 @@ public class Login {
.method(Method.POST)
.execute();
//#endregion
}*/
}
}

View File

@ -10,6 +10,7 @@ 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;
import xyz.thastertyn.Types.Predmet;
@ -26,11 +27,24 @@ public class Znamky {
private Options schoolYearOptions = new Options("Skolni R.");
private Options schoolHalfYearOptions = new Options("Pololeti");
public void downloadZnamky(/*, int schoolYearId, int schoolYearHalfId*/) throws UnknownHostException, IOException
public void downloadZnamky() throws UnknownHostException, IOException
{
//String url = String.format("https://www.spsejecna.cz/score/student?schoolYearId=%d&schoolYearHalfId=%d", schoolYearId, schoolYearHalfId);
Document znamkyDokumentHTML = Downloader.download("https://www.spsejecna.cz/score/student").get();
download("https://www.spsejecna.cz/score/student");
}
public void downloadZnamky(Choice choice) throws UnknownHostException, IOException
{
download(String.format(
"https://www.spsejecna.cz/score/student?schoolYearId=%s&schoolYearHalfId=%s",
choice.getChoices().get(0),
choice.getChoices().get(1)));
}
private void download(String url) throws UnknownHostException, IOException
{
//String url =
Document znamkyDokumentHTML = Downloader.download(url).get();
// Predmety ulozene v <tr>
Elements[] radkyPredmetuHTML = znamkyDokumentHTML
.select("table.score")

View File

@ -0,0 +1,18 @@
package xyz.thastertyn.Types;
import java.util.ArrayList;
import java.util.Arrays;
public class Choice {
private ArrayList<String> choices;
public Choice(String... choices)
{
this.choices.addAll(Arrays.asList(choices));
}
public ArrayList<String> getChoices()
{
return choices;
}
}

View File

@ -1,7 +1,9 @@
package xyz.thastertyn.UserInterface.Content;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import xyz.thastertyn.Types.Choice;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Label;
@ -11,8 +13,10 @@ import com.googlecode.lanterna.gui2.Label;
public abstract class JecnaContent {
public abstract Panel getPanel();
public abstract void download();
public abstract void downloadDefault();
protected abstract void download(Choice choice);
public abstract boolean hasStarted();
public abstract Label getLabel();
public abstract void showOptions(final WindowBasedTextGUI textGUI);
protected abstract void setGUI();
}

View File

@ -9,6 +9,7 @@ import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import xyz.thastertyn.Tuples.Pair;
import xyz.thastertyn.Types.Choice;
public class OmluvnyList extends JecnaContent{
@ -19,16 +20,18 @@ public class OmluvnyList extends JecnaContent{
private boolean hasStarted = false;
@Override
public void download() {
public void downloadDefault()
{
download(null);
}
@Override
protected void download(Choice choice)
{
try{
omluvnyList.downloadOmluvnyList();
setGUI();
ArrayList<Pair<String, String>> a = omluvnyList.getData();
for(Pair<String, String> p : a)
{
omluvnyPanel.addComponent(new Label(p.getValue0() + " - " + p.getValue1()));
}
hasStarted = true;
}catch(UnknownHostException e)
{
@ -39,6 +42,17 @@ public class OmluvnyList extends JecnaContent{
}
}
@Override
protected void setGUI()
{
ArrayList<Pair<String, String>> a = omluvnyList.getData();
for(Pair<String, String> p : a)
{
omluvnyPanel.addComponent(new Label(p.getValue0() + " - " + p.getValue1()));
}
}
@Override
public Panel getPanel() {
return omluvnyPanel;

View File

@ -6,9 +6,10 @@ import java.net.UnknownHostException;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.table.Table;
import xyz.thastertyn.Types.Choice;
public class Rozvrh extends JecnaContent {
private Panel rozvrhPanel = new Panel();
@ -25,26 +26,18 @@ public class Rozvrh extends JecnaContent {
Table<String> t = new Table<>(labels);
@Override
public void download()
public void downloadDefault()
{
download(null);
}
@Override
protected void download(Choice choice)
{
try{
rozvrh.downloadRozvrh();
String[][] rozvrhArray = rozvrh.getRozvrh();
String[] den = new String[11];
for(int i = 0; i < rozvrhArray.length; i++)
{
den[0] = daysLabels[i];
for(int j = 1; j < rozvrhArray[i].length + 1; j++)
{
den[j] = (rozvrhArray[i][j - 1].isBlank()) ? " - " : rozvrhArray[i][j - 1];
}
t.getTableModel().addRow(den);
}
rozvrhPanel.addComponent(t);
setGUI();
hasStarted = true;
}catch(UnknownHostException e)
{
@ -55,6 +48,26 @@ public class Rozvrh extends JecnaContent {
}
}
@Override
protected void setGUI()
{
String[][] rozvrhArray = rozvrh.getRozvrh();
String[] den = new String[11];
for(int i = 0; i < rozvrhArray.length; i++)
{
den[0] = daysLabels[i];
for(int j = 1; j < rozvrhArray[i].length + 1; j++)
{
den[j] = (rozvrhArray[i][j - 1].isBlank()) ? " - " : rozvrhArray[i][j - 1];
}
t.getTableModel().addRow(den);
}
rozvrhPanel.addComponent(t);
}
@Override
public boolean hasStarted()
{

View File

@ -9,7 +9,8 @@ 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 com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import xyz.thastertyn.Types.Choice;
public class Sdeleni extends JecnaContent {
@ -20,33 +21,40 @@ public class Sdeleni extends JecnaContent {
private boolean hasStarted = false;
@Override
public void downloadDefault()
{
download(null);
}
@Override
public void download()
protected void download(Choice choice)
{
if(!hasStarted)
try{
sdeleni.downloadSdeleni();
hasStarted = true;
}catch(UnknownHostException e)
{
try{
sdeleniPanel.setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1)
.setRightMarginSize(1));
sdeleni.downloadSdeleni();
}catch(IOException e)
{
}
}
ArrayList<String> sdeleniList = sdeleni.getSdeleni();
@Override
protected void setGUI()
{
sdeleniPanel.setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1)
.setRightMarginSize(1));
for(String s : sdeleniList)
{
sdeleniPanel.addComponent(new Label(s));
}
hasStarted = true;
}catch(UnknownHostException e)
{
ArrayList<String> sdeleniList = sdeleni.getSdeleni();
}catch(IOException e)
{
}
for(String s : sdeleniList)
{
sdeleniPanel.addComponent(new Label(s));
}
}

View File

@ -15,6 +15,7 @@ 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.Dialogs.OptionsDialog;
@ -49,66 +50,23 @@ public class Znamky extends JecnaContent {
private boolean hasStarted = false;
@Override
public void download()
public void downloadDefault()
{
download(null);
}
@Override
protected void download(Choice choice)
{
try{
znamky.downloadZnamky();
ArrayList<Predmet> predmety = znamky.getPredmety();
HashMap<Integer, SimpleTheme> barvy = new HashMap<>();
barvy.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
barvy.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY));
barvy.put(3, new SimpleTheme(ANSI.BLACK, DOBRY));
barvy.put(4, new SimpleTheme(ANSI.BLACK, DOSTATECNY));
barvy.put(5, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY));
barvy.put(-1, new SimpleTheme(ANSI.WHITE, NEHODNOCEN));
// Sloupec pro jmena predmetu
Panel jmemaPredmetu = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro znamky z predmetu
Panel znamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro vyslednou znamku
Panel vysledneZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_RIGHT)
.addTo(mainPanel);
for(Predmet predmet : predmety)
if(choice != null)
{
Panel jednotliveZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL))
.addTo(znamky);
if(predmet.getZnamky().isEmpty())
{
jednotliveZnamky.addComponent(new Label(""));
}
for(Znamka znamka : predmet.getZnamky())
{
Label znamkaLabel = new Label(znamka.getText());
znamkaLabel.setTheme(barvy.get(znamka.getZnamka()));
jednotliveZnamky.addComponent(znamkaLabel);
}
double prumer = predmet.getPrumer();
Label vysl = new Label(String.format("%.2f", prumer));
vysl.setTheme(barvy.get((int) Math.round(prumer)));
jmemaPredmetu.addComponent(new Label(predmet.getJmenoPredmetu()));
vysledneZnamky.addComponent(vysl);
znamky.downloadZnamky(choice);
}else{
znamky.downloadZnamky();
}
setGUI();
hasStarted = true;
}catch(IOException e)
@ -117,6 +75,67 @@ public class Znamky extends JecnaContent {
}
}
@Override
protected void setGUI()
{
ArrayList<Predmet> predmety = znamky.getPredmety();
HashMap<Integer, SimpleTheme> barvy = new HashMap<>();
barvy.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
barvy.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY));
barvy.put(3, new SimpleTheme(ANSI.BLACK, DOBRY));
barvy.put(4, new SimpleTheme(ANSI.BLACK, DOSTATECNY));
barvy.put(5, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY));
barvy.put(-1, new SimpleTheme(ANSI.WHITE, NEHODNOCEN));
// Sloupec pro jmena predmetu
Panel jmemaPredmetu = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro znamky z predmetu
Panel znamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_LEFT)
.addTo(mainPanel);
// Sloupec pro vyslednou znamku
Panel vysledneZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.VERTICAL))
.setLayoutData(ALIGN_RIGHT)
.addTo(mainPanel);
for(Predmet predmet : predmety)
{
Panel jednotliveZnamky = new Panel()
.setLayoutManager(new LinearLayout(Direction.HORIZONTAL))
.addTo(znamky);
if(predmet.getZnamky().isEmpty())
{
jednotliveZnamky.addComponent(new Label(""));
}
for(Znamka znamka : predmet.getZnamky())
{
Label znamkaLabel = new Label(znamka.getText());
znamkaLabel.setTheme(barvy.get(znamka.getZnamka()));
jednotliveZnamky.addComponent(znamkaLabel);
}
double prumer = predmet.getPrumer();
Label vysl = new Label(String.format("%.2f", prumer));
vysl.setTheme(barvy.get((int) Math.round(prumer)));
jmemaPredmetu.addComponent(new Label(predmet.getJmenoPredmetu()));
vysledneZnamky.addComponent(vysl);
}
}
@Override
public Panel getPanel()
{
@ -139,6 +158,11 @@ public class Znamky extends JecnaContent {
public void showOptions(WindowBasedTextGUI textGUI) {
OptionsDialog d = new OptionsDialog(znamky.getOptions().getValue0(), znamky.getOptions().getValue1());
d.showDialog(textGUI);
Choice c = d.showDialog(textGUI);
if(c != null)
{
download(c);
}
}
}

View File

@ -1,6 +1,9 @@
package xyz.thastertyn.UserInterface.Dialogs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.ComboBox;
@ -12,14 +15,27 @@ import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Option;
import xyz.thastertyn.Types.Options;
public class OptionsDialog extends DialogWindow {
private List<ComboBox<Option>> boxs;
private boolean useData = false;
public OptionsDialog(Options... options)
{
super("Choose from below");
boxs = new ArrayList<>();
for(Options o : options)
{
boxs.add(new ComboBox<Option>(o.getOptions()));
}
Panel mainPanel = new Panel()
.setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1)
@ -28,9 +44,11 @@ public class OptionsDialog extends DialogWindow {
Panel optionPanel = new Panel().setLayoutManager(new GridLayout(3))
.addTo(mainPanel);
for(int i = 0; i < options.length; i++)
{
optionPanel.addComponent(new ComboBox<>(options[i].getOptions()));
optionPanel.addComponent(boxs.get(i));
if(i != options.length - 1)
{
@ -60,6 +78,7 @@ public class OptionsDialog extends DialogWindow {
public void onOK()
{
close();
useData = true;
}
public void onCancel()
@ -68,8 +87,15 @@ public class OptionsDialog extends DialogWindow {
}
@Override
public String showDialog(WindowBasedTextGUI textGUI) {
public Choice showDialog(WindowBasedTextGUI textGUI) {
super.showDialog(textGUI);
return "";
return (useData) ?
new Choice( // User pressed ok
boxs.stream()
.map(b -> b.getSelectedItem().toString())
.toArray(String[]::new))
:
null; // User pressed Cancel
}
}

View File

@ -69,7 +69,7 @@ public class WindowSwitchListener implements WindowListener {
if(!contents[current].hasStarted())
{
contents[current].download();
contents[current].downloadDefault();
}
holderPanel.removeAllComponents();