V 0.8.0: Merge branch 'development'
[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 import "components"
5
6 Page {
7     SilicaFlickable {
8         id: view
9         pressDelay: 0
10         anchors.fill: parent
11
12         function updateStation() {
13             view.state = "loading"
14             console.log("Updating station with " + schedule.name + ", " + schedule.code)
15             schedule.fetch(schedule.name, schedule.code)
16         }
17
18         PullDownMenu {
19             MenuItem {
20                 text: qsTr("Update Schedule")
21                 onClicked: view.updateStation()
22             }
23             MenuItem {
24                 text: (schedule.type === StationScheduleModel.ArrivalSchedule ?
25                           qsTr("Show Departures") :
26                           qsTr("Show Arrivals"))
27                 onClicked: (schedule.type = schedule.type === StationScheduleModel.ArrivalSchedule ?
28                                StationScheduleModel.DepartureSchedule :
29                                StationScheduleModel.ArrivalSchedule)
30             }
31             MenuLabel {
32                 text: (schedule.type === StationScheduleModel.DepartureSchedule ? qsTr("Departures") : qsTr("Arrivals"))
33             }
34         }
35         PageHeader {
36             id: header
37             title: schedule.name
38         }
39         SilicaListView {
40             id: stationScheduleView
41             anchors.top: header.bottom
42             anchors.bottom: parent.bottom
43             clip: true
44             visible: false
45             width: parent.width
46             cacheBuffer: 4 * Theme.itemSizeExtraLarge
47             model: schedule
48             delegate: StationScheduleDelegate {
49                 width: stationScheduleView.width
50                 type: schedule.type
51                 arrivalTime: model.arrivalTime
52                 departureTime: model.departureTime
53                 train: model.train
54                 arrivalStation: model.arrivalStation
55                 departureStation: model.departureStation
56                 delay: model.delay
57                 actualPlatform: model.actualPlatform
58                 expectedPlatfrom: model.expectedPlatform
59             }
60             ViewPlaceholder {
61                 enabled: stationScheduleView.count === 0
62                 text: qsTr("No trains are scheduled at this time")
63             }
64         }
65         BusyIndicator {
66             id: busyIndicator
67             anchors.centerIn: parent
68             running: visible
69             size: BusyIndicatorSize.Large
70         }
71         Item {
72             id: errorDisplay
73             anchors.fill: parent
74             Column {
75                 anchors.fill: parent
76                 spacing: Theme.paddingLarge
77                 Label {
78                     textFormat: Text.StyledText
79                     wrapMode: Text.WordWrap
80                     text: "<p>" + qsTr("Error!") + "</p><p>" + schedule.error + "</p>"
81                     width: parent.width
82                     font.pixelSize: Theme.fontSizeHuge
83                     horizontalAlignment: Text.AlignHCenter
84                 }
85             }
86         }
87         states: [
88             State {
89                 name: "loading"
90                 when: !completed
91                 PropertyChanges {
92                     target: stationScheduleView
93                     visible: false
94                 }
95                 PropertyChanges {
96                     target: errorDisplay
97                     visible: false
98                 }
99                 PropertyChanges {
100                     target: busyIndicator
101                     visible: true
102                 }
103             },
104             State  {
105                 name: "ready"
106                 PropertyChanges {
107                     target: stationScheduleView
108                     visible: true
109                 }
110                 PropertyChanges {
111                     target: errorDisplay
112                     visible: false
113                 }
114                 PropertyChanges {
115                     target: busyIndicator
116                     visible: false
117                 }
118             },
119             State {
120                 name: "error"
121                 when: schedule.error
122                 PropertyChanges {
123                     target: stationScheduleView
124                     visible: false
125                 }
126                 PropertyChanges {
127                     target: errorDisplay
128                     visible: true
129                 }
130                 PropertyChanges {
131                     target: busyIndicator
132                     visible: false
133                 }
134             }
135         ]
136
137         /*
138         StationScheduleModel {
139             id: schedule
140             onNameChanged: view.updateStation()
141             onLayoutChanged: if (error) view.state = "error"
142                              else view.state = "ready"
143         }
144         */
145         Connections {
146             target: schedule
147             // onNameChanged: view.updateStation()
148             onLayoutChanged: if (schedule.error) view.state = "error"
149                              else view.state = "ready"
150         }
151         Component.onCompleted: {
152             updateTimer.triggered.connect(view.updateStation)
153             view.state = "loading"
154         }
155         VerticalScrollDecorator {}
156     }
157 }