Qt World Summit Conference App
import Felgo 3.0
import QtQuick 2.0
Item {
id: dataModel
property alias dispatcher: logicConnection.target
property var version: undefined
property var schedule: undefined
property var speakers: undefined
property var tracks: undefined
property var favorites: undefined
property var talks: undefined
property var contacts: undefined
property var ratings: undefined
property var preparedTracks
property var preparedSpeakers
property int localAppStarts: 0
property bool feedBackSent: false
property string timeZone: "+0100"
property var webStorage: undefined
readonly property bool loading: api.loadingCount > 0
readonly property bool loaded: !!schedule && !!speakers
property bool initialized: false
property bool notificationsEnabled: true
onNotificationsEnabledChanged: storage.setValue("notificationsEnabled", notificationsEnabled)
signal loadingFailed()
signal favoriteAdded(var talk)
signal favoriteRemoved(var talk)
Connections {
id: logicConnection
onInitializeDataModel: {
dataModel.version = storage.getValue("version")
dataModel.schedule = storage.getValue("schedule")
dataModel.speakers = storage.getValue("speakers")
dataModel.tracks = storage.getValue("tracks")
dataModel.talks = storage.getValue("talks")
dataModel.ratings = storage.getValue("ratings")
dataModel.notificationsEnabled = storage.getValue("notificationsEnabled") !== undefined ? storage.getValue("notificationsEnabled") : true
dataModel.localAppStarts = storage.getValue("localAppStarts") || 0
dataModel.feedBackSent = storage.getValue("feedBackSent") || false
dataModel.webStorage = webStorageItem
dataModel.favorites = webStorage.getValue("favorites")
dataModel.contacts = webStorage.getValue("contacts")
if(dataModel.speakers) dataModel.preparedSpeakers = viewHelper.prepareSpeakers(dataModel.speakers)
if(dataModel.tracks) dataModel.preparedTracks = viewHelper.prepareTracks(dataModel.tracks)
dataModel.initialized = true
}
onResetOrInitializeDataModel: {
if(!userInitiallyInSync) {
dataModel.favorites = undefined
dataModel.initialized = false
}
else if(!dataModel.initialized) {
logic.initializeDataModel(webStorage)
}
}
onIncreaseLocalAppStarts: {
if(!initialized)
return
localAppStarts++
storage.setValue("localAppStarts",localAppStarts)
}
onLoadData: {
if(initialized && !loading) {
api.checkAPIVersion()
}
}
onSetFeedBackSent: {
if(!initialized)
return
feedBackSent = !!value
storage.setValue("feedBackSent", feedBackSent)
}
onStoreRating: {
if(!ratings) ratings = {}
ratings[id] = rating
storage.setValue("ratings", ratings)
}
onToggleFavorite: {
if(dataModel.favorites === undefined)
dataModel.favorites = { }
if(dataModel.favorites[item.id]) {
delete dataModel.favorites[item.id]
dataModel.favoriteRemoved(item)
}
else {
dataModel.favorites[item.id] = item.id
dataModel.favoriteAdded(item)
}
webStorage.setValue("favorites", dataModel.favorites, function(data) {
console.debug("server favorites data:", JSON.stringify(data))
if(data["conflict"]) {
dataModel.favorites = data.mergedData
}
})
favoritesChanged()
}
onRemoveContact: {
if(dataModel.contacts !== undefined && dataModel.contacts[id]) {
delete dataModel.contacts[id]
webStorage.setValue("contacts", dataModel.contacts, function(data) {
if(data["conflict"]) {
dataModel.contacts = data.mergedData
}
})
dataModel.contactsChanged()
}
}
onLoadContact: {
api.sendGetRequest(Qt.resolvedUrl("https://www.qtworldsummit.com/2019/api/attendee/show/?id="+id), successHandler, errorHandler)
}
onAddContact: {
if(dataModel.contacts === undefined)
dataModel.contacts = { }
dataModel.contacts[id] = contact
webStorage.setValue("contacts", dataModel.contacts, function(data) {
if(data["conflict"]) {
dataModel.contacts = data.mergedData
}
})
dataModel.contactsChanged()
}
onClearContacts: {
webStorage.setValue("contacts",{})
}
onClearCache: {
var favorites = dataModel.favorites
var contacts = dataModel.contacts
_.reset()
dataModel.favorites = favorites
dataModel.contacts = contacts
storage.clearAll()
initialized = true
}
onSetNotificationsEnabled: {
notificationsEnabled = value
}
}
QtWSApi {
id: api
}
Storage {
id: storage
databaseName: "localStorage"
}
Item {
id: _
function reset() {
dataModel.initialized = false
dataModel.version = undefined
dataModel.schedule = undefined
dataModel.speakers = undefined
dataModel.tracks = undefined
dataModel.talks = undefined
dataModel.ratings = undefined
dataModel.favorites = undefined
dataModel.contacts = undefined
dataModel.notificationsEnabled = true
localAppStarts = 0
feedBackSent = false
}
}
function isFavorite(id) {
return dataModel.favorites !== undefined && dataModel.favorites[id] !== undefined
}
function getRating(id) {
if(!ratings || !(id in ratings)) return -1