Skip to content

Commit dc2e461

Browse files
committed
Improved community support for image actions an empty albums
1 parent 9948b61 commit dc2e461

4 files changed

Lines changed: 51 additions & 32 deletions

File tree

lib/network/albums.dart

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,6 @@ import 'package:piwigo_ng/services/preferences_service.dart';
99
import 'api_client.dart';
1010
import '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-
2512
Future<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
);

lib/network/api_client.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import 'dart:convert';
12
import 'dart:io';
23

34
import 'package:cookie_jar/cookie_jar.dart';
45
import 'package:dio/adapter.dart';
56
import 'package:dio/dio.dart';
67
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
8+
import 'package:flutter/foundation.dart';
79
import 'package:piwigo_ng/services/preferences_service.dart';
810

911
import 'api_interceptor.dart';
@@ -140,3 +142,19 @@ class SSLHttpOverrides extends HttpOverrides {
140142
..badCertificateCallback = ApiClient.piwigoSSLBypass;
141143
}
142144
}
145+
146+
Map<String, dynamic> tryParseJson(String data) {
147+
try {
148+
return json.decode(data);
149+
} on FormatException catch (_) {
150+
if (kDebugMode) {
151+
print('Invalid json data');
152+
print(data);
153+
}
154+
int start = data.indexOf('{');
155+
int end = data.lastIndexOf('}');
156+
String parsedData = data.substring(start, end + 1);
157+
if (kDebugMode) print("Parsed : $parsedData");
158+
return json.decode(parsedData);
159+
}
160+
}

lib/network/images.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Future<ApiResponse<List<ImageModel>>> fetchImages(
6868
Response response = await ApiClient.get(queryParameters: queries);
6969

7070
if (response.statusCode == 200) {
71-
var jsonImages = json.decode(response.data)['result']['images'];
71+
var jsonImages = tryParseJson(response.data)['result']['images'];
7272
List<ImageModel> images = List<ImageModel>.from(
7373
jsonImages.map((image) => ImageModel.fromJson(image)),
7474
);
@@ -77,8 +77,8 @@ Future<ApiResponse<List<ImageModel>>> fetchImages(
7777
}
7878
} on DioError catch (e) {
7979
debugPrint('Fetch images: ${e.message}');
80-
} on Error catch (e) {
81-
debugPrint('Fetch images: ${e.stackTrace}');
80+
} catch (e) {
81+
debugPrint('Fetch images: $e');
8282
}
8383
return ApiResponse(error: ApiErrors.fetchImagesError);
8484
}

lib/views/album/album_page.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class _AlbumPageState extends State<AlbumPage> {
313313
),
314314
if (orientation == Orientation.landscape && _selectedList.isNotEmpty)
315315
..._imageActions,
316-
if (widget.isAdmin)
316+
if (widget.isAdmin || _currentAlbum.canUpload)
317317
PopupMenuButton(
318318
tooltip: appStrings.imageOptions_title,
319319
position: PopupMenuPosition.under,
@@ -617,6 +617,8 @@ class _AlbumPageState extends State<AlbumPage> {
617617
),
618618
];
619619

620-
return widget.isAdmin ? adminActions : userActions;
620+
return widget.isAdmin || _currentAlbum.canUpload
621+
? adminActions
622+
: userActions;
621623
}
622624
}

0 commit comments

Comments
 (0)