Skip to content

Commit

Permalink
Add observer for add, update and delete reminder, and update event
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Aug 4, 2017
1 parent 124518a commit 6a84089
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
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;

Expand All @@ -12,6 +17,8 @@
import com.kunzisoft.remembirthday.provider.EventProvider;
import com.kunzisoft.remembirthday.provider.UpdateBirthdayToContactTask;

import java.util.ArrayList;

/**
* Abstract class to encapsulate the management of the birthday dialog.
* @author joker on 06/07/17.
Expand Down Expand Up @@ -110,20 +117,39 @@ public void onClickPositiveButton(DateUnknownYear dateUnknownYear) {
updateBirthdayToContactTask.execute();

// Update event in calendar
// TODO UPDATE
/*
long calendarId = CalendarProvider.getCalendar(AbstractBuddyActivity.this);
if (calendarId != -1) {
EventProvider.insert(AbstractBuddyActivity.this,
calendarId,
CalendarEvent.buildCalendarEventFromContact(
AbstractBuddyActivity.this,
contact),
contact);
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 {
Log.e("CalendarSyncAdapter", "Unable to create calendar");
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());
}
}
*/
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import com.kunzisoft.remembirthday.R;
import com.kunzisoft.remembirthday.adapter.AutoMessageAdapter;
import com.kunzisoft.remembirthday.adapter.MenuAdapter;
import com.kunzisoft.remembirthday.adapter.ReminderNotificationsAdapter;
import com.kunzisoft.remembirthday.adapter.ReminderCalendarObserver;
import com.kunzisoft.remembirthday.adapter.ReminderCalendarNotificationsAdapter;
import com.kunzisoft.remembirthday.animation.AnimationCircle;
import com.kunzisoft.remembirthday.element.CalendarEvent;
import com.kunzisoft.remembirthday.element.Contact;
Expand Down Expand Up @@ -71,7 +72,7 @@ public class DetailsBuddyFragment extends Fragment implements ActionContactMenu{
protected AutoMessageAdapter autoMessagesAdapter;

protected RecyclerView remindersListView;
protected ReminderNotificationsAdapter remindersAdapter;
protected ReminderCalendarNotificationsAdapter remindersAdapter;

private RecyclerView menuListView;
private GridLayoutManager gridLayoutManager;
Expand Down Expand Up @@ -181,7 +182,7 @@ public void onActivityCreated(Bundle savedInstanceState) {

if(PreferencesManager.isCustomCalendarActive(getContext())) {
// Add default reminders and link view to adapter
remindersAdapter = new ReminderNotificationsAdapter(getContext(), contact.getBirthday());
remindersAdapter = new ReminderCalendarNotificationsAdapter(getContext(), contact.getBirthday());
remindersListView.setAdapter(remindersAdapter);

// Build default elements
Expand All @@ -191,6 +192,10 @@ public void onActivityCreated(Bundle savedInstanceState) {
event.addReminders(reminders);
Log.d(TAG, "Get event from calendar : " + event.toString());
remindersAdapter.addReminders(reminders);

// Attach observer for changes
remindersAdapter.registerReminderObserver(
new ReminderCalendarObserver(getContext(), event));
} else {
Log.e(TAG, "Error when get event from calendar");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,25 @@ public abstract class AbstractReminderAdapter<E extends Reminder, T extends Remi

private SimpleDateFormat reminderDateFormatter;

private List<ReminderDataObserver<E>> reminderDataObservers;

public AbstractReminderAdapter(Context context, DateUnknownYear anniversary) {
this.context = context;
this.anniversary = anniversary;
listReminders = new LinkedList<>();
this.listReminders = new LinkedList<>();
this.reminderDataObservers = new ArrayList<>();
}

public void registerReminderObserver(ReminderDataObserver<E> reminderDataObserver) {
this.reminderDataObservers.add(reminderDataObserver);
}

public void unregisterReminderObserver(ReminderDataObserver<E> reminderDataObserver) {
this.reminderDataObservers.remove(reminderDataObserver);
}

public void unregisterAllReminderObservers() {
this.reminderDataObservers.clear();
}

/**
Expand All @@ -46,6 +61,10 @@ public void addReminders(List<E> reminders) {
if(!reminders.isEmpty()) {
listReminders.addAll(reminders);
this.notifyItemRangeChanged(start, reminders.size() - 1);
// Notify all customs observers
for(ReminderDataObserver<E> observer : reminderDataObservers) {
observer.onRemindersAdded(reminders);
}
}
}

Expand All @@ -56,13 +75,12 @@ public void addReminders(List<E> reminders) {
public void addReminder(E reminder) {
listReminders.add(reminder);
this.notifyItemChanged(listReminders.size() - 1);
// Notify all customs observers
for(ReminderDataObserver<E> observer : reminderDataObservers) {
observer.onReminderAdded(reminder);
}
}

/**
* Add a default reminder to the list, init day with delta of anniversary, hour and minute to default
*/
public abstract void addDefaultItem(int deltaDay);

/**
* Add a default reminder to the list with the day of anniversary, init hour and minute to default
*/
Expand Down Expand Up @@ -121,6 +139,10 @@ public void onClick(View view) {
int position = listReminders.indexOf(reminder);
notifyItemRemoved(position);
listReminders.remove(position);
// Notify observable
for(ReminderDataObserver<E> observer : reminderDataObservers) {
observer.onReminderDeleted(reminder);
}
}
}

Expand Down Expand Up @@ -148,6 +170,11 @@ public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {

// Set text in view
((TextView) view).setText(reminderDateFormatter.format(reminder.getDate()));

// Notify observable
for(ReminderDataObserver<E> observer : reminderDataObservers) {
observer.onReminderUpdated(reminder);
}
}
}, reminder.getHourOfDay(), reminder.getMinuteOfHour(), true);
timePickerDialog.show();
Expand All @@ -171,10 +198,30 @@ public OnDaySelected(E reminder, List<Integer> listDays) {
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// New Date when delta days is selected
reminder.setDeltaDay(listDays.get(position));
// Notify observable
for(ReminderDataObserver<E> observer : reminderDataObservers) {
observer.onReminderUpdated(reminder);
}
Log.d(this.getClass().getSimpleName(), "Assign new date for reminder : " + reminder.getDate().toString());
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
}

/**
* Use to create observers of reminders
* @param <E> Reminder type
*/
public interface ReminderDataObserver<E> {

void onReminderAdded(E reminder);
void onRemindersAdded(List<E> reminders);

void onReminderUpdated(E reminder);
void onRemindersUpdated(List<E> reminders);

void onReminderDeleted(E reminder);
void onRemindersDeleted(List<E> reminders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ public AutoMessageAdapter(Context context, DateUnknownYear anniversary) {
super(context, anniversary);
}

@Override
public void addDefaultItem(int deltaDay) {
int[] defaultTime = PreferencesManager.getDefaultTime(context);
addReminder(new AutoMessage(anniversary.getDate(), defaultTime[0], defaultTime[1], deltaDay));
}

@Override
public void addDefaultItem() {
addDefaultItem(0);
int[] defaultTime = PreferencesManager.getDefaultTime(context);
addReminder(new AutoMessage(anniversary.getDate(), defaultTime[0], defaultTime[1], 0));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@
/**
* Adapter who manage list of reminders
*/
public class ReminderNotificationsAdapter extends AbstractReminderAdapter<Reminder, ReminderViewHolder> {
public class ReminderCalendarNotificationsAdapter extends AbstractReminderAdapter<Reminder, ReminderViewHolder> {

public ReminderNotificationsAdapter(Context context, DateUnknownYear anniversary) {
public ReminderCalendarNotificationsAdapter(Context context, DateUnknownYear anniversary) {
super(context, anniversary);
}

@Override
public void addDefaultItem(int deltaDay) {
int[] defaultTime = PreferencesManager.getDefaultTime(context);
addReminder(new Reminder(anniversary.getDate(), defaultTime[0], defaultTime[1], deltaDay));
}

@Override
public void addDefaultItem() {
addDefaultItem(0);
int[] defaultTime = PreferencesManager.getDefaultTime(context);
addReminder(new Reminder(anniversary.getDate(), defaultTime[0], defaultTime[1], 0));
}

@Override
public ReminderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_reminder_notifs, parent, false);
return new AutoMessageViewHolder(itemView);
return new ReminderViewHolder(itemView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.kunzisoft.remembirthday.adapter;

import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.Context;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.provider.CalendarContract;
import android.util.Log;

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

import java.util.ArrayList;
import java.util.List;

/**
* Observer who do actions for reminders
* Created by joker on 04/08/17.
*/
public class ReminderCalendarObserver implements AbstractReminderAdapter.ReminderDataObserver<Reminder> {

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

public ReminderCalendarObserver(Context context, CalendarEvent calendarEvent) {
this.context = context;
this.event = calendarEvent;
this.contentResolver = context.getContentResolver();
this.ops = new ArrayList<>();
}

@Override
public void onReminderAdded(Reminder reminder) {
ops.add(ReminderProvider.insert(context, event.getId(), reminder));
//TODO Add id to reminder
applyBatch();
}

@Override
public void onRemindersAdded(List<Reminder> reminders) {
//TODO Add id to reminder
for(Reminder reminder : reminders) {
ops.add(ReminderProvider.insert(context, event.getId(), reminder));
}
applyBatch();
}

@Override
public void onReminderUpdated(Reminder reminder) {
ops.add(ReminderProvider.update(context, event.getId(), reminder));
applyBatch();
}

@Override
public void onRemindersUpdated(List<Reminder> reminders) {
for(Reminder reminder : reminders) {
ops.add(ReminderProvider.update(context, event.getId(), reminder));
}
applyBatch();
}

@Override
public void onReminderDeleted(Reminder reminder) {
ops.add(ReminderProvider.delete(context, event.getId(), reminder));
applyBatch();
}

@Override
public void onRemindersDeleted(List<Reminder> reminders) {
for(Reminder reminder : reminders) {
ops.add(ReminderProvider.delete(context, event.getId(), reminder));
}
applyBatch();
}

/**
* Apply operations
*/
private void applyBatch() {
try {
ContentProviderResult[] contentProviderResults =
contentResolver.applyBatch(CalendarContract.AUTHORITY, ops);
for(ContentProviderResult result : contentProviderResults)
if(result.uri != null)
Log.d(this.getClass().getSimpleName(), result.uri.toString());
} catch (RemoteException|OperationApplicationException e) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
} finally {
ops.clear();
}
}
}

This file was deleted.

Loading

0 comments on commit 6a84089

Please sign in to comment.