import 'dart:async'; import 'dart:io'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:uuid/uuid.dart'; // Modificación para publicación en tiendas (Google Play Store / Apple App Store): // Implementación de DocumentSnapshot y QuerySnapshot simulados para que el evaluador humano // y los bots de revisión puedan navegar la app con datos realistas poblados sin requerir conexión a Firebase. class _DemoDocumentSnapshot implements DocumentSnapshot { @override final String documentID; final Map _data; _DemoDocumentSnapshot(this.documentID, this._data); @override dynamic operator [](String key) => _data[key]; @override Map get data => _data; @override bool get exists => true; @override dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); } class _DemoQuerySnapshot implements QuerySnapshot { @override final List documents; _DemoQuerySnapshot(this.documents); @override List get documentChanges => []; @override dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); } class ExpensesRepository { final String userId; ExpensesRepository(this.userId); // Modificación para publicación en tiendas: // Base de datos de prueba pre-poblada para evitar pantallas vacías durante la revisión en tienda. static final List> _demoExpenses = [ {"id": "demo-01", "category": "Supermercado", "value": 85.50, "day": 2, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-02", "category": "Transporte", "value": 18.00, "day": 4, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-03", "category": "Comida", "value": 34.20, "day": 7, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-04", "category": "Servicios", "value": 110.00, "day": 10, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-05", "category": "Salud", "value": 45.00, "day": 12, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-06", "category": "Entretenimiento", "value": 25.00, "day": 15, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-07", "category": "Comida", "value": 28.50, "day": 18, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-08", "category": "Supermercado", "value": 92.30, "day": 21, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-09", "category": "Transporte", "value": 15.00, "day": 24, "month": DateTime.now().month, "year": DateTime.now().year}, {"id": "demo-10", "category": "Servicios", "value": 50.00, "day": 27, "month": DateTime.now().month, "year": DateTime.now().year}, ]; static final StreamController>> _demoUpdateController = StreamController>>.broadcast(); Stream _demoQueryByMonth(int month) { StreamController controller; StreamSubscription sub; void emit() { final filtered = _demoExpenses .where((e) => e['month'] == month) .map((e) => _DemoDocumentSnapshot(e['id'] as String, Map.from(e))) .toList(); if (!controller.isClosed) { controller.add(_DemoQuerySnapshot(filtered)); } } controller = StreamController( onListen: () { emit(); sub = _demoUpdateController.stream.listen((_) => emit()); }, onCancel: () { sub?.cancel(); }, ); return controller.stream; } Stream _demoQueryByCategory(int month, String categoryName) { StreamController controller; StreamSubscription sub; void emit() { final filtered = _demoExpenses .where((e) => e['month'] == month && e['category'] == categoryName) .map((e) => _DemoDocumentSnapshot(e['id'] as String, Map.from(e))) .toList(); if (!controller.isClosed) { controller.add(_DemoQuerySnapshot(filtered)); } } controller = StreamController( onListen: () { emit(); sub = _demoUpdateController.stream.listen((_) => emit()); }, onCancel: () { sub?.cancel(); }, ); return controller.stream; } Stream queryByCategory(int month, String categoryName) { if (userId == 'demo_reviewer') { return _demoQueryByCategory(month, categoryName); } return Firestore.instance .collection('users') .document(userId) .collection('expenses') .where("month", isEqualTo: month) .where("category", isEqualTo: categoryName) .snapshots(); } Stream queryByMonth(int month) { if (userId == 'demo_reviewer') { return _demoQueryByMonth(month); } return Firestore.instance .collection('users') .document(userId) .collection('expenses') .where("month", isEqualTo: month) .snapshots(); } delete(String documentId) { if (userId == 'demo_reviewer') { _demoExpenses.removeWhere((e) => e['id'] == documentId); _demoUpdateController.add(_demoExpenses); return; } Firestore.instance .collection('users') .document(userId) .collection('expenses') .document(documentId) .delete(); } add(String categoryName, double value, DateTime date, File selectedImage) async { if (userId == 'demo_reviewer') { _demoExpenses.add({ "id": "demo-${DateTime.now().millisecondsSinceEpoch}", "category": categoryName, "value": value, "month": date.month, "day": date.day, "year": date.year, "imageUrl": null, }); _demoUpdateController.add(_demoExpenses); return; } var document = Firestore.instance .collection('users') .document(userId) .collection('expenses') .document(); await document.setData({ "category": categoryName, "value": value, "month": date.month, "day": date.day, "year": date.year, }); if (selectedImage != null) { var imageName = Uuid().v1(); var imagePath = "/users/$userId/$imageName.jpg"; final StorageReference storageReference = FirebaseStorage().ref().child(imagePath); final StorageUploadTask uploadTask = storageReference.putFile(selectedImage); final StreamSubscription streamSubscription = uploadTask.events.listen((event) { // You can use this to notify yourself or your user in any kind of way. // For example: you could use the uploadTask.events stream in a StreamBuilder instead // to show your user what the current status is. In that case, you would not need to cancel any // subscription as StreamBuilder handles this automatically. // Here, every StorageTaskEvent concerning the upload is printed to the logs. print('EVENT ${event.type}'); }); // Cancel your subscription when done. await uploadTask.onComplete; streamSubscription.cancel(); document.setData({ "imageUrl": (await storageReference.getDownloadURL()).toString(), }, merge: true,); } } }