Skip to content

Commit

Permalink
Merge pull request tuxmobil#130 from EventFahrplan/housekeeping
Browse files Browse the repository at this point in the history
Simplify lecture filtering and counting.
  • Loading branch information
johnjohndoe authored Jan 10, 2019
2 parents cb43279 + 9329c36 commit 6dd440b
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 83 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ dependencies {
testImplementation Libs.assertjAndroid
testImplementation Libs.supportLibraryAnnotations
testImplementation Libs.mockitoCore
testImplementation Libs.mockitoKotlin
testImplementation Libs.threeTenBp
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void onParseDone(Boolean result, String version) {
MyApp.LogDebug(LOG_TAG, "parseDone: " + result + " , numDays=" + MyApp.meta.getNumDays());
MyApp.task_running = TASKS.NONE;
MyApp.fahrplan_xml = null;
List<Lecture> changesList = FahrplanMisc.readChanges(this);
AppRepository appRepository = AppRepository.Companion.getInstance(this);
List<Lecture> changesList = FahrplanMisc.readChanges(appRepository);
if (!changesList.isEmpty()) {
showScheduleUpdateNotification(version, changesList.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ public void onCreate(Bundle savedInstanceState) {
}

FragmentActivity activity = getActivity();
changesList = FahrplanMisc.readChanges(activity);
Meta meta = AppRepository.Companion.getInstance(activity).readMeta();
AppRepository appRepository = AppRepository.Companion.getInstance(activity);
changesList = FahrplanMisc.readChanges(appRepository);
Meta meta = appRepository.readMeta();
mAdapter = new LectureChangesArrayAdapter(activity, changesList, meta.getNumDays());
MyApp.LogDebug(LOG_TAG, "onCreate, " + changesList.size() + " changes");
}
Expand Down Expand Up @@ -128,7 +129,8 @@ public void onDetach() {
}

public void onRefresh() {
List<Lecture> updatedChanges = FahrplanMisc.readChanges(getActivity());
AppRepository appRepository = AppRepository.Companion.getInstance(getActivity());
List<Lecture> updatedChanges = FahrplanMisc.readChanges(appRepository);
if (changesList != null) {
changesList.clear();
changesList.addAll(updatedChanges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public void onResume() {

private void initStarredList() {
FragmentActivity activity = getActivity();
starredList = FahrplanMisc.getStarredLectures(activity);
AppRepository appRepository = AppRepository.Companion.getInstance(activity);
starredList = FahrplanMisc.getStarredLectures(appRepository);
Meta meta = appRepository.readMeta();
mAdapter = new LectureArrayAdapter(activity, starredList, meta.getNumDays());
MyApp.LogDebug(LOG_TAG, "initStarredList: " + starredList.size() + " favorites");
Expand Down Expand Up @@ -174,7 +174,8 @@ public void onDetach() {
}

public void onRefresh(@NonNull Context context) {
List<Lecture> starred = FahrplanMisc.getStarredLectures(context);
AppRepository appRepository = AppRepository.Companion.getInstance(context);
List<Lecture> starred = FahrplanMisc.getStarredLectures(appRepository);
if (starredList != null) {
starredList.clear();
starredList.addAll(starred);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AppRepository private constructor(val context: Context) {
// Parsing
scheduleNetworkRepository.parseSchedule(fetchScheduleResult.scheduleXml, fetchScheduleResult.eTag,
onUpdateLectures = { lectures ->
val oldLectures = FahrplanMisc.loadLecturesForAllDays(context)
val oldLectures = FahrplanMisc.loadLecturesForAllDays(this)
val newLectures = lectures.toLecturesAppModel2()
val hasChanged = ScheduleChanges.hasScheduleChanged(newLectures, oldLectures)
if (hasChanged) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,19 +698,11 @@ public static void loadLectureList(Context context, int day, boolean force) {
return;
}

MyApp.lectureList = FahrplanMisc.loadLecturesForDayIndex(context, day);
if (MyApp.lectureList == null) {
AppRepository appRepository = AppRepository.Companion.getInstance(context);
MyApp.lectureList = FahrplanMisc.getUncanceledLectures(appRepository, day);
if (MyApp.lectureList.isEmpty()) {
return;
}

int lectureIndex = MyApp.lectureList.size() - 1;
while (lectureIndex >= 0) {
Lecture l = MyApp.lectureList.get(lectureIndex);
if (l.changedIsCanceled) {
MyApp.lectureList.remove(lectureIndex);
}
lectureIndex--;
}
MyApp.lectureListDay = day;

MyApp.roomsMap.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ void showChangesDialog() {
Fragment fragment = findFragment(ChangesDialog.FRAGMENT_TAG);
if (fragment == null) {
requiresScheduleReload = true;
List<Lecture> changedLectures = FahrplanMisc.readChanges(this);
AppRepository appRepository = AppRepository.Companion.getInstance(this);
List<Lecture> changedLectures = FahrplanMisc.readChanges(appRepository);
Meta meta = appRepository.readMeta();
String scheduleVersion = meta.getVersion();
DialogFragment changesDialog = ChangesDialog.newInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.text.format.Time;

import org.ligi.tracedroid.logging.Log;
Expand All @@ -31,11 +32,15 @@
import nerd.tuxmobil.fahrplan.congress.models.SchedulableAlarm;
import nerd.tuxmobil.fahrplan.congress.repositories.AppRepository;

import static kotlin.collections.CollectionsKt.count;
import static kotlin.collections.CollectionsKt.filterNot;


public class FahrplanMisc {

private static final String LOG_TAG = "FahrplanMisc";
private static final int ALL_DAYS = -1;
@VisibleForTesting
public static final int ALL_DAYS = -1;
private static final DateFormat TIME_TEXT_DATE_FORMAT =
SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);

Expand Down Expand Up @@ -165,21 +170,19 @@ public void onRescheduleInitialAlarm(long interval, long nextFetch) {
}

@NonNull
public static List<Lecture> loadLecturesForAllDays(@NonNull Context context) {
return loadLecturesForDayIndex(context, ALL_DAYS);
public static List<Lecture> loadLecturesForAllDays(@NonNull AppRepository appRepository) {
return loadLecturesForDayIndex(appRepository, ALL_DAYS);
}

/**
* Load all Lectures from the DB on the day specified
*
* @param context The Android Context
* @param day The day to load lectures for (0..), or -1 for all days
* @param appRepository The application repository to retrieve lectures from.
* @param day The day to load lectures for (0..), or -1 for all days
* @return ArrayList of Lecture objects
*/
@NonNull
public static List<Lecture> loadLecturesForDayIndex(@NonNull Context context, int day) {
AppRepository appRepository = AppRepository.Companion.getInstance(context);

public static List<Lecture> loadLecturesForDayIndex(@NonNull AppRepository appRepository, int day) {
List<Lecture> lectures;
if (day == ALL_DAYS) {
MyApp.LogDebug(LOG_TAG, "Loading lectures for all days.");
Expand All @@ -192,7 +195,7 @@ public static List<Lecture> loadLecturesForDayIndex(@NonNull Context context, in

List<Highlight> highlights = appRepository.readHighlights();
for (Highlight highlight : highlights) {
MyApp.LogDebug(LOG_TAG, "highlight = " + highlight);
MyApp.LogDebug(LOG_TAG, highlight.toString());
for (Lecture lecture : lectures) {
if (lecture.lecture_id.equals("" + highlight.getEventId())) {
lecture.highlight = highlight.isHighlight();
Expand All @@ -202,84 +205,51 @@ public static List<Lecture> loadLecturesForDayIndex(@NonNull Context context, in
return lectures;
}

public static int getChangedLectureCount(@NonNull List<Lecture> list, boolean favsOnly) {
int count = 0;
if (list.isEmpty()) {
return 0;
}
for (int lectureIndex = 0; lectureIndex < list.size(); lectureIndex++) {
Lecture l = list.get(lectureIndex);
if (l.isChanged() && (!favsOnly || l.highlight)) {
count++;
}
}
MyApp.LogDebug(LOG_TAG, "getChangedLectureCount " + favsOnly + ":" + count);
public static int getChangedLectureCount(@NonNull final List<Lecture> list, boolean favsOnly) {
int count = count(list, event -> event.isChanged() && (!favsOnly || event.highlight));
MyApp.LogDebug(LOG_TAG, count + " changed lectures, favsOnly = " + favsOnly);
return count;
}

public static int getNewLectureCount(@NonNull List<Lecture> list, boolean favsOnly) {
int count = 0;
if (list.isEmpty()) {
return 0;
}
for (int lectureIndex = 0; lectureIndex < list.size(); lectureIndex++) {
Lecture l = list.get(lectureIndex);
if (l.changedIsNew && (!favsOnly || l.highlight)) count++;
}
MyApp.LogDebug(LOG_TAG, "getNewLectureCount " + favsOnly + ":" + count);
public static int getNewLectureCount(@NonNull final List<Lecture> list, boolean favsOnly) {
int count = count(list, event -> event.changedIsNew && (!favsOnly || event.highlight));
MyApp.LogDebug(LOG_TAG, count + " new lectures, favsOnly = " + favsOnly);
return count;
}

public static int getCancelledLectureCount(@NonNull List<Lecture> list, boolean favsOnly) {
int count = 0;
if (list.isEmpty()) {
return 0;
}
for (int lectureIndex = 0; lectureIndex < list.size(); lectureIndex++) {
Lecture l = list.get(lectureIndex);
if (l.changedIsCanceled && (!favsOnly || l.highlight)) count++;
}
MyApp.LogDebug(LOG_TAG, "getCancelledLectureCount " + favsOnly + ":" + count);
public static int getCancelledLectureCount(@NonNull final List<Lecture> list, boolean favsOnly) {
int count = count(list, event -> event.changedIsCanceled && (!favsOnly || event.highlight));
MyApp.LogDebug(LOG_TAG, count + " canceled lectures, favsOnly = " + favsOnly);
return count;
}

@NonNull
public static List<Lecture> readChanges(Context context) {
public static List<Lecture> readChanges(@NonNull AppRepository appRepository) {
MyApp.LogDebug(LOG_TAG, "readChanges");
List<Lecture> changesList = loadLecturesForAllDays(context);
List<Lecture> changesList = loadLecturesForAllDays(appRepository);
if (changesList.isEmpty()) {
return changesList;
}
int lectureIndex = changesList.size() - 1;
while (lectureIndex >= 0) {
Lecture l = changesList.get(lectureIndex);
if (!l.isChanged() && !l.changedIsCanceled && !l.changedIsNew) {
changesList.remove(l);
}
lectureIndex--;
}
changesList = filterNot(changesList, event -> !event.isChanged() && !event.changedIsCanceled && !event.changedIsNew);
MyApp.LogDebug(LOG_TAG, changesList.size() + " lectures changed.");
return changesList;
}

@NonNull
public static List<Lecture> getStarredLectures(@NonNull Context context) {
List<Lecture> starredList = loadLecturesForAllDays(context);
public static List<Lecture> getStarredLectures(@NonNull AppRepository appRepository) {
List<Lecture> starredList = loadLecturesForAllDays(appRepository);
if (starredList.isEmpty()) {
return starredList;
}
int lectureIndex = starredList.size() - 1;
while (lectureIndex >= 0) {
Lecture l = starredList.get(lectureIndex);
if (!l.highlight) {
starredList.remove(l);
}
if (l.changedIsCanceled) {
starredList.remove(l);
}
lectureIndex--;
}
starredList = filterNot(starredList, event -> !event.highlight || event.changedIsCanceled);
MyApp.LogDebug(LOG_TAG, starredList.size() + " lectures starred.");
return starredList;
}

@NonNull
public static List<Lecture> getUncanceledLectures(@NonNull AppRepository appRepository, int dayIndex) {
List<Lecture> lectures = FahrplanMisc.loadLecturesForDayIndex(appRepository, dayIndex);
return filterNot(lectures, event -> event.changedIsCanceled);
}

}
Loading

0 comments on commit 6dd440b

Please sign in to comment.