Move filtering to javascript

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-06-02 09:51:14 +02:00
parent 64c2e946f4
commit a5509aa253
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
4 changed files with 81 additions and 103 deletions

View File

@ -81,6 +81,9 @@ class AppSettingsController extends Controller {
/** @var ILogger */
private $logger;
/** @var array */
private $allApps = [];
/**
* @param string $appName
* @param IRequest $request
@ -197,6 +200,41 @@ class AppSettingsController extends Controller {
return $formattedCategories;
}
private function fetchApps() {
$appClass = new \OC_App();
$apps = $appClass->listAllApps();
foreach ($apps as $app) {
$app['installed'] = true;
$this->allApps[$app['id']] = $app;
}
$apps = $this->getAppsForCategory('');
foreach ($apps as $app) {
$app['appstore'] = true;
if (!array_key_exists($app['id'], $this->allApps)) {
$this->allApps[$app['id']] = $app;
} else {
$this->allApps[$app['id']] = array_merge($this->allApps[$app['id']], $app);
}
}
// add bundle information
$bundles = $this->bundleFetcher->getBundles();
foreach($bundles as $bundle) {
foreach($bundle->getAppIdentifiers() as $identifier) {
foreach($this->allApps as &$app) {
if($app['id'] === $identifier) {
$app['bundleId'] = $bundle->getIdentifier();
continue;
}
}
}
}
}
private function getAllApps() {
return $this->allApps;
}
/**
* Get all available apps in a category
*
@ -204,75 +242,16 @@ class AppSettingsController extends Controller {
* @return JSONResponse
* @throws \Exception
*/
public function listApps($category = ''): JSONResponse {
$appClass = new \OC_App();
public function listApps(): JSONResponse {
switch ($category) {
case 'installed':
$apps = $appClass->listAllApps();
break;
case 'updates':
$apps = $this->getAppsWithUpdates();
break;
case 'enabled':
$apps = $appClass->listAllApps();
$apps = array_filter($apps, function ($app) {
return $app['active'];
});
break;
case 'disabled':
$apps = $appClass->listAllApps();
$apps = array_filter($apps, function ($app) {
return !$app['active'];
});
break;
case 'app-bundles':
$bundles = $this->bundleFetcher->getBundles();
$apps = [];
$installedApps = $appClass->listAllApps();
$appstoreApps = $this->getAppsForCategory();
foreach($bundles as $bundle) {
foreach($bundle->getAppIdentifiers() as $identifier) {
$alreadyMatched = false;
foreach($installedApps as $app) {
if($app['id'] === $identifier) {
$app['bundleId'] = $bundle->getIdentifier();
$apps[] = $app;
$alreadyMatched = true;
continue;
}
}
if (!$alreadyMatched) {
foreach ($appstoreApps as $app) {
if ($app['id'] === $identifier) {
$app['bundleId'] = $bundle->getIdentifier();
$apps[] = $app;
continue;
}
}
}
}
}
break;
default:
$apps = $this->getAppsForCategory($category);
break;
}
// Fetch all apps from appstore
$allAppStoreApps = [];
$fetchedApps = $this->appFetcher->get();
foreach ($fetchedApps as $app) {
$allAppStoreApps[$app['id']] = $app;
}
$this->fetchApps();
$apps = $this->getAllApps();
$dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
// Extend existing app details
$apps = array_map(function($appData) use ($allAppStoreApps, $dependencyAnalyzer) {
$appstoreData = $allAppStoreApps[$appData['id']];
$appData['appstoreData'] = $appstoreData;
$apps = array_map(function($appData) use ($dependencyAnalyzer) {
$appstoreData = $appData['appstoreData'];
$appData['screenshot'] = isset($appstoreData['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/'.base64_encode($appstoreData['screenshots'][0]['url']) : '';
$newVersion = $this->installer->isUpdateAvailable($appData['id']);
@ -310,7 +289,7 @@ class AppSettingsController extends Controller {
}
/**
* Get all apps for a category
* Get all apps for a category from the app store
*
* @param string $requestedCategory
* @return array

View File

@ -80,15 +80,45 @@ export default {
return this.$store.getters.loading('list');
},
apps() {
return this.$store.getters.getApps
let apps = this.$store.getters.getAllApps
.filter(app => app.name.toLowerCase().search(this.search.toLowerCase()) !== -1)
.sort(function (a, b) {
if (a.active !== b.active) {
return (a.active ? -1 : 1)
}
if (a.update !== b.update) {
return (a.update ? -1 : 1)
}
return OC.Util.naturalSortCompare(a.name, b.name);
});
if (this.category === 'installed') {
return apps.filter(app => app.installed);
}
if (this.category === 'enabled') {
return apps.filter(app => app.active);
}
if (this.category === 'disabled') {
return apps.filter(app => !app.active);
}
if (this.category === 'app-bundles') {
return apps.filter(app => app.bundles);
}
if (this.category === 'updates') {
return apps.filter(app => app.update);
}
// filter app store categories
return apps.filter(app => {
return app.appstore && app.category !== undefined &&
(app.category === this.category || app.category.indexOf(this.category) > -1);
});
},
bundles() {
return this.$store.getters.getServerData.bundles;
},
bundleApps() {
return function(bundle) {
return this.$store.getters.getApps
return this.$store.getters.getAllApps
.filter(app => app.bundleId === bundle);
}
},

View File

@ -26,7 +26,6 @@ import Vue from 'vue';
const state = {
apps: [],
allApps: [],
categories: [],
updateCount: 0,
loading: {},
@ -58,12 +57,8 @@ const mutations = {
state.categories = categoriesArray;
},
setApps(state, apps) {
state.apps = apps;
},
setAllApps(state, apps) {
state.allApps = apps;
state.apps = apps;
},
setError(state, {appId, error}) {
@ -145,19 +140,8 @@ const getters = {
getCategories(state) {
return state.categories;
},
getApps(state) {
return state.apps.concat([]).sort(function (a, b) {
if (a.active !== b.active) {
return (a.active ? -1 : 1)
}
if (a.update !== b.update) {
return (a.update ? -1 : 1)
}
return OC.Util.naturalSortCompare(a.name, b.name);
});
},
getAllApps(state) {
return state.allApps;
return state.apps;
},
getUpdateCount(state) {
return state.updateCount;
@ -279,23 +263,12 @@ const actions = {
}).catch((error) => context.commit('API_FAILURE', { appId, error }));
},
getApps(context, { category }) {
context.commit('startLoading', 'list');
return api.get(OC.generateUrl(`settings/apps/list?category=${category}`))
.then((response) => {
context.commit('setApps', response.data.apps);
context.commit('stopLoading', 'list');
return true;
})
.catch((error) => context.commit('API_FAILURE', error))
},
getAllApps(context) {
context.commit('startLoading', 'all');
context.commit('startLoading', 'list');
return api.get(OC.generateUrl(`settings/apps/list`))
.then((response) => {
context.commit('setAllApps', response.data.apps);
context.commit('stopLoading', 'all');
context.commit('stopLoading', 'list');
return true;
})
.catch((error) => context.commit('API_FAILURE', error))

View File

@ -69,7 +69,6 @@ export default {
},
beforeMount() {
this.$store.dispatch('getCategories');
this.$store.dispatch('getApps', {category: this.category});
this.$store.dispatch('getAllApps');
this.$store.dispatch('getGroups');
this.$store.commit('setUpdateCount', this.$store.getters.getServerData.updateCount)
@ -89,11 +88,8 @@ export default {
}
},
watch: {
// watch url change and group select
category: function (val, old) {
this.$store.commit('resetApps');
this.setSearch('');
this.$store.dispatch('getApps', { category: this.category });
}
},
computed: {
@ -110,7 +106,7 @@ export default {
return this.$store.getters.getCategories;
},
apps() {
return this.$store.getters.getApps;
return this.$store.getters.getAllApps;
},
updateCount() {
return this.$store.getters.getUpdateCount;