Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New constraints and defaults (forbid DUE=START) (#309)
* New constraints and defaults: - forbid DUE=START; - use defaults satisfying DUE>START; - shift DUE if START violates constraint * replace TimeFieldAdapter with FieldAdapter<Time>
- Loading branch information
Showing
16 changed files
with
405 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
opentasks/src/main/java/org/dmfs/tasks/model/adapters/CustomizedDefaultFieldAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.dmfs.tasks.model.adapters; | ||
|
||
import android.content.ContentValues; | ||
import android.database.Cursor; | ||
|
||
import org.dmfs.tasks.model.ContentSet; | ||
import org.dmfs.tasks.model.OnContentChangeListener; | ||
import org.dmfs.tasks.model.defaults.Default; | ||
|
||
|
||
/** | ||
* Enhances an existing {@link FieldAdapter} with a custom default value generator. | ||
* | ||
* @param <Type> Type of the {@link FieldAdapter} | ||
*/ | ||
public class CustomizedDefaultFieldAdapter<Type> extends FieldAdapter<Type> { | ||
|
||
private final FieldAdapter<Type> mFieldAdapter; | ||
private final Default<Type> mDefault; | ||
|
||
/** | ||
* Constructor for a new CustomizedDefaultFieldAdapter | ||
* @param fieldAdapter FieldAdapter which forms the base for this Adapter. | ||
* @param defaultGenerator Custom default value generator. | ||
*/ | ||
public CustomizedDefaultFieldAdapter(FieldAdapter<Type> fieldAdapter, Default<Type> defaultGenerator) { | ||
if (fieldAdapter == null) { | ||
throw new IllegalArgumentException("fieldAdapter must not be null"); | ||
} | ||
if (defaultGenerator == null) { | ||
throw new IllegalArgumentException("defaultGenerator must not be null"); | ||
} | ||
this.mFieldAdapter = fieldAdapter; | ||
this.mDefault = defaultGenerator; | ||
} | ||
|
||
@Override | ||
public Type get(ContentSet values) { | ||
return mFieldAdapter.get(values); | ||
} | ||
|
||
@Override | ||
public Type get(Cursor cursor) { | ||
return mFieldAdapter.get(cursor); | ||
} | ||
|
||
/** | ||
* Get a default value for the {@link FieldAdapter} based on the {@link Default} instance. | ||
* | ||
* @param values The {@link ContentSet}. | ||
* @return A default Value | ||
*/ | ||
@Override | ||
public Type getDefault(ContentSet values) { | ||
Type defaultValue = mFieldAdapter.getDefault(values); | ||
return mDefault.getCustomDefault(values, defaultValue); | ||
} | ||
|
||
@Override | ||
public void set(ContentSet values, Type value) { | ||
mFieldAdapter.set(values, value); | ||
} | ||
|
||
@Override | ||
public void set(ContentValues values, Type value) { | ||
mFieldAdapter.set(values, value); | ||
} | ||
|
||
@Override | ||
public void registerListener(ContentSet values, OnContentChangeListener listener, boolean initialNotification) { | ||
mFieldAdapter.registerListener(values, listener, initialNotification); | ||
} | ||
|
||
@Override | ||
public void unregisterListener(ContentSet values, OnContentChangeListener listener) { | ||
mFieldAdapter.unregisterListener(values, listener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
opentasks/src/main/java/org/dmfs/tasks/model/constraints/After.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.dmfs.tasks.model.constraints; | ||
|
||
import android.text.format.Time; | ||
|
||
import org.dmfs.tasks.model.ContentSet; | ||
import org.dmfs.tasks.model.adapters.FieldAdapter; | ||
import org.dmfs.tasks.model.defaults.Default; | ||
import org.dmfs.tasks.model.defaults.DefaultAfter; | ||
|
||
|
||
/** | ||
* Ensure a time is after a specific reference time. The new value will be set using {@link DefaultAfter} otherwise. | ||
*/ | ||
public class After extends AbstractConstraint<Time> | ||
{ | ||
private final FieldAdapter<Time> mReferenceAdapter; | ||
private final Default<Time> mDefault; | ||
|
||
|
||
public After(FieldAdapter<Time> referenceAdapter) | ||
{ | ||
mReferenceAdapter = referenceAdapter; | ||
mDefault = new DefaultAfter(referenceAdapter); | ||
} | ||
|
||
|
||
@Override | ||
public Time apply(ContentSet currentValues, Time oldValue, Time newValue) | ||
{ | ||
Time reference = mReferenceAdapter.get(currentValues); | ||
if (reference != null && newValue != null && !newValue.after(reference)) | ||
{ | ||
newValue.set(mDefault.getCustomDefault(currentValues, reference)); | ||
} | ||
return newValue; | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
opentasks/src/main/java/org/dmfs/tasks/model/constraints/BeforeOrShiftTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.dmfs.tasks.model.constraints; | ||
|
||
import android.text.format.Time; | ||
|
||
import org.dmfs.tasks.model.ContentSet; | ||
import org.dmfs.tasks.model.adapters.FieldAdapter; | ||
import org.dmfs.tasks.model.defaults.Default; | ||
import org.dmfs.tasks.model.defaults.DefaultAfter; | ||
|
||
|
||
/** | ||
* Ensure a time is before a specific reference time. | ||
* Otherwise, shift the reference time by the same amount that value has been shifted. | ||
* If this still violated the constraint, then set the reference value to its default value. | ||
*/ | ||
public class BeforeOrShiftTime extends AbstractConstraint<Time> | ||
{ | ||
private final FieldAdapter<Time> mReferenceAdapter; | ||
private final Default<Time> mDefault; | ||
|
||
|
||
public BeforeOrShiftTime(FieldAdapter<Time> referenceAdapter) | ||
{ | ||
mReferenceAdapter = referenceAdapter; | ||
mDefault = new DefaultAfter(null); | ||
} | ||
|
||
|
||
@Override | ||
public Time apply(ContentSet currentValues, Time oldValue, Time newValue) | ||
{ | ||
Time reference = mReferenceAdapter.get(currentValues); | ||
if (reference != null && newValue != null) | ||
{ | ||
if (oldValue != null && !newValue.before(reference)) | ||
{ | ||
// try to shift the reference value | ||
long diff = newValue.toMillis(false) - oldValue.toMillis(false); | ||
if (diff > 0) | ||
{ | ||
boolean isAllDay = reference.allDay; | ||
reference.set(reference.toMillis(false) + diff); | ||
|
||
// ensure the event is still allday if is was allday before. | ||
if (isAllDay) | ||
{ | ||
reference.set(reference.monthDay, reference.month, reference.year); | ||
} | ||
mReferenceAdapter.set(currentValues, reference); | ||
} | ||
} | ||
if (!newValue.before(reference)) | ||
{ | ||
// constraint is still violated, so set reference to its default value | ||
reference.set(mDefault.getCustomDefault(currentValues, newValue)); | ||
mReferenceAdapter.set(currentValues, reference); | ||
} | ||
} | ||
return newValue; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.