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

Re: Question about setting weekview event #18

Closed shalinipk closed 9 years ago

shalinipk commented 9 years ago

Hello,

This is with reference to the issue I opened earlier, with the same title. I cannot reopen the issue, because it was not closed by me. Hence opening a new issue:

Question : Is there a way to set the list of events in WeekView other than returning List of WeekViewEvents in onMonthChange() ?

Your solution:

Currently the week view does not provide a ready-made solution for async event loading. But there is a simple hack. Use the following code to achieve what you want.

@Override public List onMonthChange(int newYear, int newMonth) { // Populate the week view with some events. List events = new ArrayList();

// Fetch events for the date user selected.
fetchEventsinBackground();

}

// Callback when fetchEvents finishes. public void setEventList(Activity activity, final List eventModels) {

// Hide the progress dialog.
if(mProgressDialog != null && mProgressDialog.isShowing()) {
    mProgressDialog.dismiss();
}

// Refresh the week view.
activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // Store the returned events in a global variable for later use.
        mEventModels = eventModels;

        // This line will trigger the method 'onMonthChange()' again.
        mWeekView.notifyDatasetChanged();
    }
});

}

@Override public List onMonthChange(int year, int month) { // TODO Be sure to check if the variables year and month equals the year and month of the events of mEventModels. return getWeekViewEventsFromEventModels(mEventModels); }

Thank you for the response, however it did not work for me.

Java doesn't support 2 methods with same signature, so I can't have something like this in the same class:

@Override public List onMonthChange(int year, int month) { // TODO Be sure to check if the variables year and month equals the year and month of the events of mEventModels. return getWeekViewEventsFromEventModels(mEventModels); } @Override public List onMonthChange(int newYear, int newMonth) { // Populate the week view with some events. List events = new ArrayList();

// Fetch events for the date user selected.
fetchEventsinBackground();

} If mWeekView.notifyDatasetChanged(); triggers onMonthChange() again, then it results in an infinite loop with fetchEventsinBackground() being called again & again!

Is there a better way out ? Thanks for all the help!

alamkanak commented 9 years ago

Sorry. I should have been more precise about the code. Here, check out the new code.

private List<EventModel> mEventModels = new ArrayList<EventModel>();

@Override
public List onMonthChange(int newYear, int newMonth) {
    // Check if you've already fetched the events for this month on the previous call.
    if (mEventModels.size() == 0 || !containsEvents(mEventModels, newYear, newMonth)) {
        // Fetch events for the date user selected.
        fetchEventsInBackground();
        return new ArrayList<WeekViewEvent>();
    }

    // If we've reached up to this point then it means we've already fetched the data in the previous call.
    return getWeekViewEventsFromEventModels(mEventModels, year, month);
}

// Callback when fetchEvents finishes.
public void setEventList(Activity activity, final List<EventModel> eventModels) {

    // Hide the progress dialog.
    if(mProgressDialog != null && mProgressDialog.isShowing()) {
        mProgressDialog.dismiss();
    }

    // Refresh the week view.
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // Store the returned events in a global variable for later use.
            mEventModels.addAll(eventModels);

            // This line will trigger the method 'onMonthChange()' again.
            mWeekView.notifyDatasetChanged();
        }
    });
}

private boolean containsEvents(List eventModels, year, month) {
    // TODO Do a search in the 'eventModels' list to see if it contains the events of the given 'year' and 'month'. If not then return false.
}

private boolean getWeekViewEventsFromEventModels(List eventModels, year, month) {
    // TODO Filter the events from the eventModels by year and month. It would be good idea if you remove the events that are not in the desired range (i.e. where the month is not month+-1). This will release some memory.
}
shalinipk commented 9 years ago

Thank you so much, I will try it out

saecmca commented 9 years ago

Hi

saecmca commented 9 years ago

I am not able to get your sample.me not work

alamkanak commented 9 years ago

@saecmca I'd suggest you to learn importing using gradle projects to android studio first.

saecmca commented 9 years ago

I am working Android Eclipse sdk

rohinisagar commented 9 years ago

