Friendiqa/v0.001/source-android/qml/NewsTab.qml

155 lines
5.8 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){
newsStack.push({item:"qrc:/qml/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;
}
initialItem:Rectangle {
color: "white"
width:root.width-2*mm
height:root.height-8*mm
Button {
id: newMessageButton
text: qsTr("+")
anchors.top: parent.top
anchors.right: parent.right
onClicked: {
Helperjs.readField("screen_name",root.db,"contacts",root.login.username,function(friends){
newsStack.push({item:"qrc:/qml/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.centerIn:update
//anchors.right: update.left
//anchors.top:parent.top
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)
})
}
}
}
}