Skip to content

Commit

Permalink
Confirm before discard, home saves, back discards
Browse files Browse the repository at this point in the history
  • Loading branch information
abaker committed Feb 2, 2016
1 parent 798f854 commit ca3dd86
Show file tree
Hide file tree
Showing 37 changed files with 172 additions and 131 deletions.
87 changes: 38 additions & 49 deletions src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
import org.tasks.ui.MenuColorizer;
import org.tasks.ui.TaskEditControlFragment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
Expand All @@ -77,7 +80,6 @@ public static TaskEditFragment newTaskEditFragment(boolean isNewTask, Task task)
TaskEditFragment taskEditFragment = new TaskEditFragment();
taskEditFragment.isNewTask = isNewTask;
taskEditFragment.model = task;
taskEditFragment.applyModel = true;
return taskEditFragment;
}

Expand All @@ -94,7 +96,6 @@ public static TaskEditFragment newTaskEditFragment(boolean isNewTask, Task task)
* Task in progress (during orientation change)
*/
private static final String EXTRA_TASK = "extra_task"; //$NON-NLS-1$
private static final String EXTRA_APPLY_MODEL = "extra_apply_model";
private static final String EXTRA_IS_NEW_TASK = "extra_is_new_task";

/**
Expand Down Expand Up @@ -151,7 +152,6 @@ public static TaskEditFragment newTaskEditFragment(boolean isNewTask, Task task)

/** task model */
Task model = null;
private boolean applyModel = false;

