Qt World Summit Conference App
import Felgo 4.0
import QtQuick 2.0
import "../common"
Item {
id: dayScheduleItem
property var scheduleData: []
signal itemClicked(var item);
readonly property bool loading: _.loadingCount > 0
property alias emptyText: listView.emptyText
property bool searchAllowed: true
property alias listView: listView
signal searchAccepted(string text)
Component.onCompleted: {
listView.positionViewAtEnd()
listView.positionViewAtBeginning()
updater.updateJumpToNow()
jumpToNow()
}
Item {
id: _
property int loadingCount: 0
}
JumpToNowButton {
id: jumpToNowTop
z: 1
y: show ? dp(5) : -height
opacity: show ? 1 : 0
text: "now"
visible: !Theme.isIos
property bool show: false
onClicked: {
if(text === "now")
jumpToNow()
else
listView.positionViewAtBeginning()
show = false
}
}
JumpToNowButton {
id: jumpToNowBottom
z: 1
y: show ? parent.height - height - dp(5) : parent.height
opacity: show ? 1 : 0
text: "now"
property bool show: false
onClicked: { jumpToNow(); show = false; }
}
Connections {
id: updater
target: listView
onFirstItemIndexChanged: updateJumpToNow()
onLastItemIndexChanged: updateJumpToNow()
function updateJumpToNow() {
if(!scheduleData || scheduleData.length == 0) {
jumpToNowTop.show = false
jumpToNowBottom.show = false
return
}
var now = getCurrentDateTime()
var nowItemIdx = itemIndexForTime(now.getTime())
var nowItemDate = getEventDateTime(scheduleData[nowItemIdx])
if(nowItemDate.getFullYear() !== now.getFullYear() || nowItemDate.getMonth() !== now.getMonth() || nowItemDate.getDate() !== now.getDate())
nowItemIdx = 0
if(nowItemIdx === 0)
jumpToNowTop.text = "to top"
else
jumpToNowTop.text = "now"
if(nowItemIdx === (scheduleData.length - 1))
jumpToNowBottom.text = "to bottom"
else
jumpToNowBottom.text = "now"
if(nowItemIdx >= listView.firstItemIndex && nowItemIdx <= listView.lastItemIndex) {
jumpToNowTop.show = false
jumpToNowBottom.show = false
}
else if(nowItemIdx > listView.firstItemIndex && listView.firstItemIndex >= 0) {
jumpToNowTop.show = false
jumpToNowBottom.show = true
}
else if(nowItemIdx < listView.lastItemIndex) {
jumpToNowTop.show = true
jumpToNowBottom.show = false
}
else {
jumpToNowTop.show = false
jumpToNowBottom.show = false
}
}
}
AppListView {
id: listView
width: parent.width
height: parent.height - y
model: JsonListModel {
id: listModel
source: scheduleData
keyField: "id"
}
cacheBuffer: 10000
header: SearchBar {
visible: searchAllowed
height: searchAllowed ? implicitHeight : 0
onAccepted: searchAccepted(text)
iosAlternateStyle: true
}
property int firstItemIndex: nearestIndexAt(listView.width * 0.5, contentY, true)
property int lastItemIndex: nearestIndexAt(listView.width * 0.5, contentY + listView.height, false)
property real relaxationStep: dp(10)
function nearestIndexAt(xPos, yPos, nextFirst) {
var index = listView.indexAt(xPos, yPos)
var relaxationStep = dp(10)
var relaxation = relaxationStep
while(index === -1 && relaxation <= listView.height * 0.5) {
var next = listView.indexAt(listView.width * 0.5, yPos + relaxation)
var prev = listView.indexAt(listView.width * 0.5, yPos - relaxation)
if(nextFirst)
index = next > -1 ? next : prev
else
index = prev > -1 ? prev : next
relaxation += relaxationStep
}
return index
}
function scrollToIndex(idx) {
listView.positionViewAtIndex(idx, ListView.Beginning)
}
delegate: ScheduleListItem {
item: listModel.get(index)
onSelected: dayScheduleItem.itemClicked(item)
parentView: listView
onLoadingChanged: {
if(loading)
_.loadingCount++
else
_.loadingCount--
}
Component.onDestruction: if(loading) _.loadingCount--
}
section.property: "section"
section.delegate: SimpleSection {
style.compactStyle: true
textItem.horizontalAlignment: Text.AlignHCenter
}
emptyText.text: "No events."
}
function toggleFavorite(item) {
logic.toggleFavorite(item)
}
function itemIndexForTime(time) {
var i = -1
for(var idx in scheduleData) {
i++
var data = scheduleData[idx]
var eventTime = getEventDateTime(data).getTime()
if(eventTime >= time) {
break
}
}
return i
}
function getEventDateTime(event) {
var date = new Date(event.day+"T"+event.start+".000"+dataModel.timeZone)
return date
}
function getCurrentDateTime() {
return new Date()
}
function jumpToNow() {
var now = getCurrentDateTime().getTime()
var nowItemIndex = itemIndexForTime(now)
if(nowItemIndex >= 0)