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