~marcinello/ubuntu-rssreader-app/fix-1297463

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem
import Ubuntu.Components.Popups 0.1

import "databasemodule_v2.js" as DB
import "listview"

Tab {
    id: topicTab

    property int topicId: -1
    property bool isListMode: pageStack.isListView

    property bool isActive: false

    page: isListMode ? listPage : gridPage

    onIsListModeChanged: {
        if (topicId == -1) // Ignore first undefined state.
            return

        reload()
    }

    function reload() {
        // console.log('TopicTab reload', topicId)
        clear()

        var articlesByTopic = []
        var topicArticles = DB.loadArticles({"tagId" : topicId})

        for (var j = 0; j < topicArticles.rows.length; j++) {
            var ca = topicArticles.rows.item(j)

            var anObj = {"tagName" : "",
                "tagId" : topicId,
                "title" : ca.title,
                "content" : ca.content,
                "link" : ca.link,
                "description" : ca.description,
                "pubdate" : ca.pubdate,
                "status" : ca.status, // ?
                "favourite" : ca.favourite,
                "feedId" : ca.feed_id,
                "image" : ca.image,
                "feed_name" : ca.feed_name,
                "media_groups" : ca.media_groups,
                "id" : ca.id }

            articlesByTopic.push(anObj)
        }

        // Sort for list mode.
        if (isListMode) {
            articlesByTopic.sort(function(a,b) {
                if (a.feedId === b.feedId)
                    return b.pubdate - a.pubdate
                else return b.feedId - a.feedId
            })
        }

        for(var i = 0; i < articlesByTopic.length; i++)
            commonModel.append(articlesByTopic[i])
    }

    function clear() {
        isActive = false
        gridPage.clear()
        listPage.clear()
        commonModel.clear()
    }

    function showContent() {
        console.log("showContent: ", topicId, isActive)
        if (isActive)
            return

        isActive = true
        if (isListMode)
            listPage.reload()
        else gridPage.reload()
    }

    /* All views should use this method to access shared model.
     */
    function getModel() {
        return commonModel
    }

    function updateStatusInModel(articleId, status) {
        for ( var i = 0; i < commonModel.count; i++) {
            if (commonModel.get(i).id == articleId) {
                commonModel.get(i).status = status
                break
            }
        }
    }

    function updateFavouriteInModel(articleId, fav) {
        for ( var i = 0; i < commonModel.count; i++) {
            if (commonModel.get(i).id == articleId) {
                commonModel.get(i).favourite = fav
                return
            }
        }
    }

    ListModePage {
        id: listPage

        visible: isListMode
        topicId: topicTab.topicId

        tools: mainCommonToolbar
    }

    GridModePage {
        id: gridPage

        visible: !isListMode
        topicId: topicTab.topicId

        tools: mainCommonToolbar
    }

    ListModel {
        id: commonModel
    }

    Label {
        id: lblEmptyBase

        text: i18n.tr("There are no articles to show")
        anchors.centerIn: parent
        visible: commonModel.count == 0
        fontSize: "large"
    }
}