153 lines
5.7 KiB
Dart
153 lines
5.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'dart:io';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/Components/loading.dart';
|
|
import 'package:manager_app/Components/message_notification.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/Screens/Resources/new_resource_popup.dart';
|
|
import 'package:manager_app/Screens/Resources/resource_body_grid.dart';
|
|
import 'package:manager_app/Screens/Resources/show_resource_popup.dart';
|
|
import 'package:manager_app/app_context.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:managerapi/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
|
|
class ResourcesScreen extends StatefulWidget {
|
|
final Function onGetResult; //return ResourceDTO
|
|
final bool isImage;
|
|
final bool isAddButton;
|
|
const ResourcesScreen({
|
|
Key key,
|
|
this.isImage = false,
|
|
this.onGetResult,
|
|
this.isAddButton = true,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_ResourcesScreenState createState() => _ResourcesScreenState();
|
|
}
|
|
|
|
class _ResourcesScreenState extends State<ResourcesScreen> {
|
|
String filter;
|
|
bool isLoading;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
return FutureBuilder(
|
|
future: getResources(widget.onGetResult, widget.isImage, appContext),
|
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
var tempOutput = new List<ResourceDTO>.from(snapshot.data);
|
|
// tempOutput.add(ResourceDTO(id: null));
|
|
return ResourceBodyGrid(resources: tempOutput, isImage: widget.isImage, isAddButton: widget.isAddButton, onSelect: (value) async {
|
|
if (widget.onGetResult == null) {
|
|
// Main screen
|
|
if (value.id == null) {
|
|
var result = await showNewResource(appContext, context);
|
|
if (result != null)
|
|
{
|
|
await create(result[0], result[1], result[2], appContext, context);
|
|
setState(() {}); // For refresh
|
|
}
|
|
} else {
|
|
showResource(value, appContext, context, size);
|
|
}
|
|
} else {
|
|
// Result for select modal
|
|
widget.onGetResult(value);
|
|
}
|
|
},);//bodyGrid(tempOutput, size, appContext);
|
|
} else if (snapshot.connectionState == ConnectionState.none) {
|
|
return Text("No data");
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
height: size.height * 0.2,
|
|
child: Loading()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> getResources(Function onGetResult, bool isImage, dynamic appContext) async {
|
|
List<ResourceDTO> resources = await appContext.getContext().clientAPI.resourceApi.resourceGet();
|
|
if (onGetResult != null && isImage) {
|
|
resources = resources.where((element) => element.type == ResourceType.image || element.type == ResourceType.imageUrl).toList();
|
|
}
|
|
return resources;
|
|
}
|
|
|
|
Future<ResourceDTO> create(ResourceDTO resourceDTO, List<File> files, List<PlatformFile> filesWeb, AppContext appContext, context) async {
|
|
switch(resourceDTO.type) {
|
|
case ResourceType.image:
|
|
case ResourceType.video:
|
|
|
|
var request = http.MultipartRequest('POST', Uri.parse(appContext.getContext().clientAPI.resourceApi.apiClient.basePath+"/api/Resource/upload"));
|
|
|
|
if (kIsWeb) {
|
|
for (PlatformFile file in filesWeb) {
|
|
request.files.add(
|
|
await http.MultipartFile.fromBytes(
|
|
'picture',
|
|
file.bytes,
|
|
filename: file.name
|
|
)
|
|
);
|
|
}
|
|
} else {
|
|
for (File file in files) {
|
|
request.files.add(
|
|
await http.MultipartFile(
|
|
'picture',
|
|
file.readAsBytes().asStream(),
|
|
file.lengthSync(),
|
|
filename: file.path.toString().split("/").last
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
ManagerAppContext managerAppContext = appContext.getContext();
|
|
request.headers["authorization"]="Bearer ${managerAppContext.token.accessToken}";
|
|
request.fields['label'] = resourceDTO.label;
|
|
request.fields['type'] = ResourceType.image.toString();
|
|
|
|
var res = await request.send();
|
|
final respStr = await res.stream.bytesToString();
|
|
|
|
if (res.statusCode == 200) {
|
|
showNotification(Colors.green, kWhite, 'La ressource a été créée avec succès', context, null);
|
|
return null;
|
|
} else {
|
|
showNotification(kPrimaryColor, kWhite, 'Une erreur est survenue lors de la création de la ressource', context, null);
|
|
}
|
|
break;
|
|
case ResourceType.imageUrl:
|
|
case ResourceType.videoUrl:
|
|
if (resourceDTO.data != null) {
|
|
// test if Correct url
|
|
bool _validURL = Uri.parse(resourceDTO.data).isAbsolute;
|
|
if(_validURL) {
|
|
ResourceDTO newResource = await appContext.getContext().clientAPI.resourceApi.resourceCreate(resourceDTO);
|
|
showNotification(Colors.green, kWhite, 'La ressource a été créée avec succès', context, null);
|
|
|
|
return newResource;
|
|
} else {
|
|
showNotification(Colors.orange, kWhite, 'L\'url est invalide', context, null);
|
|
}
|
|
} else {
|
|
showNotification(Colors.orange, kWhite, 'Veuillez remplir le champ URL', context, null);
|
|
}
|
|
break;
|
|
}
|
|
} |