Fix error reporting in the station page
[quandoparte] / application / resources / harmattan / qml / StationPage.qml
1 import QtQuick 1.1
2 import com.nokia.meego 1.0
3 import net.cirulla.quandoparte 1.0
4 import "uiconstants.js" as UiConstants
5
6 Page {
7     property alias name: schedule.name
8     property alias code: schedule.code
9
10     tools: ToolBarLayout {
11         id: toolBar
12         ToolIcon { iconId: "icon-m-toolbar-back"; onClicked: pageStack.pop() }
13         ToolIcon { iconId: "icon-m-toolbar-refresh"; onClicked: updateStation() }
14         ToolIcon { iconId: "icon-m-toolbar-view-menu"; onClicked: menu.open() }
15     }
16     PageHeader {
17         id: header
18         anchors.top: parent.top
19         selectedIndex: schedule.type
20         options: [
21             qsTr("Departures"),
22             qsTr("Arrivals")
23         ]
24     }
25     InfoBar {
26         id: infoBar
27         anchors.top: header.bottom
28         text: parent.name
29     }
30     Binding {
31         target: schedule
32         property: "type"
33         value: header.selectedIndex
34     }
35     LabelStyle {
36         id: labelStyle
37     }
38     Item {
39         id: view
40         anchors {
41             top: infoBar.bottom
42             bottom: parent.bottom
43             left: parent.left
44             right: parent.right
45         }
46         DroppedShadow {
47             id: shadow
48             anchors.top: view.top
49         }
50         ListView {
51             id: stationScheduleView
52             clip: true
53             visible: false
54             width: parent.width
55             cacheBuffer: 10
56             anchors {
57                 top: shadow.top
58                 bottom: parent.bottom
59             }
60             model: schedule
61             delegate: StationScheduleDelegate {
62                 width: stationScheduleView.width
63                 type: schedule.type
64                 arrivalTime: model.arrivalTime
65                 departureTime: model.departureTime
66                 train: model.train
67                 arrivalStation: model.arrivalStation
68                 departureStation: model.departureStation
69                 delay: model.delay
70                 actualPlatform: model.actualPlatform
71                 expectedPlatfrom: model.expectedPlatform
72             }
73         }
74         ScrollDecorator {
75             id: decorator
76             flickableItem: stationScheduleView
77         }
78         BusyIndicator {
79             id: busyIndicator
80             platformStyle: BusyIndicatorStyle {
81                 size: "large"
82             }
83             anchors.centerIn: parent
84             running: visible
85         }
86         Item {
87             id: errorDisplay
88             anchors.centerIn: parent
89             Column {
90                 anchors.centerIn: parent
91                 spacing: UiConstants.DefaultMargin
92                 Text {
93                     text: qsTr("Error!")
94                     width: parent.width
95                     font.pixelSize: UiConstants.HeaderFontPixelSize
96                     font.bold: UiConstants.HeaderFontBoldness
97                     horizontalAlignment: Text.AlignHCenter
98                 }
99                 Text {
100                     text: schedule.error
101                     width: parent.width
102                     font.pixelSize: UiConstants.HeaderFontPixelSize
103                     font.bold: UiConstants.DefaultFontBoldness
104                     horizontalAlignment: Text.AlignHCenter
105                 }
106             }
107         }
108         states: [
109             State {
110                 name: "loading"
111                 when: !completed
112                 PropertyChanges {
113                     target: stationScheduleView
114                     visible: false
115                 }
116                 PropertyChanges {
117                     target: errorDisplay
118                     visible: false
119                 }
120                 PropertyChanges {
121                     target: busyIndicator
122                     visible: true
123                 }
124             },
125             State {
126                 name: "ready"
127                 PropertyChanges {
128                     target: stationScheduleView
129                     visible: true
130                 }
131                 PropertyChanges {
132                     target: errorDisplay
133                     visible: false
134                 }
135                 PropertyChanges {
136                     target: busyIndicator
137                     visible: false
138                 }
139             },
140             State {
141                 name: "error"
142                 when: schedule.error
143                 PropertyChanges {
144                     target: stationScheduleView
145                     visible: false
146                 }
147                 PropertyChanges {
148                     target: errorDisplay
149                     visible: true
150                 }
151                 PropertyChanges {
152                     target: busyIndicator
153                     visible: false
154                 }
155             }
156         ]
157     }
158     StationScheduleModel {
159         id: schedule
160         onNameChanged: updateStation()
161         onLayoutChanged: if (error) view.state = "error"
162                          else view.state = "ready"
163     }
164     Component.onCompleted: {
165         updateTimer.timeout.connect(updateStation)
166         view.state = "loading"
167     }
168     function updateStation() {
169         view.state = "loading"
170         console.log("Updating station with " + schedule.name + ", " + schedule.code)
171         schedule.fetch(schedule.name, schedule.code)
172     }
173 }