Friendiqa/v0.002/Develop/source-android/qml/newsqml/NewsTab.qml

193 lines
7.3 KiB
QML

import QtQuick 2.0
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.2
import QtQuick.LocalStorage 2.0 as Sql
import "qrc:/js/news.js" as Newsjs
import "qrc:/js/helper.js" as Helperjs
Item {
property string newstabStatus:"news"
function showNews(newsToShow){
newsBusy.running=false;
var currentTime= new Date();
var msg = {'currentTime': currentTime, 'model': newsModel,'news':newsToShow};
newsWorker.sendMessage(msg);
}
function onFriendsMessages(friend){print(" Friend "+friend);
newstabStatus="friendmessage";
Newsjs.newsfromdb(db,root.login.username, function(dbnews){showNews(dbnews)},friend)
}
function onDirectMessage(friend){
print(root.login.server);
newsStack.push({item:"qrc:/qml/newsqml/MessageSend.qml",properties:{"reply_to_user": friend,"directmessage":1,"login":root.login}});
}
function cleanNews(database){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
var maxnewsrs = tx.executeSql("SELECT DISTINCT maxnews FROM config");
var maxnews=maxnewsrs.rows.item(0);
var newscountrs = tx.executeSql('SELECT COUNT(*) from news');
var newscount = newscountrs.rows.item(0);
if (newscount>maxnews){var lastvalidtimers= tx.executeSql('select created_at from news ORDER BY created_at DESC LIMIT ' +(newscount-maxnews));
var lastvalidtime=lastvalidtimers.rows.item(-1);
var deleters = tx.executeSql('DELETE from news WHERE created_at<'+lastvalidtime)}
});
Qt.quit()
}
StackView{
id: newsStack
anchors.fill:parent
focus: true
Keys.onReleased: {if (event.key === Qt.Key_Back && stackView.depth > 1) {
stackView.pop(); event.accepted = true;
}
else if(event.key === Qt.Key_Back &&newstabStatus=="conversation"){
newstabStatus="news"
newsModel.clear();
Newsjs.newsfromdb(root.db,root.login.username, function(dbnews){
showNews(dbnews)
})}
}
initialItem:Rectangle {
y:1
color: "white"
width:root.width-2*mm
height:root.height-8*mm
ComboBox{
y:mm
width: 8*mm
model: ListModel{
id: cbModel
ListElement{text: qsTr("News")}
ListElement{text: qsTr("Favorites")}
}
onCurrentIndexChanged:{
if (currentIndex==0){newsModel.clear();
Newsjs.newsfromdb(root.db,root.login.username, function(dbnews){
showNews(dbnews)
})}
else if (currentIndex==1){
newsBusy.running=true;
Newsjs.requestFavorites(root.login,db,root,function(news){
Newsjs.storeNews(root.login,root.db,news,root,function(){
Newsjs.favoritesfromdb(db,root.login.username,function(newsarray){
showNews(newsarray)
});
}
)})
}
}}
Button {
id: newMessageButton
text: qsTr("+")
anchors.top: parent.top
anchors.right: parent.right
onClicked: {
var groups=[];
Helperjs.readData(root.db,"groups",root.login.username,function(groupobject){
groups=groupobject});
Helperjs.readData(root.db,"contacts",root.login.username,function(friends){
newsStack.push({item:"qrc:/qml/newsqml/MessageSend.qml",properties:{"contacts": friends,"login":root.login}})
},"isFriend",1);
}
}
Button {
id: quitButton
text: qsTr("Quit")
anchors.top: parent.top
anchors.right: newMessageButton.left
onClicked: {cleanNews(root.db)}
}
Component { id:footerComponent
Rectangle{
border.color: "#EEEEEE"
border.width: 1
width:parent.width
height:6*mm
Text{
font.pixelSize: 1.5*mm
anchors.centerIn: parent
text:qsTr("More")
}
MouseArea{anchors.fill:parent
onClicked:{
var currentTime= new Date();
if(newstabStatus=="news"){
var lastnews_id=newsModel.get(newsModel.count-1).newsitemobject.created_at;
Newsjs.newsfromdb(root.db,root.login.username, function(news){
var msg = {'currentTime': currentTime, 'model': newsModel,'news':news,'appendnews':true};
newsWorker.sendMessage(msg);
},false,lastnews_id)}
else if(newstabStatus=="friendmessage"){
Newsjs.newsfromdb(root.db,root.login.username, function(news){
var msg = {'currentTime': currentTime, 'model': newsModel,'news':news,'appendnews':true};
newsWorker.sendMessage(msg);
},newsModel.get(newsModel.count-1).newsitemobject.uid,newsModel.get(newsModel.count-1).newsitemobject.created_at)}
}}
}
}
ListView {
id: newsView
anchors.fill: parent
anchors.topMargin: 8*root.mm
anchors.leftMargin: 3*root.mm; anchors.rightMargin: root.mm
anchors.bottomMargin: 1*root.mm
clip: true
spacing: 0
footer: footerComponent
model: newsModel
delegate: Newsitem{}
}
ListModel{id: newsModel}
WorkerScript {
id: newsWorker
source: "qrc:/js/newsworker.js"
}
Button {
id: update
anchors.top: parent.top
anchors.right: quitButton.left
text: "Update"
onClicked: { //try{newsModel.clear()} catch(e){}
newsBusy.running=true;
root.contactLoadType="news";
Newsjs.getFriendsTimeline(login,db,contactlist,newstab,function(ns,nc){
root.news=ns;root.newContacts=nc;root.currentContact=0;
if (ns.length==0){
Newsjs.getDirectMessage(root.login,root.db,root,function(dbnews){showNews(dbnews)});
newsBusy.running=false}
})}
}
BusyIndicator{
id: newsBusy
anchors.horizontalCenter: newsView.horizontalCenter
anchors.top:parent.top
anchors.topMargin: 2*mm
width:7*mm
height: 7*mm
}
Component.onCompleted: {
root.messageSignal.connect(onFriendsMessages);
root.directmessageSignal.connect(onDirectMessage);
root.newsSignal.connect(showNews);
try{newsModel.clear()} catch(e){}
Newsjs.newsfromdb(root.db,root.login.username, function(dbnews){
showNews(dbnews)
})
}
}
}
}