~qqworini/ubuntu-rssreader-app/uitk-1_3

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import QtQuick 2.3
import Ubuntu.Components 1.1
import Ubuntu.Components.ListItems 1.0 as ListItem
import Ubuntu.Components.Popups 1.0

import "databasemodule_v2.js" as DB

Tab {
    id: baseTab

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

    property Item listPage: listModePage
    property Item gridPage: gridModePage

    /* Tab displays its contents only when selected. */
    property bool __isActive: false

    page: Page {
        id: contentPage

        head.actions: pageStack ? pageStack.commonHeadActions : null

        ListModePage {
            id: listModePage

            visible: isListMode
            anchors.fill: parent
            topicId: baseTab.topicId
        }

        GridModePage {
            id: gridModePage

            visible: !isListMode
            anchors.fill: parent
            topicId: baseTab.topicId
        }
    }

    onIsListModeChanged: modeChangeHandler()

    function modeChangeHandler() {
        if (topicId == -1 || !__isActive) // Ignore first undefined state.
            return

        reloadTab("modeChangeHandler")
    }

    /* Tab will be reloaded if it wasn't activated before or if there is
     * well known purpose for it.
     */
    function reloadTab(purpose) {
        console.log("BaseTab reloading:", topicId, __isActive, purpose)
        if (__isActive && !purpose)
            return

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

    function reloadInternal() {
        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,
                "author": ca.author,
                "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
            })
        }

        var commonModel = getModel()
        // Works ~2 times faster.
        commonModel.append(articlesByTopic)
//        for(var i = 0; i < articlesByTopic.length; i++)
//            commonModel.append(articlesByTopic[i])
    }

    function clear() {
        __isActive = false
        gridPage.clear()
        listPage.clear()
        getModel().clear()
    }

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

    function updateStatusInModel(articleId, status) {
        var usedModel = getModel()

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

    function updateFavouriteInModel(articleId, fav) {
        var usedModel = getModel()

        for ( var i = 0; i < usedModel.count; i++) {
            if (usedModel.get(i).id === articleId) {
                usedModel.get(i).favourite = fav
                break
            }
        }
    }

    ListModel {
        id: commonSharedModel
    }

    Label {
        id: lblEmptyBase

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