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