This repository has been archived on 2023-11-19. You can view files and clone it, but cannot push or open issues or pull requests.
goober-bb10/assets/StreamTab.qml

379 lines
13 KiB
QML
Raw Normal View History

2016-09-22 23:21:20 +00:00
/*
* Copyright (C) 2016 Morgan McMillian <gilag@monkeystew.com>
*
* This file is apart of the Goober application, a client for pnut.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import bb.cascades 1.4
import com.monkeystew.pnut 1.0
import com.monkeystew.qtimer 1.0
NavigationPane {
id: nav
property alias title: titleBar.title
2016-09-22 23:21:20 +00:00
property string stream_type
property string endpoint
2016-10-21 19:24:29 +00:00
property int unread: -1
property bool refreshpull: false
2016-10-21 23:53:19 +00:00
property bool active
2016-10-30 22:52:29 +00:00
property bool at_end: false
2016-10-21 23:53:19 +00:00
signal openLogin()
signal closeLogin()
2016-09-22 23:21:20 +00:00
Page {
titleBar: TitleBar {
id: titleBar
}
2016-09-22 23:21:20 +00:00
Container {
ListView {
id: streamView
dataModel: ArrayDataModel {
id: postModel
}
leadingVisual: RefreshItem {
onRefresh: {
refreshpull = true
pnut.getStream(endpoint, Pnut.STREAM_NEWER);
}
}
2016-09-22 23:21:20 +00:00
listItemComponents: [
ListItemComponent {
id: root
PostItem {}
}
]
2016-09-24 15:13:45 +00:00
onTriggered: {
2016-10-30 16:13:53 +00:00
if (!postModel.data(indexPath).is_deleted) {
var page = threadPage.createObject();
pnut.threadReceived.connect(page.loadThread);
pnut.getThread(postModel.data(indexPath).thread_id);
page.follow.connect(pnut.followUser)
page.unfollow.connect(pnut.unfollowUser)
page.block.connect(pnut.blockUser)
page.unblock.connect(pnut.unblockUser)
page.mute.connect(pnut.muteUser)
page.unmute.connect(pnut.unmuteUser)
nav.push(page);
}
2016-09-24 15:13:45 +00:00
}
2016-10-21 13:09:34 +00:00
function viewProfile(userobj) {
var page = profilePage.createObject();
page.user = userobj
//pnut.userStreamReceived.connect(page.loadThread)
//pnut.getUserStream(userobj.id)
2016-10-21 13:09:34 +00:00
page.follow.connect(pnut.followUser)
page.unfollow.connect(pnut.unfollowUser)
page.block.connect(pnut.blockUser)
page.unblock.connect(pnut.unblockUser)
page.mute.connect(pnut.muteUser)
page.unmute.connect(pnut.unmuteUser)
//page.getPosts.connect(pnut.getUserStream)
2016-10-21 13:09:34 +00:00
nav.push(page);
}
2016-09-22 23:21:20 +00:00
function sendReply(text, pid) {
pnut.sendReply(text, pid);
}
function getUserName() {
2016-09-24 15:13:45 +00:00
return _app.setting("username");
}
function addBookmark(pid) {
pnut.setBookmark(pid);
}
function delBookmark(pid) {
pnut.deleteBookmark(pid);
}
function repost(pid) {
pnut.repost(pid);
}
function delRepost(pid) {
pnut.deleteRepost(pid);
}
function delPost(pid) {
pnut.deletePost(pid);
}
2016-10-30 17:53:58 +00:00
function getPost(pid) {
pnut.getPost(pid)
}
2016-11-04 21:16:03 +00:00
function ccOnReply() {
if (_app.setting("cc") === "true") {
return true
} else {
return false
}
}
function copyText(text) {
_app.copyText(text)
}
function showImage(filename, mtype) {
console.log('-- showImage --')
console.log(filename)
_app.showImage(filename, mtype)
}
2016-10-21 19:24:29 +00:00
attachedObjects: [
ListScrollStateHandler {
onAtEndChanged: {
if (atEnd) {
2016-10-30 22:52:29 +00:00
if (!at_end && pnut.beforeId != 0) {
pnut.getStream(endpoint, Pnut.STREAM_OLDER);
}
2016-10-21 19:24:29 +00:00
}
}
onAtBeginningChanged: {
if (atBeginning) {
unread = 0
nav.parent.unreadContentCount = unread
}
}
}
]
2016-09-22 23:21:20 +00:00
}
}
actions: [
ActionItem {
title: qsTr("New Post")
ActionBar.placement: ActionBarPlacement.Signature
onTriggered: {
newPostSheet.open();
newPostSheet.input.requestFocus();
}
2016-10-21 13:09:34 +00:00
imageSource: "asset:///icons/ic_compose.png"
2016-10-21 19:24:29 +00:00
shortcuts: [
SystemShortcut {
type: SystemShortcuts.CreateNew
}
]
2016-09-22 23:21:20 +00:00
},
ActionItem {
2016-10-21 19:24:29 +00:00
title: qsTr("To Top")
2016-09-22 23:21:20 +00:00
ActionBar.placement: ActionBarPlacement.InOverflow
onTriggered: {
2016-10-21 19:24:29 +00:00
//refreshpull = true
//pnut.getStream(endpoint, Pnut.STREAM_NEWER);
streamView.scrollToPosition(ScrollPosition.Beginning, ScrollAnimation.None);
2016-09-22 23:21:20 +00:00
}
imageSource: "asset:///icons/ic_to_top.png"
2016-10-21 19:24:29 +00:00
shortcuts: [
SystemShortcut {
type: SystemShortcuts.JumpToTop
}
]
2016-09-22 23:21:20 +00:00
},
ActionItem {
title: qsTr("Reload")
ActionBar.placement: ActionBarPlacement.InOverflow
onTriggered: {
reload()
2016-09-22 23:21:20 +00:00
}
imageSource: "asset:///icons/ic_reload.png"
shortcuts: [
Shortcut {
key: "l"
}
]
2016-09-22 23:21:20 +00:00
},
ActionItem {
2016-10-21 19:24:29 +00:00
title: qsTr("To Bottom")
2016-09-22 23:21:20 +00:00
ActionBar.placement: ActionBarPlacement.InOverflow
onTriggered: {
streamView.scrollToPosition(ScrollPosition.End, ScrollAnimation.None);
2016-10-21 19:24:29 +00:00
//pnut.getStream(endpoint, Pnut.STREAM_OLDER);
2016-09-22 23:21:20 +00:00
}
imageSource: "asset:///icons/ic_to_bottom.png"
2016-10-21 19:24:29 +00:00
shortcuts: [
SystemShortcut {
type: SystemShortcuts.JumpToBottom
}
]
2016-09-22 23:21:20 +00:00
}
]
actionBarAutoHideBehavior: ActionBarAutoHideBehavior.HideOnScroll
actionBarVisibility: ChromeVisibility.Overlay
2016-09-22 23:21:20 +00:00
}
attachedObjects: [
2016-09-24 15:13:45 +00:00
ComponentDefinition {
id: threadPage
source: "ThreadPage.qml"
},
2016-10-21 13:09:34 +00:00
ComponentDefinition {
id: profilePage
source: "ProfilePage.qml"
},
2016-09-22 23:21:20 +00:00
NewPostSheet {
id: newPostSheet
onSendPost: {
2016-10-21 23:53:19 +00:00
console.log("send: " + text)
pnut.sendPost(text)
2016-10-01 18:18:40 +00:00
}
},
WebViewSheet {
id: webViewSheet
},
2016-09-22 23:21:20 +00:00
QTimer {
id: timer
interval: 30000
onTimeout: {
pnut.getStream(endpoint, Pnut.STREAM_NEWER);
}
},
Pnut {
id: pnut
onStreamReceived: {
2016-10-30 16:13:53 +00:00
2016-10-30 22:52:29 +00:00
// stream.forEach(function(post) {
// if (post.is_deleted) {
// console.log("got a deleted post")
// }
// })
2016-10-30 16:13:53 +00:00
2016-09-22 23:21:20 +00:00
switch (rtype) {
case Pnut.STREAM_OLDER:
2016-10-21 13:09:34 +00:00
//streamView.scrollToPosition(ScrollPosition.End, ScrollAnimation.None);
2016-10-30 22:52:29 +00:00
if (stream.length < 50) {
at_end = true
}
2016-09-22 23:21:20 +00:00
postModel.append(stream);
break;
case Pnut.STREAM_NEWER:
postModel.insert(0, stream);
2016-10-21 19:24:29 +00:00
if (stream.length > 0) {
update_app_cover(stream.length)
}
if (refreshpull) {
refreshpull = false
streamView.scrollToPosition(ScrollPosition.Beginning, ScrollAnimation.None);
}
2016-09-22 23:21:20 +00:00
break;
}
pnut.beforeId = postModel.value(postModel.size() - 1).id;
pnut.sinceId = postModel.value(0).id;
}
2016-10-30 17:53:58 +00:00
onPostReceived: {
var idx
for (var i = 0; i< postModel.size(); i++) {
if (postModel.value(i).id === post.id) {
idx = i
break
}
}
postModel.replace(idx, post)
}
2016-09-24 15:13:45 +00:00
2016-10-01 18:18:40 +00:00
onAuthorizationRequired: {
2016-10-21 23:53:19 +00:00
console.log("__Got a auth required thingy")
openLogin()
timer.stop();
2016-10-01 18:18:40 +00:00
}
2016-09-22 23:21:20 +00:00
onAuthorizationReceived: {
2016-10-21 23:53:19 +00:00
console.log("-- THIS --")
webViewSheet.close()
2016-10-21 23:53:19 +00:00
closeLogin()
2016-09-22 23:21:20 +00:00
pnut.getStream(endpoint, Pnut.STREAM_OLDER);
timer.start();
2016-10-21 23:53:19 +00:00
console.log("__Should be closed now")
2016-09-22 23:21:20 +00:00
}
onOpenBrowser: {
//console.log("-- open browser --")
webViewSheet.url = url
webViewSheet.open()
}
onFollowSuccess: update_user_page(user)
onUnfollowSuccess: update_user_page(user)
onBlockSuccess: update_user_page(user)
onUnblockSuccess: update_user_page(user)
onMuteSuccess: update_user_page(user)
onUnmuteSuccess: update_user_page(user)
function update_user_page(user) {
nav.top.user = user
reload()
}
2016-10-21 19:24:29 +00:00
function update_app_cover(count) {
if (count > 0 && unread >=0) {
unread += count
} else {
unread = 0
}
nav.parent.unreadContentCount = unread
}
2016-09-22 23:21:20 +00:00
}
]
onPopTransitionEnded: {
page.destroy();
}
onCreationCompleted: {
setEndpoint()
2016-09-22 23:21:20 +00:00
if (_app.setting("access_token") && _app.setting("access_token").length > 0) {
2016-10-21 23:53:19 +00:00
console.log("__I think I'm authenticated")
pnut.getUserInfo()
pnut.getStream(endpoint, Pnut.STREAM_OLDER)
2016-09-22 23:21:20 +00:00
timer.start();
} else {
2016-10-21 23:53:19 +00:00
console.log("__I am not authenticated yet")
openLogin()
timer.stop()
2016-09-22 23:21:20 +00:00
}
}
function setEndpoint() {
switch (stream_type) {
case "Home":
var hometype = (_app.setting("unified") === "true") ? "unified" : "me"
endpoint = "/posts/streams/" + hometype
break;
case "Mentions":
endpoint = "/users/me/mentions"
break;
2016-10-30 22:52:29 +00:00
case "Bookmarks":
endpoint = "/users/me/bookmarks"
break;
case "Global":
default:
endpoint = "/posts/streams/global"
break;
}
}
2016-10-21 23:53:19 +00:00
function login() {
pnut.authorize()
}
2016-10-01 18:18:40 +00:00
function logout() {
2016-10-21 23:53:19 +00:00
pnut.logout()
2016-10-01 18:18:40 +00:00
}
function reload() {
setEndpoint()
postModel.clear()
pnut.beforeId = 0
pnut.sinceId = 0
pnut.getStream(endpoint, Pnut.STREAM_OLDER);
}
function createPost(text) {
newPostSheet.open()
newPostSheet.text = text
newPostSheet.input.requestFocus();
}
2016-09-22 23:21:20 +00:00
}