mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-19 21:20:43 +01:00
add rescheduling monthly and yearly events
This commit is contained in:
parent
71161651f4
commit
1d65ad305e
@ -9,7 +9,7 @@ public class Constants {
|
|||||||
|
|
||||||
public static final int DAY = 86400;
|
public static final int DAY = 86400;
|
||||||
public static final int WEEK = 604800;
|
public static final int WEEK = 604800;
|
||||||
public static final int MONTH = 2592000;
|
public static final int MONTH = 2592000; // exact value not taken into account, Joda is used for adding months and years
|
||||||
public static final int YEAR = 31536000;
|
public static final int YEAR = 31536000;
|
||||||
|
|
||||||
// Shared Preferences
|
// Shared Preferences
|
||||||
|
@ -217,6 +217,7 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
events.addAll(currEvents);
|
events.addAll(currEvents);
|
||||||
}
|
}
|
||||||
|
|
||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import android.widget.Toast;
|
|||||||
import com.simplemobiletools.calendar.models.Event;
|
import com.simplemobiletools.calendar.models.Event;
|
||||||
import com.simplemobiletools.calendar.receivers.NotificationReceiver;
|
import com.simplemobiletools.calendar.receivers.NotificationReceiver;
|
||||||
|
|
||||||
|
import org.joda.time.DateTime;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
public static int adjustAlpha(int color, float factor) {
|
public static int adjustAlpha(int color, float factor) {
|
||||||
@ -25,16 +27,41 @@ public class Utils {
|
|||||||
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void scheduleNotification(Context context, Event event) {
|
public static void scheduleNextEvent(Context context, Event event) {
|
||||||
if (event.getReminderMinutes() == -1) {
|
int startTS = event.getStartTS() - event.getReminderMinutes() * 60;
|
||||||
return;
|
int newTS = 0;
|
||||||
|
if (event.getRepeatInterval() == Constants.DAY || event.getRepeatInterval() == Constants.WEEK) {
|
||||||
|
while (startTS < System.currentTimeMillis() / 1000 + 5) {
|
||||||
|
startTS += event.getRepeatInterval();
|
||||||
|
}
|
||||||
|
newTS = startTS;
|
||||||
|
} else if (event.getRepeatInterval() == Constants.MONTH) {
|
||||||
|
newTS = getNewTS(startTS, true);
|
||||||
|
} else if (event.getRepeatInterval() == Constants.YEAR) {
|
||||||
|
newTS = getNewTS(startTS, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduleEventIn(context, event.getStartTS(), event);
|
if (newTS != 0)
|
||||||
|
Utils.scheduleEventIn(context, newTS, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getNewTS(int ts, boolean isMonthly) {
|
||||||
|
DateTime dateTime = Formatter.getDateTimeFromTS(ts);
|
||||||
|
while (dateTime.isBeforeNow()) {
|
||||||
|
dateTime = isMonthly ? dateTime.plusMonths(1) : dateTime.plusYears(1);
|
||||||
|
}
|
||||||
|
return (int) (dateTime.getMillis() / 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void scheduleNotification(Context context, Event event) {
|
||||||
|
if (event.getReminderMinutes() == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
scheduleNextEvent(context, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void scheduleEventIn(Context context, int notifTS, Event event) {
|
public static void scheduleEventIn(Context context, int notifTS, Event event) {
|
||||||
final long delayFromNow = (long) notifTS * 1000 - event.getReminderMinutes() * 60000 - System.currentTimeMillis();
|
final long delayFromNow = (long) notifTS * 1000 - System.currentTimeMillis();
|
||||||
if (delayFromNow < 0)
|
if (delayFromNow < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -355,9 +355,9 @@ public class EventActivity extends SimpleActivity implements DBHelper.DBOperatio
|
|||||||
if (DateTime.now().isAfter(mEventStartDateTime.getMillis())) {
|
if (DateTime.now().isAfter(mEventStartDateTime.getMillis())) {
|
||||||
Utils.showToast(getApplicationContext(), R.string.past_event_added);
|
Utils.showToast(getApplicationContext(), R.string.past_event_added);
|
||||||
} else {
|
} else {
|
||||||
Utils.scheduleNotification(getApplicationContext(), event);
|
|
||||||
Utils.showToast(getApplicationContext(), R.string.event_added);
|
Utils.showToast(getApplicationContext(), R.string.event_added);
|
||||||
}
|
}
|
||||||
|
Utils.scheduleNotification(getApplicationContext(), event);
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,18 +38,7 @@ public class NotificationReceiver extends BroadcastReceiver {
|
|||||||
notificationManager.notify(id, notification);
|
notificationManager.notify(id, notification);
|
||||||
|
|
||||||
if (event.getRepeatInterval() != 0)
|
if (event.getRepeatInterval() != 0)
|
||||||
scheduleNextEvent(context, event);
|
Utils.scheduleNextEvent(context, event);
|
||||||
}
|
|
||||||
|
|
||||||
private void scheduleNextEvent(Context context, Event event) {
|
|
||||||
if (event.getRepeatInterval() == Constants.DAY || event.getRepeatInterval() == Constants.WEEK) {
|
|
||||||
int startTS = event.getStartTS();
|
|
||||||
while (startTS < System.currentTimeMillis() / 1000 + 5) {
|
|
||||||
startTS += event.getRepeatInterval();
|
|
||||||
}
|
|
||||||
|
|
||||||
Utils.scheduleEventIn(context, startTS, event);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private PendingIntent getPendingIntent(Context context, Event event) {
|
private PendingIntent getPendingIntent(Context context, Event event) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user