Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Fixes #155 : Modified widget to use NarrowFilter #411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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;
}
Expand All @@ -66,7 +76,12 @@ public Where<Message, Object> modWhere(Where<Message, Object> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Calendar.getInstance() method returns a Calendar object with the current date, so in case the minimum date is today, there is no need to modify it.

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<Message, Object> 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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a TAG filed in the class and use that as a parameter of Log.d

QueryBuilder<Message, Object> 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);
Expand Down