Skip to content

Commit d26c0fd

Browse files
committed
fixed #84 (may cause other issues)
1 parent 7698e58 commit d26c0fd

7 files changed

Lines changed: 188 additions & 100 deletions

File tree

lib/api/CategoryAPI.dart

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,62 @@
11
import 'dart:convert';
22

33
import 'package:dio/dio.dart';
4+
import 'package:flutter/material.dart';
45
import 'API.dart';
6+
import 'SessionAPI.dart';
57

68
Future<Map<String,dynamic>> fetchAlbums(String albumID) async {
7-
8-
Map<String, String> queries = {
9+
Map<String, dynamic> queries = {
910
"format": "json",
1011
"method": "pwg.categories.getList",
1112
"cat_id": albumID,
1213
"thumbnail_size": API.prefs.getString('thumbnail_size'),
1314
};
15+
List<String> uploadCategoryIdList = [];
16+
if (await methodExist('community.categories.getList')) {
17+
queries['faked_by_community'] = false;
18+
var communityResult = await fetchCommunityAlbums(albumID);
19+
uploadCategoryIdList = communityResult['result']['categories'].map<String>(
20+
(cat) => cat['id'].toString(),
21+
).toList();
22+
}
23+
24+
try {
25+
Response response = await API().dio.get('ws.php', queryParameters: queries);
26+
27+
if (response.statusCode == 200) {
28+
Map<String, dynamic> result = json.decode(response.data);
29+
if(result['stat'] == 'fail') return result;
30+
var categoryList = result['result']['categories'];
31+
for (var cat in categoryList) {
32+
bool canUpload = false;
33+
if (API.prefs.getString('user_status') != 'normal'
34+
|| uploadCategoryIdList.contains(cat['id'].toString())) {
35+
canUpload = true;
36+
}
37+
cat['can_upload'] = canUpload;
38+
}
39+
result['result']['categories'] = categoryList;
40+
return result;
41+
} else {
42+
return {
43+
'stat': 'fail',
44+
'result': response.statusMessage
45+
};
46+
}
47+
} on DioError {
48+
return {
49+
'stat': 'fail',
50+
'result': 'error',
51+
};
52+
}
53+
}
54+
Future<Map<String,dynamic>> fetchCommunityAlbums(String albumID) async {
55+
Map<String, dynamic> queries = {
56+
"format": "json",
57+
"method": "community.categories.getList",
58+
"cat_id": albumID,
59+
};
1460

1561
try {
1662
Response response = await API().dio.get('ws.php', queryParameters: queries);
@@ -76,8 +122,7 @@ Future<dynamic> addCategory(String catName, String catDesc, String parent) async
76122
'result': response.statusMessage
77123
};
78124
}
79-
} catch(e) {
80-
var error = e as DioError;
125+
} on DioError catch(error) {
81126
return {
82127
'stat': 'fail',
83128
'result': error.message
@@ -170,7 +215,7 @@ Future<dynamic> editCategory(int catId, String catName, String catDesc) async {
170215
return json.decode(response.data);
171216
}
172217
} catch (e) {
173-
print('Dio move category error $e');
218+
debugPrint('Dio move category error $e');
174219
return e;
175220
}
176221
}

lib/api/SessionAPI.dart

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22

33
import 'package:dio/dio.dart';
4+
import 'package:flutter/material.dart';
45

56
import 'API.dart';
67

