Skip to content

Commit

Permalink
Open task after saved with editor. #254
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Keszthelyi committed Jun 27, 2017
1 parent c53c2ec commit f1165a7
Showing 1 changed file with 76 additions and 94 deletions.
170 changes: 76 additions & 94 deletions opentasks/src/main/java/org/dmfs/tasks/EditTaskFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,98 +712,80 @@ public void onNothingSelected(AdapterView<?> arg0)
/**
* Persist the current task (if anything has been edited) and close the editor.
*/
public void saveAndExit()
{
// TODO: put that in a background task
Activity activity = getActivity();

int resultCode = Activity.RESULT_CANCELED;
Intent result = null;
int toastId = -1;

if (mEditor != null)
{
mEditor.updateValues();
}

if (mValues.isInsert() || mValues.isUpdate())
{
if (TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues)))
{
// there is no title, try to set one from the description or check list

String description = TaskFieldAdapters.DESCRIPTION.get(mValues);
if (description != null)
{
// remove spaces and empty lines
description = description.trim();
}

if (!TextUtils.isEmpty(description))
{
// we have a description, use it to make up a title
int eol = description.indexOf('\n');
TaskFieldAdapters.TITLE.set(mValues, description.substring(0, eol > 0 ? eol : Math.min(description.length(), 100)));
}
else
{
// no description, try to find a non-empty checklist item
List<CheckListItem> checklist = TaskFieldAdapters.CHECKLIST.get(mValues);
if (checklist != null && checklist.size() > 0)
{
for (CheckListItem item : checklist)
{
String trimmedItem = item.text.trim();
if (!TextUtils.isEmpty(trimmedItem))
{
TaskFieldAdapters.TITLE.set(mValues, trimmedItem);
break;
}
}
}
}
}

if (!TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues)) || mValues.isUpdate())
{

if (mValues.updatesAnyKey(RECURRENCE_VALUES))
{
mValues.ensureUpdates(RECURRENCE_VALUES);
}

mTaskUri = mValues.persist(activity);

// return proper result
result = new Intent();
result.setData(mTaskUri);
resultCode = Activity.RESULT_OK;
toastId = R.string.activity_edit_task_task_saved;
}
else
{
toastId = R.string.activity_edit_task_empty_task_not_saved;
}
}
else
{
Log.i(TAG, "nothing to save");
}

if (toastId != -1)
{
Toast.makeText(activity, toastId, Toast.LENGTH_SHORT).show();
}

if (result != null)
{
activity.setResult(resultCode, result);
}
else
{
activity.setResult(resultCode);
}

activity.finish();
}
public void saveAndExit()
{
// TODO: put that in a background task
Activity activity = getActivity();

if (mEditor != null)
{
mEditor.updateValues();
}

if (mValues.isInsert() || mValues.isUpdate())
{
if (TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues)))
{
// there is no title, try to set one from the description or check list

String description = TaskFieldAdapters.DESCRIPTION.get(mValues);
if (description != null)
{
// remove spaces and empty lines
description = description.trim();
}

if (!TextUtils.isEmpty(description))
{
// we have a description, use it to make up a title
int eol = description.indexOf('\n');
TaskFieldAdapters.TITLE.set(mValues, description.substring(0, eol > 0 ? eol : Math.min(description.length(), 100)));
}
else
{
// no description, try to find a non-empty checklist item
List<CheckListItem> checklist = TaskFieldAdapters.CHECKLIST.get(mValues);
if (checklist != null && checklist.size() > 0)
{
for (CheckListItem item : checklist)
{
String trimmedItem = item.text.trim();
if (!TextUtils.isEmpty(trimmedItem))
{
TaskFieldAdapters.TITLE.set(mValues, trimmedItem);
break;
}
}
}
}
}

if (!TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues)) || mValues.isUpdate())
{

if (mValues.updatesAnyKey(RECURRENCE_VALUES))
{
mValues.ensureUpdates(RECURRENCE_VALUES);
}

mTaskUri = mValues.persist(activity);

activity.setResult(Activity.RESULT_OK, new Intent().setData(mTaskUri));
Toast.makeText(activity, R.string.activity_edit_task_task_saved, Toast.LENGTH_SHORT).show();
activity.finish();
activity.startActivity(new Intent("android.intent.action.VIEW", mTaskUri));
}
else
{
activity.setResult(Activity.RESULT_CANCELED);
Toast.makeText(activity, R.string.activity_edit_task_empty_task_not_saved, Toast.LENGTH_SHORT).show();
activity.finish();
}
}
else
{
Log.i(TAG, "nothing to save");
}

}
}

0 comments on commit f1165a7

Please sign in to comment.