@@ -9,19 +9,6 @@ import 'package:piwigo_ng/services/preferences_service.dart';
99import 'api_client.dart' ;
1010import 'authentication.dart' ;
1111
12- Map <String , dynamic > tryParseJson (String data) {
13- try {
14- return json.decode (data);
15- } on FormatException catch (_) {
16- debugPrint ('Invalid json data' );
17- debugPrint (data);
18- int start = data.indexOf ('{' );
19- int end = data.lastIndexOf ('}' );
20- String parsedData = data.substring (start, end + 1 );
21- return json.decode (parsedData);
22- }
23- }
24-
2512Future <ApiResponse <List <AlbumModel >>> fetchAlbums (int albumID) async {
2613 final Map <String , dynamic > queries = {
2714 'format' : 'json' ,
@@ -31,17 +18,8 @@ Future<ApiResponse<List<AlbumModel>>> fetchAlbums(int albumID) async {
3118 };
3219
3320 try {
34- List <String > uploadCategoryIdList = [];
3521 if (await methodExist ('community.categories.getList' )) {
3622 queries['faked_by_community' ] = false ;
37- ApiResponse communityResult = await fetchCommunityAlbums (albumID);
38- if (communityResult.hasData) {
39- uploadCategoryIdList = communityResult.data.map <String >(
40- (cat) {
41- return cat.id.toString ();
42- },
43- ).toList ();
44- }
4523 }
4624
4725 Response response = await ApiClient .get (
@@ -53,15 +31,36 @@ Future<ApiResponse<List<AlbumModel>>> fetchAlbums(int albumID) async {
5331 tryParseJson (response.data)['result' ]['categories' ];
5432 List <AlbumModel > albums = List <AlbumModel >.from (jsonAlbums.map (
5533 (album) {
56- bool canUpload = false ;
57- if ((appPreferences.getBool (Preferences .isAdminKey) ?? false ) ||
58- uploadCategoryIdList.contains (album['id' ].toString ())) {
59- canUpload = true ;
60- }
34+ bool canUpload =
35+ appPreferences.getBool (Preferences .isAdminKey) ?? false ;
6136 album['can_upload' ] = canUpload;
6237 return AlbumModel .fromJson (album);
6338 },
6439 ));
40+
41+ if (await methodExist ('community.categories.getList' )) {
42+ ApiResponse communityResult = await fetchCommunityAlbums (albumID);
43+ if (! communityResult.hasData || communityResult.data! .isEmpty) {
44+ return ApiResponse <List <AlbumModel >>(
45+ data: albums,
46+ );
47+ }
48+ if (communityResult.hasData) {
49+ for (AlbumModel communityAlbum in communityResult.data) {
50+ int index =
51+ albums.indexWhere ((album) => album.id == communityAlbum.id);
52+ if (index >= 0 ) {
53+ AlbumModel newAlbum = albums.elementAt (index);
54+ newAlbum.canUpload = true ;
55+ albums[index] = newAlbum;
56+ } else {
57+ communityAlbum.canUpload = true ;
58+ albums.add (communityAlbum);
59+ }
60+ }
61+ }
62+ }
63+
6564 return ApiResponse <List <AlbumModel >>(
6665 data: albums,
6766 );
0 commit comments