@@ -68,7 +69,24 @@ Future<Map<String, dynamic>> sessionStatus() async {
6869
Response response = await API().dio.get('ws.php', queryParameters: queries);
6970
return json.decode(response.data);
7071
} on DioError catch (e) {
71-
print('Dio error $e');
72+
debugPrint('Dio error $e');
73+
return {"stat": "KO",
74+
"message": e.message,
75+
};
76+
}
77+
}
78+
79+
Future<Map<String, dynamic>> communityStatus() async {
80+
Map<String, String> queries = {
81+
'format': 'json',
82+
'method': 'community.session.getStatus'
83+
};
84+
85+
try {
86+
Response response = await API().dio.get('ws.php', queryParameters: queries);
87+
return json.decode(response.data);
88+
} on DioError catch (e) {
89+
debugPrint('Dio error $e');
7290
return {"stat": "KO",
7391
"message": e.message,
7492
};
@@ -101,7 +119,19 @@ void savePreferences(Map<String, dynamic> status, {
101119
API.storage.write(key: 'password', value: password);
102120
API.prefs.setBool("is_logged", isLogged);
103121
API.prefs.setBool("is_guest", isGuest);
104-
API.prefs.setString("user_status", status["status"]);
122+
if (await methodExist('community.session.getStatus')) {
123+
var community = await communityStatus();
124+
if(community['stat'] == 'ok') {
125+
API.prefs.setString("user_status", community['result']["real_user_status"]);
126+
API.prefs.setBool("community", true);
127+
} else {
128+
API.prefs.setString("user_status", status["status"]);
129+
API.prefs.setBool("community", false);
130+
}
131+
} else {
132+
API.prefs.setString("user_status", status["status"]);
133+
API.prefs.setBool("community", false);
134+
}
105135
API.prefs.setString("base_url", url);
106136

107137
API.prefs.setString("default_album", "Root Album");
@@ -125,7 +155,7 @@ Future<Map<String, dynamic>> getMethods() async {
125155
Response response = await API().dio.get('ws.php', queryParameters: queries);
126156
return json.decode(response.data);
127157
} catch (e) {
128-
print('Dio error $e');
158+
debugPrint('Dio error $e');
129159
return {"stat": "KO"};
130160
}
131161
}

lib/services/upload/Uploader.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:piwigo_ng/constants/SettingsConstants.dart';
1414
import 'package:piwigo_ng/views/components/snackbars.dart';
1515
import 'package:provider/provider.dart';
1616

17+
import '../../api/SessionAPI.dart';
1718
import '../../views/RootCategoryViewPage.dart';
1819
import '../UploadStatusProvider.dart';
1920
import 'chunked_uploader.dart';
@@ -103,8 +104,9 @@ class Uploader {
103104

104105
try {
105106
await uploadCompleted(uploadedImages, int.parse(category));
106-
await communityUploadCompleted(uploadedImages, int.parse(category));
107-
107+
if(await methodExist('community.images.uploadCompleted')) {
108+
await communityUploadCompleted(uploadedImages, int.parse(category));
109+
}
108110
// cleanTempDirectory();
109111
} on DioError catch (e) {
110112
debugPrint(e.message);

lib/views/CategoryViewPage.dart

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class _CategoryViewPageState extends State<CategoryViewPage> with SingleTickerPr
3434
Future<Map<String,dynamic>> _albumsFuture;
3535
Future<Map<String,dynamic>> _imagesFuture;
3636

37+
bool _canUpload = false;
3738
bool _isEditMode;
3839
int _page;
3940
int _nbImages;
@@ -76,7 +77,6 @@ class _CategoryViewPageState extends State<CategoryViewPage> with SingleTickerPr
7677
imageList.addAll(newListPage);
7778
}
7879
setState(() {
79-
print('Fetch images of page $_page');
8080
_getData();
8181
});
8282
}
@@ -296,13 +296,15 @@ class _CategoryViewPageState extends State<CategoryViewPage> with SingleTickerPr
296296

297297
handleAlbumSnapshot(AsyncSnapshot albumSnapshot, int nbImages) {
298298
var albums = albumSnapshot.data['result']['categories'];
299-
int nbImages = _nbImages;
300-
if(albums.length > 0 && albums[0]["id"].toString() == widget.category) {
301-
nbImages = albums[0]["total_nb_images"];
302-
_nbImages = nbImages;
299+
if(albums.length > 0 && albums.first["id"].toString() == widget.category) {
300+
_nbImages = albums.first["total_nb_images"];
301+
_canUpload = albums.first["can_upload"] ?? false;
302+
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
303+
setState(() {});
304+
});
303305
}
304306
albums.removeWhere((category) =>
305-
(category["id"].toString() == widget.category)
307+
(category["id"].toString() == widget.category)
306308
);
307309
return albums;
308310
}
@@ -439,81 +441,79 @@ class _CategoryViewPageState extends State<CategoryViewPage> with SingleTickerPr
439441
});
440442
},
441443
),
442-
SpeedDialChild(
443-
elevation: 5,
444-
labelWidget: Text(appStrings(context).categoryUpload_images, style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.white)),
445-
child: Icon(Icons.add_to_photos),
446-
backgroundColor: _theme.floatingActionButtonTheme.backgroundColor,
447-
foregroundColor: _theme.floatingActionButtonTheme.foregroundColor,
448-
onTap: () async {
449-
try {
450-
ScaffoldMessenger.of(context).removeCurrentSnackBar();
451-
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
452-
content: Row(
453-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
454-
mainAxisSize: MainAxisSize.min,
455-
children: [
456-
Text(appStrings(context).loadingHUD_label),
457-
CircularProgressIndicator(),
458-
],
459-
),
460-
duration: Duration(days: 365),
461-
));
462-
final List<XFile> images = ((await FilePicker.platform.pickFiles(
463-
type: FileType.media,
464-
allowMultiple: true,
465-
)) ?.files ?? []).map<XFile>((e) => XFile(e.path, name: e.name, bytes: e.bytes)).toList();
466-
ScaffoldMessenger.of(context).removeCurrentSnackBar();
467-
if(images.isNotEmpty) {
468-
Navigator.push(context, MaterialPageRoute(
469-
builder: (context) => UploadGalleryViewPage(imageData: images, category: widget.category)
470-
)).whenComplete(() {
471-
setState(() {
472-
print('After upload'); // refresh
473-
});
474-
});
444+
if(_canUpload) ... [
445+
SpeedDialChild(
446+
elevation: 5,
447+
labelWidget: Text(appStrings(context).categoryUpload_images, style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.white)),
448+
child: Icon(Icons.add_to_photos),
449+
backgroundColor: _theme.floatingActionButtonTheme.backgroundColor,
450+
foregroundColor: _theme.floatingActionButtonTheme.foregroundColor,
451+
onTap: () async {
452+
try {
453+
ScaffoldMessenger.of(context).removeCurrentSnackBar();
454+
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
455+
content: Row(
456+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
457+
mainAxisSize: MainAxisSize.min,
458+
children: [
459+
Text(appStrings(context).loadingHUD_label),
460+
CircularProgressIndicator(),
461+
],
462+
),
463+
duration: Duration(days: 365),
464+
));
465+
final List<XFile> images = ((await FilePicker.platform.pickFiles(
466+
type: FileType.media,
467+
allowMultiple: true,
468+
)) ?.files ?? []).map<XFile>((e) => XFile(e.path, name: e.name, bytes: e.bytes)).toList();
469+
ScaffoldMessenger.of(context).removeCurrentSnackBar();
470+
if(images.isNotEmpty) {
471+
Navigator.push(context, MaterialPageRoute(
472+
builder: (context) => UploadGalleryViewPage(imageData: images, category: widget.category)
473+
)).whenComplete(() {
474+
setState(() {});
475+
});
476+
}
477+
} catch (e) {
478+
debugPrint('${e.toString()}');
479+
}
475480
}
476-
} catch (e) {
477-
print('${e.toString()}');
478-
}
479-
}
480-
),
481-
SpeedDialChild(
482-
elevation: 5,
483-
labelWidget: Text(appStrings(context).categoryUpload_take, style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.white)),
484-
child: Icon(Icons.photo_camera_rounded),
485-
backgroundColor: _theme.floatingActionButtonTheme.backgroundColor,
486-
foregroundColor: _theme.floatingActionButtonTheme.foregroundColor,
487-
onTap: () async {
488-
try {
489-
ScaffoldMessenger.of(context).removeCurrentSnackBar();
490-
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
491-
content: Row(
492-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
493-
mainAxisSize: MainAxisSize.min,
494-
children: [
495-
Text(appStrings(context).loadingHUD_label),
496-
CircularProgressIndicator(),
497-
],
498-
),
499-
duration: Duration(days: 365),
500-
));
501-
final XFile image = await ImagePicker().pickImage(source: ImageSource.camera);
502-
ScaffoldMessenger.of(context).removeCurrentSnackBar();
503-
if(image != null) {
504-
Navigator.push(context, MaterialPageRoute(
505-
builder: (context) => UploadGalleryViewPage(imageData: [image], category: widget.category)
506-
)).whenComplete(() {
507-
setState(() {
508-
print('After upload'); // refresh
481+
),
482+
SpeedDialChild(
483+
elevation: 5,
484+
labelWidget: Text(appStrings(context).categoryUpload_take, style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.white)),
485+
child: Icon(Icons.photo_camera_rounded),
486+
backgroundColor: _theme.floatingActionButtonTheme.backgroundColor,
487+
foregroundColor: _theme.floatingActionButtonTheme.foregroundColor,
488+
onTap: () async {
489+
try {
490+
ScaffoldMessenger.of(context).removeCurrentSnackBar();
491+
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
492+
content: Row(
493+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
494+
mainAxisSize: MainAxisSize.min,
495+
children: [
496+
Text(appStrings(context).loadingHUD_label),
497+
CircularProgressIndicator(),
498+
],
499+
),
500+
duration: Duration(days: 365),
501+
));
502+
final XFile image = await ImagePicker().pickImage(source: ImageSource.camera);
503+
ScaffoldMessenger.of(context).removeCurrentSnackBar();
504+
if(image != null) {
505+
Navigator.push(context, MaterialPageRoute(
506+
builder: (context) => UploadGalleryViewPage(imageData: [image], category: widget.category)
507+
)).whenComplete(() {
508+
setState(() {});
509509
});
510-
});
510+
}
511+
} catch (e) {
512+
debugPrint('Dio error ${e.toString()}');
511513
}
512-
} catch (e) {
513-
print('Dio error ${e.toString()}');
514514
}
515-
}
516-
),
515+
),
516+
],
517517
],
518518
);
519519
}
@@ -541,7 +541,7 @@ class _CategoryViewPageState extends State<CategoryViewPage> with SingleTickerPr
541541
itemBuilder: (BuildContext context, int index) {
542542
var album = albums[index];
543543
return AlbumListItem(album,
544-
isAdmin: widget.isAdmin,
544+
isAdmin: API.prefs.getString('user_status') != 'normal',
545545
onClose: () {
546546
setState(() {
547547
_getData();
@@ -694,14 +694,14 @@ class _CategoryViewPageState extends State<CategoryViewPage> with SingleTickerPr
694694
padding: const EdgeInsets.all(8.0),
695695
child: Stack(
696696
children: <Widget>[
697-
widget.isAdmin? Align(
697+
widget.isAdmin || _canUpload ? Align(
698698
alignment: Alignment.bottomRight,
699699
child: _createUploadActionButton,
700-
) : Container(),
700+
) : const SizedBox(),
701701
Align(
702702
alignment: Alignment.bottomRight,
703703
child: Container(
704-
margin: EdgeInsets.only(bottom: 0, right: widget.isAdmin? 70 : 0),
704+
margin: EdgeInsets.only(bottom: 0, right: widget.isAdmin || _canUpload ? 70 : 0),
705705
child: FloatingActionButton(
706706
backgroundColor: Color(0xff868686),
707707
onPressed: () {

lib/views/LoginViewPage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class _LoginViewPageState extends State<LoginViewPage> {
9090
url = url.substring(0, url.lastIndexOf('/'));
9191
urlController.text = url;
9292
usernameController.text = await API.storage.read(key: "username") ?? '';
93-
passwordController.text = '';
93+
passwordController.text = await API.storage.read(key: "password") ?? '';
9494
if(mounted) setState(() {});
9595
}
9696
}

0 commit comments

Comments
 (0)