/
QuickAddDialogFragment.java
497 lines (404 loc) · 15 KB
/
QuickAddDialogFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Copyright 2017 dmfs GmbH
*
* 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;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import org.dmfs.android.retentionmagic.SupportDialogFragment;
import org.dmfs.android.retentionmagic.annotations.Parameter;
import org.dmfs.android.retentionmagic.annotations.Retain;
import org.dmfs.provider.tasks.AuthorityUtil;
import org.dmfs.tasks.contract.TaskContract;
import org.dmfs.tasks.contract.TaskContract.TaskLists;
import org.dmfs.tasks.contract.TaskContract.Tasks;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.utils.RecentlyUsedLists;
import org.dmfs.tasks.utils.SafeFragmentUiRunnable;
import org.dmfs.tasks.utils.TasksListCursorSpinnerAdapter;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
/**
* A quick add dialog. It allows the user to enter a new task without having to deal with the full blown editor interface. At present it support task with a
* title only, but there is an option to fire up the full editor.
*
* @author Marten Gajda <marten@dmfs.org>
*/
public class QuickAddDialogFragment extends SupportDialogFragment
implements OnEditorActionListener, LoaderManager.LoaderCallbacks<Cursor>, OnItemSelectedListener, OnClickListener, TextWatcher
{
/**
* The minimal duration for the "Task completed" info to be visible
*/
private final static int COMPLETION_DELAY_BASE = 500; // ms
/**
* The maximum time to add for the first time the "Task completed" info is shown.
*/
private final static int COMPLETION_DELAY_MAX = 1500; // ms
private final static String ARG_LIST_ID = "list_id";
private final static String ARG_CONTENT = "content";
public static final String LIST_LOADER_URI = "uri";
public static final String LIST_LOADER_FILTER = "filter";
public static final String LIST_LOADER_VISIBLE_LISTS_FILTER = TaskLists.SYNC_ENABLED + "=1";
/**
* Projection into the task list.
*/
private final static String[] TASK_LIST_PROJECTION = new String[] {
TaskContract.TaskListColumns._ID, TaskContract.TaskListColumns.LIST_NAME,
TaskContract.TaskListSyncColumns.ACCOUNT_TYPE, TaskContract.TaskListSyncColumns.ACCOUNT_NAME, TaskContract.TaskListColumns.LIST_COLOR };
/**
* This interface provides a convenient way to get column indices of {@link #TASK_LIST_PROJECTION} without any overhead.
*/
private interface TASK_LIST_PROJECTION_VALUES
{
int id = 0;
@SuppressWarnings("unused")
int list_name = 1;
@SuppressWarnings("unused")
int account_type = 2;
@SuppressWarnings("unused")
int account_name = 3;
@SuppressWarnings("unused")
int list_color = 4;
}
public interface OnTextInputListener
{
void onTextInput(String inputText);
}
@Parameter(key = ARG_LIST_ID)
private long mListId = -1;
@Parameter(key = ARG_CONTENT)
private ContentSet mInitialContent;
@Retain(permanent = true, key = "quick_add_list_id", classNS = "")
private long mSelectedListId = -1;
@Retain
private int mLastColor = Color.WHITE;
@Retain(permanent = true, key = "quick_add_save_count", classNS = "")
private int mSaveCounter = 0;
private boolean mClosing;
private View mColorBackground;
private Spinner mListSpinner;
private EditText mEditText;
private View mConfirmation;
private View mContent;
private View mSaveButton;
private View mSaveAndNextButton;
private TasksListCursorSpinnerAdapter mTaskListAdapter;
private String mAuthority;
/**
* Create a {@link QuickAddDialogFragment} with the given title and initial text value.
*
* @return A new {@link QuickAddDialogFragment}.
*/
public static QuickAddDialogFragment newInstance(long listId)
{
QuickAddDialogFragment fragment = new QuickAddDialogFragment();
Bundle args = new Bundle();
args.putLong(ARG_LIST_ID, listId);
fragment.setArguments(args);
return fragment;
}
/**
* Create a {@link QuickAddDialogFragment} with the given title and initial text value.
*
* @return A new {@link QuickAddDialogFragment}.
*/
public static QuickAddDialogFragment newInstance(ContentSet content)
{
QuickAddDialogFragment fragment = new QuickAddDialogFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_CONTENT, content);
args.putLong(ARG_LIST_ID, -1);
fragment.setArguments(args);
return fragment;
}
public QuickAddDialogFragment()
{
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = super.onCreateDialog(savedInstanceState);
// hide the actual dialog title, we have our own...
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
final Context contextThemeWrapperDark = new ContextThemeWrapper(getActivity(), R.style.Base_Theme_AppCompat);
View view = inflater.inflate(R.layout.fragment_quick_add_dialog, container);
ViewGroup headerContainer = (ViewGroup) view.findViewById(R.id.header_container);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapperDark);
localInflater.inflate(R.layout.fragment_quick_add_dialog_header, headerContainer);
if (savedInstanceState == null)
{
if (mListId >= 0)
{
mSelectedListId = mListId;
}
}
mColorBackground = view.findViewById(R.id.color_background);
mColorBackground.setBackgroundColor(mLastColor);
mListSpinner = (Spinner) view.findViewById(R.id.task_list_spinner);
mTaskListAdapter = new TasksListCursorSpinnerAdapter(getActivity(), R.layout.list_spinner_item_selected_quick_add, R.layout.list_spinner_item_dropdown);
mListSpinner.setAdapter(mTaskListAdapter);
mListSpinner.setOnItemSelectedListener(this);
mEditText = (EditText) view.findViewById(android.R.id.input);
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setOnEditorActionListener(this);
mEditText.addTextChangedListener(this);
mConfirmation = view.findViewById(R.id.created_confirmation);
mContent = view.findViewById(R.id.content);
mSaveButton = view.findViewById(android.R.id.button1);
mSaveButton.setOnClickListener(this);
mSaveAndNextButton = view.findViewById(android.R.id.button2);
mSaveAndNextButton.setOnClickListener(this);
view.findViewById(android.R.id.edit).setOnClickListener(this);
mAuthority = AuthorityUtil.taskAuthority(getActivity());
afterTextChanged(mEditText.getEditableText());
setListUri(TaskLists.getContentUri(mAuthority), LIST_LOADER_VISIBLE_LISTS_FILTER);
return view;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bundle)
{
return new CursorLoader(getActivity(), bundle.getParcelable(LIST_LOADER_URI), TASK_LIST_PROJECTION, bundle.getString(LIST_LOADER_FILTER), null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
mTaskListAdapter.changeCursor(cursor);
if (cursor != null)
{
if (mSelectedListId == -1)
{
mSelectedListId = mListId;
}
// set the list that was used the last time the user created an event
// iterate over all lists and select the one that matches the given id
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
long listId = cursor.getLong(TASK_LIST_PROJECTION_VALUES.id);
if (listId == mSelectedListId)
{
mListSpinner.setSelection(cursor.getPosition());
break;
}
cursor.moveToNext();
}
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader)
{
mTaskListAdapter.changeCursor(null);
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (EditorInfo.IME_ACTION_DONE == actionId)
{
notifyUser(true /* close afterwards */);
createTask();
return true;
}
return false;
}
private void setListUri(Uri uri, String filter)
{
if (this.isAdded())
{
Bundle bundle = new Bundle();
bundle.putParcelable(LIST_LOADER_URI, uri);
bundle.putString(LIST_LOADER_FILTER, filter);
getLoaderManager().restartLoader(-2, bundle, this);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Cursor c = (Cursor) parent.getItemAtPosition(position);
mLastColor = TaskFieldAdapters.LIST_COLOR.get(c);
mColorBackground.setBackgroundColor(mLastColor);
mSelectedListId = id;
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
/**
* Launch the task editor activity.
*/
private void editTask()
{
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(Tasks.getContentUri(mAuthority));
Bundle extraBundle = new Bundle();
extraBundle.putParcelable(EditTaskActivity.EXTRA_DATA_CONTENT_SET, buildContentSet());
intent.putExtra(EditTaskActivity.EXTRA_DATA_BUNDLE, extraBundle);
getActivity().startActivity(intent);
}
/**
* Store the task.
*/
private void createTask()
{
ContentSet content = buildContentSet();
RecentlyUsedLists.use(getContext(), content.getAsLong(Tasks.LIST_ID)); // update recently used lists
content.persist(getActivity());
}
private ContentSet buildContentSet()
{
ContentSet task;
if (mInitialContent != null)
{
task = new ContentSet(mInitialContent);
}
else
{
// add a new task on the tasks table
task = new ContentSet(Tasks.getContentUri(mAuthority));
}
task.put(Tasks.LIST_ID, mListSpinner.getSelectedItemId());
TaskFieldAdapters.TITLE.set(task, mEditText.getText().toString());
return task;
}
@Override
public void onClick(View v)
{
int id = v.getId();
mSaveButton.setEnabled(false);
mSaveAndNextButton.setEnabled(false);
if (id == android.R.id.button1)
{
// "save" pressed
notifyUser(true /* close afterwards */);
createTask();
}
else if (id == android.R.id.button2)
{
// "save and continue" pressed
notifyUser(false /* reset view */);
createTask();
}
else if (id == android.R.id.edit)
{
// "edit" pressed
editTask();
dismiss();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void afterTextChanged(Editable s)
{
// disable buttons if there is no title
boolean enabled = s == null || s.length() != 0;
mSaveButton.setEnabled(enabled);
mSaveAndNextButton.setEnabled(enabled);
}
private void notifyUser(boolean close)
{
mContent.animate().alpha(0).setDuration(250).start();
mConfirmation.setAlpha(0);
mConfirmation.setVisibility(View.VISIBLE);
mConfirmation.animate().alpha(1).setDuration(250).start();
if (close)
{
delayedDismiss();
}
else
{
// We use a dynamic duration. When you hit "save & continue" for the very first time we use a rather long delay, that gets closer to
// COMPLETION_DELAY_BASE with every time you do that.
int duration = COMPLETION_DELAY_BASE + COMPLETION_DELAY_MAX / ++mSaveCounter;
mContent.postDelayed(mReset, duration);
}
}
private void delayedDismiss()
{
mContent.postDelayed(mDismiss, 1000);
mClosing = true;
}
@Override
public void onPause()
{
super.onPause();
if (mClosing)
{
mContent.removeCallbacks(mDismiss);
dismiss();
}
}
/**
* A {@link Runnable} that closes the dialog.
*/
private final Runnable mDismiss = new SafeFragmentUiRunnable(this, this::dismiss);
/**
* A {@link Runnable} that resets the editor view.
*/
private final Runnable mReset = new SafeFragmentUiRunnable(this, new Runnable()
{
@Override
public void run()
{
mContent.animate().alpha(1).setDuration(250).start();
mConfirmation.animate().alpha(0).setDuration(250).start();
mSaveButton.setEnabled(true);
mSaveAndNextButton.setEnabled(true);
// reset view
mEditText.selectAll();
// bring the keyboard up again
mEditText.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(mEditText, 0);
}
});
}