private boolean showEditComments;
private TaskEditFragmentCallbackHandler callback;
Expand All @@ -169,7 +169,6 @@ public void onCreate(Bundle savedInstanceState) {
// if we were editing a task already, restore it
if (savedInstanceState != null) {
model = savedInstanceState.getParcelable(EXTRA_TASK);
applyModel = savedInstanceState.getBoolean(EXTRA_APPLY_MODEL);
isNewTask = savedInstanceState.getBoolean(EXTRA_IS_NEW_TASK);
}

Expand Down Expand Up @@ -199,8 +198,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

notificationManager.cancel(model.getId());

applyModel = false;

if (!showEditComments) {
commentsBar.setVisibility(View.GONE);
}
Expand Down Expand Up @@ -321,30 +318,21 @@ public void run() {
* ======================================================================
*/

private String getTitle() {
return getEditTitleControlSet().getTitle();
}

/** Save task model from values in UI components */
public void save() {
String title = getTitle();
if (title == null) {
return;
}

if (title.length() > 0) {
model.setDeletionDate(0L);
}

if (title.length() == 0) {
return;
}

private List<TaskEditControlFragment> getFragments() {
List<TaskEditControlFragment> fragments = new ArrayList<>();
for (int fragmentId : rowIds) {
TaskEditControlFragment fragment = (TaskEditControlFragment) getFragmentManager().findFragmentById(fragmentId);
if (fragment == null) {
break;
}
fragments.add(fragment);
}
return fragments;
}

/** Save task model from values in UI components */
public void save() {
for (TaskEditControlFragment fragment : getFragments()) {
fragment.apply(model);
}
taskService.save(model);
Expand All @@ -363,6 +351,7 @@ public void save() {
}

removeExtrasFromIntent(getActivity().getIntent());
callback.taskEditFinished();
}

private EditTitleControlSet getEditTitleControlSet() {
Expand All @@ -382,21 +371,11 @@ private <T extends TaskEditControlFragment> T getFragment(int tag) {
return (T) getFragmentManager().findFragmentByTag(getString(tag));
}

public void onBackPressed() {
if(getTitle().length() == 0) {
discardButtonClick();
} else {
save();
}
callback.taskEditFinished();
}

/**
* Helper to remove task edit specific info from activity intent
*/
public static void removeExtrasFromIntent(Intent intent) {
if (intent != null) {
intent.removeExtra(TaskListActivity.OPEN_TASK);
intent.removeExtra(TOKEN_PICTURE_IN_PROGRESS);
}
}
Expand All @@ -407,13 +386,34 @@ public static void removeExtrasFromIntent(Intent intent) {
* ======================================================================
*/

protected void discardButtonClick() {
public void discardButtonClick() {
boolean hasChanges = false;
for (TaskEditControlFragment fragment : getFragments()) {
hasChanges |= fragment.hasChanges(model);
}
if (hasChanges) {
dialogBuilder.newMessageDialog(R.string.discard_confirmation)
.setPositiveButton(R.string.keep_editing, null)
.setNegativeButton(R.string.discard, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
discard();
}
})
.show();
} else {
discard();
}
}

public void discard() {
if (isNewTask) {
TimerPlugin.stopTimer(notificationManager, taskService, getActivity(), model);
taskDeleter.delete(model);
}

removeExtrasFromIntent(getActivity().getIntent());
callback.taskEditFinished();
}

protected void deleteButtonClick() {
Expand All @@ -440,13 +440,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
hideKeyboard();

switch (item.getItemId()) {
case R.id.menu_save:
case R.id.menu_save:
save();
callback.taskEditFinished();
return true;
case R.id.menu_discard:
discardButtonClick();
callback.taskEditFinished();
return true;
case R.id.menu_record_note:
startRecordingAudio();
Expand All @@ -455,12 +450,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
deleteButtonClick();
return true;
case android.R.id.home:
if (getTitle().trim().length() == 0) {
discardButtonClick();
} else {
save();
}
callback.taskEditFinished();
save();
return true;
}

Expand Down Expand Up @@ -504,7 +494,6 @@ public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putParcelable(EXTRA_TASK, model);
outState.putBoolean(EXTRA_APPLY_MODEL, applyModel);
outState.putBoolean(EXTRA_IS_NEW_TASK, isNewTask);
}

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/com/todoroo/astrid/activity/TaskListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void onClick(View v) {
hideKeyboard();
navigationDrawer.openDrawer();
} else {
taskEditFragment.onBackPressed();
taskEditFragment.save();
}
}
});
Expand Down Expand Up @@ -247,7 +247,7 @@ private void loadTaskListFragment(boolean onCreate, TaskListFragment taskListFra

private void loadTaskEditFragment(boolean onCreate, TaskEditFragment taskEditFragment, List<TaskEditControlFragment> taskEditControlFragments) {
if (isSinglePaneLayout()) {
updateToolbar(R.drawable.ic_arrow_back_24dp, false);
updateToolbar(R.drawable.ic_save_24dp, false);
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
Expand Down Expand Up @@ -367,7 +367,7 @@ public void onFilterItemClicked(FilterListItem item) {
TaskEditFragment.removeExtrasFromIntent(getIntent());
TaskEditFragment tef = getTaskEditFragment();
if (tef != null) {
getTaskEditFragment().onBackPressed();
getTaskEditFragment().save();
}

// If showing both fragments, directly update the tasklist-fragment
Expand Down Expand Up @@ -446,7 +446,7 @@ public void onTaskListItemClicked(long taskId) {
TaskEditFragment taskEditFragment = getTaskEditFragment();

if (taskEditFragment != null) {
taskEditFragment.onBackPressed();
taskEditFragment.save();
}

Task task = loadItem(taskId);
Expand Down Expand Up @@ -519,7 +519,7 @@ public void onBackPressed() {
}

if (getTaskEditFragment() != null) {
getTaskEditFragment().onBackPressed();
getTaskEditFragment().discardButtonClick();
return;
}

Expand Down Expand Up @@ -744,11 +744,6 @@ private Task loadItem(long taskId) {
model = taskService.createWithValues(values, null);
}

if (model.getTitle().length() == 0) {
// set deletion date until task gets a title
model.setDeletionDate(DateUtilities.now());
}

return model;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ protected void onTaskDelete(Task task) {
TaskEditFragment tef = activity.getTaskEditFragment();
if (tef != null) {
if (task.getId() == tef.model.getId()) {
tef.discardButtonClick();
tef.discard();
}
}
TimerPlugin.stopTimer(notificationManager, taskService, context, task);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,25 @@ public void initialize(boolean isNewTask, Task task) {
repeatUntilValue = task.getRepeatUntil();
}

@Override
public boolean hasChanges(Task original) {
return !getRecurrenceValue().equals(original.getRecurrence()) || repeatUntilValue != original.getRepeatUntil();
}

@Override
public void apply(Task task) {
task.setRecurrence(getRecurrenceValue());
task.setRepeatUntil(repeatUntilValue);
}

private String getRecurrenceValue() {
String result = getRecurrence();

if (type.getSelectedItemPosition() == TYPE_COMPLETION_DATE && !TextUtils.isEmpty(result)) {
result += ";FROM=COMPLETION"; //$NON-NLS-1$
}

task.setRecurrence(result);
task.setRepeatUntil(repeatUntilValue);
return result;
}

private void applyRecurrence() {
Expand Down
50 changes: 32 additions & 18 deletions src/main/java/com/todoroo/astrid/tags/TagsControlSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -322,6 +321,11 @@ public int controlId() {
return TAG;
}

@Override
public boolean hasChanges(Task original) {
return !getExistingTags(original.getUUID()).equals(getSelectedTags(false));
}

protected void refreshDisplayView() {
String tagString = buildTagString();
if (!TextUtils.isEmpty(tagString)) {
Expand All @@ -333,34 +337,44 @@ protected void refreshDisplayView() {
}
}

/**
* Save the given array of tags into the database
*/
private boolean synchronizeTags(long taskId, String taskUuid) {
Query query = Query.select(Metadata.PROPERTIES).where(
Criterion.and(
TaskToTagMetadata.TASK_UUID.eq(taskUuid),
Metadata.DELETION_DATE.eq(0))
);
Set<TagData> existingTags = newHashSet(transform(metadataDao.toList(query), new Function<Metadata, TagData>() {
@Override
public TagData apply(Metadata metadata) {
return tagDataDao.getByUuid(metadata.getValue(TaskToTagMetadata.TAG_UUID));
}
}));
Set<TagData> selectedTags = newHashSet(transform(getTagList(), new Function<String, TagData>() {
private Set<TagData> getSelectedTags(final boolean createMissingTags) {
return newHashSet(transform(getTagList(), new Function<String, TagData>() {
@Override
public TagData apply(String tagName) {
TagData tagData = tagDataDao.getTagByName(tagName, TagData.PROPERTIES);
if (tagData == null) {
// create missing tags
tagData = new TagData();
tagData.setName(tagName);
tagDataDao.persist(tagData);
if (createMissingTags) {
tagDataDao.persist(tagData);
}
}
return tagData;
}
}));
}

private Set<TagData> getExistingTags(String taskUuid) {
Query query = Query.select(Metadata.PROPERTIES).where(
Criterion.and(
TaskToTagMetadata.TASK_UUID.eq(taskUuid),
Metadata.DELETION_DATE.eq(0))
);
return newHashSet(transform(metadataDao.toList(query), new Function<Metadata, TagData>() {
@Override
public TagData apply(Metadata metadata) {
return tagDataDao.getByUuid(metadata.getValue(TaskToTagMetadata.TAG_UUID));
}
}));
}

/**
* Save the given array of tags into the database
*/
private boolean synchronizeTags(long taskId, String taskUuid) {
Set<TagData> existingTags = getExistingTags(taskUuid);
Set<TagData> selectedTags = getSelectedTags(true);
Sets.SetView<TagData> added = difference(selectedTags, existingTags);
Sets.SetView<TagData> removed = difference(existingTags, selectedTags);
deleteLinks(taskId, taskUuid, removed);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/todoroo/astrid/timers/TimerControlSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ public void initialize(boolean isNewTask, Task task) {
estimatedSeconds = task.getEstimatedSeconds();
}

@Override
public boolean hasChanges(Task original) {
return elapsed.getTimeDurationInSeconds() != original.getElapsedSeconds() ||
estimated.getTimeDurationInSeconds() != original.getEstimatedSeconds();
}

@Override
public void apply(Task task) {
task.setElapsedSeconds(elapsed.getTimeDurationInSeconds());
Expand Down
Loading

0 comments on commit ca3dd86

Please sign in to comment.