what is the EventModel in List mEventModels = new ArrayList(). how design EventModel class.

entropitor commented 9 years ago

WeekViewEvent

rohinisagar commented 9 years ago

private List mEventModels = new ArrayList();

here i can use WeekViewEvent.

entropitor commented 9 years ago

private List<WeekViewEvent> mEventModels = newArrayList<WeekViewEvent>()

rohinisagar commented 9 years ago

thank you...its work. how to design Event like below image https://mail.google.com/mail/u/0/?ui=2&ik=9857f02e0d&view=fimg&th=14e4a2b90a50266b&attid=0.6&disp=inline&realattid=f_ibkup0op18&safe=1&attbid=ANGjdJ8EKb9wgUOpWxlpwrNivFJuS_WUf-7ok0oPWC1gTIFij1IB_a2K4vDVTJs9FvF2EnGc061hkOwReIlLE3DBScHONdjjdTSmHnB48jUAmjTDU08esK-yN9YIwxA&ats=1436415839773&rm=14e4a2b90a50266b&zw&sz=w1256-h523

entropitor commented 9 years ago

no permission to view image. But I suggest you take a look in the sample app: https://github.com/alamkanak/Android-Week-View/tree/master/sample

nrazon commented 8 years ago

@alamkanak I am trying to implement this library but still can success getting events with asynctask without infinite looping.

The sample code to prevent infinite loop you provided for on MonthChange event is this: if (events.size() == 0 || !containsEvents(events, newYear, newMonth)) {}

But here is my problem; I have events for december but not for january 2016.Because of I don't have any event for next month infinite loop starts because containsEvent for 2106/01 return false. Calender logic prefetches 1 month before and 1 month after. so onMonthChange fires 3 times. First it fires for 2015/12 and gets events, Than 2015/11 and still get events, Than 2016/01 and no events. Each these 3 calls to onMonthChange tries to get events from cloud with asynctask and as a result it calls setEventList() as you wrote. Each response for 105 and 2015/11 calls mWeekView.notifyDatasetChanged(); after mEventModels.addAll(eventModels); And every notifyDatasetChanged call fires onMonthChanges again for all 3 months.

Has anyone success from getting events with asynctask here and can you please share your way todo this?

entropitor commented 8 years ago

@nrazon It's best to keep a boolean for every month/year combo if you already created an asynctask to get these events. If you get another request for the same month, you just return an empty list as long as you don't have any events for that month.

dhavalocked commented 8 years ago

how to stop user from scrolling back (older Day than Today) from today?

waynem81 commented 8 years ago

May I see the fetchEventsInBackground() Async? I am having trouble adding my beans to mEventsModel. Using the mEventModels.addAll(eventModels), what data is stored in eventModels?

entropitor commented 8 years ago

@waynem81 You'll probably have to convert your bean to an instance of WeekViewEvent

waynem81 commented 8 years ago

Thank you @caske33. I was able to figure it out! I currently don't have memory issues, but I am curious how I would implement this portion. I want to be able to remove the events if they are outside of the range.

private boolean getWeekViewEventsFromEventModels(List eventModels, year, month) { // TODO Filter the events from the eventModels by year and month. It would be good idea if you remove the events that are not in the desired range (i.e. where the month is not month+-1). This will release some memory. }

entropitor commented 8 years ago

just loop over the events and add the ones that match to a new list and return that list?

miroslavign commented 8 years ago

This concept turned out to be a decent workaround for async loading of events from Google calendar using content providers.

fabioDnaweb commented 8 years ago

Hi guys, you can post a complete example to use a http call; I have a custom web API that returns a list of events, but I would make the http call only when there are of new events during scroll future/past.

Your solution?

THX

entropitor commented 8 years ago

Check the async example in the repo @fabioDnaweb

jas017 commented 7 years ago

There is an error in your code. The return type mentioned here is booleanand the onMonthChangemethod expects a list.

private boolean getWeekViewEventsFromEventModels(List eventModels, year, month) {
    // TODO Filter the events from the eventModels by year and month. It would be good idea if you remove the events that are not in the desired range (i.e. where the month is not month+-1). This will release some memory.
}