Skip to content

Commit

Permalink
New methods for events and reminders providers
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Aug 7, 2017
1 parent 302fef3 commit 131358d
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
package com.kunzisoft.remembirthday.activity;

import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.OperationApplicationException;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.CalendarContract;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.kunzisoft.remembirthday.element.CalendarEvent;
import com.kunzisoft.remembirthday.element.Contact;
import com.kunzisoft.remembirthday.element.DateUnknownYear;
import com.kunzisoft.remembirthday.provider.ActionBirthdayInDatabaseTask;
import com.kunzisoft.remembirthday.provider.CalendarProvider;
import com.kunzisoft.remembirthday.provider.EventProvider;

import java.util.ArrayList;

/**
* Abstract class to encapsulate the management of the birthday dialog.
* @author joker on 06/07/17.
Expand Down Expand Up @@ -104,51 +95,19 @@ public OnClickDialogListener(Contact contact) {
}

@Override
public void onClickPositiveButton(DateUnknownYear dateUnknownYear) {
public void onClickPositiveButton(DateUnknownYear newBirthday) {
// Update current birthday in database
ActionBirthdayInDatabaseTask.UpdateBirthdayToContactTask updateBirthdayToContactTask =
new ActionBirthdayInDatabaseTask.UpdateBirthdayToContactTask(
AbstractBuddyActivity.this,
contact.getDataAnniversaryId(),
contact.getBirthday(),
dateUnknownYear);
newBirthday);
updateBirthdayToContactTask.setCallbackActionBirthday(AbstractBuddyActivity.this);
updateBirthdayToContactTask.execute();

// Update event in calendar
CalendarEvent event = EventProvider.getNextEventFromContact(AbstractBuddyActivity.this, contact);
if(event == null) {
long calendarId = CalendarProvider.getCalendar(AbstractBuddyActivity.this);
if (calendarId != -1) {
EventProvider.insert(AbstractBuddyActivity.this,
calendarId,
// TODO get reminders from list
CalendarEvent.buildCalendarEventFromContact(
AbstractBuddyActivity.this,
contact),
contact);
} else {
Log.e("CalendarSyncAdapter", "Unable to create calendar");
}
} else {
event.setDateStart(DateUnknownYear.getNextAnniversary(dateUnknownYear));
event.setAllDay(true);
Log.e(getClass().getSimpleName(), event.toString());
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
ContentProviderOperation contentProviderOperation = EventProvider.update(event);
operations.add(contentProviderOperation);
try {
ContentProviderResult[] contentProviderResults =
getContentResolver().applyBatch(CalendarContract.AUTHORITY, operations);
for(ContentProviderResult contentProviderResult : contentProviderResults) {
Log.d(getClass().getSimpleName(), contentProviderResult.toString());
if (contentProviderResult.uri != null)
Log.d(getClass().getSimpleName(), contentProviderResult.uri.toString());
}
} catch (RemoteException|OperationApplicationException e) {
Log.e(this.getClass().getSimpleName(), "Unable to update event : " + e.getMessage());
}
}
EventProvider.updateEvent(AbstractBuddyActivity.this, contact, newBirthday);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public void afterActionBirthdayInDatabase(DateUnknownYear birthday, Action actio
.replace(R.id.activity_details_buddy_fragment_details_buddy, details, TAG_DETAILS_FRAGMENT)
.commit();
*/
// TODO Change when new version of FABOptions
setResult(Activity.RESULT_OK);
finish();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,17 @@ public void onActivityCreated(Bundle savedInstanceState) {

// Build default elements
CalendarEvent event = EventProvider.getNextEventFromContact(getContext(), contact);

if(event != null) {
List<Reminder> reminders = ReminderProvider.getRemindersFromEvent(getContext(), event);
event.addReminders(reminders);
Log.d(TAG, "Get event from calendar : " + event.toString());
remindersAdapter.addReminders(reminders);

// Attach observer for changes
// TODO Update each event with all reminders
remindersAdapter.registerReminderObserver(
new ReminderCalendarObserver(getContext(), event));
new ReminderCalendarObserver(getContext(), contact, event));
} else {
Log.e(TAG, "Error when get event from calendar");
}
Expand Down Expand Up @@ -372,6 +374,9 @@ public void onClick(DialogInterface dialog, int id) {
removeBirthdayFromContactTask.setCallbackActionBirthday(
(ActionBirthdayInDatabaseTask.CallbackActionBirthday) getActivity());
removeBirthdayFromContactTask.execute();

// Delete event in calendar
EventProvider.deleteEventsFromContact(getContext(), contact);
}
});
builderDialog.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void onClickPositiveButton(DateUnknownYear dateUnknownYear) {
// TODO Encapsulate
long calendarId = CalendarProvider.getCalendar(ListContactsActivity.this);
if (calendarId != -1) {
CalendarEvent event = CalendarEvent.buildCalendarEventFromContact(
CalendarEvent event = CalendarEvent.buildDefaultEventFromContactToSave(
ListContactsActivity.this,
contactWithRawIdSelected);
allOperationList.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import android.util.Log;

import com.kunzisoft.remembirthday.element.CalendarEvent;
import com.kunzisoft.remembirthday.element.Contact;
import com.kunzisoft.remembirthday.element.Reminder;
import com.kunzisoft.remembirthday.provider.EventProvider;
import com.kunzisoft.remembirthday.provider.ReminderProvider;

import java.util.ArrayList;
Expand All @@ -23,20 +25,22 @@
public class ReminderCalendarObserver implements AbstractReminderAdapter.ReminderDataObserver<Reminder> {

private Context context;
private CalendarEvent event;
private CalendarEvent baseEvent;
private List<CalendarEvent> afterEvents;
private ContentResolver contentResolver;
private ArrayList<ContentProviderOperation> ops;

public ReminderCalendarObserver(Context context, CalendarEvent calendarEvent) {
public ReminderCalendarObserver(Context context, Contact contact, CalendarEvent baseEvent) {
this.context = context;
this.event = calendarEvent;
this.afterEvents = EventProvider.getEventsSaveForeEachYearAfterNextEvent(context, contact);
this.baseEvent = baseEvent;
this.contentResolver = context.getContentResolver();
this.ops = new ArrayList<>();
}

@Override
public void onReminderAdded(Reminder reminder) {
ops.add(ReminderProvider.insert(context, event.getId(), reminder));
ops.add(ReminderProvider.insert(context, baseEvent.getId(), reminder));
//TODO Add id to reminder
applyBatch();
}
Expand All @@ -45,35 +49,50 @@ public void onReminderAdded(Reminder reminder) {
public void onRemindersAdded(List<Reminder> reminders) {
//TODO Add id to reminder
for(Reminder reminder : reminders) {
ops.add(ReminderProvider.insert(context, event.getId(), reminder));
ops.add(ReminderProvider.insert(context, baseEvent.getId(), reminder));
}
applyBatch();
}

@Override
public void onReminderUpdated(Reminder reminder) {
ops.add(ReminderProvider.update(context, event.getId(), reminder));
// TODO with link
ops.add(ReminderProvider.update(context, baseEvent.getId(), reminder));
for(CalendarEvent afterEvent : afterEvents) {
//ops.add(ReminderProvider.updateWithUnknownId(context, afterEvent.getId(), reminder, newMinutes));
}
applyBatch();
}

@Override
public void onRemindersUpdated(List<Reminder> reminders) {
for(Reminder reminder : reminders) {
ops.add(ReminderProvider.update(context, event.getId(), reminder));
ops.add(ReminderProvider.update(context, baseEvent.getId(), reminder));
for(CalendarEvent afterEvent : afterEvents) {
// TODO with links
//ops.add(ReminderProvider.update(context, afterEvent.getId(), reminder));
}
}
applyBatch();
}

@Override
public void onReminderDeleted(Reminder reminder) {
ops.add(ReminderProvider.delete(context, event.getId(), reminder));
ops.add(ReminderProvider.delete(context, baseEvent.getId(), reminder));
for(CalendarEvent afterEvent : afterEvents) {
ops.add(ReminderProvider.deleteWithUnknownId(context, afterEvent.getId(), reminder));
}
applyBatch();
}

@Override
public void onRemindersDeleted(List<Reminder> reminders) {
// TODO delete
for(Reminder reminder : reminders) {
ops.add(ReminderProvider.delete(context, event.getId(), reminder));
ops.add(ReminderProvider.delete(context, baseEvent.getId(), reminder));
for(CalendarEvent afterEvent : afterEvents) {
ops.add(ReminderProvider.deleteWithUnknownId(context, afterEvent.getId(), reminder));
}
}
applyBatch();
}
Expand All @@ -94,4 +113,5 @@ private void applyBatch() {
ops.clear();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static String getEventTitleFromContact(Context context, Contact contact)
return "";
}

public static CalendarEvent buildCalendarEventFromContact(Context context, Contact contact) {
public static CalendarEvent buildDefaultEventFromContactToSave(Context context, Contact contact) {
CalendarEvent event = new CalendarEvent(getEventTitleFromContact(context, contact),
contact.getNextBirthday(), true);
int[] defaultTime = PreferencesManager.getDefaultTime(context);
Expand Down Expand Up @@ -74,7 +74,7 @@ public CalendarEvent(String title, Date dateStart, Date dateStop) {
* @param another Base event
*/
public CalendarEvent(CalendarEvent another) {
this.id = another.id;
this.id = ID_UNDEFINED;
this.dateStart = another.dateStart;
this.dateStop = another.dateStop;
setAllDay(another.allDay);
Expand Down Expand Up @@ -218,6 +218,33 @@ public String toString() {
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

CalendarEvent that = (CalendarEvent) o;

if (allDay != that.allDay) return false;
if (dateStart != null ? !dateStart.equals(that.dateStart) : that.dateStart != null)
return false;
if (dateStop != null ? !dateStop.equals(that.dateStop) : that.dateStop != null)
return false;
if (title != null ? !title.equals(that.title) : that.title != null) return false;
return description != null ? description.equals(that.description) : that.description == null;

}

@Override
public int hashCode() {
int result = dateStart != null ? dateStart.hashCode() : 0;
result = 31 * result + (dateStop != null ? dateStop.hashCode() : 0);
result = 31 * result + (allDay ? 1 : 0);
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
return result;
}

@Override
public int describeContents() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ public Date getDate() {
return date;
}

/**
* Get the date with a specific year
* @param year Year
* @return Date with year
*/
public Date getDateWithYear(int year) {
return getDateWithYear(this.date, year);
}

/**
* Get the date with a specific year
* @param date Date
* @param year Year
* @return Date with year
*/
public static Date getDateWithYear(Date date, int year) {
return new DateTime(date).withYear(year).toDate();
}

/**
* Assign a new date, only day, month and year are used.
* @param date The new date
Expand Down Expand Up @@ -172,7 +191,6 @@ public static Date getNextAnniversary(Date date) {
}
}


/**
* Number of years between a date and today
* @param date Date: Year for calculate number
Expand Down
Loading

0 comments on commit 131358d

Please sign in to comment.