manager-app/lib/Screens/Resources/show_resource_popup.dart

150 lines
6.0 KiB
Dart

import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:manager_app/Components/confirmation_dialog.dart';
import 'package:manager_app/Components/message_notification.dart';
import 'package:manager_app/Components/rounded_button.dart';
import 'package:manager_app/Components/string_input_container.dart';
import 'package:manager_app/Models/managerContext.dart';
import 'package:manager_app/app_context.dart';
import 'package:manager_app/constants.dart';
import 'package:manager_app/l10n/app_localizations.dart';
import 'package:manager_api_new/api.dart';
import 'dart:html' as html;
import 'get_element_for_resource.dart';
Future<ResourceDTO?> showResource(ResourceDTO resourceDTO, AppContext appContext, BuildContext context, Size size) async {
var result = await showDialog(
builder: (BuildContext context) => Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))
),
child: SizedBox(
width: 520,
child: Padding(
padding: const EdgeInsets.fromLTRB(24, 20, 24, 24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Flexible(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 80,
child: StringInputContainer(
label: "Label :",
initialValue: resourceDTO.label != null ? resourceDTO.label : "",
onChanged: (value) {
resourceDTO.label = value;
},
),
),
const SizedBox(height: 8),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: resourceDTO.type == ResourceType.Audio ? 80 : 300,
maxWidth: resourceDTO.type == ResourceType.Audio ? 200 : double.infinity,
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
border: Border.all(width: 3, color: kSecond),
),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: getElementForResource(resourceDTO, appContext),
),
),
),
],
),
),
),
const SizedBox(height: 16),
Wrap(
spacing: 8,
runSpacing: 8,
alignment: WrapAlignment.end,
children: [
RoundedButton(
text: "Retour",
icon: Icons.undo,
color: kSecond,
press: () => Navigator.of(context).pop(),
fontSize: 16,
),
RoundedButton(
text: "Supprimer",
icon: Icons.delete,
color: kError,
textColor: kWhite,
press: () => delete(resourceDTO, appContext, context),
fontSize: 16,
),
RoundedButton(
text: AppLocalizations.of(context)!.download,
icon: Icons.download,
color: kPrimaryColor,
press: () {
if (resourceDTO.url != null) {
html.AnchorElement anchorElement = html.AnchorElement(href: resourceDTO.url!);
anchorElement.download = '${resourceDTO.label}.json';
anchorElement.target = '_blank';
anchorElement.click();
}
},
fontSize: 16,
),
RoundedButton(
text: AppLocalizations.of(context)!.save,
icon: Icons.check,
color: kPrimaryColor,
press: () {
if (resourceDTO.label != null && resourceDTO.label!.length > 2) {
Navigator.pop(context, resourceDTO);
}
},
fontSize: 16,
),
],
),
],
),
),
),
), context: context
);
return result;
}
Future<void> delete(ResourceDTO resourceDTO, AppContext appContext, context) async {
showConfirmationDialog(
AppLocalizations.of(context)!.resourceDeleteConfirm,
() {},
() async {
await (appContext.getContext() as ManagerAppContext).clientAPI!.resourceApi!.resourceDelete(resourceDTO.id!);
FirebaseStorage storage = FirebaseStorage.instance;
Reference storageReference = storage.ref().child('pictures/${appContext.getContext().instanceId}/${resourceDTO.id}');
try {
await storageReference.delete();
print('Fichier supprimé avec succès');
} catch (e) {
print('Erreur lors de la suppression du fichier : $e');
}
try {} catch (e) {}
ManagerAppContext managerAppContext = appContext.getContext();
appContext.setContext(managerAppContext);
Navigator.of(context).pop();
showNotification(kSuccess, kWhite, 'La ressource a été supprimée avec succès', context, null);
},
context
);
}