Changeset 250


Ignore:
Timestamp:
07/13/06 16:43:36 (13 years ago)
Author:
Nicholas Riley
Message:

Refactor editing, coalese Alarms and alarm list. Try (and fail) to recognize exit from alarm time editor.

Location:
trunk/hiptop/pester
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/hiptop/pester/locale/en_US/Pester.rsrc

    r248 r250  
    1818
    1919string  ID_VERSION      "V@APP_VERSION_MAJOR@.@APP_VERSION_MINOR@"
     20
     21int EVENT_CANCEL_ALARM 1
     22int EVENT_TIME_EDITOR_LOST_FOCUS 2
    2023
    2124splashScreen ID_SPLASH_SCREEN_RESOURCE
     
    8487        title = "Set Alarm"
    8588        showCancel
    86 #       className = "net.sabi.pester.AlarmSetDialog"
     89        className = "net.sabi.pester.AlarmSetDialog"
    8790        widthPct = 100
    8891        centerUnderTitleBar
     
    158161                        alignBaseline = ID_AT_BUTTON
    159162                        positionToRight = ID_AT_BUTTON : 1
     163                        className = "net.sabi.pester.AlarmTimeEditor"
    160164       
    161165                dateEditor
  • trunk/hiptop/pester/net/sabi/pester/Alarm.java

    r249 r250  
    2121    private String mMessage;
    2222    private int mType;
    23     private int mInterval; // XXX rename to 'interval'
     23    private int mPeriod;
    2424    private Date mDate;
    2525    private RingToneObject mAlert;
     
    3838    }
    3939    public int getPeriod() {
    40         return mInterval;
     40        return mPeriod;
    4141    }
    4242    public boolean getUsesPeriod() {
     
    5858    public void setPeriod(int period, boolean repeating) {
    5959        mType = repeating ? TYPE_PERIODIC_REPEATING : TYPE_PERIODIC;
    60         mInterval = period;
     60        mPeriod = period;
    6161    }
    6262    public void setDate(Date date) {
     
    7575            dataStream.writeByte(VERSION_1);
    7676            dataStream.writeUTF(mMessage);
    77             dataStream.writeInt(mInterval);
     77            dataStream.writeInt(mPeriod);
    7878            dataStream.writeInt(mDate.getUnixTimeGMT());
    7979            dataStream.writeInt(mType);
     
    9696            }
    9797            mMessage = dataStream.readUTF();
    98             mInterval = dataStream.readInt();
     98            mPeriod = dataStream.readInt();
    9999            mType = dataStream.readInt();
    100100        } catch (Exception e) {
     
    128128        if (getUsesPeriod()) {
    129129            mDate = new Date();
    130             mDate.addSeconds(mInterval);
     130            mDate.addSeconds(mPeriod);
    131131        }
    132132        resume();
     
    154154        switch (mType) {
    155155        case TYPE_PERIODIC:
    156             sb.append("periodic (").append(mInterval).append("s) "); break;
     156            sb.append("periodic (").append(mPeriod).append("s) "); break;
    157157        case TYPE_PERIODIC_REPEATING:
    158             sb.append("repeating periodic (").append(mInterval).append("s) ");
     158            sb.append("repeating periodic (").append(mPeriod).append("s) ");
    159159            break;
    160160        case TYPE_DATE:
     
    170170            return null;
    171171        String layout = LocaleUtils.getDateTimePattern();
    172         String timeFormat = LocaleUtils.getMediumTimeFormat();
    173172        String dateFormat = LocaleUtils.getMediumDateFormat();
    174         String timeString = DateFormat.withFormat(timeFormat, date);
    175         String dateString = DateFormat.withFormat(dateFormat, date);
     173        String dateString;
     174        String timeFormat;
    176175        if (relative) {
     176            timeFormat = LocaleUtils.getShortTimeFormat();
    177177            int daysFromToday = date.getDaysBetween(new Date());
    178178            if (daysFromToday == 0)
     
    182182            else if (daysFromToday < 7)
    183183                dateString = date.getDayString();
    184         }
     184            else
     185                dateString = DateFormat.withFormat(dateFormat, date);
     186        } else {
     187            dateString = DateFormat.withFormat(dateFormat, date);
     188            timeFormat = LocaleUtils.getMediumTimeFormat();
     189        }
     190        String timeString = DateFormat.withFormat(timeFormat, date);
    185191        return StringFormat.withFormat(layout, timeString, dateString);
    186192    }
  • trunk/hiptop/pester/net/sabi/pester/AlarmListView.java

    r249 r250  
    33import danger.app.Application;
    44import danger.app.Event;
    5 import danger.audio.ToneClass;
    6 import danger.audio.ToneRights;
    7 
    8 import danger.internal.Date;
    95import danger.ui.ActiveListView;
    10 import danger.ui.Button;
    11 import danger.ui.CheckBox;
    12 import danger.ui.DateTimeEditor;
    13 import danger.ui.DatePicker;
    14 import danger.ui.DialogWindow;
    15 import danger.ui.MenuItem;
    16 import danger.ui.NumberEditor;
    17 import danger.ui.PopupMenu;
    18 import danger.ui.RadioButton;
    19 import danger.ui.Rect;
    20 import danger.ui.RingTonePicker;
    21 import danger.ui.TypeAheadTextField;
    226import danger.util.ActiveList;
    23 import danger.util.StdActiveList;
    24 import danger.util.DEBUG;
    257
    268public class AlarmListView extends ActiveListView
    279    implements Resources, Commands {
    28     private static Pester sPester;
     10
     11    // XXX some of these shouldn't conceptually be static
    2912    private static AlarmListWindow sAlarmListWindow;
    30     // XXX some of these shouldn't conceptually be static, but who cares
    31     private static StdActiveList sAlarmList;
    32     private static Alarms sAlarms;
     13    private static AlarmSetDialog sAlarmSetDialog;
    3314
    34     private static DialogWindow sEditWindow;
    35     private TypeAheadTextField sMessageField;
    36     private RadioButton sInButton, sAtButton;
    37     private NumberEditor sPeriodField;
    38     private PopupMenu sPeriodUnitsPopup;
    39     // XXX don't allow <5 second repeating alarms
    40     private CheckBox sRepeatCheckBox;
    41     private DateTimeEditor sTimeEditor, sDateEditor;
    42     private DatePicker sDatePicker;
    43     private RingTonePicker sAlertPicker;
    44     private Button sDiscardAlarmButton, sSetAlarmButton;
     15    public void onDecoded() {
     16        sAlarmListWindow = (AlarmListWindow)getWindow();
     17        setList(Alarms.getList());
     18        super.onDecoded();
     19    }
    4520
    46     private static Alarm sFocusedAlarm, sEditingAlarm;
    47 
    48     private static final int EVENT_CANCEL_ALARM = -1; // XXX move this?
    49    
    50     public void onDecoded() {
    51         sPester = (Pester)Application.getCurrentApp();
    52         sAlarmListWindow = (AlarmListWindow)getWindow();
    53         sAlarmList = new StdActiveList();
    54         setList(sAlarmList);
    55 
    56         sEditWindow = sPester.getDialog(ID_ALARM_SET_DIALOG, this);
    57         sEditWindow.setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
    58         sMessageField = (TypeAheadTextField)sEditWindow.getDescendantWithID(ID_MESSAGE_FIELD);
    59         sEditWindow.disableBottomRightButtonOnEmptyField(sMessageField);
    60         sInButton = (RadioButton)sEditWindow.getDescendantWithID(ID_IN_BUTTON);
    61         sAtButton = (RadioButton)sEditWindow.getDescendantWithID(ID_AT_BUTTON);
    62         sPeriodField = (NumberEditor)sEditWindow.getDescendantWithID(ID_PERIOD_FIELD);
    63         sPeriodUnitsPopup = (PopupMenu)sEditWindow.getDescendantWithID(ID_PERIOD_UNITS_POPUP);
    64         sRepeatCheckBox = (CheckBox)sEditWindow.getDescendantWithID(ID_PERIOD_REPEAT_CHECKBOX);
    65         sTimeEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_TIME_EDITOR);
    66         sDateEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_DATE_EDITOR);
    67         sDatePicker = (DatePicker)sEditWindow.getDescendantWithID(ID_DATE_PICKER);
    68         sAlertPicker = (RingTonePicker)sEditWindow.getDescendantWithID(ID_ALERT_PICKER);
    69         sSetAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_SET_ALARM_BUTTON);
    70         sDiscardAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
    71         sInButton.setValue(1);
    72         sDateEditor.limitToDangerEpoch();
    73         sAlertPicker.setGroupFilter(ToneClass.FOREGROUND | ToneClass.CUSTOM);
    74         sAlertPicker.setRights(ToneRights.FULL);
    75         // this doesn't work properly yet
    76         // sAlertPicker.setCanRecord(true);
    77 
    78         super.onDecoded();
     21    protected AlarmSetDialog alarmSetDialog() {
     22        if (sAlarmSetDialog == null)
     23            sAlarmSetDialog = AlarmSetDialog.getDialog();
     24        return sAlarmSetDialog;
    7925    }
    8026
     
    8329        case Event.DEVICE_BUTTON_BACK:
    8430        case Event.DEVICE_BUTTON_CANCEL:
    85             sPester.returnToLauncher();
     31            Application.getCurrentApp().returnToLauncher();
    8632            return true;
    8733        }
     
    8935    }
    9036
    91     protected void editDateOrPeriod(Alarm alarm) {
    92         boolean usesPeriod = alarm.getUsesPeriod();
    93         if (usesPeriod) {
    94             int period = alarm.getPeriod();
    95             for (int i = sPeriodUnitsPopup.itemCount() - 1 ; i >= 0 ; --i) {
    96                 int unitsPerSec = sPeriodUnitsPopup.getItem(i).getEvent().data;
    97                 if (period % unitsPerSec == 0) {
    98                     sPeriodUnitsPopup.setValue(i);
    99                     sPeriodField.setValue(period / unitsPerSec);
    100                     break;
    101                 }
    102             }
    103             sRepeatCheckBox.setValue(alarm.getRepeating() ? 1 : 0);
    104         }
    105 
    106         Date date = alarm.getDate();
    107         sTimeEditor.setDate(date);
    108         sDateEditor.setDate(date);
    109         sDatePicker.setDate(date);
    110 
    111         sInButton.setValue(usesPeriod ? 1 : 0);
    112         sAtButton.setValue(usesPeriod ? 0 : 1);
    113     }
    114 
    115     protected void editAlarm(Alarm alarm, boolean asNew) {
    116         sEditingAlarm = alarm;
    117         alarm.beginEditing();
    118         if (asNew) {
    119             sDiscardAlarmButton.hide();
    120         } else {
    121             sMessageField.setText(alarm.getMessage());
    122             sDiscardAlarmButton.show();
    123             editDateOrPeriod(alarm);
    124             sAlertPicker.setRingTone(sEditingAlarm.getAlert());
    125         }
    126         constrainDate();
    127         sEditWindow.show();
    128     }
    129 
    130     protected Date editingDate() {
    131         Date date = sDateEditor.getDate();
    132         date.setTime(sTimeEditor.getDate().getTime());
    133         date.setSeconds(0);
    134         return date;
    135     }
    136 
    137     // XXX make this into a more general validation method;
    138     // XXX check before dismissing dialog that alarm is valid
    139     // XXX and display alarm date
    140     // XXX default to now somehow?  Would be useful.
    141     protected void constrainDate() {
    142         // XXX schedule every minute (second?) if in AT mode
    143         // XXX could use onPeriodicPulse() perhaps
    144         Date now = new Date();
    145         sDateEditor.min(now);
    146         sDatePicker.min(now);
    147         sSetAlarmButton.setEnabled(sInButton.getValue() == 1 ||
    148                                    editingDate().compareTo(now) > 0);
    149     }
    150 
    15137    public boolean receiveEvent(Event e) {
    15238        switch (e.type) {
    15339        case EVENT_NEW_ALARM:
    154             editAlarm(new Alarm(), true);
    155             return true;
    156         case EVENT_SET_ALARM:
    157             sEditingAlarm.setMessage(sMessageField.toString());
    158             if (sInButton.getValue() == 1) {
    159                 int itemIndex = sPeriodUnitsPopup.getValue();
    160                 MenuItem itemAtIndex = sPeriodUnitsPopup.getItem(itemIndex);
    161                 Event event = itemAtIndex.getEvent();
    162                 sEditingAlarm.setPeriod(sPeriodField.getValue() * event.data,
    163                                         sRepeatCheckBox.getValue() == 1);
    164             } else {
    165                 sEditingAlarm.setDate(editingDate());
    166             }
    167             sEditingAlarm.setAlert(sAlertPicker.getRingTone());
    168             // schedule before adding so the tooltip is correct
    169             sEditingAlarm.schedule();
    170             if (sEditingAlarm != sFocusedAlarm) { // new alarm
    171                 sAlarmList.addItem(sEditingAlarm);
    172                 setFocusedItem(sEditingAlarm);
    173                 sFocusedAlarm = sEditingAlarm;
    174             }
    175             return true;
    176         case EVENT_CANCEL_ALARM:
    177             if (sEditingAlarm == sFocusedAlarm) // existing alarm
    178                 sEditingAlarm.resume();
    179             sEditingAlarm = null;
     40            alarmSetDialog().editAlarm(new Alarm(), true);
    18041            return true;
    18142        case EVENT_DISCARD_ALARM:
    18243            // XXX confirmation
    183             sAlarmList.removeItem(sFocusedAlarm);
    184             return true;
    185         case EVENT_IN:
    186             sAtButton.setValue(0);
    187             constrainDate();
    188             return true;
    189         case EVENT_AT:
    190             sInButton.setValue(0);
    191             constrainDate();
    192             return true;
    193         case EVENT_TIME_EDITOR:
    194             Date date = editingDate();
    195             // XXX do this on exit from the field, not on change
    196             if (date.compareTo(new Date()) <= 0) {
    197                 date.addDays(1);
    198                 sDatePicker.setDate(date);
    199                 sDateEditor.setDate(date);
    200             }
    201             constrainDate();
    202             return true;
    203         case EVENT_DATE_EDITOR:
    204             sDatePicker.setDate(sDateEditor.getDate());
    205             constrainDate();
    206             return true;
    207         case EVENT_DATE_PICKER:
    208             sDateEditor.setDate((Date)e.argument);
    209             constrainDate();
     44            Alarms.removeAlarm((Alarm)getFocusedItem());
    21045            return true;
    21146        }
     
    21449
    21550    protected void itemActivated(Object item) {
    216         editAlarm(sFocusedAlarm, false);
    217     }
    218     protected void itemFocused(Object item) {
    219         sFocusedAlarm = (Alarm)item;
     51        if (item == null)
     52            return;
     53        alarmSetDialog().editAlarm((Alarm)item, false);
    22054    }
    22155
     
    22559    public void onItemAdded(ActiveList list, Object item, int index) {
    22660        sAlarmListWindow.updateAlarmCount();
     61        setFocus(index);
    22762        super.onItemAdded(list, item, index);
    22863    }
  • trunk/hiptop/pester/net/sabi/pester/Alarms.java

    r237 r250  
    33import java.util.Comparator;
    44import danger.app.DataStore;
     5import danger.util.StdActiveList;
    56
    6 public class Alarms {
     7public class Alarms extends StdActiveList {
    78    // max # records in a datastore
    89    private static final int MAX_ALARM_COUNT = 50;
    910
    10     private static Alarms sAlarms = null;
     11    private static Alarms sAlarmList = null;
    1112    private static DataStore sDataStore;
    1213
     
    1415        sDataStore = DataStore.createDataStore("alarms", true /* auto sync */);
    1516    }
    16    
    17     public static Alarms getInstance() {
    18         if (sAlarms == null)
    19             sAlarms = new Alarms();
    2017
    21         return sAlarms;
     18    public static Alarms getList() {
     19        if (sAlarmList == null)
     20            sAlarmList = new Alarms();
     21        return sAlarmList;
     22    }
     23
     24    public static void addAlarm(Alarm alarm) {
     25        sAlarmList.addItem(alarm);
     26    }
     27    public static void removeAlarm(Alarm alarm) {
     28        sAlarmList.removeItem(alarm);
    2229    }
    2330
Note: See TracChangeset for help on using the changeset viewer.