From 2fe2b3d77f8846d81f54271bd653ea95c79ada30 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Mon, 13 Feb 2012 22:37:39 -0800 Subject: [PATCH] Refactor most task edit control sets to lazy load --- .../astrid/actfm/EditPeopleControlSet.java | 16 + .../astrid/alarms/AlarmControlSet.java | 34 +- .../todoroo/astrid/gcal/GCalControlSet.java | 51 +-- .../astrid/opencrx/OpencrxControlSet.java | 26 +- .../producteev/ProducteevControlSet.java | 184 ++++++----- .../astrid/repeats/RepeatControlSet.java | 203 ++++++------ .../todoroo/astrid/tags/TagsControlSet.java | 72 +++-- .../taskrabbit/TaskRabbitControlSet.java | 16 + .../TaskRabbitDeadlineControlSet.java | 16 + .../TaskRabbitLocationControlSet.java | 16 + .../taskrabbit/TaskRabbitNameControlSet.java | 16 + .../TaskRabbitSpinnerControlSet.java | 16 + .../astrid/timers/TimerActionControlSet.java | 27 +- .../astrid/timers/TimerControlSet.java | 38 ++- .../astrid/helper/TaskEditControlSet.java | 43 ++- .../todoroo/astrid/ui/DeadlineControlSet.java | 36 ++- .../astrid/ui/EditNotesControlSet.java | 54 ++-- .../todoroo/astrid/ui/EditTextControlSet.java | 22 +- .../astrid/ui/HideUntilControlSet.java | 43 +-- .../astrid/ui/ImportanceControlSet.java | 100 +++--- .../todoroo/astrid/ui/PopupControlSet.java | 82 ++--- .../astrid/ui/RandomReminderControlSet.java | 13 +- .../todoroo/astrid/ui/ReminderControlSet.java | 88 +++--- .../todoroo/astrid/ui/UrgencyControlSet.java | 297 ------------------ 24 files changed, 725 insertions(+), 784 deletions(-) delete mode 100644 astrid/src/com/todoroo/astrid/ui/UrgencyControlSet.java diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java index b1da68fab..493c72277 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java @@ -520,12 +520,28 @@ public class EditPeopleControlSet extends PopupControlSet { // --- events + @Override + protected void readFromTaskPrivate() { + // Nothing, we don't lazy load this control set yet + } + @Override public String writeToModel(Task model) { // do nothing, we use a separate method return null; } + @Override + protected String writeToModelPrivate(Task task) { + // Nothing, we don't lazy load this control set yet + return null; + } + + @Override + protected void afterInflate() { + // Nothing, we don't lazy load this control set yet + } + /** * Save sharing settings * @param toast toast to show after saving is finished diff --git a/astrid/plugin-src/com/todoroo/astrid/alarms/AlarmControlSet.java b/astrid/plugin-src/com/todoroo/astrid/alarms/AlarmControlSet.java index 448d202d7..dcc052c6f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/alarms/AlarmControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/alarms/AlarmControlSet.java @@ -30,29 +30,18 @@ public final class AlarmControlSet extends TaskEditControlSet { // --- instance variables - private final LinearLayout alertsContainer; - private final Activity activity; - private final DateAndTimeDialog pickerDialog; + private LinearLayout alertsContainer; + private DateAndTimeDialog pickerDialog; public AlarmControlSet(Activity activity, int layout) { //View v = LayoutInflater.from(activity).inflate(R.layout.alarm_control, parent, true); super(activity, layout); - this.activity = activity; - this.alertsContainer = (LinearLayout) getView().findViewById(R.id.alert_container); - getView().findViewById(R.id.alarms_add).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View arg0) { - addAlarm(new Date()); - } - }); - - pickerDialog = new DateAndTimeDialog(activity, 0); } @Override - public void readFromTask(Task task) { + protected void readFromTaskPrivate() { alertsContainer.removeAllViews(); - TodorooCursor cursor = AlarmService.getInstance().getAlarms(task.getId()); + TodorooCursor cursor = AlarmService.getInstance().getAlarms(model.getId()); try { for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) addAlarm(new Date(cursor.get(AlarmFields.TIME))); @@ -62,7 +51,20 @@ public final class AlarmControlSet extends TaskEditControlSet { } @Override - public String writeToModel(Task task) { + protected void afterInflate() { + this.alertsContainer = (LinearLayout) getView().findViewById(R.id.alert_container); + getView().findViewById(R.id.alarms_add).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View arg0) { + addAlarm(new Date()); + } + }); + + pickerDialog = new DateAndTimeDialog(activity, 0); + } + + @Override + protected String writeToModelPrivate(Task task) { LinkedHashSet alarms = new LinkedHashSet(); for(int i = 0; i < alertsContainer.getChildCount(); i++) { Long dateValue = (Long) alertsContainer.getChildAt(i).getTag(); diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java b/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java index b4a2c6915..39fcab4c7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java @@ -13,7 +13,6 @@ import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; -import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; @@ -46,26 +45,25 @@ public class GCalControlSet extends PopupControlSet { @Autowired private ExceptionService exceptionService; - private final Activity activity; - private Uri calendarUri = null; - private Task myTask; private final CalendarResult calendars; private boolean hasEvent = false; - private final Spinner calendarSelector; + private Spinner calendarSelector; + private final int title; public GCalControlSet(final Activity activity, int viewLayout, int displayViewLayout, int title) { super(activity, viewLayout, displayViewLayout, title); DependencyInjectionService.getInstance().inject(this); - ViewGroup parent = (ViewGroup) getView().getParent(); - parent.removeView(getView()); + this.title = title; + calendars = Calendars.getCalendars(); + } + + @Override + protected void afterInflate() { ((LinearLayout) getDisplayView()).addView(getView()); //hack for spinner - this.activity = activity; this.calendarSelector = (Spinner) getView().findViewById(R.id.calendars); - - calendars = Calendars.getCalendars(); ArrayList items = new ArrayList(); Collections.addAll(items, calendars.calendars); items.add(0, activity.getString(R.string.gcal_TEA_nocal)); @@ -92,9 +90,8 @@ public class GCalControlSet extends PopupControlSet { } @Override - public void readFromTask(Task task) { - this.myTask = task; - String uri = GCalHelper.getTaskEventUri(task); + protected void readFromTaskPrivate() { + String uri = GCalHelper.getTaskEventUri(model); if(!TextUtils.isEmpty(uri)) { try { calendarUri = Uri.parse(uri); @@ -112,7 +109,7 @@ public class GCalControlSet extends PopupControlSet { hasEvent = true; } catch (Exception e) { exceptionService.reportError("unable-to-parse-calendar: " + //$NON-NLS-1$ - task.getValue(Task.CALENDAR_URI), e); + model.getValue(Task.CALENDAR_URI), e); } } refreshDisplayView(); @@ -120,7 +117,7 @@ public class GCalControlSet extends PopupControlSet { @SuppressWarnings("nls") @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { boolean gcalCreateEventEnabled = Preferences.getStringValue(R.string.gcal_p_default) != null && !Preferences.getStringValue(R.string.gcal_p_default).equals("-1"); if ((gcalCreateEventEnabled || calendarSelector.getSelectedItemPosition() != 0) && @@ -190,7 +187,7 @@ public class GCalControlSet extends PopupControlSet { if(cursor == null || cursor.getCount() == 0) { // event no longer exists, recreate it calendarUri = null; - writeToModel(myTask); + writeToModel(model); return; } cursor.moveToFirst(); @@ -210,12 +207,22 @@ public class GCalControlSet extends PopupControlSet { @Override protected void refreshDisplayView() { TextView calendar = (TextView) getDisplayView().findViewById(R.id.calendar_display_which); - if (hasEvent) { - calendar.setText(R.string.gcal_TEA_has_event); - } else if (calendarSelector.getSelectedItemPosition() != 0) { - calendar.setText((String)calendarSelector.getSelectedItem()); + if (initialized) { + if (hasEvent) { + calendar.setText(R.string.gcal_TEA_has_event); + } else if (calendarSelector.getSelectedItemPosition() != 0) { + calendar.setText((String)calendarSelector.getSelectedItem()); + } else { + calendar.setText(R.string.gcal_TEA_none_selected); + } } else { - calendar.setText(R.string.gcal_TEA_none_selected); + if (!TextUtils.isEmpty(model.getValue(Task.CALENDAR_URI))) { + calendar.setText(R.string.gcal_TEA_has_event); + } else if (calendars.defaultIndex + 1 != 0) { + calendar.setText(calendars.calendars[calendars.defaultIndex + 1]); + } else { + calendar.setText(R.string.gcal_TEA_none_selected); + } } } @@ -224,6 +231,8 @@ public class GCalControlSet extends PopupControlSet { return new OnClickListener() { @Override public void onClick(View v) { + if (calendarSelector == null) + getView(); // Force load if (!hasEvent) { calendarSelector.performClick(); } else { diff --git a/astrid/plugin-src/com/todoroo/astrid/opencrx/OpencrxControlSet.java b/astrid/plugin-src/com/todoroo/astrid/opencrx/OpencrxControlSet.java index 0d508bad9..2a8211e47 100644 --- a/astrid/plugin-src/com/todoroo/astrid/opencrx/OpencrxControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/opencrx/OpencrxControlSet.java @@ -183,13 +183,11 @@ public class OpencrxControlSet extends PopupControlSet { // --- instance variables - private final Activity activity; + private Spinner assignedToSelector; + private Spinner creatorSelector; - private final Spinner assignedToSelector; - private final Spinner creatorSelector; - - private final AutoCompleteTextView assignedToTextInput; - private final AutoCompleteTextView creatorTextInput; + private AutoCompleteTextView assignedToTextInput; + private AutoCompleteTextView creatorTextInput; private ArrayList users = null; private ArrayList dashboards = null; @@ -204,10 +202,11 @@ public class OpencrxControlSet extends PopupControlSet { public OpencrxControlSet(final Activity activity, int viewLayout, int displayViewLayout, int title) { super(activity, viewLayout, displayViewLayout, title); DependencyInjectionService.getInstance().inject(this); + } - this.activity = activity; - - //View view = LayoutInflater.from(activity).inflate(R.layout.opencrx_control, parent, true); + @Override + protected void afterInflate() { + //View view = LayoutInflater.from(activity).inflate(R.layout.opencrx_control, parent, true); this.assignedToSelector = (Spinner) getView().findViewById(R.id.opencrx_TEA_task_assign); TextView emptyView = new TextView(activity); @@ -218,16 +217,15 @@ public class OpencrxControlSet extends PopupControlSet { this.assignedToTextInput = (AutoCompleteTextView) getView().findViewById(R.id.opencrx_TEA_contact_textinput); this.creatorTextInput = (AutoCompleteTextView) getView().findViewById(R.id.opencrx_TEA_creator_textinput); - } @Override - public void readFromTask(Task task) { + protected void readFromTaskPrivate() { - Metadata metadata = getTaskMetadata(task.getId()); + Metadata metadata = getTaskMetadata(model.getId()); if(metadata == null) - metadata = OpencrxCoreUtils.INSTANCE.newMetadata(task.getId()); + metadata = OpencrxCoreUtils.INSTANCE.newMetadata(model.getId()); // Fill the dashboard-spinner and set the current dashboard long dashboardId = OpencrxCoreUtils.INSTANCE.getDefaultCreator(); @@ -339,7 +337,7 @@ public class OpencrxControlSet extends PopupControlSet { } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { Metadata metadata = getTaskMetadata(task.getId()); try { if (metadata == null) { diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java index 88bbfb0e8..71fc54aa0 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java @@ -43,14 +43,8 @@ import com.todoroo.astrid.ui.PopupControlSet; */ public class ProducteevControlSet extends PopupControlSet { - // --- instance variables - - private final Activity activity; - - //private final View view; - private Task myTask; - private final Spinner responsibleSelector; - private final Spinner dashboardSelector; + private Spinner responsibleSelector; + private Spinner dashboardSelector; private ArrayList users = null; private ArrayList dashboards = null; @@ -63,87 +57,6 @@ public class ProducteevControlSet extends PopupControlSet { public ProducteevControlSet(final Activity activity, int layout, int displayViewLayout, int title) { super(activity, layout, displayViewLayout, title); DependencyInjectionService.getInstance().inject(this); - - this.activity = activity; - this.displayText.setText(activity.getString(R.string.producteev_TEA_control_set_display)); - - //view = LayoutInflater.from(activity).inflate(R.layout.producteev_control, parent, true); - - this.responsibleSelector = (Spinner) getView().findViewById(R.id.producteev_TEA_task_assign); - TextView emptyView = new TextView(activity); - emptyView.setText(activity.getText(R.string.producteev_no_dashboard)); - responsibleSelector.setEmptyView(emptyView); - - this.dashboardSelector = (Spinner) getView().findViewById(R.id.producteev_TEA_dashboard_assign); - this.dashboardSelector.setOnItemSelectedListener(new OnItemSelectedListener() { - - @Override - public void onItemSelected(AdapterView spinnerParent, View spinnerView, - int position, long id) { - final Spinner dashSelector = (Spinner) spinnerParent; - ProducteevDashboard dashboard = (ProducteevDashboard) dashSelector.getSelectedItem(); - if (dashboard.getId() == ProducteevUtilities.DASHBOARD_CREATE) { - // let the user create a new dashboard - final EditText editor = new EditText(ProducteevControlSet.this.activity); - OnClickListener okListener = new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Activity context = ProducteevControlSet.this.activity; - String newDashboardName = editor.getText().toString(); - if (newDashboardName == null || newDashboardName.length() == 0) { - dialog.cancel(); - } else { - // create the real dashboard, select it in the spinner and refresh responsiblespinner - ProgressDialog progressDialog = com.todoroo.andlib.utility.DialogUtilities.progressDialog(context, - context.getString(R.string.DLG_wait)); - try { - progressDialog.show(); - JSONObject newDashJSON = ProducteevSyncProvider.getInvoker().dashboardsCreate( - newDashboardName).getJSONObject("dashboard"); //$NON-NLS-1$ - StoreObject local = ProducteevDataService.getInstance().updateDashboards(newDashJSON, true); - if (local != null) { - ProducteevDashboard newDashboard = new ProducteevDashboard(local); - ArrayAdapter adapter = (ArrayAdapter) dashSelector.getAdapter(); - adapter.insert(newDashboard, adapter.getCount()-1); - dashSelector.setSelection(adapter.getCount()-2); - refreshResponsibleSpinner(newDashboard.getUsers()); - DialogUtilities.dismissDialog(context, progressDialog); - } - } catch (Exception e) { - DialogUtilities.dismissDialog(context, progressDialog); - DialogUtilities.okDialog(context, - context.getString(R.string.DLG_error, e.getMessage()), - null); - exceptionService.reportError("pdv-create-dashboard", e); //$NON-NLS-1$ - dashSelector.setSelection(0); - } - } - - } - }; - OnClickListener cancelListener = new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - dashboardSelector.setSelection(lastDashboardSelection); - } - }; - DialogUtilities.viewDialog(ProducteevControlSet.this.activity, - ProducteevControlSet.this.activity.getString(R.string.producteev_create_dashboard_name), - editor, - okListener, - cancelListener); - } else { - refreshResponsibleSpinner(dashboard.getUsers()); - lastDashboardSelection = position; - } - } - - @Override - public void onNothingSelected(AdapterView spinnerParent) { - // - } - }); } /** @@ -152,7 +65,7 @@ public class ProducteevControlSet extends PopupControlSet { * @param newUsers the new userlist to show in the responsibleSelector */ private void refreshResponsibleSpinner(ArrayList newUsers) { - Metadata metadata = ProducteevDataService.getInstance().getTaskMetadata(myTask.getId()); + Metadata metadata = ProducteevDataService.getInstance().getTaskMetadata(model.getId()); long responsibleId = -1; if(metadata != null && metadata.containsNonNullValue(ProducteevTask.RESPONSIBLE_ID)) responsibleId = metadata.getValue(ProducteevTask.RESPONSIBLE_ID); @@ -192,9 +105,8 @@ public class ProducteevControlSet extends PopupControlSet { } @Override - public void readFromTask(Task task) { - this.myTask = task; - Metadata metadata = ProducteevDataService.getInstance().getTaskMetadata(myTask.getId()); + protected void readFromTaskPrivate() { + Metadata metadata = ProducteevDataService.getInstance().getTaskMetadata(model.getId()); if(metadata == null) metadata = ProducteevTask.newMetadata(); @@ -241,7 +153,91 @@ public class ProducteevControlSet extends PopupControlSet { } @Override - public String writeToModel(Task task) { + protected void afterInflate() { + + this.displayText.setText(activity.getString(R.string.producteev_TEA_control_set_display)); + + //view = LayoutInflater.from(activity).inflate(R.layout.producteev_control, parent, true); + + this.responsibleSelector = (Spinner) getView().findViewById(R.id.producteev_TEA_task_assign); + TextView emptyView = new TextView(activity); + emptyView.setText(activity.getText(R.string.producteev_no_dashboard)); + responsibleSelector.setEmptyView(emptyView); + + this.dashboardSelector = (Spinner) getView().findViewById(R.id.producteev_TEA_dashboard_assign); + this.dashboardSelector.setOnItemSelectedListener(new OnItemSelectedListener() { + + @Override + public void onItemSelected(AdapterView spinnerParent, View spinnerView, + int position, long id) { + final Spinner dashSelector = (Spinner) spinnerParent; + ProducteevDashboard dashboard = (ProducteevDashboard) dashSelector.getSelectedItem(); + if (dashboard.getId() == ProducteevUtilities.DASHBOARD_CREATE) { + // let the user create a new dashboard + final EditText editor = new EditText(ProducteevControlSet.this.activity); + OnClickListener okListener = new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Activity context = ProducteevControlSet.this.activity; + String newDashboardName = editor.getText().toString(); + if (newDashboardName == null || newDashboardName.length() == 0) { + dialog.cancel(); + } else { + // create the real dashboard, select it in the spinner and refresh responsiblespinner + ProgressDialog progressDialog = com.todoroo.andlib.utility.DialogUtilities.progressDialog(context, + context.getString(R.string.DLG_wait)); + try { + progressDialog.show(); + JSONObject newDashJSON = ProducteevSyncProvider.getInvoker().dashboardsCreate( + newDashboardName).getJSONObject("dashboard"); //$NON-NLS-1$ + StoreObject local = ProducteevDataService.getInstance().updateDashboards(newDashJSON, true); + if (local != null) { + ProducteevDashboard newDashboard = new ProducteevDashboard(local); + ArrayAdapter adapter = (ArrayAdapter) dashSelector.getAdapter(); + adapter.insert(newDashboard, adapter.getCount()-1); + dashSelector.setSelection(adapter.getCount()-2); + refreshResponsibleSpinner(newDashboard.getUsers()); + DialogUtilities.dismissDialog(context, progressDialog); + } + } catch (Exception e) { + DialogUtilities.dismissDialog(context, progressDialog); + DialogUtilities.okDialog(context, + context.getString(R.string.DLG_error, e.getMessage()), + null); + exceptionService.reportError("pdv-create-dashboard", e); //$NON-NLS-1$ + dashSelector.setSelection(0); + } + } + + } + }; + OnClickListener cancelListener = new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + dashboardSelector.setSelection(lastDashboardSelection); + } + }; + DialogUtilities.viewDialog(ProducteevControlSet.this.activity, + ProducteevControlSet.this.activity.getString(R.string.producteev_create_dashboard_name), + editor, + okListener, + cancelListener); + } else { + refreshResponsibleSpinner(dashboard.getUsers()); + lastDashboardSelection = position; + } + } + + @Override + public void onNothingSelected(AdapterView spinnerParent) { + // + } + }); + } + + @Override + protected String writeToModelPrivate(Task task) { Metadata metadata = ProducteevDataService.getInstance().getTaskMetadata(task.getId()); try { if (metadata == null) { diff --git a/astrid/plugin-src/com/todoroo/astrid/repeats/RepeatControlSet.java b/astrid/plugin-src/com/todoroo/astrid/repeats/RepeatControlSet.java index 2a5de1442..bee076c21 100644 --- a/astrid/plugin-src/com/todoroo/astrid/repeats/RepeatControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/repeats/RepeatControlSet.java @@ -61,18 +61,18 @@ public class RepeatControlSet extends PopupControlSet { private static final int TYPE_DUE_DATE = 0; private static final int TYPE_COMPLETION_DATE = 1; - // --- instance variables - - private final Activity activity; //private final CheckBox enabled; private boolean doRepeat = true; - private final Button value; - private final Spinner interval; - private final Spinner type; - private final LinearLayout repeatContainer; - private final LinearLayout daysOfWeekContainer; + private Button value; + private Spinner interval; + private Spinner type; + private LinearLayout repeatContainer; + private LinearLayout daysOfWeekContainer; private final CompoundButton[] daysOfWeek = new CompoundButton[7]; - private Task model; + + private String recurrence; + private int repeatValue; + private int intervalValue; private final List listeners = new LinkedList(); @@ -91,74 +91,19 @@ public class RepeatControlSet extends PopupControlSet { public RepeatControlSet(Activity activity, int viewLayout, int displayViewLayout, int title) { super(activity, viewLayout, displayViewLayout, title); DependencyInjectionService.getInstance().inject(this); - - this.activity = activity; - value = (Button) getView().findViewById(R.id.repeatValue); - interval = (Spinner) getView().findViewById(R.id.repeatInterval); - type = (Spinner) getView().findViewById(R.id.repeatType); - repeatContainer = (LinearLayout) getView().findViewById(R.id.repeatContainer); - daysOfWeekContainer = (LinearLayout) getView().findViewById(R.id.repeatDayOfWeekContainer); - setRepeatValue(1); - - // set up days of week - DateFormatSymbols dfs = new DateFormatSymbols(); - Calendar calendar = Calendar.getInstance(); - calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f/14); - LinearLayout.LayoutParams textLp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f/14); - for(int i = 0; i < 7; i++) { - CheckBox checkBox = new CheckBox(activity); - int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); - checkBox.setPadding(0, 0, 0, 0); - checkBox.setLayoutParams(lp); - checkBox.setTag(Weekday.values()[dayOfWeek - 1]); - checkBox.setButtonDrawable(R.drawable.btn_check_small); - - TextView label = new TextView(activity); - label.setTextAppearance(activity, R.style.TextAppearance_GEN_EditLabel); - label.setLayoutParams(textLp); - label.setTextSize(14); - label.setText(dfs.getShortWeekdays()[dayOfWeek].substring(0, 1)); - - daysOfWeek[i] = checkBox; - calendar.add(Calendar.DATE, 1); - daysOfWeekContainer.addView(checkBox); - daysOfWeekContainer.addView(label); - } - - // set up listeners - value.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - repeatValueClick(); - } - }); - - interval.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parentView, View view, int position, long id) { - daysOfWeekContainer.setVisibility(position == INTERVAL_WEEKS ? View.VISIBLE : View.GONE); - } - - @Override - public void onNothingSelected(AdapterView arg0) { - // - } - }); - daysOfWeekContainer.setVisibility(View.GONE); } /** Set up the repeat value button */ private void setRepeatValue(int newValue) { + repeatValue = newValue; value.setText(activity.getString(R.string.repeat_every, newValue)); - value.setTag(newValue); } protected void repeatValueClick() { - final int tagValue = (Integer)value.getTag(); final Runnable openDialogRunnable = new Runnable() { public void run() { - int dialogValue = tagValue; + int dialogValue = repeatValue; if(dialogValue == 0) dialogValue = 1; @@ -186,56 +131,71 @@ public class RepeatControlSet extends PopupControlSet { listeners.remove(listener); } - - @SuppressWarnings("nls") @Override public void readFromTask(Task task) { - model = task; - - String recurrence = task.getValue(Task.RECURRENCE); + super.readFromTask(task); + recurrence = model.getValue(Task.RECURRENCE); if(recurrence == null) recurrence = ""; - Date date; - if(model.getValue(Task.DUE_DATE) == 0) - date = new Date(); - else - date = new Date(model.getValue(Task.DUE_DATE)); - int dayOfWeek = date.getDay(); - for(int i = 0; i < 7; i++) - daysOfWeek[i].setChecked(i == dayOfWeek); - - // read recurrence rule if(recurrence.length() > 0) { try { RRule rrule = new RRule(recurrence); - - setRepeatValue(rrule.getInterval()); + repeatValue = rrule.getInterval(); switch(rrule.getFreq()) { case DAILY: - interval.setSelection(INTERVAL_DAYS); + intervalValue = INTERVAL_DAYS; break; case WEEKLY: { - interval.setSelection(INTERVAL_WEEKS); + intervalValue = INTERVAL_WEEKS; break; } case MONTHLY: - interval.setSelection(INTERVAL_MONTHS); + intervalValue = INTERVAL_MONTHS; break; case HOURLY: - interval.setSelection(INTERVAL_HOURS); + intervalValue = INTERVAL_HOURS; break; case MINUTELY: - interval.setSelection(INTERVAL_MINUTES); + intervalValue = INTERVAL_MINUTES; break; case YEARLY: - interval.setSelection(INTERVAL_YEARS); + intervalValue = INTERVAL_YEARS; break; default: // an unhandled recurrence exceptionService.reportError("repeat-unhandled-rule", //$NON-NLS-1$ new Exception("Unhandled rrule frequency: " + recurrence)); } + } catch (Exception e) { + // invalid RRULE + recurrence = ""; //$NON-NLS-1$ + exceptionService.reportError("repeat-parse-exception", e); + } + } + doRepeat = recurrence.length() > 0; + refreshDisplayView(); + } + + @SuppressWarnings("nls") + @Override + protected void readFromTaskPrivate() { + Date date; + if(model.getValue(Task.DUE_DATE) == 0) + date = new Date(); + else + date = new Date(model.getValue(Task.DUE_DATE)); + int dayOfWeek = date.getDay(); + for(int i = 0; i < 7; i++) + daysOfWeek[i].setChecked(i == dayOfWeek); + + // read recurrence rule + if(recurrence.length() > 0) { + try { + RRule rrule = new RRule(recurrence); + + setRepeatValue(rrule.getInterval()); + interval.setSelection(intervalValue); // clear all day of week checks, then update them for(int i = 0; i < 7; i++) @@ -258,7 +218,7 @@ public class RepeatControlSet extends PopupControlSet { doRepeat = recurrence.length() > 0; // read flag - if(task.getFlag(Task.FLAGS, Task.FLAG_REPEAT_AFTER_COMPLETION)) + if(model.getFlag(Task.FLAGS, Task.FLAG_REPEAT_AFTER_COMPLETION)) type.setSelection(TYPE_COMPLETION_DATE); else type.setSelection(TYPE_DUE_DATE); @@ -266,9 +226,64 @@ public class RepeatControlSet extends PopupControlSet { refreshDisplayView(); } + @Override + protected void afterInflate() { + value = (Button) getView().findViewById(R.id.repeatValue); + interval = (Spinner) getView().findViewById(R.id.repeatInterval); + type = (Spinner) getView().findViewById(R.id.repeatType); + repeatContainer = (LinearLayout) getView().findViewById(R.id.repeatContainer); + daysOfWeekContainer = (LinearLayout) getView().findViewById(R.id.repeatDayOfWeekContainer); + setRepeatValue(1); + + // set up days of week + DateFormatSymbols dfs = new DateFormatSymbols(); + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f/14); + LinearLayout.LayoutParams textLp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f/14); + for(int i = 0; i < 7; i++) { + CheckBox checkBox = new CheckBox(activity); + int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + checkBox.setPadding(0, 0, 0, 0); + checkBox.setLayoutParams(lp); + checkBox.setTag(Weekday.values()[dayOfWeek - 1]); + checkBox.setButtonDrawable(R.drawable.btn_check_small); + + TextView label = new TextView(activity); + label.setTextAppearance(activity, R.style.TextAppearance_GEN_EditLabel); + label.setLayoutParams(textLp); + label.setTextSize(14); + label.setText(dfs.getShortWeekdays()[dayOfWeek].substring(0, 1)); + + daysOfWeek[i] = checkBox; + calendar.add(Calendar.DATE, 1); + daysOfWeekContainer.addView(checkBox); + daysOfWeekContainer.addView(label); + } + + // set up listeners + value.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + repeatValueClick(); + } + }); + + interval.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parentView, View view, int position, long id) { + daysOfWeekContainer.setVisibility(position == INTERVAL_WEEKS ? View.VISIBLE : View.GONE); + } + + @Override + public void onNothingSelected(AdapterView arg0) { + // + } + }); + daysOfWeekContainer.setVisibility(View.GONE); + } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { String result; if(!doRepeat) result = ""; //$NON-NLS-1$ @@ -278,7 +293,7 @@ public class RepeatControlSet extends PopupControlSet { } RRule rrule = new RRule(); - rrule.setInterval((Integer)value.getTag()); + rrule.setInterval(repeatValue); switch(interval.getSelectedItemPosition()) { case INTERVAL_DAYS: rrule.setFreq(Frequency.DAILY); @@ -333,10 +348,9 @@ public class RepeatControlSet extends PopupControlSet { TextView repeatDisplay = (TextView) getDisplayView().findViewById(R.id.display_row_edit); ImageView repeatImage = (ImageView) getDisplayView().findViewById(R.id.repeat_image_icon); if (doRepeat) { - String[] dateAbbrev = activity.getResources().getStringArray( R.array.repeat_interval_short); - String date = String.format("%s %s", (Integer)value.getTag(), dateAbbrev[interval.getSelectedItemPosition()]); //$NON-NLS-1$ + String date = String.format("%s %s", repeatValue, dateAbbrev[intervalValue]); //$NON-NLS-1$ String text = String.format(activity.getString(R.string.repeat_detail_duedate), date); // Every freq int repeatDisplay.setText(text); @@ -345,7 +359,6 @@ public class RepeatControlSet extends PopupControlSet { repeatImage.setImageResource(repeatIcon.data); } else { - repeatDisplay.setText(R.string.repeat_never); repeatImage.setImageResource(R.drawable.icn_edit_repeats); } diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java b/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java index 4741dddcf..13b39466e 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java @@ -2,7 +2,6 @@ package com.todoroo.astrid.tags; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashSet; import android.app.Activity; @@ -44,32 +43,19 @@ public final class TagsControlSet extends PopupControlSet { //private final Spinner tagSpinner; //@Autowired private TagDataService tagDataService; private final TagService tagService = TagService.getInstance(); - private final ArrayList allTagNames; + private ArrayList allTagNames; - private final LinearLayout newTags; - private final ListView selectedTags; + private LinearLayout newTags; + private ListView selectedTags; private boolean populated = false; - private final HashMap tagIndices; + private HashMap tagIndices; //private final LinearLayout tagsContainer; - private final Activity activity; private final TextView tagsDisplay; public TagsControlSet(Activity activity, int viewLayout, int displayViewLayout, int title) { super(activity, viewLayout, displayViewLayout, title); DependencyInjectionService.getInstance().inject(this); - this.activity = activity; - Tag[] allTags = getTagArray(); - allTagNames = getTagNames(allTags); - tagIndices = buildTagIndices(allTagNames); - - selectedTags = (ListView) getView().findViewById(R.id.existingTags); - selectedTags.setAdapter(new ArrayAdapter(activity, - R.layout.simple_list_item_multiple_choice_themed, allTagNames)); - selectedTags.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); - - this.newTags = (LinearLayout) getView().findViewById(R.id.newTags); - tagsDisplay = (TextView) getDisplayView().findViewById(R.id.display_row_edit); this.displayText.setText(activity.getString(R.string.TEA_tags_label)); } @@ -124,17 +110,23 @@ public final class TagsControlSet extends PopupControlSet { private LinkedHashSet getTagSet() { LinkedHashSet tags = new LinkedHashSet(); - for(int i = 0; i < selectedTags.getAdapter().getCount(); i++) { - if (selectedTags.isItemChecked(i)) - tags.add(allTagNames.get(i)); - } + if (initialized) { + for(int i = 0; i < selectedTags.getAdapter().getCount(); i++) { + if (selectedTags.isItemChecked(i)) + tags.add(allTagNames.get(i)); + } - for(int i = 0; i < newTags.getChildCount(); i++) { - TextView tagName = (TextView) newTags.getChildAt(i).findViewById(R.id.text1); - if(tagName.getText().length() == 0) - continue; + for(int i = 0; i < newTags.getChildCount(); i++) { + TextView tagName = (TextView) newTags.getChildAt(i).findViewById(R.id.text1); + if(tagName.getText().length() == 0) + continue; - tags.add(tagName.getText().toString()); + tags.add(tagName.getText().toString()); + } + } else { + if (model.getTransitory("tags") != null) { + return (LinkedHashSet) model.getTransitory("tags"); + } } return tags; } @@ -228,15 +220,15 @@ public final class TagsControlSet extends PopupControlSet { } @Override - public void readFromTask(Task task) { + protected void readFromTaskPrivate() { newTags.removeAllViews(); for (int i = 0; i < selectedTags.getCount(); i++) { // clear all selected items selectedTags.setItemChecked(i, false); } - if(task.getId() != AbstractModel.NO_ID) { - TodorooCursor cursor = tagService.getTags(task.getId()); - HashSet tags = new HashSet(cursor.getCount()); + if(model.getId() != AbstractModel.NO_ID) { + TodorooCursor cursor = tagService.getTags(model.getId()); + LinkedHashSet tags = new LinkedHashSet(cursor.getCount()); try { for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { String tag = cursor.get(TagService.TAG); @@ -246,7 +238,7 @@ public final class TagsControlSet extends PopupControlSet { } finally { cursor.close(); } - task.putTransitory("tags", tags); //$NON-NLS-1$ + model.putTransitory("tags", tags); //$NON-NLS-1$ } addTag("", false); //$NON-NLS-1$ refreshDisplayView(); @@ -254,7 +246,21 @@ public final class TagsControlSet extends PopupControlSet { } @Override - public String writeToModel(Task task) { + protected void afterInflate() { + Tag[] allTags = getTagArray(); + allTagNames = getTagNames(allTags); + tagIndices = buildTagIndices(allTagNames); + + selectedTags = (ListView) getView().findViewById(R.id.existingTags); + selectedTags.setAdapter(new ArrayAdapter(activity, + R.layout.simple_list_item_multiple_choice_themed, allTagNames)); + selectedTags.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); + + this.newTags = (LinearLayout) getView().findViewById(R.id.newTags); + } + + @Override + protected String writeToModelPrivate(Task task) { // this is a case where we're asked to save but the UI was not yet populated if(!populated) return null; diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java index 47b582d4e..b8392b68c 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java @@ -125,6 +125,22 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned }; } + @Override + protected void readFromTaskPrivate() { + // Nothing, we don't lazy load this control set yet + } + + @Override + protected String writeToModelPrivate(Task task) { + // Nothing, we don't lazy load this control set yet + return null; + } + + @Override + protected void afterInflate() { + // Nothing, we don't lazy load this control set yet + } + @Override public String writeToModel(Task task) { return null; diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitDeadlineControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitDeadlineControlSet.java index c43a54b22..439101b6b 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitDeadlineControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitDeadlineControlSet.java @@ -83,6 +83,22 @@ public class TaskRabbitDeadlineControlSet extends PopupControlSet implements Tas refreshDisplayView(); } + @Override + protected void afterInflate() { + // Nothing, we don't lazy load this control set yet + } + + @Override + protected void readFromTaskPrivate() { + // Nothing, we don't lazy load this control set yet + } + + @Override + protected String writeToModelPrivate(Task task) { + // Nothing, we don't lazy load this control set yet + return null; + } + @Override public void saveToDatabase(JSONObject json, String key) throws JSONException { diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java index 36bbd0729..42518e450 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java @@ -196,6 +196,22 @@ public class TaskRabbitLocationControlSet extends TaskEditControlSet implements // } + @Override + protected void afterInflate() { + // Nothing, we don't lazy load this control set yet + } + + @Override + protected void readFromTaskPrivate() { + // Nothing, we don't lazy load this control set yet + } + + @Override + protected String writeToModelPrivate(Task task) { + // Nothing, we don't lazy load this control set yet + return null; + } + @Override public String writeToModel(Task task) { return null; diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitNameControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitNameControlSet.java index ad52f396b..f71c10304 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitNameControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitNameControlSet.java @@ -155,11 +155,27 @@ public class TaskRabbitNameControlSet extends PopupControlSet implements TaskRab // } + @Override + protected void readFromTaskPrivate() { + // Nothing, we don't lazy load this control set yet + } + @Override public String writeToModel(Task task) { return null; } + @Override + protected String writeToModelPrivate(Task task) { + // Nothing, we don't lazy load this control set yet + return null; + } + + @Override + protected void afterInflate() { + // Nothing, we don't lazy load this control set yet + } + @Override protected void onOkClick() { super.onOkClick(); diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitSpinnerControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitSpinnerControlSet.java index d5ce84dbc..a37853f95 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitSpinnerControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitSpinnerControlSet.java @@ -193,6 +193,22 @@ public class TaskRabbitSpinnerControlSet extends TaskEditControlSet implements T return; } + @Override + protected void readFromTaskPrivate() { + // Nothing, we don't lazy load this control set yet + } + + @Override + protected String writeToModelPrivate(Task task) { + // Nothing, we don't lazy load this control set yet + return null; + } + + @Override + protected void afterInflate() { + // Nothing, we don't lazy load this control set yet + } + @Override public String writeToModel(Task task) { return null; diff --git a/astrid/plugin-src/com/todoroo/astrid/timers/TimerActionControlSet.java b/astrid/plugin-src/com/todoroo/astrid/timers/TimerActionControlSet.java index e6a67cd02..9e8ed977c 100644 --- a/astrid/plugin-src/com/todoroo/astrid/timers/TimerActionControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/timers/TimerActionControlSet.java @@ -24,13 +24,10 @@ public class TimerActionControlSet extends TaskEditControlSet { private final Chronometer chronometer; private final LinearLayout timerContainer; private boolean timerActive; - private Task task; - private final Activity activity; private final List listeners = new LinkedList(); public TimerActionControlSet(Activity activity, View parent) { super(activity, -1); - this.activity = activity; timerContainer = (LinearLayout) parent.findViewById(R.id.timer_container); timerButton = (ImageView) parent.findViewById(R.id.timer_button); timerContainer.setOnClickListener(timerListener); @@ -39,19 +36,23 @@ public class TimerActionControlSet extends TaskEditControlSet { @Override @SuppressWarnings("hiding") - public void readFromTask(Task task) { - if (task.getValue(Task.TIMER_START) == 0) + protected void readFromTaskPrivate() { + if (model.getValue(Task.TIMER_START) == 0) timerActive = false; else timerActive = true; - this.task = task; updateDisplay(); } + @Override + protected void afterInflate() { + // Do nothing + } + @Override @SuppressWarnings("hiding") - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { // Nothing to do here return null; } @@ -60,15 +61,15 @@ public class TimerActionControlSet extends TaskEditControlSet { @Override public void onClick(View v) { if (timerActive) { - TimerPlugin.updateTimer(activity, task, false); + TimerPlugin.updateTimer(activity, model, false); for(TimerActionListener listener : listeners) - listener.timerStopped(task); + listener.timerStopped(model); chronometer.stop(); } else { - TimerPlugin.updateTimer(activity, task, true); + TimerPlugin.updateTimer(activity, model, true); for(TimerActionListener listener : listeners) - listener.timerStarted(task); + listener.timerStarted(model); chronometer.start(); } timerActive = !timerActive; @@ -86,10 +87,10 @@ public class TimerActionControlSet extends TaskEditControlSet { timerButton.setImageResource(drawable); - long elapsed = task.getValue(Task.ELAPSED_SECONDS) * 1000L; + long elapsed = model.getValue(Task.ELAPSED_SECONDS) * 1000L; if (timerActive) { chronometer.setVisibility(View.VISIBLE); - elapsed += DateUtilities.now() - task.getValue(Task.TIMER_START); + elapsed += DateUtilities.now() - model.getValue(Task.TIMER_START); chronometer.setBase(SystemClock.elapsedRealtime() - elapsed); if (elapsed > DateUtilities.ONE_DAY); { chronometer.setOnChronometerTickListener(new OnChronometerTickListener() { diff --git a/astrid/plugin-src/com/todoroo/astrid/timers/TimerControlSet.java b/astrid/plugin-src/com/todoroo/astrid/timers/TimerControlSet.java index b9d17e2ce..649343c9b 100644 --- a/astrid/plugin-src/com/todoroo/astrid/timers/TimerControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/timers/TimerControlSet.java @@ -25,6 +25,19 @@ public class TimerControlSet extends PopupControlSet implements TimerActionListe super(activity, viewLayout, displayViewLayout, title); this.displayText.setText(activity.getString(R.string.TEA_timer_controls)); + + } + + @Override + protected void readFromTaskPrivate() { + estimated.readFromTask(model); + estimated.getView(); // force load + elapsed.readFromTask(model); + elapsed.getView(); // force load + } + + @Override + protected void afterInflate() { estimated = new TimeDurationTaskEditControlSet(activity, getView(), Task.ESTIMATED_SECONDS, R.id.estimatedDuration, 0, R.string.DLG_hour_minutes ); @@ -34,15 +47,11 @@ public class TimerControlSet extends PopupControlSet implements TimerActionListe } @Override - public void readFromTask(Task task) { - estimated.readFromTask(task); - elapsed.readFromTask(task); - } - - @Override - public String writeToModel(Task task) { - estimated.writeToModel(task); - elapsed.writeToModel(task); + protected String writeToModelPrivate(Task task) { + if (initialized) { + estimated.writeToModel(task); + elapsed.writeToModel(task); + } return null; } @@ -66,12 +75,17 @@ public class TimerControlSet extends PopupControlSet implements TimerActionListe } @Override - public void readFromTask(Task task) { - controlSet.setTimeDuration(task.getValue(property)); + public void readFromTaskPrivate() { + controlSet.setTimeDuration(model.getValue(property)); + } + + @Override + protected void afterInflate() { + // Nothing } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { task.setValue(property, controlSet.getTimeDurationInSeconds()); return null; } diff --git a/astrid/src/com/todoroo/astrid/helper/TaskEditControlSet.java b/astrid/src/com/todoroo/astrid/helper/TaskEditControlSet.java index da25fac87..563438a4c 100644 --- a/astrid/src/com/todoroo/astrid/helper/TaskEditControlSet.java +++ b/astrid/src/com/todoroo/astrid/helper/TaskEditControlSet.java @@ -13,16 +13,27 @@ import com.todoroo.astrid.data.Task; */ public abstract class TaskEditControlSet { - private final View view; + protected final Activity activity; + private final int viewLayout; + private View view; + protected Task model; + protected boolean initialized = false; public TaskEditControlSet(Activity activity, int viewLayout) { - if (viewLayout != -1) - this.view = LayoutInflater.from(activity).inflate(viewLayout, null); - else - this.view = null; + this.activity = activity; + this.viewLayout = viewLayout; } public View getView() { + if (view == null && !initialized) { + if (viewLayout != -1) { + view = LayoutInflater.from(activity).inflate(viewLayout, null); + afterInflate(); + } + if (model != null) + readFromTaskPrivate(); + this.initialized = true; + } return view; } @@ -33,12 +44,30 @@ public abstract class TaskEditControlSet { /** * Read data from model to update the control set */ - public abstract void readFromTask(Task task); + public void readFromTask(Task task) { + this.model = task; + if (initialized) + readFromTaskPrivate(); + } + + + /** + * Called once to setup the ui with data from the task + */ + protected abstract void readFromTaskPrivate(); /** * Write data from control set to model * @return text appended to the toast */ - public abstract String writeToModel(Task task); + public String writeToModel(Task task) { + if (initialized) { + return writeToModelPrivate(task); + } + return null; + } + + protected abstract String writeToModelPrivate(Task task); + protected abstract void afterInflate(); } \ No newline at end of file diff --git a/astrid/src/com/todoroo/astrid/ui/DeadlineControlSet.java b/astrid/src/com/todoroo/astrid/ui/DeadlineControlSet.java index fafe28eae..93d24d3c1 100644 --- a/astrid/src/com/todoroo/astrid/ui/DeadlineControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/DeadlineControlSet.java @@ -16,14 +16,31 @@ import com.todoroo.astrid.data.Task; public class DeadlineControlSet extends PopupControlSet { private boolean isQuickadd = false; - private final DateAndTimePicker dateAndTimePicker; + private DateAndTimePicker dateAndTimePicker; + private final View[] extraViews; public DeadlineControlSet(Activity activity, int viewLayout, int displayViewLayout, View...extraViews) { super(activity, viewLayout, displayViewLayout, 0); + this.extraViews = extraViews; + this.displayText.setText(activity.getString(R.string.TEA_when_header_label)); + } + + @Override + protected void refreshDisplayView() { + String toDisplay; + if (initialized) + toDisplay = dateAndTimePicker.getDisplayString(activity, isQuickadd, isQuickadd); + else + toDisplay = DateAndTimePicker.getDisplayString(activity, model.getValue(Task.DUE_DATE), isQuickadd, isQuickadd); + + TextView dateDisplay = (TextView) getDisplayView().findViewById(R.id.display_row_edit); + dateDisplay.setText(toDisplay); + } + @Override + protected void afterInflate() { dateAndTimePicker = (DateAndTimePicker) getView().findViewById(R.id.date_and_time); LinearLayout extras = (LinearLayout) getView().findViewById(R.id.datetime_extras); - this.displayText.setText(activity.getString(R.string.TEA_when_header_label)); for (View v : extraViews) { LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f); extras.addView(v, lp); @@ -42,27 +59,20 @@ public class DeadlineControlSet extends PopupControlSet { } @Override - protected void refreshDisplayView() { - TextView dateDisplay = (TextView) getDisplayView().findViewById(R.id.display_row_edit); - String toDisplay = dateAndTimePicker.getDisplayString(activity, isQuickadd, isQuickadd); - dateDisplay.setText(toDisplay); - } - - @Override - public void readFromTask(Task task) { - long dueDate = task.getValue(Task.DUE_DATE); + protected void readFromTaskPrivate() { + long dueDate = model.getValue(Task.DUE_DATE); initializeWithDate(dueDate); refreshDisplayView(); } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { long dueDate = dateAndTimePicker.constructDueDate(); task.setValue(Task.DUE_DATE, dueDate); return null; } - public void initializeWithDate(long dueDate) { + private void initializeWithDate(long dueDate) { dateAndTimePicker.initializeWithDate(dueDate); } diff --git a/astrid/src/com/todoroo/astrid/ui/EditNotesControlSet.java b/astrid/src/com/todoroo/astrid/ui/EditNotesControlSet.java index 0515c81f0..77301c951 100644 --- a/astrid/src/com/todoroo/astrid/ui/EditNotesControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/EditNotesControlSet.java @@ -18,28 +18,23 @@ import com.todoroo.astrid.ui.TextViewWithMeasureListener.OnTextMeasureListener; public class EditNotesControlSet extends PopupControlSet { - protected final EditText editText; - protected final TextViewWithMeasureListener notesPreview; - private final LinearLayout notesBody; + protected EditText editText; + protected TextViewWithMeasureListener notesPreview; + private LinearLayout notesBody; public EditNotesControlSet(Activity activity, int viewLayout, int displayViewLayout) { super(activity, viewLayout, displayViewLayout, R.string.TEA_note_label); - editText = (EditText) getView().findViewById(R.id.notes); - notesPreview = (TextViewWithMeasureListener) getDisplayView().findViewById(R.id.display_row_edit); - notesBody = (LinearLayout) getDisplayView().findViewById(R.id.notes_body); - dialog.getWindow() - .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); - notesPreview.setOnTextSizeChangedListener(new OnTextMeasureListener() { - @Override - public void onTextSizeChanged() { - setupGravity(); - } - }); } @Override protected void refreshDisplayView() { - notesPreview.setText(editText.getText()); + CharSequence textToUse; + if (initialized) + textToUse = editText.getText(); + else + textToUse = model.getValue(Task.NOTES); + + notesPreview.setText(textToUse); setupGravity(); linkifyDisplayView(); } @@ -52,14 +47,35 @@ public class EditNotesControlSet extends PopupControlSet { } @Override - public void readFromTask(Task task) { - editText.setTextKeepState(task.getValue(Task.NOTES)); - notesPreview.setText(task.getValue(Task.NOTES)); + protected void afterInflate() { + editText = (EditText) getView().findViewById(R.id.notes); + notesPreview = (TextViewWithMeasureListener) getDisplayView().findViewById(R.id.display_row_edit); + notesBody = (LinearLayout) getDisplayView().findViewById(R.id.notes_body); + notesPreview.setOnTextSizeChangedListener(new OnTextMeasureListener() { + @Override + public void onTextSizeChanged() { + setupGravity(); + } + }); + } + + @Override + protected void additionalDialogSetup() { + super.additionalDialogSetup(); + dialog.getWindow() + .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE + | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); + } + + @Override + protected void readFromTaskPrivate() { + editText.setTextKeepState(model.getValue(Task.NOTES)); + notesPreview.setText(model.getValue(Task.NOTES)); linkifyDisplayView(); } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { task.setValue(Task.NOTES, editText.getText().toString()); return null; } diff --git a/astrid/src/com/todoroo/astrid/ui/EditTextControlSet.java b/astrid/src/com/todoroo/astrid/ui/EditTextControlSet.java index 8e64812a6..7e71f86b6 100644 --- a/astrid/src/com/todoroo/astrid/ui/EditTextControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/EditTextControlSet.java @@ -22,10 +22,10 @@ import com.todoroo.astrid.service.TaskService; * */ public class EditTextControlSet extends TaskEditControlSet { - private final EditText editText; + private EditText editText; private final StringProperty property; - protected Task model; protected CheckBox completeBox; + private final int editTextId; @Autowired private TaskService taskService; @@ -34,16 +34,20 @@ public class EditTextControlSet extends TaskEditControlSet { public EditTextControlSet(Activity activity, int layout, StringProperty property, int editText) { super(activity, layout); this.property = property; - this.editText = (EditText) getView().findViewById(editText); - this.completeBox = (CheckBox) getView().findViewById(R.id.completeBox); + this.editTextId = editText; DependencyInjectionService.getInstance().inject(this); } @Override - public void readFromTask(Task task) { - model = task; - editText.setTextKeepState(task.getValue(property)); - completeBox.setChecked(task.isCompleted()); + protected void afterInflate() { + this.editText = (EditText) getView().findViewById(editTextId); + this.completeBox = (CheckBox) getView().findViewById(R.id.completeBox); + } + + @Override + protected void readFromTaskPrivate() { + editText.setTextKeepState(model.getValue(property)); + completeBox.setChecked(model.isCompleted()); completeBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { @@ -58,7 +62,7 @@ public class EditTextControlSet extends TaskEditControlSet { } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { task.setValue(property, editText.getText().toString()); boolean newState = completeBox.isChecked(); if (newState != task.isCompleted()) { diff --git a/astrid/src/com/todoroo/astrid/ui/HideUntilControlSet.java b/astrid/src/com/todoroo/astrid/ui/HideUntilControlSet.java index 09ab2345a..df2f9085d 100644 --- a/astrid/src/com/todoroo/astrid/ui/HideUntilControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/HideUntilControlSet.java @@ -5,7 +5,6 @@ import java.util.Date; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; -import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; @@ -31,20 +30,17 @@ public class HideUntilControlSet extends PopupControlSet implements OnItemSelect private static final int EXISTING_TIME_UNSET = -2; //private final CheckBox enabled; - private final Spinner spinner; + private Spinner spinner; private int previousSetting = Task.HIDE_UNTIL_NONE; + private final int title; + private int selection; private long existingDate = EXISTING_TIME_UNSET; public HideUntilControlSet(Activity activity, int viewLayout, int displayViewLayout, int title) { super(activity, viewLayout, displayViewLayout, title); - this.spinner = (Spinner) getView().findViewById(R.id.hideUntil); - this.spinner.setOnItemSelectedListener(this); - this.spinner.setPromptId(title); + this.title = title; this.displayText.setText(activity.getString(R.string.TEA_hideUntil_label)); - ViewGroup parent = (ViewGroup) getView().getParent(); - parent.removeView(getView()); - ((LinearLayout) getDisplayView()).addView(getView()); // hack to make listeners work } private ArrayAdapter adapter; @@ -145,6 +141,7 @@ public class HideUntilControlSet extends PopupControlSet implements OnItemSelect } else { previousSetting = position; } + selection = spinner.getSelectedItemPosition(); refreshDisplayView(); } @@ -180,6 +177,8 @@ public class HideUntilControlSet extends PopupControlSet implements OnItemSelect return new OnClickListener() { @Override public void onClick(View v) { + if (spinner == null) // Force load + getView(); spinner.performClick(); } }; @@ -187,9 +186,16 @@ public class HideUntilControlSet extends PopupControlSet implements OnItemSelect @Override protected void refreshDisplayView() { - HideUntilValue value = adapter.getItem(spinner.getSelectedItemPosition()); TextView auxDisplay = (TextView) getDisplayView().findViewById(R.id.display_row_edit); - auxDisplay.setText(value.toString()); + auxDisplay.setText(adapter.getItem(selection).toString()); + } + + @Override + protected void afterInflate() { + this.spinner = (Spinner) getView().findViewById(R.id.hideUntil); + this.spinner.setOnItemSelectedListener(this); + this.spinner.setPromptId(title); + ((LinearLayout) getDisplayView()).addView(getView()); // hack to make listeners work } @Override @@ -205,7 +211,6 @@ public class HideUntilControlSet extends PopupControlSet implements OnItemSelect // For the hide until due case, we need the time component long dueTime = task.hasDueTime() ? task.getValue(Task.DUE_DATE)/1000L*1000L : dueDay.getTime(); - int selection = 0; if(date == 0) { selection = 0; date = 0; @@ -220,17 +225,15 @@ public class HideUntilControlSet extends PopupControlSet implements OnItemSelect date = 0; } - if (selection == 0) { - //enabled.setChecked(false); - //spinner.setVisibility(View.GONE); - } else { - //enabled.setChecked(true); - //spinner.setVisibility(View.VISIBLE); - } - HideUntilValue[] list = createHideUntilList(date); adapter = new ArrayAdapter( activity, android.R.layout.simple_spinner_item, list); + + super.readFromTask(task); + } + + @Override + protected void readFromTaskPrivate() { adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); @@ -239,7 +242,7 @@ public class HideUntilControlSet extends PopupControlSet implements OnItemSelect } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { if(adapter == null || spinner == null) return null; HideUntilValue item = adapter.getItem(spinner.getSelectedItemPosition()); diff --git a/astrid/src/com/todoroo/astrid/ui/ImportanceControlSet.java b/astrid/src/com/todoroo/astrid/ui/ImportanceControlSet.java index bce5f6f24..0ee998a56 100644 --- a/astrid/src/com/todoroo/astrid/ui/ImportanceControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/ImportanceControlSet.java @@ -24,7 +24,7 @@ import com.todoroo.astrid.producteev.ProducteevUtilities; */ public class ImportanceControlSet extends TaskEditControlSet { private final List buttons = new LinkedList(); - private final int[] colors; + private int[] colors; //private final int grayColor; private final List listeners = new LinkedList(); @@ -34,6 +34,54 @@ public class ImportanceControlSet extends TaskEditControlSet { public ImportanceControlSet(Activity activity, int layout) { super(activity, layout); + } + + public void setImportance(Integer i) { + for(CompoundButton b : buttons) { + if(b.getTag() == i) { + b.setTextSize(getTextSize()); + b.setChecked(true); + //if (i.intValue() == Task.IMPORTANCE_LEAST) + // b.setTextColor(grayColor); + b.setBackgroundResource(R.drawable.importance_background_selected); + } else { + b.setTextSize(getTextSize()); + b.setChecked(false); + b.setTextColor(colors[(Integer)b.getTag()]); + b.setBackgroundResource(0); + } + } + + for (ImportanceChangedListener l : listeners) { + l.importanceChanged(i, colors[i]); + } + } + + private int getTextSize() { + if (ProducteevUtilities.INSTANCE.isLoggedIn()) + return 14; + else + return 24; + } + + public Integer getImportance() { + for(CompoundButton b : buttons) + if(b.isChecked()) + return (Integer) b.getTag(); + return null; + } + + public void addListener(ImportanceChangedListener listener) { + listeners.add(listener); + } + + public void removeListener(ImportanceChangedListener listener) { + if (listeners.contains(listener)) + listeners.remove(listener); + } + + @Override + protected void afterInflate() { LinearLayout container = (LinearLayout) getView().findViewById(R.id.importance_container); colors = Task.getImportanceColors(activity.getResources()); @@ -96,57 +144,13 @@ public class ImportanceControlSet extends TaskEditControlSet { } } - public void setImportance(Integer i) { - for(CompoundButton b : buttons) { - if(b.getTag() == i) { - b.setTextSize(getTextSize()); - b.setChecked(true); - //if (i.intValue() == Task.IMPORTANCE_LEAST) - // b.setTextColor(grayColor); - b.setBackgroundResource(R.drawable.importance_background_selected); - } else { - b.setTextSize(getTextSize()); - b.setChecked(false); - b.setTextColor(colors[(Integer)b.getTag()]); - b.setBackgroundResource(0); - } - } - - for (ImportanceChangedListener l : listeners) { - l.importanceChanged(i, colors[i]); - } - } - - private int getTextSize() { - if (ProducteevUtilities.INSTANCE.isLoggedIn()) - return 14; - else - return 24; - } - - public Integer getImportance() { - for(CompoundButton b : buttons) - if(b.isChecked()) - return (Integer) b.getTag(); - return null; - } - - public void addListener(ImportanceChangedListener listener) { - listeners.add(listener); - } - - public void removeListener(ImportanceChangedListener listener) { - if (listeners.contains(listener)) - listeners.remove(listener); - } - @Override - public void readFromTask(Task task) { - setImportance(task.getValue(Task.IMPORTANCE)); + protected void readFromTaskPrivate() { + setImportance(model.getValue(Task.IMPORTANCE)); } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { if(getImportance() != null) task.setValue(Task.IMPORTANCE, getImportance()); return null; diff --git a/astrid/src/com/todoroo/astrid/ui/PopupControlSet.java b/astrid/src/com/todoroo/astrid/ui/PopupControlSet.java index 65a3787df..3ec55b29b 100644 --- a/astrid/src/com/todoroo/astrid/ui/PopupControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/PopupControlSet.java @@ -16,15 +16,30 @@ import android.widget.TextView; import com.timsu.astrid.R; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DialogUtilities; +import com.todoroo.astrid.data.Task; import com.todoroo.astrid.helper.TaskEditControlSet; import com.todoroo.astrid.service.ThemeService; public abstract class PopupControlSet extends TaskEditControlSet { protected final View displayView; - protected final Activity activity; - protected final Dialog dialog; + protected Dialog dialog; protected final TextView displayText; + private final String titleString; + + final DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface d, int which) { + onOkClick(); + } + }; + + final DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface d) { + onCancelClick(); + } + }; public PopupControlSet(Activity activity, int viewLayout, int displayViewLayout, final int title) { super(activity, viewLayout); @@ -40,27 +55,7 @@ public abstract class PopupControlSet extends TaskEditControlSet { this.displayText = null; } - final DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - onOkClick(); - } - }; - - final DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface d) { - onCancelClick(); - } - }; - - this.activity = activity; - - - String titleString = (title > 0) ? activity.getString(title) : ""; - - dialog = buildDialog(titleString, okListener, cancelListener); - + titleString = (title > 0) ? activity.getString(title) : ""; if (displayView != null) { displayView.setOnClickListener(getDisplayClickListener()); @@ -71,27 +66,29 @@ public abstract class PopupControlSet extends TaskEditControlSet { return displayView; } - protected Dialog buildDialog(String title, final DialogInterface.OnClickListener okListener, DialogInterface.OnCancelListener cancelListener) { + protected Dialog buildDialog(String title, final DialogInterface.OnClickListener okClickListener, DialogInterface.OnCancelListener cancelClickListener) { int theme = ThemeService.getEditDialogTheme(); - final Dialog d = new Dialog(activity, theme); + dialog = new Dialog(activity, theme); if (title.length() == 0) - d.requestWindowFeature(Window.FEATURE_NO_TITLE); + dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); else - d.setTitle(title); + dialog.setTitle(title); + View v = getView(); - d.setContentView(v, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); + + dialog.setContentView(v, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); Button dismiss = (Button) v.findViewById(R.id.edit_dlg_ok); if (dismiss != null) { dismiss.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { - okListener.onClick(d, 0); - DialogUtilities.dismissDialog(activity, d); + okClickListener.onClick(dialog, 0); + DialogUtilities.dismissDialog(activity, dialog); } }); } - LayoutParams params = d.getWindow().getAttributes(); + LayoutParams params = dialog.getWindow().getAttributes(); params.width = LayoutParams.FILL_PARENT; params.height = LayoutParams.WRAP_CONTENT; Configuration config = activity.getResources().getConfiguration(); @@ -100,22 +97,31 @@ public abstract class PopupControlSet extends TaskEditControlSet { DisplayMetrics metrics = activity.getResources().getDisplayMetrics(); params.width = metrics.widthPixels / 2; } - d.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); + dialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); - d.setOnCancelListener(cancelListener); - d.setOwnerActivity(PopupControlSet.this.activity); - return d; + dialog.setOnCancelListener(cancelClickListener); + dialog.setOwnerActivity(PopupControlSet.this.activity); + additionalDialogSetup(); + return dialog; } protected OnClickListener getDisplayClickListener() { return new OnClickListener() { @Override public void onClick(View v) { + if (dialog == null) { + buildDialog(titleString, okListener, cancelListener); + } dialog.show(); } }; } + protected void additionalDialogSetup() { + // Will be called after dialog is set up. + // Subclasses can override + } + protected void onOkClick() { refreshDisplayView(); } @@ -124,5 +130,11 @@ public abstract class PopupControlSet extends TaskEditControlSet { refreshDisplayView(); } + @Override + public void readFromTask(Task task) { + super.readFromTask(task); + refreshDisplayView(); + } + protected abstract void refreshDisplayView(); } diff --git a/astrid/src/com/todoroo/astrid/ui/RandomReminderControlSet.java b/astrid/src/com/todoroo/astrid/ui/RandomReminderControlSet.java index 2842690db..a4ad45f00 100644 --- a/astrid/src/com/todoroo/astrid/ui/RandomReminderControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/RandomReminderControlSet.java @@ -25,12 +25,14 @@ public class RandomReminderControlSet extends TaskEditControlSet { private final CheckBox settingCheckbox; private final Spinner periodSpinner; + private final View parentView; private boolean periodSpinnerInitialized = false; private final int[] hours; public RandomReminderControlSet(Activity activity, View parentView, int layout) { super(activity, layout); + this.parentView = parentView; settingCheckbox = (CheckBox) parentView.findViewById(R.id.reminder_random); periodSpinner = (Spinner) parentView.findViewById(R.id.reminder_random_interval); periodSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @@ -63,8 +65,13 @@ public class RandomReminderControlSet extends TaskEditControlSet { } @Override - public void readFromTask(Task task) { - long time = task.getValue(Task.REMINDER_PERIOD); + protected void afterInflate() { + // Nothing to do here + } + + @Override + protected void readFromTaskPrivate() { + long time = model.getValue(Task.REMINDER_PERIOD); boolean enabled = time > 0; if(time <= 0) { @@ -80,7 +87,7 @@ public class RandomReminderControlSet extends TaskEditControlSet { } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { if(settingCheckbox.isChecked()) { int hourValue = hours[periodSpinner.getSelectedItemPosition()]; task.setValue(Task.REMINDER_PERIOD, hourValue * DateUtilities.ONE_HOUR); diff --git a/astrid/src/com/todoroo/astrid/ui/ReminderControlSet.java b/astrid/src/com/todoroo/astrid/ui/ReminderControlSet.java index 36fc2b8e3..170b42c22 100644 --- a/astrid/src/com/todoroo/astrid/ui/ReminderControlSet.java +++ b/astrid/src/com/todoroo/astrid/ui/ReminderControlSet.java @@ -1,5 +1,8 @@ package com.todoroo.astrid.ui; +import java.util.ArrayList; +import java.util.List; + import android.app.Activity; import android.view.View; import android.widget.ArrayAdapter; @@ -18,44 +21,26 @@ import com.todoroo.astrid.data.Task; * */ public class ReminderControlSet extends PopupControlSet { - private final CheckBox during, after; - private final Spinner mode; - private final LinearLayout remindersBody; + private CheckBox during, after; + private Spinner mode; + private LinearLayout remindersBody; + private final List extraViews; - private final RandomReminderControlSet randomControlSet; - private final AlarmControlSet alarmControl; + private RandomReminderControlSet randomControlSet; + private AlarmControlSet alarmControl; public ReminderControlSet(Activity activity, int viewLayout, int displayViewLayout) { super(activity, viewLayout, displayViewLayout, R.string.TEA_reminders_group_label); - during = (CheckBox) getView().findViewById(R.id.reminder_due); - after = (CheckBox) getView().findViewById(R.id.reminder_overdue); - mode = (Spinner) getView().findViewById(R.id.reminder_alarm); - - randomControlSet = new RandomReminderControlSet(activity, getView(), -1); - alarmControl = new AlarmControlSet(activity, R.layout.control_set_alarms); - - remindersBody = (LinearLayout) getView().findViewById(R.id.reminders_body); - remindersBody.addView(alarmControl.getView()); - + extraViews = new ArrayList(); displayText.setText(activity.getString(R.string.TEA_reminders_group_label)); - String[] list = new String[] { - activity.getString(R.string.TEA_reminder_mode_once), - activity.getString(R.string.TEA_reminder_mode_five), - activity.getString(R.string.TEA_reminder_mode_nonstop), - }; - final ArrayAdapter adapter = new ArrayAdapter( - activity, android.R.layout.simple_spinner_item, list); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - mode.setAdapter(adapter); - } - }); } public void addViewToBody(View v) { - remindersBody.addView(v, 0); + if (remindersBody != null) + remindersBody.addView(v, 0); + else + extraViews.add(v); + } public void setValue(int flags) { @@ -88,14 +73,47 @@ public class ReminderControlSet extends PopupControlSet { } @Override - public void readFromTask(Task task) { - setValue(task.getValue(Task.REMINDER_FLAGS)); - randomControlSet.readFromTask(task); - alarmControl.readFromTask(task); + protected void afterInflate() { + during = (CheckBox) getView().findViewById(R.id.reminder_due); + after = (CheckBox) getView().findViewById(R.id.reminder_overdue); + mode = (Spinner) getView().findViewById(R.id.reminder_alarm); + + randomControlSet = new RandomReminderControlSet(activity, getView(), -1); + alarmControl = new AlarmControlSet(activity, R.layout.control_set_alarms); + alarmControl.readFromTask(model); + + remindersBody = (LinearLayout) getView().findViewById(R.id.reminders_body); + remindersBody.addView(alarmControl.getView()); + while (extraViews.size() > 0) { + addViewToBody(extraViews.remove(0)); + } + + String[] list = new String[] { + activity.getString(R.string.TEA_reminder_mode_once), + activity.getString(R.string.TEA_reminder_mode_five), + activity.getString(R.string.TEA_reminder_mode_nonstop), + }; + final ArrayAdapter adapter = new ArrayAdapter( + activity, android.R.layout.simple_spinner_item, list); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + mode.setAdapter(adapter); + } + }); + } + + @Override + protected void readFromTaskPrivate() { + setValue(model.getValue(Task.REMINDER_FLAGS)); + // Calls to get view will force other control sets to load + randomControlSet.readFromTask(model); + randomControlSet.readFromTaskPrivate(); } @Override - public String writeToModel(Task task) { + protected String writeToModelPrivate(Task task) { task.setValue(Task.REMINDER_FLAGS, getValue()); // clear snooze if task is being edited task.setValue(Task.REMINDER_SNOOZE, 0L); diff --git a/astrid/src/com/todoroo/astrid/ui/UrgencyControlSet.java b/astrid/src/com/todoroo/astrid/ui/UrgencyControlSet.java deleted file mode 100644 index 7c9e1b18f..000000000 --- a/astrid/src/com/todoroo/astrid/ui/UrgencyControlSet.java +++ /dev/null @@ -1,297 +0,0 @@ -package com.todoroo.astrid.ui; - -import java.util.Calendar; -import java.util.Date; -import java.util.concurrent.atomic.AtomicBoolean; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.content.DialogInterface.OnCancelListener; -import android.content.DialogInterface.OnDismissListener; -import android.text.format.DateUtils; -import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.TextView; -import android.widget.TimePicker; - -import com.timsu.astrid.R; -import com.todoroo.andlib.utility.DateUtilities; -import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.helper.TaskEditControlSet; -import com.todoroo.astrid.ui.DeadlineTimePickerDialog.OnDeadlineTimeSetListener; - -public class UrgencyControlSet extends TaskEditControlSet implements OnDeadlineTimeSetListener { - - private static final int SPECIFIC_DATE = -1; - - private final Button dateButton; - private final Button timeButton; - private ArrayAdapter urgencyAdapter; - private final TextView auxDisplay; - - private final Activity activity; - - private long dueDateValue = 0; - private long dueTimeValue = 0; - - /** - * Container class for urgencies - * - * @author Tim Su - * - */ - private class UrgencyValue { - public String label; - public int setting; - public long dueDate; - - public UrgencyValue(String label, int setting) { - this.label = label; - this.setting = setting; - dueDate = Task.createDueDate(setting, 0); - } - - public UrgencyValue(String label, int setting, long dueDate) { - this.label = label; - this.setting = setting; - this.dueDate = dueDate; - } - - @Override - public String toString() { - return label; - } - } - - public UrgencyControlSet(Activity activity, int layout, int extensionViewId, int auxDisplayId, int...quickWhenIds) { - super(activity, layout); - this.activity = activity; - this.dateButton = (Button) getView().findViewById(R.id.urgency_date); - this.timeButton = (Button) getView().findViewById(R.id.urgency_time); - View extensionView = activity.findViewById(extensionViewId); - for (int id : quickWhenIds) { - View quickWhen = extensionView.findViewById(id); - if (quickWhen != null) { - quickWhen.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dateButton.performClick(); - } - }); - } - } - auxDisplay = (TextView) extensionView.findViewById(auxDisplayId); - - dateButton.setOnClickListener(dateButtonClick); - - timeButton.setOnClickListener(timeButtonClick); - } - - // --- events - - View.OnClickListener dateButtonClick = new View.OnClickListener() { - @Override - public void onClick(View v) { - showUrgencySpinner(dueDateValue); - } - }; - - View.OnClickListener timeButtonClick = new View.OnClickListener() { - @Override - public void onClick(View v) { - boolean hasTime = Task.hasDueTime(dueTimeValue); - int hour = 18, minute = 0; - if(hasTime) { - Date date = new Date(dueTimeValue); - hour = date.getHours(); - minute = date.getMinutes(); - } - - DeadlineTimePickerDialog timePicker = new DeadlineTimePickerDialog(activity, - UrgencyControlSet.this, - hour, minute, - DateUtilities.is24HourFormat(activity), true); - - timePicker.show(); - } - }; - - - /** - * set up urgency adapter and picks the right selected item - * @param dueDate - */ - private void showUrgencySpinner(long dueDate) { - // set up base urgency list - String[] labels = activity.getResources().getStringArray(R.array.TEA_urgency); - UrgencyValue[] urgencyValues = new UrgencyValue[labels.length]; - urgencyValues[0] = new UrgencyValue(labels[0], - Task.URGENCY_NONE); - urgencyValues[1] = new UrgencyValue(labels[1], - Task.URGENCY_SPECIFIC_DAY_TIME, SPECIFIC_DATE); - urgencyValues[2] = new UrgencyValue(labels[2], - Task.URGENCY_TODAY); - urgencyValues[3] = new UrgencyValue(labels[3], - Task.URGENCY_TOMORROW); - String dayAfterTomorrow = DateUtils.getDayOfWeekString( - new Date(DateUtilities.now() + 2 * DateUtilities.ONE_DAY).getDay() + - Calendar.SUNDAY, DateUtils.LENGTH_LONG); - urgencyValues[4] = new UrgencyValue(dayAfterTomorrow, - Task.URGENCY_DAY_AFTER); - urgencyValues[5] = new UrgencyValue(labels[5], - Task.URGENCY_NEXT_WEEK); - urgencyValues[6] = new UrgencyValue(labels[6], - Task.URGENCY_IN_TWO_WEEKS); - urgencyValues[7] = new UrgencyValue(labels[7], - Task.URGENCY_NEXT_MONTH); - - // search for setting - int selection = -1; - for(int i = 0; i < urgencyValues.length; i++) - if(urgencyValues[i].dueDate == dueDate) { - selection = i; - break; - } - - if(selection == -1) { - UrgencyValue[] updated = new UrgencyValue[labels.length + 1]; - for(int i = 0; i < labels.length; i++) - updated[i+1] = urgencyValues[i]; - if(Task.hasDueTime(dueDate)) { - Date dueDateAsDate = new Date(dueDate); - updated[0] = new UrgencyValue(DateUtilities.getDateStringWithTime(activity, dueDateAsDate), - Task.URGENCY_SPECIFIC_DAY_TIME, dueDate); - } else { - updated[0] = new UrgencyValue(DateUtilities.getDateString(activity, new Date(dueDate)), - Task.URGENCY_SPECIFIC_DAY, dueDate); - } - selection = 0; - urgencyValues = updated; - } - - urgencyAdapter = new ArrayAdapter( - activity, android.R.layout.simple_dropdown_item_1line, - urgencyValues); - - new AlertDialog.Builder(activity) - .setTitle(R.string.TEA_urgency_label) - .setAdapter(urgencyAdapter, spinnerClickListener) - .setIcon(R.drawable.gl_date_blue) - .show().setOwnerActivity(activity); - } - - DialogInterface.OnClickListener spinnerClickListener = new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int position) { - UrgencyValue item = urgencyAdapter.getItem(position); - if(item.dueDate == SPECIFIC_DATE) { - customSetting = item.setting; - Date date = new Date(dueDateValue == 0 ? DateUtilities.now() : dueDateValue); - date.setSeconds(0); - - final CalendarDialog calendarDialog = new CalendarDialog(activity, date); - final AtomicBoolean cancelled = new AtomicBoolean(false); - calendarDialog.show(); - calendarDialog.setOnDismissListener(new OnDismissListener() { - @Override - public void onDismiss(DialogInterface arg0) { - if (!cancelled.get()) - setDateFromCalendar(calendarDialog); - } - }); - - calendarDialog.setOnCancelListener(new OnCancelListener() { - @Override - public void onCancel(DialogInterface arg0) { - cancelled.set(true); - } - }); - } else { - dueDateValue = item.dueDate; - if(dueDateValue == 0) - dueTimeValue = 0; - updateButtons(); - } - } - }; - - // --- date setting logic - - int customSetting; - - private void setDateFromCalendar(CalendarDialog calendarDialog) { - Date date = calendarDialog.getCalendarDate(); - date.setMinutes(0); - dueDateValue = Task.createDueDate(customSetting, date.getTime()); - - updateButtons(); - } - - public void onTimeSet(TimePicker view, boolean hasTime, int hourOfDay, int minute) { - if(!hasTime) - dueTimeValue = 0; - else { - Date date = new Date(); - date.setHours(hourOfDay); - date.setMinutes(minute); - date.setSeconds(0); - dueTimeValue = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, date.getTime()); - - if(dueDateValue == 0) - dueDateValue = DateUtilities.now(); - } - updateButtons(); - } - - @SuppressWarnings("nls") - private void updateButtons() { - String auxString = ""; - if(dueDateValue == 0) { - dateButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.gl_date, 0, 0, 0); - dateButton.setText(R.string.TEA_urgency_none); - } else { - dateButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.gl_date_blue, 0, 0, 0); - dateButton.setText(DateUtilities.getDateString(activity, new Date(dueDateValue))); - auxString += dateButton.getText(); - } - - if(dueTimeValue == 0 || !Task.hasDueTime(dueTimeValue)) { - timeButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.gl_time, 0, 0, 0); - timeButton.setText(R.string.TEA_urgency_none); - } else { - timeButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.gl_time_blue, 0, 0, 0); - timeButton.setText(DateUtilities.getTimeString(activity, new Date(dueTimeValue))); - auxString += (", " + timeButton.getText()); - } - - if (auxDisplay != null) { - auxDisplay.setText(auxString); - } - } - - // --- setting up values - - @Override - public void readFromTask(Task task) { - dueTimeValue = dueDateValue = task.getValue(Task.DUE_DATE); - updateButtons(); - } - - @Override - public String writeToModel(Task task) { - Date date = new Date(dueDateValue); - if(dueTimeValue > 0) { - Date time = new Date(dueTimeValue); - date.setHours(time.getHours()); - date.setMinutes(time.getMinutes()); - date.setSeconds(time.getSeconds()); - } else { - date.setTime(Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, dueDateValue)); - } - - task.setValue(Task.DUE_DATE, date.getTime()); - return null; - } - -} \ No newline at end of file