Sorted assignments

This commit is contained in:
Caesar2011
2018-07-06 00:15:45 +02:00
parent 4ca7654f94
commit d492d13842
5 changed files with 136 additions and 84 deletions

View File

@@ -23,6 +23,7 @@ import de.sebse.fuplanner.services.KVV.types.Gradebook;
import de.sebse.fuplanner.services.KVV.types.Lecturer;
import de.sebse.fuplanner.services.KVV.types.LoginToken;
import de.sebse.fuplanner.services.KVV.types.Modules;
import de.sebse.fuplanner.tools.AssignmentList;
import de.sebse.fuplanner.tools.AsyncQueue;
import de.sebse.fuplanner.tools.EventList;
import de.sebse.fuplanner.tools.Regex;
@@ -243,14 +244,14 @@ public class KVVModuleList extends HTTPService {
});
}
private void getAssignmentsUpgrade(String ID, final NetworkCallback<ArrayList<Assignment>> callback, final NetworkErrorCallback errorCallback) {
private void getAssignmentsUpgrade(String ID, final NetworkCallback<AssignmentList> callback, final NetworkErrorCallback errorCallback) {
get(String.format("https://kvv.imp.fu-berlin.de/direct/assignment/site/%s.json", ID), token.getCookies(), response ->{
String body = response.getParsed();
if (body == null) {
errorCallback.onError(new NetworkError(101301, 403, "No assignments retrieved!"));
return;
}
ArrayList<Assignment> assignments = new ArrayList<>();
AssignmentList assignments = new AssignmentList();
try {
JSONObject json = new JSONObject(body);
JSONArray sites = json.getJSONArray("assignment_collection");

View File

@@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import de.sebse.fuplanner.tools.AssignmentList;
import de.sebse.fuplanner.tools.Compare;
import de.sebse.fuplanner.tools.EventList;
import de.sebse.fuplanner.tools.Regex;
@@ -189,7 +190,7 @@ public class Modules implements Iterable<Modules.Module>, Serializable {
public final String description;
private final String ID;
public ArrayList<Announcement> announcements;
public ArrayList<Assignment> assignments;
public AssignmentList assignments;
public EventList events;
public ArrayList<Gradebook> gradebook;

View File

@@ -0,0 +1,19 @@
package de.sebse.fuplanner.tools;
import de.sebse.fuplanner.services.KVV.types.Assignment;
import de.sebse.fuplanner.tools.logging.Logger;
public class AssignmentList extends DateSortedList<Assignment> {
@Override
long getDateByItem(Assignment item) {
Logger log = new Logger(this);
log.d(item.getTitle(), Conversion.getModifiedDateTime(item.getDueDate()));
return item.getDueDate();
}
@Override
public boolean reversed() {
return true;
}
}

View File

@@ -0,0 +1,108 @@
package de.sebse.fuplanner.tools;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import de.sebse.fuplanner.services.KVV.types.Event;
public abstract class DateSortedList<T> extends ArrayList<T> {
private int split = -1;
public T getPast(int index) {
if (split < 0)
sort();
if (index >= split)
throw new ArrayIndexOutOfBoundsException(String.format("Index %d out of bounds! Only %d past events found!", index, split));
if (reversed())
index = sizePast() - index - 1;
return this.get(index);
}
public T getUpcoming(int index) {
if (split < 0)
sort();
index += split;
if (index >= this.size())
throw new ArrayIndexOutOfBoundsException(String.format("Index %d out of bounds! Only %d upcoming events found!", index-split, this.size()-split));
if (reversed())
index = sizeUpcoming() - index - 1;
return this.get(index);
}
@Override
public T get(int index) {
if (split < 0)
sort();
if (reversed())
index = size() - index - 1;
return super.get(index);
}
public int sizePast() {
if (split < 0)
sort();
return split;
}
public int sizeUpcoming() {
if (split < 0)
sort();
return this.size()-split;
}
public boolean add(T event) {
split = -1;
return super.add(event);
}
public void sort() {
Collections.sort(this, ((e1, e2) -> Long.compare(getDateByItem(e1), getDateByItem(e2))));
long now = System.currentTimeMillis();
split = 0;
for (T event: this) {
if (getDateByItem(event) < now)
split++;
else
break;
}
}
public Iterator<T> getEventsOfMonth(int year, int month) {
if (split < 0)
sort();
final int[] i = {0};
Calendar minC = Calendar.getInstance();
minC.set(year, month-1, 1, 0, 0);
Calendar maxC = Calendar.getInstance();
maxC.set(year, month, 1, 0, 0);
for (; i[0] < this.size(); i[0]++) {
if (getDateByItem(this.get(i[0])) > minC.getTimeInMillis()) {
break;
}
}
return new Iterator<T>() {
@Override
public boolean hasNext() {
return i[0] < DateSortedList.this.size() && getDateByItem(DateSortedList.this.get(i[0])) < maxC.getTimeInMillis();
}
@Override
public T next() {
if (hasNext()) {
return DateSortedList.this.get(i[0]++);
} else {
return null;
}
}
};
}
public boolean reversed() {
return false;
}
abstract long getDateByItem(T item);
}

View File

@@ -1,88 +1,11 @@
package de.sebse.fuplanner.tools;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import de.sebse.fuplanner.services.KVV.types.Event;
public class EventList extends ArrayList<Event> {
private int split = 0;
public class EventList extends DateSortedList<Event> {
public Event getPast(int index) {
if (split < 0)
sort();
if (index >= split)
throw new ArrayIndexOutOfBoundsException(String.format("Index %d out of bounds! Only %d past events found!", index, split));
return this.get(index);
}
public Event getUpcoming(int index) {
if (split < 0)
sort();
index += split;
if (index >= this.size())
throw new ArrayIndexOutOfBoundsException(String.format("Index %d out of bounds! Only %d upcoming events found!", index-split, this.size()-split));
return this.get(index);
}
public int sizePast() {
if (split < 0)
sort();
return split;
}
public int sizeUpcoming() {
if (split < 0)
sort();
return this.size()-split;
}
public boolean add(Event event) {
split = -1;
return super.add(event);
}
public void sort() {
Collections.sort(this, ((e1, e2) -> Long.compare(e1.getEndDate(), e2.getEndDate())));
long now = System.currentTimeMillis();
split = 0;
for (Event event : this) {
if (event.getEndDate() < now)
split++;
else
break;
}
}
public Iterator<Event> getEventsOfMonth(int year, int month) {
if (split < 0)
sort();
final int[] i = {0};
Calendar minC = Calendar.getInstance();
minC.set(year, month-1, 1, 0, 0);
Calendar maxC = Calendar.getInstance();
maxC.set(year, month, 1, 0, 0);
for (; i[0] < this.size(); i[0]++) {
if (this.get(i[0]).getEndDate() > minC.getTimeInMillis()) {
break;
}
}
return new Iterator<Event>() {
@Override
public boolean hasNext() {
return i[0] < EventList.this.size() && EventList.this.get(i[0]).getEndDate() < maxC.getTimeInMillis();
}
@Override
public Event next() {
if (hasNext()) {
return EventList.this.get(i[0]++);
} else {
return null;
}
}
};
@Override
long getDateByItem(Event item) {
return item.getEndDate();
}
}