Created method to retrieve an event from firestore only once
This commit is contained in:
parent
a9de09efd6
commit
31cb7a0e89
5 changed files with 45 additions and 5 deletions
|
|
@ -3,11 +3,14 @@ import 'dart:io';
|
|||
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import '../models/task_model.dart';
|
||||
import '../models/event_model.dart';
|
||||
import '../models/user_model.dart';
|
||||
import '../resources/authService.dart';
|
||||
import '../resources/firebase_storage_provider.dart';
|
||||
import '../resources/firestore_provider.dart';
|
||||
|
||||
// TODO: Add validation
|
||||
|
||||
class NewImageBloc {
|
||||
final AuthService _auth = authService;
|
||||
final FirestoreProvider _firestore = firestoreProvider;
|
||||
|
|
@ -37,6 +40,10 @@ class NewImageBloc {
|
|||
event = newEvent;
|
||||
}
|
||||
|
||||
Future<void> submit() {
|
||||
List<String> events = _user.value.events;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_picture.close();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ class FirebaseStorageProvider {
|
|||
: _storage = storage ?? FirebaseStorage.instance.ref(),
|
||||
_uuid = uuid ?? Uuid();
|
||||
|
||||
// TODO: Sanitize the folder argument, it's too complicated.
|
||||
|
||||
/// Uploads a given file to the firebase storage bucket.
|
||||
///
|
||||
/// It returns a [StorageUploadTask] which contains the status of the upload.
|
||||
|
|
|
|||
|
|
@ -235,7 +235,7 @@ class FirestoreProvider {
|
|||
}
|
||||
|
||||
/// Returns a Stream of a single event.
|
||||
Observable<EventModel> getEvent(String userId, String eventId) {
|
||||
Observable<EventModel> getEventObservable(String userId, String eventId) {
|
||||
final mappedStream = _firestore
|
||||
.collection('users/$userId/Events')
|
||||
.document(eventId)
|
||||
|
|
@ -252,6 +252,30 @@ class FirestoreProvider {
|
|||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
//TODO: add tests for this method.
|
||||
|
||||
/// Returns an [EventModel].
|
||||
/// Only one out of id or name can be provided, if both are provided id
|
||||
/// will have higher priority.
|
||||
Future<EventModel> getEvent(String userId,
|
||||
{String eventId, String eventName}) async {
|
||||
DocumentSnapshot documentSnapshot;
|
||||
if (eventId != null) {
|
||||
documentSnapshot =
|
||||
await _firestore.document('users/$userId/events/$eventId').get();
|
||||
} else {
|
||||
final querySnapshot = await _firestore
|
||||
.collection('users/$userId/events')
|
||||
.where('name', isEqualTo: eventName)
|
||||
.getDocuments();
|
||||
documentSnapshot = querySnapshot.documents.first;
|
||||
}
|
||||
return EventModel.fromFirestore(
|
||||
documentSnapshot.data,
|
||||
id: documentSnapshot.documentID,
|
||||
);
|
||||
}
|
||||
|
||||
/// Deletes an event from firestore.
|
||||
///
|
||||
/// It does not delete all the dependent items, this includes tasks and media.
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ class _NewImageScreenState extends State<NewImageScreen> {
|
|||
height: 40,
|
||||
isExpanded: true,
|
||||
radius: 8,
|
||||
onTap: () {},
|
||||
onTap: () => onSubmit(),
|
||||
child: Text(
|
||||
'Submit',
|
||||
style: kSmallTextStyle,
|
||||
|
|
@ -111,7 +111,7 @@ class _NewImageScreenState extends State<NewImageScreen> {
|
|||
return CustomDropdownButton(
|
||||
isExpanded: true,
|
||||
value: dropdownValue,
|
||||
onChanged: (String value) => onDropdownChanged(bloc, value),
|
||||
onChanged: (String value) => onDropdownChanged(value),
|
||||
hint: Text('Event'),
|
||||
items: events.map((String event) {
|
||||
return CustomDropdownMenuItem(
|
||||
|
|
@ -130,13 +130,18 @@ class _NewImageScreenState extends State<NewImageScreen> {
|
|||
);
|
||||
}
|
||||
|
||||
void onDropdownChanged(NewImageBloc blco, String value) {
|
||||
void onDropdownChanged(String value) {
|
||||
bloc.setEvent(value);
|
||||
setState(() {
|
||||
dropdownValue = value;
|
||||
});
|
||||
}
|
||||
|
||||
void onSubmit() async {
|
||||
await bloc.submit();
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
bloc.dispose();
|
||||
super.dispose();
|
||||
|
|
|
|||
|
|
@ -75,6 +75,8 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
|
|||
);
|
||||
}
|
||||
|
||||
// TODO: Refactor to avoid using the dropdownValue property and instead use
|
||||
// a stream from the bloc.
|
||||
Widget buildDropdownSection(List<String> events) {
|
||||
return Row(
|
||||
children: <Widget>[
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue