manager-app/lib/Screens/Resources/new_resource_popup.dart
2021-05-28 19:51:05 +02:00

169 lines
6.0 KiB
Dart

import 'dart:io';
import 'package:manager_app/Components/resource_tab.dart';
import 'package:flutter/material.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:managerapi/api.dart';
import 'package:http/http.dart' as http;
void showNewResource(AppContext appContext, BuildContext context) {
ResourceDetailDTO resourceDetailDTO = new ResourceDetailDTO();
Size size = MediaQuery.of(context).size;
var fileName;
File fileToSend;
showDialog(
builder: (BuildContext context) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))
),
content: Container(
width: size.width *0.5,
child: SingleChildScrollView(
child: Column(
children: [
Text("Nouvelle ressource", style: new TextStyle(fontSize: 25, fontWeight: FontWeight.w400)),
Column(
children: [
StringInputContainer(
label: "Nom :",
initialValue: resourceDetailDTO.label,
onChanged: (value) {
resourceDetailDTO.label = value;
},
),
if (fileName != null) new Text(fileName),
],
),
Container(
width: size.width *0.5,
height: size.height *0.5,
child: ResourceTab(
resourceDetailDTO: resourceDetailDTO,
onFileUpload: (File file) {
fileToSend = file;
},
)
),
],
),
),
),
actions: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Align(
alignment: AlignmentDirectional.bottomEnd,
child: Container(
width: 175,
height: 70,
child: RoundedButton(
text: "Annuler",
icon: Icons.undo,
color: kSecond,
press: () {
Navigator.of(context).pop();
},
fontSize: 20,
),
),
),
Align(
alignment: AlignmentDirectional.bottomEnd,
child: Container(
width: 150,
height: 70,
child: RoundedButton(
text: "Créer",
icon: Icons.check,
color: kPrimaryColor,
textColor: kWhite,
press: () {
create(resourceDetailDTO, fileToSend, appContext, context);
},
fontSize: 20,
),
),
),
],
),
],
), context: context
);
}
void create(ResourceDetailDTO resourceDetailDTO, File file, AppContext appContext, context) async {
if (resourceDetailDTO.label != null) {
switch(resourceDetailDTO.type) {
case ResourceType.image:
case ResourceType.video:
if (file != null) {
var request = http.MultipartRequest('POST', Uri.parse(appContext.getContext().clientAPI.resourceApi.apiClient.basePath+"/api/Resource/upload"));
request.files.add(
await http.MultipartFile(
'picture',
file.readAsBytes().asStream(),
file.lengthSync(),
filename: file.path.toString().split("/").last
)
);
// Todo Add header with bearer token (get via managercontext - token value)
//request.headers.addEntries(newEntries)
request.fields['label'] = resourceDetailDTO.label;
request.fields['type'] = ResourceType.image.toString();
var res = await request.send();
if (res.statusCode == 200) {
// To refresh only (UGLY COOOOODE)
ManagerAppContext managerAppContext = appContext.getContext();
appContext.setContext(managerAppContext);
showNotification(Colors.green, kWhite, 'La ressource a été créée avec succès', context);
} else {
showNotification(kPrimaryColor, kWhite, 'Une erreur est survenue lors de la création de la ressource', context);
}
} else {
showNotification(Colors.orange, kWhite, 'Aucun fichier n\'a été chargé', context);
}
Navigator.of(context).pop();
break;
case ResourceType.imageUrl:
case ResourceType.videoUrl:
if (resourceDetailDTO.data != null) {
// test if Correct url
bool _validURL = Uri.parse(resourceDetailDTO.data).isAbsolute;
if(_validURL) {
Navigator.of(context).pop();
ResourceDetailDTO newResource = await appContext.getContext().clientAPI.resourceApi.resourceCreate(resourceDetailDTO);
// To refresh only (UGLY COOOOODE)
ManagerAppContext managerAppContext = appContext.getContext();
appContext.setContext(managerAppContext);
showNotification(Colors.green, kWhite, 'La ressource a été créée avec succès', context);
} else {
showNotification(Colors.orange, kWhite, 'L\'url est invalide', context);
}
} else {
showNotification(Colors.orange, kWhite, 'Veuillez remplir le champ URL', context);
}
break;
}
} else {
showNotification(Colors.orange, kWhite, 'Veuillez donner un nom à la ressource', context);
}
}