mobdr/lib/ui/sync/upload_photos.dart

188 lines
5.6 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as path;
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:mobdr/service/shared_prefs.dart';
import 'package:mobdr/config/constant.dart';
class UploadPhotosPage extends StatefulWidget {
final int id_visite;
final List<String> photoPaths;
UploadPhotosPage({required this.id_visite, required this.photoPaths});
@override
_UploadPhotosPageState createState() => _UploadPhotosPageState();
}
class _UploadPhotosPageState extends State<UploadPhotosPage> {
bool _isUploading = false;
bool _isFinished = false;
int _totalUploaded = 0;
int _totalPhotos = 0;
@override
void initState() {
super.initState();
_totalPhotos = widget.photoPaths.length;
}
Future<void> _uploadPhotos() async {
setState(() {
_isUploading = true;
_isFinished = false;
});
for (int i = 0; i < widget.photoPaths.length; i++) {
String photoPath = SharedPrefs().photosDir + "/" + widget.photoPaths[i];
//String filename = path.basename(photoPath);
// Upload the photo
int photoId = await uploadPhoto(photoPath);
if (photoId != -1) {
_totalUploaded++;
}
}
setState(() {
_isUploading = false;
_isFinished = true;
});
}
Future<int> uploadPhoto(String photoPath) async {
try {
final url = Uri.parse(SERVLET_API);
final file = File(photoPath);
final bytes = await file.readAsBytes();
final multipartRequest = http.MultipartRequest('POST', url)
..fields['id_visite'] = widget.id_visite.toString()
..files.add(http.MultipartFile.fromBytes(
'photo',
bytes,
filename: path.basename(photoPath),
contentType: MediaType('image', 'jpeg'),
));
final headers = {'Cookie': "pguid=${SharedPrefs().guid};"};
multipartRequest.headers.addAll(headers);
final response = await multipartRequest.send();
final responseString = await response.stream.bytesToString();
final jsonResponse = jsonDecode(responseString);
final idPhotoString = jsonResponse[0]['id_photo'];
final idPhoto = int.parse(idPhotoString);
return idPhoto;
} catch (e) {
print('Error uploading photo: $e');
return -1;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Upload Photos'),
centerTitle: true,
),
body: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Upload Photos',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
SizedBox(height: 16),
Expanded(
child: Stack(
children: [
Center(
child: Visibility(
visible: _isUploading,
child: Container(
height: 150,
width: 150,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
Theme.of(context).primaryColor,
),
strokeWidth: 10,
),
),
),
),
Center(
child: Visibility(
visible: !_isUploading && _totalUploaded == _totalPhotos,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.check_circle,
size: 48,
color: Theme.of(context).primaryColor,
),
SizedBox(height: 16),
Text(
'Upload complete',
textAlign: TextAlign.center,
),
],
),
),
),
Center(
child: Visibility(
visible: !_isUploading && _totalUploaded != _totalPhotos,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.cloud_upload,
size: 48,
color: Colors.grey,
),
SizedBox(height: 16),
Text(
'Tap the button below to upload your photos',
textAlign: TextAlign.center,
),
],
),
),
),
],
),
),
SizedBox(height: 16),
Visibility(
visible: !_isUploading,
child: ElevatedButton(
child: Text('Upload Photos ($_totalUploaded / $_totalPhotos)'),
onPressed: _isFinished ? null : _uploadPhotos,
),
),
],
),
),
);
}
}