~charlesk/ubuntu-clock-app/lp-1275560

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2013, 2014 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Nekhelesh Ramananthan <krnekhelesh@gmail.com>
 */

import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem
import "../common/ClockUtils.js" as Utils
import "../common"

Page {
    id: alarmPage
    objectName: "alarmPage"

    property string currentTime
    property alias modelCount: listSavedAlarm.count
    property int enabledAlarmCount: 0
    property var nextAlarm
    property real tempPosition

    Component.onCompleted: {
        Utils.log("AlarmPage loaded")
        alarmModel.count > 0 ? get_next_active_alarm() : Utils.log("No Alarms Present")
        tempPosition = clockAnimationContainer.contentY
        currentTime = Utils.convertTime(new Date().getHours(), new Date().getMinutes(), new Date().getUTCSeconds(), appSetting.contents.timeFormat)
    }

    onModelCountChanged: modelCount == 0 ? clockAnimationContainer.contentY = tempPosition : undefined

    actions: [
        Action {
            id: addAlarmAction
            iconSource: Qt.resolvedUrl("../images/add_icon.png")
            text: i18n.tr("Add alarm")
            description: i18n.tr("Add a new alarm")
            keywords: i18n.tr("Add;Alarm;Alert;New;Tone;Bell")
            onTriggered: pagestack.push(Qt.resolvedUrl("AddAlarmPage.qml"), {"isNewAlarm": true})
        }
    ]

    function onTimerUpdate(now) {
        currentTime = Utils.convertTime(new Date().getHours(), new Date().getMinutes(), new Date().getUTCSeconds(), appSetting.contents.timeFormat)
    }

    function get_next_active_alarm()
    {
        enabledAlarmCount = 0
        var tempAlarm;

        for(var i=0; i<alarmModel.count; i++) {
            tempAlarm = alarmModel.get(i)
            if (tempAlarm.enabled) {
                enabledAlarmCount += 1
                nextAlarm = alarmModel.get(i)
                break;
            }
        }

        if (enabledAlarmCount !== 0) {
            for (var i=0; i<alarmModel.count; i++) {
                tempAlarm = alarmModel.get(i)
                if (tempAlarm.enabled)
                    if (tempAlarm.date < nextAlarm.date)
                        nextAlarm = alarmModel.get(i)
            }
            nameAlarm.text = nextAlarm.message
            alarmOccurrence.text = format_day_string(nextAlarm.daysOfWeek, nextAlarm.type)
            alarmFace.hours = Utils.circlePositiontoHours(Qt.formatTime(nextAlarm.date, "hh AP").split(' ')[0], "12-hour")
            alarmFace.minutes = Qt.formatTime(nextAlarm.date, "mm")
            alarmFace.innerLabel.text = Utils.convertTime(Qt.formatTime(nextAlarm.date, "hh"), Qt.formatTime(nextAlarm.date, "mm"), 0, alarmFace.timeFormat)
        }
        else {
            nameAlarm.text = "";
            alarmOccurrence.text = "";
            alarmFace.hours = 0;
            alarmFace.minutes = 0;
            alarmFace.innerLabel.text = "00:00"
        }

    }

    function format_day_string(value, type) {
        var occurs = get_day(value, type);

        if (value != Alarm.Daily) {
            if (occurs.length > 1)
                return i18n.tr("Every ") + occurs
            else
                return i18n.tr("Once on ")  + occurs
        }
        else
            return i18n.tr("Daily")
    }

    function get_day(value, type) {
        var occurs = [];
        if (value & Alarm.Monday) occurs.push(Qt.locale().standaloneDayName(1, Locale.ShortFormat));
        if (value & Alarm.Tuesday) occurs.push(Qt.locale().standaloneDayName(2, Locale.ShortFormat));
        if (value & Alarm.Wednesday) occurs.push(Qt.locale().standaloneDayName(3, Locale.ShortFormat));
        if (value & Alarm.Thursday) occurs.push(Qt.locale().standaloneDayName(4, Locale.ShortFormat));
        if (value & Alarm.Friday) occurs.push(Qt.locale().standaloneDayName(5, Locale.ShortFormat));
        if (value & Alarm.Saturday) occurs.push(Qt.locale().standaloneDayName(6, Locale.ShortFormat));
        if (value & Alarm.Sunday) occurs.push(Qt.locale().standaloneDayName(7, Locale.ShortFormat));
        return occurs;
    }

    Flickable {
        id: clockAnimationContainer

        clip: true
        anchors.fill: parent
        contentWidth: parent.width
        interactive: modelCount == 0 ? false : true
        contentHeight: alarmFace.height + alarmFace.anchors.topMargin + nameAlarm.height + nameAlarm.anchors.topMargin + listAlarm.height + listAlarm.anchors.topMargin + units.gu(2)

        AlarmFace {
            id: alarmFace

            states: [
                State {
                    name: "NOACTIVEALARMS"
                    when: enabledAlarmCount === 0 && modelCount !== 0
                    PropertyChanges { target: alarmFace.nextAlarmText; text: i18n.tr("No Active Alarms"); fontSize: "large"; anchors.verticalCenterOffset: 0 }
                    PropertyChanges { target: alarmFace.innerLabel; visible: false }
                    PropertyChanges { target: alarmFace.currentTimeLabel; visible: false }
                }
            ]

            anchors { top: parent.top; topMargin: units.gu(10); horizontalCenter: parent.horizontalCenter }
            currentTimeLabel.text: i18n.tr("Now") + " " + currentTime
            showSetupMessage: modelCount === 0
            onTimeFormatChanged: get_next_active_alarm()
            onPressAndHold: pagestack.push(Qt.resolvedUrl("AddAlarmPage.qml"), {"isNewAlarm": true})
        }

        // Label to show the name of the next active alarm
        Label {
            id: nameAlarm

            fontSize: "large"
            font.bold: true

            anchors {
                horizontalCenter: alarmFace.horizontalCenter
                top: alarmFace.bottom
                topMargin: units.gu(5)
            }
        }

        Label {
            id: alarmOccurrence
            anchors {
                horizontalCenter: alarmFace.horizontalCenter
                top: nameAlarm.bottom
            }
        }

        // Column element to hold the saved alarms
        Column {
            id: listAlarm

            property int dynamicTopSpacing: alarmPage.height - alarmPage.header.height - alarmFace.height - savedAlarmHeader.height - alarmFace.anchors.topMargin - divider.height;

            height: childrenRect.height
            visible: listSavedAlarm.count != 0 ? true : false
            anchors { left: parent.left; right: parent.right; top: alarmFace.bottom; topMargin: listAlarm.dynamicTopSpacing }

            ListItem.ThinDivider { id: divider }

            ListItem.Header {
                id: savedAlarmHeader
                Label {
                    text: i18n.tr("Alarms")
                    anchors { verticalCenter: parent.verticalCenter; left: parent.left; leftMargin: units.gu(2) }
                    color: Theme.palette.normal.baseText
                    fontSize: "medium"
                }
            }

            ListView {
                id: listSavedAlarm
                objectName: "listSavedAlarm"

                clip: true
                anchors { left: parent.left; right: parent.right }
                height: 4 * units.gu(6) + units.gu(1)
                model: alarmModel
                currentIndex: -1

                delegate: ListItem.Base {
                    objectName: "alarm" + index
                    Label {
                        id: alarmTime
                        objectName: "listAlarmTime" + index
                        fontSize: "large";
                        text: Utils.convertTime(date.getHours(), date.getMinutes(), 0, appSetting.contents.timeFormat)
                        anchors { verticalCenter: parent.verticalCenter; right: alarmStatus.left; rightMargin: units.gu(2) }
                        color: Theme.palette.normal.baseText
                    }

                    Label {
                        id: alarmLabel
                        objectName: "listAlarmLabel" + index
                        fontSize: "large";
                        text: message
                        elide: Text.ElideRight
                        anchors {
                            left: parent.left;
                            right: alarmTime.left
                            rightMargin: units.gu(2)
                            leftMargin: units.gu(1);
                            verticalCenter: alarmTime.top;
                            verticalCenterOffset: units.gu(0.4)
                        }
                        color: Theme.palette.normal.baseText
                    }

                    Label {
                        id: alarmSubtitle
                        objectName: "listAlarmSubtitle" + index
                        fontSize: "small";
                        text: format_day_string(daysOfWeek, type)
                        anchors { left: alarmLabel.left; top: alarmLabel.bottom }
                        color: Theme.palette.normal.backgroundText
                    }

                    Switch {
                        id: alarmStatus
                        objectName: "listAlarmStatus" + index
                        checked: model.enabled
                        anchors { right: parent.right; verticalCenter: alarmTime.verticalCenter }
                        onClicked: {
                            var alarm = alarmModel.get(index);
                            alarm.enabled = checked
                            alarm.save();
                        }
                    }

                    selected: listSavedAlarm.currentIndex == index
                    removable: true
                    confirmRemoval: true

                    onItemRemoved: {
                        var alarm = alarmModel.get(index)
                        alarm.cancel()
                    }

                    onClicked: pagestack.push(Qt.resolvedUrl("AddAlarmPage.qml"), {"isNewAlarm": false, "alarmIndex": index})
                }
            }
        }
    }

    tools: ToolbarItems {
        id: toolbarAlarm

        ToolbarButton {
            id: settings
            action: appSettingsAction
        }

        ToolbarButton {
            id: addPresetToolbarButton
            objectName: "addAlarmButton"
            action: addAlarmAction
        }
    }
}