From e9730b12bca905115b54f69fe8a60139e8abe675 Mon Sep 17 00:00:00 2001 From: Hari Date: Sun, 19 Feb 2017 18:54:06 +0530 Subject: [PATCH] Modified widget to use NarrowFilter --- .../android/filters/NarrowFilterByDate.java | 17 +++++++++- .../widget/ZulipRemoteViewsFactory.java | 32 +++++++++++-------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/zulip/android/filters/NarrowFilterByDate.java b/app/src/main/java/com/zulip/android/filters/NarrowFilterByDate.java index 06b7cdb45..c89afa989 100644 --- a/app/src/main/java/com/zulip/android/filters/NarrowFilterByDate.java +++ b/app/src/main/java/com/zulip/android/filters/NarrowFilterByDate.java @@ -2,6 +2,7 @@ import android.os.Parcel; import android.os.Parcelable; +import android.util.Log; import com.j256.ormlite.stmt.Where; import com.zulip.android.R; @@ -30,6 +31,10 @@ public NarrowFilterByDate[] newArray(int size) { } }; private Date date = new Date(); + + //If true, then messages posted since the date variable should be shown + //If false, then only messages posted during the date variable should be shown + private boolean filterSince; private static Calendar calendar = Calendar.getInstance(); private static Calendar calendar2 = Calendar.getInstance(); @@ -40,6 +45,11 @@ public NarrowFilterByDate(Date date) { this.date = date; } + public NarrowFilterByDate(Date date, boolean filterSince) { + this.date = date; + this.filterSince = filterSince; + } + public int describeContents() { return 0; } @@ -66,7 +76,12 @@ public Where modWhere(Where where) calendar.add(Calendar.DAY_OF_MONTH, 1); Date toDate = calendar.getTime(); - where.between(Message.TIMESTAMP_FIELD, fromDate, toDate); + if(filterSince) { + where.gt(Message.TIMESTAMP_FIELD, fromDate); + } + else { + where.between(Message.TIMESTAMP_FIELD, fromDate, toDate); + } return where; } diff --git a/app/src/main/java/com/zulip/android/widget/ZulipRemoteViewsFactory.java b/app/src/main/java/com/zulip/android/widget/ZulipRemoteViewsFactory.java index b17b602ed..81f6b5f25 100644 --- a/app/src/main/java/com/zulip/android/widget/ZulipRemoteViewsFactory.java +++ b/app/src/main/java/com/zulip/android/widget/ZulipRemoteViewsFactory.java @@ -10,11 +10,14 @@ import com.j256.ormlite.stmt.QueryBuilder; import com.zulip.android.R; import com.zulip.android.ZulipApp; +import com.zulip.android.filters.NarrowFilter; +import com.zulip.android.filters.NarrowFilterByDate; import com.zulip.android.models.Message; import com.zulip.android.models.MessageType; import com.zulip.android.util.ZLog; import java.sql.SQLException; +import java.util.Calendar; import java.util.List; import static com.zulip.android.widget.WidgetPreferenceFragment.FROM_PREFERENCE; @@ -34,32 +37,35 @@ public void onCreate() { } - private String setupWhere() { + private Calendar getMinDate() { + Calendar calendar = Calendar.getInstance(); switch (from) { //These values present in R.arrays.from_values case "today": - return "timestamp BETWEEN DATE('now') AND DATE('now', '+1 day')"; + break; case "yesterday": - return "DATE(timestamp) >= DATE('now', '-1 days')"; + calendar.add(Calendar.DATE, -1); + break; case "week": - return "DATE(timestamp) >= DATE('now', 'weekday 0', '-7 days')"; + calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); + break; case "all": default: - return ""; + calendar = null; } + return calendar; } @Override public void onDataSetChanged() { - Log.i("ZULIP_WIDGET", "onDataSetChanged() = Data reloaded"); - QueryBuilder queryBuilder = ZulipApp.get().getDao(Message.class).queryBuilder(); - String filter; - filter = setupWhere(); - if (!filter.equals("")) { - queryBuilder.where().raw(filter); - } - try { + Log.i("ZULIP_WIDGET", "onDataSetChanged() = Data reloaded"); + QueryBuilder queryBuilder = ZulipApp.get().getDao(Message.class).queryBuilder(); + Calendar minDate = getMinDate(); + if (minDate != null) { + NarrowFilter minDateFilter = new NarrowFilterByDate(minDate.getTime(), true); + minDateFilter.modWhere(queryBuilder.where()); + } messageList = queryBuilder.query(); } catch (SQLException e) { ZLog.logException(e);