add pickup provider
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/data/services/services.dart';
|
||||
import 'package:fcs/domain/constants.dart';
|
||||
import 'package:fcs/domain/entities/package.dart';
|
||||
import 'package:fcs/domain/entities/pickup.dart';
|
||||
import 'package:fcs/domain/entities/processing.dart';
|
||||
import 'package:fcs/helpers/firebase_helper.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:fcs/pagination/paginator_listener.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:path/path.dart' as Path;
|
||||
|
||||
class PickupModel extends BaseModel {
|
||||
// List<Processing> pickups = [];
|
||||
final log = Logger('PickupModel');
|
||||
|
||||
StreamSubscription<QuerySnapshot>? listener;
|
||||
@@ -48,7 +53,33 @@ class PickupModel extends BaseModel {
|
||||
// pickups = [];
|
||||
}
|
||||
|
||||
Future<void> createProcessing(Processing processing) async {}
|
||||
Future<void> complete(
|
||||
Pickup pickup, List<File?> files, List<String?> deletedUrls) async {
|
||||
for (String? url in deletedUrls) {
|
||||
pickup.photoUrls.remove(url);
|
||||
}
|
||||
|
||||
Future<void> updateProcessing(Processing processing) async {}
|
||||
// check files count
|
||||
var count = (pickup.photoUrls.length) + files.length - (deletedUrls.length);
|
||||
if (count > uploadPhotoLimit)
|
||||
throw Exception("Exceed number of file upload");
|
||||
|
||||
List<String> uploadedURLs = [];
|
||||
String path = Path.join(pickups_files_path, pickup.id);
|
||||
uploadedURLs = await uploadFiles(path, files);
|
||||
pickup.photoUrls.addAll(uploadedURLs);
|
||||
pickup.photoUrls.removeWhere((e) => deletedUrls.contains(e));
|
||||
|
||||
try {
|
||||
await Services.instance.pickupService.completePickup(pickup);
|
||||
} catch (e) {
|
||||
// delete newly uploaded photos if fails
|
||||
try {
|
||||
deleteStorageFromUrls(uploadedURLs);
|
||||
pickup.photoUrls.removeWhere((i) => uploadedURLs.contains(i));
|
||||
} catch (e) {}
|
||||
throw e;
|
||||
}
|
||||
return deleteStorageFromUrls(deletedUrls);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user