Added validation for the new task screen
This commit is contained in:
parent
0901688163
commit
213c244553
3 changed files with 41 additions and 18 deletions
|
|
@ -2,6 +2,7 @@ import 'dart:async';
|
|||
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import '../utils.dart' show Validators;
|
||||
import '../models/task_model.dart';
|
||||
import '../models/user_model.dart';
|
||||
import '../resources/authService.dart';
|
||||
|
|
@ -9,37 +10,38 @@ import '../resources/firestore_provider.dart';
|
|||
|
||||
// TODO: Add validation.
|
||||
|
||||
class NewTaskBloc {
|
||||
class NewTaskBloc extends Object with Validators {
|
||||
final AuthService _auth = authService;
|
||||
final FirestoreProvider _firestore = firestoreProvider;
|
||||
final _user = BehaviorSubject<UserModel>();
|
||||
final _eventName = BehaviorSubject<String>();
|
||||
final _taskText = BehaviorSubject<String>();
|
||||
|
||||
String text = '';
|
||||
TaskPriority priority = TaskPriority.high;
|
||||
|
||||
//Stream getters.
|
||||
Observable<UserModel> get userModelStream => _user.stream;
|
||||
Observable<String> get eventName => _eventName.stream;
|
||||
Observable<String> get taskText =>
|
||||
_taskText.stream.transform(validateStringNotEmpty);
|
||||
Observable<bool> get submitEnabled =>
|
||||
Observable.combineLatest2(eventName, taskText, (a, b) => true);
|
||||
|
||||
//Sinks getters.
|
||||
Function(String) get changeEventName => _eventName.sink.add;
|
||||
Function(String) get changeTaskText => _taskText.sink.add;
|
||||
|
||||
NewTaskBloc() {
|
||||
setCurrentUser();
|
||||
}
|
||||
|
||||
void setText(String newText) {
|
||||
text = newText;
|
||||
}
|
||||
|
||||
void setPriority(TaskPriority newPriority) {
|
||||
priority = newPriority;
|
||||
}
|
||||
|
||||
Future<void> submit() {
|
||||
final newTask = TaskModel(
|
||||
text: text,
|
||||
text: _taskText.value,
|
||||
priority: priority,
|
||||
event: _eventName.value,
|
||||
ownerUsername: _user.value.username,
|
||||
|
|
@ -55,6 +57,7 @@ class NewTaskBloc {
|
|||
}
|
||||
|
||||
void dispose() {
|
||||
_taskText.close();
|
||||
_user.close();
|
||||
_eventName.close();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
|
|||
children: <Widget>[
|
||||
BigTextInput(
|
||||
height: 95,
|
||||
onChanged: bloc.setText,
|
||||
onChanged: bloc.changeTaskText,
|
||||
),
|
||||
SizedBox(
|
||||
height: 15,
|
||||
|
|
@ -54,15 +54,21 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
|
|||
SizedBox(
|
||||
height: 20,
|
||||
),
|
||||
GradientTouchableContainer(
|
||||
height: 40,
|
||||
radius: 8,
|
||||
isExpanded: true,
|
||||
onTap: () => onSubmit(context, bloc),
|
||||
child: Text(
|
||||
'Submit',
|
||||
style: kSmallTextStyle,
|
||||
),
|
||||
StreamBuilder<Object>(
|
||||
stream: bloc.submitEnabled,
|
||||
builder: (context, submitSnap) {
|
||||
return GradientTouchableContainer(
|
||||
height: 40,
|
||||
radius: 8,
|
||||
isExpanded: true,
|
||||
enabled: submitSnap.hasData,
|
||||
onTap: () => onSubmit(context),
|
||||
child: Text(
|
||||
'Submit',
|
||||
style: kSmallTextStyle,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
@ -127,7 +133,7 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
|
|||
);
|
||||
}
|
||||
|
||||
void onSubmit(BuildContext context, NewTaskBloc bloc) async {
|
||||
void onSubmit(BuildContext context) async {
|
||||
await bloc.submit();
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import './models/task_model.dart';
|
||||
|
||||
|
|
@ -37,3 +39,15 @@ Color getColorFromPriority(TaskPriority priority) {
|
|||
return Colors.white;
|
||||
}
|
||||
}
|
||||
|
||||
class Validators {
|
||||
final validateStringNotEmpty = StreamTransformer<String, String>.fromHandlers(
|
||||
handleData: (String string, EventSink<String> sink) {
|
||||
if (string.isEmpty) {
|
||||
sink.addError('Text cannot be empty');
|
||||
} else {
|
||||
sink.add(string);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue