Friendiqa/source-linux/qml/newsqml/NewsStack.qml

421 lines
16 KiB
QML

// This file is part of Friendiqa
// https://git.friendi.ca/lubuwest/Friendiqa
// Copyright (C) 2017 Marco R. <thomasschmidt45@gmx.net>
//
// 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.
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations including
// the two.
//
// You must obey the GNU General Public License in all respects for all
// of the code used other than OpenSSL. If you modify file(s) with this
// exception, you may extend this exception to your version of the
// file(s), but you are not obligated to do so. If you do not wish to do
// so, delete this exception statement from your version. If you delete
// this exception statement from all source files in the program, then
// also delete it here.
//
// 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 QtQuick 2.11
import QtQuick.Controls 2.4
import "qrc:/js/news.js" as Newsjs
import "qrc:/js/helper.js" as Helperjs
import "qrc:/js/service.js" as Service
StackView{
id: newsStack
anchors.fill: parent
property string updateMethodNews: "refresh"
property var allchats: ({})
signal replySignal(var newsobject)
property int lastnewsid:0
function newstypeHandling(newstype){
newsBusy.running=true;
replySignal("");
//messagesend.state="";
newsModel.clear();
switch(newstype){
case "timeline":
newstab.newstabstatus="Timeline";
try{ Newsjs.newsfromdb(root.db,root.login,0, function(dbnews,lastid){
lastnewsid=lastid;
showNews(dbnews)
})}catch(e){Helperjs.showMessage("Error",e,root)};
break;
case "conversation":
newstab.newstabstatus="Conversations";
Newsjs.chatsfromdb(root.db,root.login,0,function(news,lastid){
lastnewsid=lastid;
showNews(news)});
break;
case "favorites":
newsStack.updateMethodNews="refresh";
newstab.newstabstatus="Favorites";
Service.updateView("Favorites");
break;
case "replies":
newsStack.updateMethodNews="refresh";
newstab.newstabstatus="Replies";
Service.updateView("Replies");
break;
case "publictimeline":
newsStack.updateMethodNews="refresh";
newstab.newstabstatus="Public Timeline";
Service.updateView("Public Timeline");
break;
case "groupnews":
newsStack.updateMethodNews="refresh";
Service.showGroups();
break;
case "search":
newsView.anchors.topMargin=7*mm;
newsBusy.running=false;
var component = Qt.createComponent("qrc:/qml/genericqml/Search.qml");
var searchItem = component.createObject(newsStack,{y:mm,width:root.width,height: 5*mm});
break;
case "refresh":
if (newstab.newstabstatus=="Timeline"){
newsStack.updateMethodNews="append"
} else {newsStack.updateMethodNews="refresh"}
//root.contactLoadType="news";
if (newsSwipeview.stacktype=="Home"){
Service.updateView(newstab.newstabstatus)
}
else if (newsSwipeview.stacktype=="DirectMessage"){
Service.updateView("Direct Messages")
}
else if (newsSwipeview.stacktype=="Notifications"){
Service.updateView("Notifications")
}
break;
default:
if (newstab.newstabstatus=="Timeline"){
newsStack.updateMethodNews="append"
} else {newsStack.updateMethodNews="refresh"}
//root.contactLoadType="news";
if (newsSwipeview.stacktype=="Home"){
Service.updateView(newstab.newstabstatus)
}
else if (newsSwipeview.stacktype=="Directmessage"){
Service.updateView("Direct Messages")
}
else if (newsSwipeview.stacktype=="Notifications"){
Service.updateView("Notifications")
}
}
}
function showNews(newsToShow){
try{if (newsStack.depth>1){newsStack.pop()}}catch(e){}
newsBusy.running=false;
var currentTime= new Date();
// downloadNotice.text=downloadNotice.text + "\n shownews start "+ Date.now();
//print("appendnews "+JSON.stringify(newsToShow))
var msg = {'currentTime': currentTime, 'model': newsModel,'news':newsToShow,'method':newsStack.updateMethodNews, 'options':globaloptions};
newsWorker.sendMessage(msg);
//newsStack.appendNews=false
}
function showContact(contact){ //print(JSON.stringify(contact));
//newstab.newstabstatus="Contact";
newsStack.push("qrc:/qml/newsqml/ContactPage.qml",{"contact": contact});
}
function search(term){//print("Search "+term)
if (term!=""){
newstab.newstabstatus="Search";
newsBusy.running=true;
newsStack.updateMethodNews="refresh";
xhr.setLogin(login.username+":"+Qt.atob(login.password));
xhr.setUrl(login.server);
xhr.setApi("/api/search");
xhr.clearParams();
xhr.setParam("q",term)
xhr.get();}
newsView.anchors.topMargin=mm
}
function getOldNews(){
var currentTime= new Date();
try{var lastnews_id=newsModel.get(newsModel.count-1).newsitemobject.created_at;} catch(e){var lastnews_id=99999999999999 }
var messagetype=0;
switch(newsSwipeview.stacktype){
case "Home":messagetype=0;break;
case "DirectMessages": messagetype=1;break;
case "Notifications":messagetype=2;break;
case "Replies":messagetype=3;break;
default:messagetype=0;
}
if(newstab.newstabstatus=="Timeline"){
Newsjs.newsfromdb(root.db,root.login, messagetype,function(news){
var msg = {'currentTime': currentTime, 'model': newsModel,'news':news,'appendnews':true, 'options':globaloptions};
newsWorker.sendMessage(msg);
},false,lastnews_id)}
if(newstab.newstabstatus=="Conversations"){
Newsjs.chatsfromdb(root.db,root.login, messagetype,function(news){
var msg = {'currentTime': currentTime, 'model': newsModel,'news':news,'appendnews':true, 'options':globaloptions};
newsWorker.sendMessage(msg);
},lastnews_id)}
// else if(newstab.newstabstatus=="Contact"){
// Newsjs.newsfromdb(root.db,root.login, function(news){
// var msg = {'currentTime': currentTime, 'model': newsModel,'news':news,'appendnews':true};
// newsWorker.sendMessage(msg);
// },newsModel.get(newsModel.count-1).newsitemobject.uid,lastnews_id)}
else if (newstab.newstabstatus=="Notifications"){}
else{
//newsStack.appendNews=true;
try{xhr.setParam("max_id",newsModel.get(newsModel.count-1).newsitemobject.id-1);}catch(e){}
xhr.get()
}
}
Connections{
target:xhr
onError:{
Helperjs.showMessage(qsTr("Network Error"),"API:\n" +login.server+api+"\n Return: \n"+data,root);
}
onSuccess:{
// downloadNotice.text=downloadNotice.text+ "\n xhr finished "+Date.now();
Service.processNews(api,data)
replySignal("")
}
}
Timer {id:replytimer; interval: 1000; running: false; repeat: false
onTriggered: {
newsBusy.running=true;
if(newstab.newstabstatus=="Conversation"){
showConversation(newsStack.timelineIndex-1,newsModel.get(0).newsitemobject)}
else{
if (newstab.newstabstatus=="Timeline"){
newsStack.updateMethodNews="append"
} else {newsStack.updateMethodNews="refresh"}
if (newsSwipeview.stacktype=="Home"){
Service.updateView(newstab.newstabstatus)
}
else if (newsSwipeview.stacktype=="DirectMessages"){
Service.updateView("Direct Messages")
}
else if (newsSwipeview.stacktype=="Replies"){
Service.updateView("Replies")
}
replySignal("")
//Service.updateView(newstab.newstabstatus)
}
}
}
initialItem: Rectangle {
id:newslistRectangle
y:1
color: "white"
//anchors.fill:parent
Component { id:footerComponent
Rectangle{
border.color: "#EEEEEE"
border.width: 1
width:newsView.width
height:6*mm
Text{
font.pixelSize: 1.5*mm
anchors.centerIn: parent
text:qsTr("More")
}
MouseArea{
anchors.fill:parent
onClicked:{
if (newsModel.count==0){
if (newsSwipeview.stacktype=="Home"){
Service.updateView(newstab.newstabstatus)
}
else if (newsSwipeview.stacktype=="DirectMessages"){
Service.updateView("Direct Messages")
}
else if (newsSwipeview.stacktype=="Notifications"){
Service.updateView("Notifications")
}
else if (newsSwipeview.stacktype=="Replies"){
Service.updateView("Replies")
}
}
else {getOldNews();}
}
}
}
}
ListView {
id: newsView
property real oldContentY:0
property bool viewdragged: false
property bool viewdraggedpositive: false
property string viewtype: "news"
anchors.fill: parent
anchors.margins: mm
clip: true
spacing: 0
header: MessageSend{id:messagesend;onHeightChanged: newsView.positionViewAtBeginning()}
footer: footerComponent
model: newsModel
delegate: Newsitem{}
onDragStarted: oldContentY=contentY
onDragEnded: {
if(verticalOvershoot<-5*mm){
viewdragged=true
}
else if (verticalOvershoot>5*mm){
viewdraggedpositive=true
}
else{
if((contentY-oldContentY)>15*mm){
swipeIndicator.visible=false;
newsSwipeview.height=rootStackItem.height;
newsSwipeview.y=0;
rootStackItem.state="fullscreen"
}
else if ((contentY-oldContentY)<-15*mm){
swipeIndicator.visible=true;
newsSwipeview.height=rootStackItem.height-12*mm;
newsSwipeview.y=5*mm;
rootStackItem.state=""
}
}
}
onViewdraggedChanged: {
if (viewdragged){
var onlynew=true;
newsBusy.running=true;
viewdragged=false
if (newstab.newstabstatus=="Timeline"){
newsStack.updateMethodNews="append"
} else {newsStack.updateMethodNews="refresh"}
//root.contactLoadType="news";
if (newsSwipeview.stacktype=="Home"){
Newsjs.getLastNews(root.login,root.db,function(currentlastnews){
if (currentlastnews>lastnewsid){
if(newstab.newstabstatus=="Timeline"){
try{ Newsjs.newsfromdb(root.db,root.login,0, function(dbnews,lastid){
lastnewsid=lastid;
showNews(dbnews)
})}catch(e){Helperjs.showMessage("Error",e,root)};
}
if(newstab.newstabstatus=="Conversations"){
Newsjs.chatsfromdb(db,root.login,0,function(news,lastid){
lastnewsid=lastid;
showNews(news)});
}
} else {
Service.updateView(newstab.newstabstatus)
}
});
}
else if (newsSwipeview.stacktype=="DirectMessages"){
Service.updateView("Direct Messages")
}
else if (newsSwipeview.stacktype=="Notifications"){
Service.updateView("Notifications")
}
else if (newsSwipeview.stacktype=="Replies"){
Service.updateView("Replies")
}
}
}
onViewdraggedpositiveChanged: {
if (viewdraggedpositive){
getOldNews();
viewdraggedpositive=false
}
}
}
ListModel{id: newsModel}
WorkerScript {
id: newsWorker
source: "qrc:/js/newsworker.js"
}
BusyIndicator{
id: newsBusy
anchors.horizontalCenter: parent.horizontalCenter
anchors.top:parent.top
anchors.topMargin: 2*mm
width:10*mm
height: 10*mm
}
Rectangle{
id:downloadNotice
property alias text: noticeText.text
color:"white"
border.color:"grey"
z:1
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom:parent.bottom
anchors.bottomMargin: 2*mm
width: noticeText.width+2*mm
height: noticeText.height+2*mm
visible: (downloadNotice.text!="")
Text{
id:noticeText
color: "grey"
anchors.centerIn: parent
width: contentWidth
height: contentHeight
font.pixelSize: 2*mm
text:""
}
}
Component.onCompleted: {
//print(newsSwipeview.stacktype);
root.newstypeSignal.connect(newstypeHandling);
root.messageSignal.connect(onFriendsMessages);
root.contactdetailsSignal.connect(showContact);
root.newsSignal.connect(showNews);
try{newsModel.clear()} catch(e){}
swipeIndicator.visible=true;
newsSwipeview.height=rootStackItem.height-12*mm;
newsSwipeview.y=5*mm;
rootStackItem.state=""
login.hasOwnProperty("newsViewType")?newstab.newstabstatus=login.newsViewType:"Conversations"
var messagetype=0;
switch(newsSwipeview.stacktype){
case "Home":messagetype=0;break;
case "DirectMessages": messagetype=1;break;
case "Notifications":messagetype=2;break;
case "Replies":messagetype=3;break;
default:messagetype=0;
}
if((login.newsViewType=="Conversations")&&(newsSwipeview.stacktype=="Home")){
Newsjs.chatsfromdb(db,login,messagetype,function(dbnews,lastid){
lastnewsid=lastid;
showNews(dbnews);
})
}
else{Newsjs.newsfromdb(db,login,messagetype,function(dbnews,lastid){
lastnewsid=lastid;
showNews(dbnews)
})}
//}
}
}
}