~aaronhoneycutt/ureadit/ureadit-personal

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
import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Components.ListItems 1.0
import "../models/QReddit"
import "../components"
import "../utils/Autolinker.js" as AutoLinkText
import "../models/QReddit/QReddit.js" as QReddit

Page {
    id: commentsPage
    property var postObj

    title: postObj.data.title

    head.contents: Label {
        text: title
        height: parent.height
        width: parent.width
        verticalAlignment: Text.AlignVCenter

        fontSize: "x-large"
        fontSizeMode: Text.Fit

        maximumLineCount: 3
        minimumPointSize: 8
        elide: Text.Right
        wrapMode: Text.WordWrap
    }

    head.actions: [
        Action {
            id: replyAction
            text: "Reply"
            iconName: "new-message"
            enabled: uReadIt.qreddit.notifier.isLoggedIn
            onTriggered: {
                var postReplyObj = new QReddit.PostObj(uReadIt.qreddit, postObj)
                mainStack.push(Qt.resolvedUrl("PostMessagePage.qml"), {'replyToObj': postReplyObj})
            }
        }

    ]

    Keys.onPressed: {
        if (event.key == Qt.Key_Home) { commentsList.contentY = 0; return; }

        if (event.key == Qt.Key_PageDown && !commentsList.atYEnd) {
            var nextY = commentsList.contentY + (commentsList.height/2);
            commentsList.contentY = nextY
            return;
        }
        if (event.key == Qt.Key_PageUp && !commentsList.atYBeginning) {
            var prevY = commentsList.contentY - (commentsList.height/2);
            if (prevY < 0) {
                prevY = 0;
            }
            commentsList.contentY = prevY
            return;
        }

    }

    UbuntuListView {
        id: commentsList
        anchors.fill: parent

        Behavior on contentY {
                SmoothedAnimation { duration: 500 }
        }

        model: PostCommentsListModel {
            id: commentsModel
            post: commentsPage.postObj.data.id
        }

        header:  PostMessageItem {
            postObj: commentsPage.postObj
            color: uReadIt.currentTheme.commentBackgroundColorOdd
            onLinkActivated: uReadIt.openUrl(link);
        }

        delegate: CommentListItem {
            postObj: commentsPage.postObj
            commentObj: new QReddit.CommentObj(uReadIt.qreddit, model);
            color: (index % 2 == 0) ? uReadIt.currentTheme.commentBackgroundColorEven : uReadIt.currentTheme.commentBackgroundColorOdd
            score: model.data.score
            likes: model.data.likes

            onLinkActivated: uReadIt.openUrl(link);

            onUpvoteClicked: {
                if (!uReadIt.qreddit.notifier.isLoggedIn) {
                    console.log("You can't vote when you're not logged in!");
                    return;
                }

                var voteConnObj = commentObj.upvote();
                var commentItem = this;
                voteConnObj.onSuccess.connect(function(response){
                    commentItem.likes = model.data.likes = commentObj.data.likes;
                    commentItem.score = model.data.score = commentObj.data.score;
                });
            }
            onDownvoteClicked: {
                if (!uReadIt.qreddit.notifier.isLoggedIn) {
                    console.log("You can't vote when you're not logged in!");
                    return;
                }
                var voteConnObj = commentObj.downvote();
                var commentItem = this;
                voteConnObj.onSuccess.connect(function(response){
                    commentItem.likes = model.data.likes = commentObj.data.likes;
                    commentItem.score = model.data.score = commentObj.data.score;
                });
            }

            onReplyClicked: {
                if (!uReadIt.qreddit.notifier.isLoggedIn) {
                    console.log("You can't reply when you're not logged in!");
                    return;
                }
                mainStack.push(Qt.resolvedUrl("PostMessagePage.qml"), {'replyToObj': commentObj})
            }
        }
    }

    head.locked: uReadIt.height < units.gu(70) ? false : true
    head.onLockedChanged: if (head.locked) head.visible = true;
    flickable: commentsList//uReadIt.height < units.gu(70) ? commentsList : null

    ActivityIndicator {
        id: loadingIndicator
        anchors.centerIn: parent
        running: commentsModel.loading
    }
}