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