Qt World Summit Conference App
import Felgo
import QtQuick
import "../common"
import "."
Item {
id: dataModel
property alias dispatcher: logicConnection.target
property var speakers: undefined
property var talks: undefined
property var schedule: undefined
property var tracks: undefined
property var eventData
property int localAppStarts: 0
property bool feedBackSent: false
property bool notificationsEnabled: true
property var favorites: undefined
property var userStorage: undefined
readonly property alias loading: api.loading
readonly property bool loaded: !!speakers && !!talks
property bool initialized: false
onNotificationsEnabledChanged: localStorage.setValue("notificationsEnabled", notificationsEnabled)
onTalksChanged: {
dataModel.favoritesChanged()
}
signal loadingFailed()
signal favoriteAdded(var talk)
signal favoriteRemoved(var talk)
function loadStorageApiData() {
let eventData = localStorage.getValue("eventData") || {}
let viewData = viewHelper.prepareData(eventData)
dataModel.talks = viewData.talks
dataModel.speakers = viewData.speakers
dataModel.schedule = viewData.schedule
dataModel.tracks = viewData.tracks
}
Connections {
id: logicConnection
function onInitializeDataModel(userStorageItem) {
dataModel.userStorage = userStorageItem
dataModel.favorites = userStorage.getValue("favorites")
dataModel.notificationsEnabled = localStorage.getValue("notificationsEnabled") !== undefined ? localStorage.getValue("notificationsEnabled") : true
dataModel.localAppStarts = localStorage.getValue("localAppStarts") || 0
dataModel.feedBackSent = localStorage.getValue("feedBackSent") || false
dataModel.initialized = true
loadStorageApiData()
}
function onIncreaseLocalAppStarts() {
if(!initialized)
return
localAppStarts++
localStorage.setValue("localAppStarts",localAppStarts)
}
function onLoadData() {
if(initialized && !loading) {
api.loadData()
}
}
function onSetFeedBackSent(value) {
if(!initialized)
return
feedBackSent = !!value
localStorage.setValue("feedBackSent", feedBackSent)
}
function onStoreRating() {
if(!ratings) ratings = {}
ratings[id] = rating
localStorage.setValue("ratings", ratings)
}
function onToggleFavorite(item) {
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)
}
userStorage.setValue("favorites", dataModel.favorites)
favoritesChanged()
}
function onClearCache() {
var favorites = dataModel.favorites
_.reset()
dataModel.favorites = favorites
localStorage.clearAll()
initialized = true
}
function onSetNotificationsEnabled() {
notificationsEnabled = value
}
}
QtWSApi {
id: api
onEventDataLoaded: eventData => {
let viewData = viewHelper.prepareData(eventData)
dataModel.talks = viewData.talks
dataModel.speakers = viewData.speakers
dataModel.schedule = viewData.schedule
dataModel.tracks = viewData.tracks
}
}
Storage {
id: localStorage
databaseName: "localStorage"
}
Item {
id: _
function reset() {
dataModel.initialized = false
dataModel.eventData = undefined
dataModel.speakers = undefined
dataModel.talks = undefined
dataModel.tracks = undefined
dataModel.schedule = undefined
dataModel.favorites = undefined
dataModel.notificationsEnabled = true
localAppStarts = 0
feedBackSent = false
}
}
function isFavorite(id) {