Search in sources :

Example 1 with NewsFeedResponse

use of app.insti.api.response.NewsFeedResponse in project IITB-App by wncc.

the class CalendarFragment method updateEvents.

private void updateEvents(CalendarDay calendarDay, final boolean setToday) {
    // Do not make duplicate calls
    if (haveMonths.contains(calendarDay)) {
        if (!setToday) {
            return;
        } else {
            setupCalendar(true);
        }
    }
    haveMonths.add(calendarDay);
    // Parsers
    String ISO_FORMAT = "yyyy-MM-dd HH:mm:ss";
    final TimeZone utc = TimeZone.getTimeZone("UTC");
    final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
    isoFormatter.setTimeZone(utc);
    // Get the start date
    final Date startDate;
    try {
        startDate = toDate(calendarDay);
    } catch (ParseException ignored) {
        return;
    }
    // Get start and end times
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    cal.add(Calendar.MONTH, -1);
    final Date oneMonthBackDate = cal.getTime();
    cal.add(Calendar.MONTH, 2);
    final Date oneMonthOnDate = cal.getTime();
    final String oneMonthBack = isoFormatter.format(oneMonthBackDate).toString();
    final String oneMonthOn = isoFormatter.format(oneMonthOnDate).toString();
    // Make the API call
    RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
    retrofitInterface.getEventsBetweenDates(Utils.getSessionIDHeader(), oneMonthBack, oneMonthOn).enqueue(new Callback<NewsFeedResponse>() {

        @Override
        public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
            if (response.isSuccessful()) {
                if (getActivity() == null || getView() == null)
                    return;
                // Concatenate the response
                NewsFeedResponse newsFeedResponse = response.body();
                List<Event> eventList = newsFeedResponse.getEvents();
                if (eventList == null)
                    return;
                // Concatenate
                for (Event event : eventList) {
                    if (!events.contains(event))
                        events.add(event);
                }
                setupCalendar(setToday);
            }
        }

        @Override
        public void onFailure(Call<NewsFeedResponse> call, Throwable t) {
            // Network Error
            Toast.makeText(getActivity(), "Failed to fetch events!", Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : Calendar(java.util.Calendar) Date(java.util.Date) TimeZone(java.util.TimeZone) NewsFeedResponse(app.insti.api.response.NewsFeedResponse) Event(app.insti.api.model.Event) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 2 with NewsFeedResponse

use of app.insti.api.response.NewsFeedResponse in project IITB-App by wncc.

the class FeedFragment method updateFeed.

private void updateFeed() {
    RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
    retrofitInterface.getNewsFeed(Utils.getSessionIDHeader()).enqueue(new Callback<NewsFeedResponse>() {

        @Override
        public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
            if (response.isSuccessful()) {
                Utils.eventCache.setList(response.body().getEvents());
                displayEvents(Utils.eventCache);
            }
            // Server Error
            feedSwipeRefreshLayout.setRefreshing(false);
        }

        @Override
        public void onFailure(Call<NewsFeedResponse> call, Throwable t) {
            // Network Error
            feedSwipeRefreshLayout.setRefreshing(false);
        }
    });
}
Also used : NewsFeedResponse(app.insti.api.response.NewsFeedResponse) RetrofitInterface(app.insti.api.RetrofitInterface)

Aggregations

RetrofitInterface (app.insti.api.RetrofitInterface)2 NewsFeedResponse (app.insti.api.response.NewsFeedResponse)2 Event (app.insti.api.model.Event)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1 TimeZone (java.util.TimeZone)1