Skip to content

Commit

Permalink
optimize planer loading
Browse files Browse the repository at this point in the history
  • Loading branch information
lucalewin committed Feb 27, 2022
1 parent b63bb4c commit 600a8fc
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 163 deletions.
93 changes: 64 additions & 29 deletions app/src/main/java/dev/lucalewin/planer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import com.google.android.material.card.MaterialCardView;
import com.google.android.material.textview.MaterialTextView;

import org.w3c.dom.Text;

import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.util.List;
Expand All @@ -22,40 +29,38 @@
import dev.lucalewin.planer.iserv.web_scraping.TaskRunner;
import dev.lucalewin.planer.preferences.Preferences;
import dev.lucalewin.planer.util.Tuple;
import dev.lucalewin.planer.views.TipsCardView;

public class MainActivity extends BaseThemeActivity {

private static final TaskRunner runner = new TaskRunner();

private Context mContext;

private ToolbarLayout toolbarLayout;
private SwipeRefreshLayout swipeRefreshLayout;

private MaterialTextView currentDayPlanerLabel;
private MaterialTextView nextDayPlanerLabel;

private TableLayout planerTableToday;
private TableLayout planerTableTomorrow;
private MaterialCardView currentPlanerContainer;
private MaterialCardView nextPlanerContainer;

private TipsCardView tipCardIservAccount;
private TipsCardView tipCardClass;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// LanguageUtil.init(this);

mContext = this;
toolbarLayout = findViewById(R.id.main_toolbar_layout);

toolbarLayout.inflateToolbarMenu(R.menu.main);
toolbarLayout.setOnToolbarMenuItemClickListener(menuItem -> {
switch (menuItem.getItemId()) {
case R.id.settings_menu_item:
startActivity(new Intent().setClass(this, SettingsActivity.class));
break;
default:
return false;
if (menuItem.getItemId() == R.id.settings_menu_item) {
startActivity(new Intent().setClass(this, SettingsActivity.class));
} else {
return false;
}
return true;
});
Expand All @@ -67,12 +72,49 @@ protected void onCreate(Bundle savedInstanceState) {
currentDayPlanerLabel = findViewById(R.id.label_planer_current_day);
nextDayPlanerLabel = findViewById(R.id.label_planer_next_day);

planerTableToday = findViewById(R.id.planer_table_today);
planerTableTomorrow = findViewById(R.id.planer_table_tomorrow);
currentPlanerContainer = findViewById(R.id.planer_current_container);
nextPlanerContainer = findViewById(R.id.planer_next_container);

tipCardIservAccount = findViewById(R.id.add_iserv_account_tip_card);
tipCardIservAccount.setOnClickListener(view -> startActivity(new Intent().setClass(this, IservAccountSettingsActivity.class)));

tipCardClass = findViewById(R.id.tip_card_set_class);
tipCardClass.setOnClickListener(view -> startActivity(new Intent().setClass(this, SettingsActivity.class)));

if (!isIservAccountSpecified()) {
// show tip card
// make user set their iserv account
tipCardIservAccount.setVisibility(View.VISIBLE);
return;
}

if (!isClassSpecified()) {
// show tip card
// make user set their class (+ courses)
tipCardClass.setVisibility(View.VISIBLE);
return;
}

loadIservData();
}

private boolean isIservAccountSpecified() {
SharedPreferences preferences = getSharedPreferences(IservAccountSettingsActivity.ISERV_SP_NAME, Context.MODE_PRIVATE);

String baseURL = preferences.getString("base_url", null);
String username = preferences.getString("username", null);
String password = preferences.getString("password", null);

System.out.println(baseURL + username + password);

return baseURL != null && username != null && password != null;
}

private boolean isClassSpecified() {
final String clazz = Preferences.getSharedPreferences(MainActivity.this).getString("class", null);
return clazz != null && !clazz.equals("");
}

private void loadIservData() {
runner.executeAsync(() -> {
swipeRefreshLayout.setRefreshing(true);
Expand All @@ -87,9 +129,6 @@ private void loadIservData() {

return Tuple.of(affectedDataToday, affectedDataTomorrow);
}, (TaskRunner.Callback<Tuple<IservPlan, IservPlan>>) (result) -> {
planerTableToday.removeViews(1, planerTableToday.getChildCount() - 1);
planerTableTomorrow.removeViews(1, planerTableTomorrow.getChildCount() - 1);

final IservPlan currentPlan = result.getFirst();
final IservPlan nextPlan = result.getSecond();

Expand All @@ -107,24 +146,20 @@ private void loadIservData() {
: nextPlan.getDay().getDisplayName(TextStyle.FULL, Locale.getDefault()));
}

final Map<String, List<IservPlanRow>> affectedDataToday = currentPlan.getClasses();
final Map<String, List<IservPlanRow>> affectedDataTomorrow = nextPlan.getClasses();

// if (LocalDateTime.now().getDayOfWeek() == affectedDataToday.)

final String clazz = Preferences.getSharedPreferences(MainActivity.this).getString("class", null);

if (clazz == null) {
// show tip card
// make user set their class (+ courses)
// this should never be reached, but safety first :)
return;
}

final List<IservPlanRow> current = affectedDataToday.get(clazz);
final List<IservPlanRow> next = affectedDataTomorrow.get(clazz);
TableLayout today = PlanerTable.initTable(MainActivity.this, clazz, currentPlan);
currentPlanerContainer.removeAllViews();
currentPlanerContainer.addView(today);

if (current != null) current.forEach(row -> planerTableToday.addView(row.toTableRow(MainActivity.this)));
if (next != null) next.forEach(row -> planerTableTomorrow.addView(row.toTableRow(MainActivity.this)));
TableLayout tomorrow = PlanerTable.initTable(MainActivity.this, clazz, nextPlan);
nextPlanerContainer.removeAllViews();
nextPlanerContainer.addView(tomorrow);

swipeRefreshLayout.setRefreshing(false);
});
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/java/dev/lucalewin/planer/PlanerTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.lucalewin.planer;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import dev.lucalewin.planer.iserv.IservPlan;
import dev.lucalewin.planer.iserv.IservPlanRow;

public class PlanerTable {

/**
*
* @param context the context of the current activity
* @param clazz the class the user is in
* @param plan the plan
* @return a list of table rows
*/
public static TableLayout initTable(Context context, String clazz, IservPlan plan) {
@SuppressLint("InflateParams")
TableLayout tableLayout = (TableLayout) LayoutInflater.from(context).inflate(R.layout.planer_table_layout, null, false);

final Map<String, List<IservPlanRow>> affectedData = plan.getClasses();

final List<IservPlanRow> rows = affectedData.get(clazz);

if (rows != null) {
rows.forEach(row -> tableLayout.addView(row.toTableRow(context)));
} else {
tableLayout.findViewById(R.id.planer_table_header).setVisibility(View.GONE);

TextView notAffectedTextView = new TextView(context);
notAffectedTextView.setText(context.getString(R.string.not_affected));
tableLayout.addView(notAffectedTextView);
}

return tableLayout;
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/dev/lucalewin/planer/iserv/IservPlan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.lucalewin.planer.iserv;

import android.widget.TableRow;

import java.time.DayOfWeek;
import java.util.List;
import java.util.Map;
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/dev/lucalewin/planer/iserv/IservPlanRow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.lucalewin.planer.iserv;

import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

Expand Down Expand Up @@ -31,22 +32,36 @@ public IservPlanRow(String hour, String missing, String replacing, String subjec
public TableRow toTableRow(Context context) {
TableRow tableRow = new TableRow(context);

TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(0, 0, 40, 0);

TextView hourTw = new TextView(context);
hourTw.setLayoutParams(layoutParams);
hourTw.setText(hour);
tableRow.addView(hourTw);

TextView missingTw = new TextView(context);
missingTw.setLayoutParams(layoutParams);
missingTw.setText(missing);
tableRow.addView(missingTw);

TextView courseTw = new TextView(context);
courseTw.setLayoutParams(layoutParams);
courseTw.setText(subject);
tableRow.addView(courseTw);

TextView typeTw = new TextView(context);
typeTw.setLayoutParams(layoutParams);
typeTw.setText(type);
tableRow.addView(typeTw);

TextView roomTw = new TextView(context);
roomTw.setLayoutParams(layoutParams);
roomTw.setText(room);
tableRow.addView(roomTw);

return tableRow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public static void login(Context context) {
IservAccountSettingsActivity.ISERV_SP_NAME, Context.MODE_PRIVATE);

base_url = sharedPreferences.getString("base_url", "");
if (!base_url.startsWith("http://") && !base_url.startsWith("https://")) {
base_url = "https://" + base_url;
}

String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
Expand Down Expand Up @@ -89,6 +92,9 @@ private static String getTodayPlanerHtml(Context context) {
IservAccountSettingsActivity.ISERV_SP_NAME, Context.MODE_PRIVATE);

base_url = sharedPreferences.getString("base_url", "");
if (!base_url.startsWith("http://") && !base_url.startsWith("https://")) {
base_url = "https://" + base_url;
}

final String login_url = base_url + "/iserv/plan/show/raw/Vertretungsplan%20Sch%C3%BCler/subst_001.htm";

Expand Down Expand Up @@ -134,6 +140,9 @@ private static String getTomorrowPlanerHtml(Context context) {
IservAccountSettingsActivity.ISERV_SP_NAME, Context.MODE_PRIVATE);

base_url = sharedPreferences.getString("base_url", "");
if (!base_url.startsWith("http://") && !base_url.startsWith("https://")) {
base_url = "https://" + base_url;
}

final String login_url = base_url + "/iserv/plan/show/raw/Vertretungsplan%20Sch%C3%BCler/subst_002.htm";

Expand Down Expand Up @@ -231,64 +240,8 @@ public static IservPlan getAffectedData(Context context, int day) {
currentPlanerRow.children().get(8).text()
));
}

classes.put(currentClass, current);
}

return new IservPlan(DayOfWeekUtil.fromGermanDayName(dayOfTheWeek), classes);

// // parse info table
// for (Element row : rows) {
// if (Objects.requireNonNull(row.getAllElements().first()).text().contains("Betroffene Klassen")) {
// String allAffectedClasses = row.getAllElements().get(2).text();
//
// if (allAffectedClasses.contains(_class)) {
//
// // parse planer
// Element planerTable = root.getElementsByClass("mon_list").first();
//
// assert planerTable != null;
// Elements planerTableRows = planerTable.selectXpath("/table/tbody/tr");
//
// outer_loop:
// {
// for (int i = 0; i < planerTableRows.size(); i++) {
// Element planerRow = planerTableRows.get(i);
//
// Element firstColumn = planerRow.children().first();
// assert firstColumn != null;
// String title = firstColumn.text();
//
// if (title.equals(_class)) {
// for (int j = i + 1; j < planerTableRows.size(); j++) {
// Element currentPlanerRow = planerTableRows.get(j);
//
// int size = currentPlanerRow.children().size();
//
// if (currentPlanerRow.children().size() == 1) {
// break outer_loop;
// }
//
// // loop through all columns of current row
//// data.add(currentPlanerRow.text());
// data.add(new IservPlanRow(
// currentPlanerRow.children().get(0).text(),
// currentPlanerRow.children().get(1).text(),
// currentPlanerRow.children().get(2).text(),
// currentPlanerRow.children().get(3).text(),
// currentPlanerRow.children().get(4).text(),
// currentPlanerRow.children().get(5).text(),
// currentPlanerRow.children().get(6).text(),
// currentPlanerRow.children().get(7).text(),
// currentPlanerRow.children().get(8).text()
// ));
// }
// }
// }
// }
// }
// }
// }
}

}
Loading

0 comments on commit 600a8fc

Please sign in to comment.