Fix check for filter criteria changes

pull/996/head
Alex Baker 5 years ago
parent 85878e80eb
commit 312cfb36ee

@ -1,7 +1,5 @@
package com.todoroo.astrid.api;
import static com.todoroo.andlib.utility.AndroidUtilities.mapToSerializedString;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
@ -32,30 +30,16 @@ public class CustomFilter extends Filter {
public CustomFilter(org.tasks.data.Filter filter) {
super(filter.getTitle(), filter.getSql(), filter.getValuesAsMap());
this.id = filter.getId();
this.criterion = filter.getCriterion();
this.tint = filter.getColor();
this.icon = filter.getIcon();
id = filter.getId();
criterion = filter.getCriterion();
tint = filter.getColor();
icon = filter.getIcon();
}
private CustomFilter(Parcel parcel) {
readFromParcel(parcel);
}
public org.tasks.data.Filter toStoreObject() {
org.tasks.data.Filter filter = new org.tasks.data.Filter();
filter.setId(id);
filter.setTitle(listingTitle);
filter.setSql(sqlQuery);
filter.setIcon(icon);
filter.setColor(tint);
if (valuesForNewTasks != null && valuesForNewTasks.size() > 0) {
filter.setValues(mapToSerializedString(valuesForNewTasks));
}
filter.setCriterion(criterion);
return filter;
}
public void setId(long id) {
this.id = id;
}

@ -8,7 +8,9 @@ package org.tasks.activities;
import static android.text.TextUtils.isEmpty;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;
import static com.todoroo.andlib.utility.AndroidUtilities.mapToSerializedString;
import android.content.Context;
import android.content.DialogInterface;
@ -30,6 +32,7 @@ import com.google.android.material.floatingactionbutton.ExtendedFloatingActionBu
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.todoroo.andlib.data.Property.CountProperty;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.sql.UnaryCriterion;
@ -47,7 +50,6 @@ import com.todoroo.astrid.core.CustomFilterItemTouchHelper;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -88,11 +90,6 @@ public class FilterSettingsActivity extends BaseListSettingsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
filter = getIntent().getParcelableExtra(TOKEN_FILTER);
if (filter == null) {
org.tasks.data.Filter f = new org.tasks.data.Filter();
f.setSql("");
this.filter = new CustomFilter(f);
}
super.onCreate(savedInstanceState);
@ -102,23 +99,24 @@ public class FilterSettingsActivity extends BaseListSettingsActivity {
name.setText(filter.listingTitle);
}
criteria =
new ArrayList<>(
CriterionInstance.fromString(
filterCriteriaProvider,
savedInstanceState == null
? filter.getCriterion()
: savedInstanceState.getString(EXTRA_CRITERIA)));
if (criteria.isEmpty()) {
if (savedInstanceState != null) {
criteria =
CriterionInstance.fromString(
filterCriteriaProvider, savedInstanceState.getString(EXTRA_CRITERIA));
} else if (filter != null) {
criteria = CriterionInstance.fromString(filterCriteriaProvider, filter.getCriterion());
} else {
CriterionInstance instance = new CriterionInstance();
instance.criterion = filterCriteriaProvider.getStartingUniverse();
instance.type = CriterionInstance.TYPE_UNIVERSE;
criteria.add(instance);
criteria = newArrayList(instance);
}
adapter = new CustomFilterAdapter(criteria, locale, this::onClick);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
new ItemTouchHelper(new CustomFilterItemTouchHelper(this, this::onMove, this::onDelete, this::updateList))
new ItemTouchHelper(
new CustomFilterItemTouchHelper(this, this::onMove, this::onDelete, this::updateList))
.attachToRecyclerView(recyclerView);
fab.setExtended(isNew() || adapter.getItemCount() <= 1);
@ -256,7 +254,7 @@ public class FilterSettingsActivity extends BaseListSettingsActivity {
@Override
protected boolean isNew() {
return filter.getId() == 0;
return filter == null;
}
@Override
@ -284,19 +282,23 @@ public class FilterSettingsActivity extends BaseListSettingsActivity {
}
if (hasChanges()) {
filter.listingTitle = newName;
filter.tint = selectedColor;
filter.icon = selectedIcon;
filter.sqlQuery = getSql();
filter.valuesForNewTasks.clear();
for (Map.Entry<String, Object> entry : getValues().entrySet()) {
filter.valuesForNewTasks.put(entry.getKey(), entry.getValue());
org.tasks.data.Filter f = new org.tasks.data.Filter();
f.setTitle(newName);
f.setColor(selectedColor);
f.setIcon(selectedIcon);
f.setValues(mapToSerializedString(getValues()));
f.setCriterion(getCriterion());
f.setSql(getSql());
if (isNew()) {
f.setId(filterDao.insert(f));
} else {
f.setId(filter.getId());
filterDao.update(f);
}
filter.setCriterion(getCriterion());
filter.setId(filterDao.insertOrUpdate(filter.toStoreObject()));
setResult(
RESULT_OK,
new Intent(TaskListFragment.ACTION_RELOAD).putExtra(MainActivity.OPEN_FILTER, filter));
new Intent(TaskListFragment.ACTION_RELOAD)
.putExtra(MainActivity.OPEN_FILTER, new CustomFilter(f)));
}
finish();
@ -308,12 +310,18 @@ public class FilterSettingsActivity extends BaseListSettingsActivity {
@Override
protected boolean hasChanges() {
return !(getNewName().equals(filter.listingTitle)
&& selectedColor == filter.tint
&& selectedIcon == filter.icon
&& getSql().equals(filter.sqlQuery)
&& getValues().equals(filter.valuesForNewTasks)
&& getCriterion().equals(filter.getCriterion()));
if (isNew()) {
return !Strings.isNullOrEmpty(getNewName())
|| selectedColor != 0
|| selectedIcon != -1
|| criteria.size() > 1;
}
return !getNewName().equals(filter.listingTitle)
|| selectedColor != filter.tint
|| selectedIcon != filter.icon
|| !getCriterion().equals(filter.getCriterion())
|| !getValues().equals(filter.valuesForNewTasks)
|| !getSql().equals(filter.getOriginalSqlQuery());
}
@Override

@ -19,11 +19,8 @@ public interface FilterDao {
@Query("SELECT * FROM filters WHERE title = :title COLLATE NOCASE LIMIT 1")
Filter getByName(String title);
@Insert(onConflict = OnConflictStrategy.REPLACE)
long insertOrUpdate(Filter storeObject);
@Insert
void insert(Filter filter);
long insert(Filter filter);
@Query("SELECT * FROM filters")
List<Filter> getFilters();

Loading…
Cancel
Save