Created the upload status service to keep track of currently being uploaded files
This commit is contained in:
parent
52623c1511
commit
f503cce0e3
4 changed files with 98 additions and 3 deletions
|
|
@ -10,6 +10,7 @@ import '../models/user_model.dart';
|
||||||
import '../resources/firestore_provider.dart';
|
import '../resources/firestore_provider.dart';
|
||||||
import '../resources/firebase_storage_provider.dart';
|
import '../resources/firebase_storage_provider.dart';
|
||||||
import '../services/auth_service.dart';
|
import '../services/auth_service.dart';
|
||||||
|
import '../services/upload_status_service.dart';
|
||||||
import '../utils.dart'
|
import '../utils.dart'
|
||||||
show kTaskListPriorityTransforemer, getImageThumbnailPath;
|
show kTaskListPriorityTransforemer, getImageThumbnailPath;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -107,8 +107,10 @@ class HomeBloc {
|
||||||
_searchBoxText.add(newText);
|
_searchBoxText.add(newText);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispose() {
|
void dispose() async {
|
||||||
|
await _searchBoxText.drain();
|
||||||
_searchBoxText.close();
|
_searchBoxText.close();
|
||||||
|
await _tasks.drain();
|
||||||
_tasks.close();
|
_tasks.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import '../utils.dart' show getImageThumbnailPath;
|
import '../utils.dart' show getImageThumbnailPath;
|
||||||
|
|
@ -165,8 +167,7 @@ class _EventScreenState extends State<EventScreen>
|
||||||
Widget buildAddPictureButton() {
|
Widget buildAddPictureButton() {
|
||||||
return GradientTouchableContainer(
|
return GradientTouchableContainer(
|
||||||
radius: 8,
|
radius: 8,
|
||||||
onTap: () =>
|
onTap: onAddPicturePressed,
|
||||||
Navigator.of(context).pushNamed('newImage/${bloc.eventName}'),
|
|
||||||
child: Icon(
|
child: Icon(
|
||||||
Icons.camera_alt,
|
Icons.camera_alt,
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
|
|
@ -191,6 +192,11 @@ class _EventScreenState extends State<EventScreen>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> onAddPicturePressed() async {
|
||||||
|
await Navigator.of(context).pushNamed('newImage/${bloc.eventName}');
|
||||||
|
print('popped the pictures');
|
||||||
|
}
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
bloc.dispose();
|
bloc.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
|
|
|
||||||
86
lib/src/services/upload_status_service.dart
Normal file
86
lib/src/services/upload_status_service.dart
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:firebase_storage/firebase_storage.dart';
|
||||||
|
import 'package:rxdart/rxdart.dart';
|
||||||
|
|
||||||
|
/// A service that maintains a record of the upload operations made to
|
||||||
|
/// the firebase storage bucket.
|
||||||
|
class _UploadStatusService {
|
||||||
|
/// A subject of the current status of uploads.
|
||||||
|
final _status = BehaviorSubject<UploadStatus>();
|
||||||
|
|
||||||
|
/// A map that contains the information about all the current upload tasks.
|
||||||
|
Map<StorageUploadTask, List<int>> tasksData =
|
||||||
|
<StorageUploadTask, List<int>>{};
|
||||||
|
|
||||||
|
/// An observable of the status for all current upload tasks.
|
||||||
|
Observable<UploadStatus> get status => _status.stream;
|
||||||
|
|
||||||
|
/// Adds a new upload task to be tracked.
|
||||||
|
///
|
||||||
|
/// The task gets automatically removed when done.
|
||||||
|
void addNewUpload(StorageUploadTask task) async {
|
||||||
|
final initialSnap = task.lastSnapshot;
|
||||||
|
// Initialize the map entry with the initial values.
|
||||||
|
tasksData[task] = [
|
||||||
|
initialSnap.bytesTransferred,
|
||||||
|
initialSnap.totalByteCount,
|
||||||
|
];
|
||||||
|
task.events.listen(
|
||||||
|
(StorageTaskEvent event) {
|
||||||
|
// Update the map with the current values.
|
||||||
|
final snap = event.snapshot;
|
||||||
|
tasksData[task] = [
|
||||||
|
snap.bytesTransferred,
|
||||||
|
snap.totalByteCount,
|
||||||
|
];
|
||||||
|
_sendUpdate();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
await task.onComplete;
|
||||||
|
tasksData.remove(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates the _status subject with the most current data.
|
||||||
|
void _sendUpdate() {
|
||||||
|
int totalBytes = 0, bytestTransferred = 0;
|
||||||
|
double percentage = 0.0;
|
||||||
|
tasksData.forEach(
|
||||||
|
(_, data) {
|
||||||
|
bytestTransferred += data[0];
|
||||||
|
totalBytes += data[1];
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (bytestTransferred != 0) {
|
||||||
|
percentage = totalBytes / bytestTransferred;
|
||||||
|
}
|
||||||
|
_status.sink.add(UploadStatus(
|
||||||
|
percentage: percentage,
|
||||||
|
numberOfFiles: tasksData.length,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
dispose() async {
|
||||||
|
await _status.drain();
|
||||||
|
_status.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The status of an upload.
|
||||||
|
class UploadStatus {
|
||||||
|
/// Percentage of the upload.
|
||||||
|
final double percentage;
|
||||||
|
|
||||||
|
/// Number of files being uploaded.
|
||||||
|
///
|
||||||
|
/// It can be 0 if nothing is currently being uploaded.
|
||||||
|
final int numberOfFiles;
|
||||||
|
|
||||||
|
/// Creates an [UploadStatus] instance.
|
||||||
|
UploadStatus({
|
||||||
|
this.percentage,
|
||||||
|
this.numberOfFiles,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
final uploadStatusService = _UploadStatusService();
|
||||||
Loading…
Add table
Add a link
Reference in a new issue