diff --git a/lib/src/blocs/new_task_bloc.dart b/lib/src/blocs/new_task_bloc.dart index e575ebb..b7d4f07 100644 --- a/lib/src/blocs/new_task_bloc.dart +++ b/lib/src/blocs/new_task_bloc.dart @@ -3,16 +3,20 @@ import 'dart:async'; import 'package:rxdart/rxdart.dart'; import '../models/task_model.dart'; +import '../models/user_model.dart'; import '../resources/authService.dart'; import '../resources/firestore_provider.dart'; class NewTaskBloc { final AuthService _auth = authService; - FirebaseUser currentUser; + final FirestoreProvider _firestore = firestoreProvider; + final _user = BehaviorSubject(); - String text; - TaskPriority priority; - String event; + String text = ''; + TaskPriority priority = TaskPriority.high; + String event = ''; + + Observable get userModelStream => _user.stream; NewTaskBloc() { setCurrentUser(); @@ -26,8 +30,24 @@ class NewTaskBloc { priority = newPriority; } + void setEvent(String newEvent) { + event = newEvent; + } + + Future submit() { + final newTask = TaskModel( + text: text, + priority: priority, + event: event, + ownerUsername: _user.value.username, + done: false, + ); + return _firestore.addTask(newTask); + } + Future setCurrentUser() async { final user = await _auth.currentUser; - currentUser = user; + final userModel = await _firestore.getUser(username: user.email); + _user.add(userModel); } } diff --git a/lib/src/screens/new_task_screen.dart b/lib/src/screens/new_task_screen.dart index 406489a..503c5d2 100644 --- a/lib/src/screens/new_task_screen.dart +++ b/lib/src/screens/new_task_screen.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import '../blocs/new_task_bloc.dart'; +import '../models/user_model.dart'; import '../widgets/custom_app_bar.dart'; import '../widgets/custom_dropdown.dart'; import '../widgets/big_text_input.dart'; @@ -25,47 +26,60 @@ class _NewTaskScreenState extends State { ); final NewTaskBloc bloc = NewTaskBloc(); + String dropdownValue; Widget build(BuildContext context) { return Scaffold( appBar: CustomAppBar( title: 'Add task', ), - body: Padding( - padding: const EdgeInsets.only(top: 15.0, left: 20.0, right: 20.0), - child: Column( - children: [ - BigTextInput( - height: 95, - onChanged: bloc.setText, + body: StreamBuilder( + stream: bloc.userModelStream, + builder: (BuildContext context, AsyncSnapshot snap) { + List events = []; + + if (snap.hasData) { + events = snap.data.events; + } + + return Padding( + padding: const EdgeInsets.only(top: 15.0, left: 20.0, right: 20.0), + child: Column( + children: [ + BigTextInput( + height: 95, + onChanged: bloc.setText, + ), + SizedBox( + height: 15, + ), + buildDropdownSection(events), + SizedBox( + height: 15, + ), + buildPrioritySelectorSection(bloc), + SizedBox( + height: 20, + ), + GradientTouchableContainer( + height: 40, + width: 330, + radius: 8, + onTap: () => onSubmit(context, bloc), + child: Text( + 'Submit', + style: kButtonStyle, + ), + ), + ], ), - SizedBox( - height: 15, - ), - buildDropdownSection(), - SizedBox( - height: 15, - ), - buildPrioritySelectorSection(bloc), - SizedBox( - height: 20, - ), - GradientTouchableContainer( - height: 40, - width: 330, - radius: 8, - child: Text( - 'Submit', - style: kButtonStyle, - ), - ), - ], - ), + ); + }, ), ); } - Widget buildDropdownSection() { + Widget buildDropdownSection(List events) { return Row( children: [ Text( @@ -74,10 +88,11 @@ class _NewTaskScreenState extends State { ), Spacer(), CustomDropdownButton( + value: dropdownValue, + onChanged: (String value) => onDropdownChanged(bloc, value), width: 230, hint: Text('Event'), - onChanged: (item) {}, - items: ['Math', 'Lenguajes', 'Redes'].map((String name) { + items: events.map((String name) { return CustomDropdownMenuItem( value: name, child: Text( @@ -106,4 +121,16 @@ class _NewTaskScreenState extends State { ], ); } + + void onDropdownChanged(NewTaskBloc bloc, String newValue) { + bloc.setEvent(newValue); + setState(() { + dropdownValue = newValue; + }); + } + + void onSubmit(BuildContext context, NewTaskBloc bloc) async { + await bloc.submit(); + Navigator.of(context).pop(); + } }