manager-app/lib/Screens/Resources/new_resource_popup.dart
2021-05-11 18:13:02 +02:00

138 lines
4.7 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,
)
),
],
),
),
),
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) {
Navigator.of(context).pop();
print(resourceDetailDTO.type);
if(resourceDetailDTO.type == ResourceType.image || resourceDetailDTO.type == ResourceType.video) {
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();
print("RESULT");
print(res.statusCode);
// TODO add message if status code not ok
} else {
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);
}
}