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

163 lines
6.1 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_api_new/api.dart';
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) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))
),
content: SingleChildScrollView(
child: Column(
children: [
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.6,
height: MediaQuery.of(context).size.height * 0.2,
child: StringInputContainer(
label: "Label :",
initialValue: resourceDTO.label != null ? resourceDTO.label : "",
onChanged: (value) {
resourceDTO.label = value;
},
),
),/*Text(
resourceDTO.label == null ? "" : resourceDTO.label,
style: new TextStyle(fontSize: 25, fontWeight: FontWeight.w400)),*/
),
),
Container(
height: resourceDTO.type == ResourceType.Audio ? size.height *0.1 : size.height *0.3,
width: resourceDTO.type == ResourceType.Audio ? size.width *0.1 : size.width *0.3,
child: Center(
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),
),
),
),
),
],
),
),
actions: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Align(
alignment: AlignmentDirectional.bottomEnd,
child: Container(
width: 175,
height: 70,
child: RoundedButton(
text: "Retour",
icon: Icons.undo,
color: kSecond,
press: () {
Navigator.of(context).pop();
},
fontSize: 20,
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Align(
alignment: AlignmentDirectional.bottomEnd,
child: Container(
width: 200,
height: 70,
child: RoundedButton(
text: "Supprimer",
icon: Icons.delete,
color: kError,
textColor: kWhite,
press: () {
delete(resourceDTO, appContext, context);
},
fontSize: 20,
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Align(
alignment: AlignmentDirectional.bottomEnd,
child: Container(
width: 220,
height: 70,
child: RoundedButton(
text: "Sauvegarder",
icon: Icons.check,
color: kPrimaryColor,
press: () {
if (resourceDTO.label != null && resourceDTO.label!.length > 2) {
Navigator.pop(context, resourceDTO);
}
},
fontSize: 20,
),
),
),
),
],
),
],
), context: context
);
return result;
}
Future<void> delete(ResourceDTO resourceDTO, AppContext appContext, context) async {
showConfirmationDialog(
"Êtes-vous sûr de vouloir supprimer cette ressource ?",
() {},
() async {
await (appContext.getContext() as ManagerAppContext).clientAPI!.resourceApi!.resourceDelete(resourceDTO.id!);
// TODO Remove from firebase storage via service or ?
FirebaseStorage storage = FirebaseStorage.instance;
Reference storageReference = storage.ref().child('pictures/${appContext.getContext().instanceId}/${resourceDTO.id}');
// Supprimer le fichier
try {
await storageReference.delete();
print('Fichier supprimé avec succès');
} catch (e) {
print('Erreur lors de la suppression du fichier : $e');
}
try {} catch (e) {}
// just to refresh
ManagerAppContext managerAppContext = appContext.getContext();
appContext.setContext(managerAppContext);
Navigator.of(context).pop();
showNotification(Colors.green, kWhite, 'La ressource a été supprimée avec succès', context, null);
},
context
);
}