Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Add null check in CalendarActivity to prevent IllegalArgumentException (
Browse files Browse the repository at this point in the history
#1089)

This commit checks whether the list of events, as returned by the TUMonline API, is null. Only if it is not, it will invoke the methods that perform the scheduling of the corresponding notifications.
  • Loading branch information
thellmund committed Sep 18, 2018
1 parent b7fc420 commit b2e6cf8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -4,7 +4,7 @@ import de.tum.`in`.tumcampusapp.api.tumonline.model.AccessToken
import de.tum.`in`.tumcampusapp.api.tumonline.model.TokenConfirmation
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.CreateEventResponse
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.DeleteEventResponse
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.Events
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.EventsResponse
import de.tum.`in`.tumcampusapp.component.tumui.grades.model.ExamList
import de.tum.`in`.tumcampusapp.component.tumui.lectures.model.LectureAppointmentsResponse
import de.tum.`in`.tumcampusapp.component.tumui.lectures.model.LectureDetailsResponse
Expand All @@ -25,7 +25,7 @@ interface TUMOnlineAPIService {
@Query("pMonateVor") start: Int,
@Query("pMonateNach") end: Int,
@Header("Cache-Control") cacheControl: String
): Call<Events>
): Call<EventsResponse>

@GET("wbservicesbasic.terminCreate")
fun createCalendarEvent(
Expand Down
Expand Up @@ -13,7 +13,7 @@ import de.tum.`in`.tumcampusapp.api.tumonline.model.TokenConfirmation
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.CalendarItem
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.CreateEventResponse
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.DeleteEventResponse
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.Events
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.EventsResponse
import de.tum.`in`.tumcampusapp.component.tumui.grades.model.ExamList
import de.tum.`in`.tumcampusapp.component.tumui.lectures.model.LectureAppointmentsResponse
import de.tum.`in`.tumcampusapp.component.tumui.lectures.model.LectureDetailsResponse
Expand All @@ -30,7 +30,7 @@ import retrofit2.Retrofit

class TUMOnlineClient(private val apiService: TUMOnlineAPIService) {

fun getCalendar(cacheControl: CacheControl): Call<Events> {
fun getCalendar(cacheControl: CacheControl): Call<EventsResponse> {
return apiService.getCalendar(
Const.CALENDAR_MONTHS_BEFORE, Const.CALENDAR_MONTHS_AFTER, cacheControl.header)
}
Expand Down
@@ -1,7 +1,6 @@
package de.tum.in.tumcampusapp.component.tumui.calendar;

import android.Manifest;
import android.arch.lifecycle.Lifecycle;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
Expand All @@ -25,8 +24,6 @@
import com.alamkanak.weekview.WeekView;
import com.alamkanak.weekview.WeekViewDisplayable;
import com.alamkanak.weekview.WeekViewEvent;
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle;
import com.trello.rxlifecycle2.LifecycleProvider;

import org.jetbrains.annotations.NotNull;
import org.joda.time.DateTime;
Expand All @@ -44,7 +41,7 @@
import de.tum.in.tumcampusapp.component.other.generic.activity.ActivityForAccessingTumOnline;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.CalendarItem;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.Event;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.Events;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.EventsResponse;
import de.tum.in.tumcampusapp.component.ui.transportation.TransportController;
import de.tum.in.tumcampusapp.database.TcaDb;
import de.tum.in.tumcampusapp.utils.Const;
Expand All @@ -59,7 +56,7 @@
/**
* Activity showing the user's calendar. Calendar items (events) are fetched from TUMOnline and displayed as blocks on a timeline.
*/
public class CalendarActivity extends ActivityForAccessingTumOnline<Events>
public class CalendarActivity extends ActivityForAccessingTumOnline<EventsResponse>
implements OnClickListener, MonthLoader.MonthChangeListener, WeekView.EventClickListener,
CalendarDetailsFragment.OnEventInteractionListener {

Expand Down Expand Up @@ -131,18 +128,24 @@ public void onRefresh() {
}

private void loadEvents(CacheControl cacheControl) {
Call<Events> apiCall = apiClient.getCalendar(cacheControl);
Call<EventsResponse> apiCall = apiClient.getCalendar(cacheControl);
fetch(apiCall);
}

@Override
protected void onDownloadSuccessful(@NonNull Events response) {
protected void onDownloadSuccessful(@NonNull EventsResponse response) {
isFetched = true;
scheduleNotifications(response.getEvents());

List<Event> events = response.getEvents();
if (events == null) {
return;
}

scheduleNotifications(events);

mDisposable.add(
Completable
.fromAction(() -> calendarController.importCalendar(response))
.fromAction(() -> calendarController.importCalendar(events))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
Expand All @@ -155,7 +158,7 @@ protected void onDownloadSuccessful(@NonNull Events response) {
);
}

private void scheduleNotifications(List<Event> events) {
private void scheduleNotifications(@NonNull List<Event> events) {
if (calendarController.hasNotificationsEnabled()) {
calendarController.scheduleNotifications(events);
}
Expand Down
Expand Up @@ -34,7 +34,6 @@
import de.tum.in.tumcampusapp.component.other.locations.model.Geo;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.CalendarItem;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.Event;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.Events;
import de.tum.in.tumcampusapp.component.tumui.calendar.model.WidgetsTimetableBlacklist;
import de.tum.in.tumcampusapp.component.tumui.lectures.model.RoomLocations;
import de.tum.in.tumcampusapp.component.ui.overview.card.Card;
Expand Down Expand Up @@ -212,18 +211,15 @@ void scheduleNotifications(List<Event> events) {
scheduler.schedule(notifications);
}

public void importCalendar(Events newEvents) {
public void importCalendar(@NonNull List<Event> events) {
// Cleanup cache before importing
removeCache();

// Import the new events
List<Event> events = newEvents.getEvents();
if (events != null) {
try {
replaceIntoDb(events);
} catch (Exception e) {
Utils.log(e);
}
try {
replaceIntoDb(events);
} catch (Exception e) {
Utils.log(e);
}

new SyncManager(mContext).replaceIntoDb(Const.SYNC_CALENDAR_IMPORT);
Expand Down
Expand Up @@ -10,4 +10,4 @@ import com.tickaroo.tikxml.annotation.Xml
* @see LecturesSearchRow
*/
@Xml(name = "events")
data class Events(@Element val events: List<Event>? = null)
data class EventsResponse(@Element val events: List<Event>? = null)
11 changes: 6 additions & 5 deletions app/src/main/java/de/tum/in/tumcampusapp/utils/CacheManager.kt
Expand Up @@ -5,7 +5,7 @@ import de.tum.`in`.tumcampusapp.api.tumonline.AccessTokenManager
import de.tum.`in`.tumcampusapp.api.tumonline.CacheControl
import de.tum.`in`.tumcampusapp.api.tumonline.TUMOnlineClient
import de.tum.`in`.tumcampusapp.component.tumui.calendar.CalendarController
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.Events
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.EventsResponse
import de.tum.`in`.tumcampusapp.component.tumui.lectures.model.LecturesResponse
import de.tum.`in`.tumcampusapp.component.ui.chat.ChatRoomController
import okhttp3.Cache
Expand All @@ -32,14 +32,15 @@ class CacheManager(private val context: Context) {
TUMOnlineClient
.getInstance(context)
.getCalendar(CacheControl.USE_CACHE)
.enqueue(object : Callback<Events> {
override fun onResponse(call: Call<Events>, response: Response<Events>) {
val events = response.body() ?: return
.enqueue(object : Callback<EventsResponse> {
override fun onResponse(call: Call<EventsResponse>, response: Response<EventsResponse>) {
val eventsResponse = response.body() ?: return
val events = eventsResponse.events ?: return
CalendarController(context).importCalendar(events)
loadRoomLocations()
}

override fun onFailure(call: Call<Events>, t: Throwable) {
override fun onFailure(call: Call<EventsResponse>, t: Throwable) {
Utils.log(t, "Error while loading calendar in CacheManager")
}
})
Expand Down

0 comments on commit b2e6cf8

Please sign in to comment.