[core] reuse the category model for feed subscriptions

This commit is contained in:
Hauke Schade 2014-11-24 23:06:56 +01:00
parent 49985d6c72
commit 81191d6346
4 changed files with 37 additions and 113 deletions

View file

@ -49,11 +49,7 @@ Sheet {
}
Component.onCompleted: {
var oldShowAll = settings.showAll
var ttrss = rootWindow.getTTRSS()
ttrss.setShowAll(true)
allCategories.update()
ttrss.setShowAll(oldShowAll)
allCategories.getAllCategories()
}
content: Flickable {

View file

@ -32,7 +32,7 @@ ListModel {
var ttrss = rootWindow.getTTRSS();
ttrss.updateCategories(function(successful, errorMessage) {
if (successful) {
root.load()
root.load(false)
}
// TODO Add a callback to update() which can be used to display
@ -41,13 +41,16 @@ ListModel {
}
/** @private */
function load() {
function load(woSpecialCategories) {
var ttrss = rootWindow.getTTRSS()
var showAll = ttrss.getShowAll()
settings.showAll = showAll
var categories = ttrss.getCategories()
var categories
if (woSpecialCategories) {
categories = ttrss.getAllCategoriesWoSpecial()
}
else {
categories = ttrss.getCategories()
}
root.clear()
@ -95,11 +98,8 @@ ListModel {
updateFinished()
}
function getAllCategories(callback) {
var ttrss = rootWindow.getTTRSS();
ttrss.getAllCategories(function(successful, errorMessage, categories) {
callback(successful, errorMessage, categories)
})
function getAllCategories() {
root.load(true)
}
function getTotalUnreadItems() {

View file

@ -194,8 +194,28 @@ function getCategories() {
var retVal = []
var i = 0
for (var cat in state['categorycache']) {
retVal[i] = state['categorycache'][cat]
i++
var include = false;
if (state['showall'] || state['categorycache'][cat]['unread'] > 0) {
retVal[i] = state['categorycache'][cat]
i++
}
}
retVal.sort(categorySort)
return retVal
}
/**
* @return {array} Sorted array of categories.
*/
function getAllCategoriesWoSpecial() {
var retVal = []
var i = 0
for (var cat in state['categorycache']) {
if (state['categorycache'][cat]['id'] >= 0) {
retVal[i] = state['categorycache'][cat]
i++
}
}
retVal.sort(categorySort)
@ -396,7 +416,7 @@ function updateCategories(callback) {
var params = {
'op': 'getCategories',
'sid': state['token'],
'unread_only': !state['showall']
'unread_only': false //!state['showall']
}
networkCall(params, function(http) { process_updateCategories(callback, http) });
@ -432,70 +452,6 @@ function process_updateCategories(callback, httpreq) {
}
}
/**
* Get all categories from server. Excludes special categories except for
* `Uncategorized`.
* @param {function} A callback function with parameters boolean (indicating
* success), string (an optional error message), and array (the categories
* as objects).
*/
function getAllCategories(callback) {
if(responsesPending['allcategories']) {
return;
}
// needs to be logged in
if(!state['token']) {
requestsPending['allcategories'] = true;
processPendingRequests(callback);
return;
}
responsesPending['allcategories'] = true;
var params = {
'op': 'getCategories',
'sid': state['token'],
'unread_only': false
}
networkCall(params, function(http) { process_getAllCategories(callback, http) });
}
/** @private */
function process_getAllCategories(callback, httpreq) {
var response = process_readyState(httpreq);
responsesPending['allcategories'] = false;
if (!response.successful) {
trace(1, "Get all categories: " + response.errorMessage);
if (callback) {
callback(false, response.errorMessage);
}
return;
}
var categories = []
for(var i = 0; i < response.content.length; i++) {
var cat = response.content[i];
if (cat.id < 0) {
// Exclude special categories
continue;
}
categories.push(cat);
}
if(!processPendingRequests(callback) && callback) {
// This action is complete (as there's no other requests to do)
// Fire callback saying all ok
callback(true, "", categories);
}
}
/**
* Update the feeds of a category.
* @param {int} The id of the category whose feeds should be updated.

View file

@ -108,39 +108,11 @@ Dialog {
size: BusyIndicatorSize.Large
}
ListModel {
CategoryModel {
id: allCategories
Component.onCompleted: {
categoryModel.getAllCategories(function(successful, errorMessage,
categories) {
if (successful) {
allCategories.load(categories)
}
// TODO make use of errorMessage
})
}
function load(categories) {
if (!categories || !categories.length) {
return
}
var ttrss = rootWindow.getTTRSS()
for(var i = 0; i < categories.length; ++i) {
var title = ttrss.html_entity_decode(categories[i].title,
'ENT_QUOTES')
if (categories[i].id === ttrss.constants['categories']['UNCATEGORIZED']) {
title = constant.uncategorizedCategory
}
allCategories.append({
name: title,
value: parseInt(categories[i].id),
});
}
allCategories.getAllCategories()
categoryChooser.setInitialIndex()
}
}