alamkanak / Android-Week-View

Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling.
Apache License 2.0
3.42k stars 1.23k forks source link

Async loading #305

Closed N7Nico closed 8 years ago

N7Nico commented 8 years ago

Hi, I'm opening this issue because I saw the previous issues about dynamically load events, but I can't make it functional... I tested all the methods posted and it goes into an infinite loop or don't display anything.

In my async task I fill a database with the asked month (provided by an API) and I add events to a private list in my class and finally return them, but several threads try to add events since onMonthChange is called 3 times and I can't figure it out how I can block the other threads or use local variables only...

Actually I wait for the answer of the API : it's functional but the UI is blocked and it's not dynamic...

Can you help me about this ?

Regards, Nicolas

PS: By the way i'm using AndroidAnnotations

`

@RestService
IntrAPI api;

@ViewById
WeekView weekView;

private IsConnected is;
private String startDate;
private String endDate;
private Calendar calendar;
private int wait;

@UiThread
public void mToast(String msg, int time) {
    Toast.makeText(getContext(), msg, time).show();
}

private void setStartDate(int newYear, int newMonth) {
    calendar = Calendar.getInstance();
    calendar.set(newYear, newMonth - 1, 1);
    calendar.set(newYear, newMonth - 1, calendar.getActualMinimum(Calendar.DATE));
    Date date = calendar.getTime();
    DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.FRANCE);
    startDate = DATE_FORMAT.format(date);
}

private void setEndDate(int newYear, int newMonth) {
    calendar = Calendar.getInstance();
    calendar.set(newYear, newMonth - 1, 1);
    calendar.set(newYear, newMonth - 1, calendar.getActualMaximum(Calendar.DATE));
    Date date = calendar.getTime();
    DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.FRANCE);
    endDate = DATE_FORMAT.format(date);
}

private boolean eventMatches(WeekViewEvent event, int year, int month) {
    return (event.getStartTime().get(Calendar.YEAR) == year && event.getStartTime().get(Calendar.MONTH) == month - 1) || (event.getEndTime().get(Calendar.YEAR) == year && event.getEndTime().get(Calendar.MONTH) == month - 1);
}

@AfterViews
void init() {
    is = new IsConnected(getActivity().getApplicationContext());
    weekView.setMonthChangeListener(this);
    weekView.setOnEventClickListener(this);
}

@Background
void setSchedule() {
    if (is.connected()) {
        Schedule.deleteAll(Schedule.class, "login = ?", user.getLogin());
        api.setCookie("PHPSESSID", user.getToken());
        try {
            PSchedule sch = new PSchedule();
            sch.init(api.getplanning(startDate, endDate));
        } catch (HttpClientErrorException e) {
            Log.d("Response", e.getResponseBodyAsString());
            Toast.makeText(getContext(), "Erreur de l'API", Toast.LENGTH_SHORT).show();
        } catch (NullPointerException e) {
            Toast.makeText(getContext(), "Erreur de l'API", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    wait = 0;
}

@Override
public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
    List<WeekViewEvent> events;

    wait = 1;
    setStartDate(newYear, newMonth);
    setEndDate(newYear, newMonth);

    setSchedule();
    while (wait == 1) ;
    events = new ArrayList<>();
    List<Schedule> pl = Schedule.findWithQuery(Schedule.class, "Select * FROM Schedule WHERE login = ?", user.getLogin());
    for (int i = 0; i < pl.size(); i++) {
        Schedule info = pl.get(i);
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
        try {
            cal.setTime(df.parse(info.getStart()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
        try {
            cale.setTime(sdf.parse(info.getEnd()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        WeekViewEvent event = new WeekViewEvent(1, info.getActititle(), cal, cale);

        events.add(event);
    }
    List<WeekViewEvent> matchedEvents = new ArrayList<>();
    for (WeekViewEvent event : events) {
        if (eventMatches(event, newYear, newMonth)) {
            matchedEvents.add(event);
        }
    }
    return matchedEvents;
}`
entropitor commented 8 years ago

See the sample AsyncActivity