Skip to content

Commit

Permalink
Half fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
domesticmouse committed Nov 27, 2018
1 parent 761fa52 commit b544351
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
27 changes: 13 additions & 14 deletions lib/bloc/painter_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:built_collection/built_collection.dart';
import 'package:rxdart/rxdart.dart';
import '../models/color.dart';
import '../models/stroke.dart';
import '../models/touch_location.dart';
Expand All @@ -25,24 +26,22 @@ class PainterBloc extends BlocBase {
int _width = 1;

// Streamed input into this BLoC
final StreamController<TouchLocation> _touchLocationController =
StreamController();
final _touchLocationController = PublishSubject<TouchLocation>();
StreamSink<TouchLocation> get touchLocation => _touchLocationController.sink;

final StreamController<Color> _colorController = StreamController();
final _colorController = PublishSubject<Color>();
StreamSink<Color> get color => _colorController.sink;

final StreamController<int> _widthController = StreamController();
final _widthController = PublishSubject<int>();
StreamSink<int> get strokeWidth => _widthController.sink;

final StreamController<void> _endTouchController = StreamController();
final _endTouchController = PublishSubject<void>();
StreamSink<void> get endTouch => _endTouchController.sink;

// Streamed output from this BLoC
final StreamController<BuiltList<Stroke>> _strokesController =
StreamController();
final _strokesController = PublishSubject<BuiltList<Stroke>>();
StreamSink<BuiltList<Stroke>> get _strokesOut => _strokesController.sink;
Stream<BuiltList<Stroke>> get strokes => _strokesController.stream;
Observable<BuiltList<Stroke>> get strokes => _strokesController.stream;

PainterBloc() {
// Adding a touch continues the current stroke
Expand All @@ -65,7 +64,7 @@ class PainterBloc extends BlocBase {
});

// And ending the touch
_touchLocationController.stream.listen((_){
_endTouchController.stream.listen((_) {
finalizeCurrentStroke();
});
}
Expand All @@ -80,11 +79,11 @@ class PainterBloc extends BlocBase {
);

void finalizeCurrentStroke() {
if (_locations.length > 0) {
_strokes = (_strokes.toBuilder()..add(_stroke)).build();
_strokesOut.add(_strokes);
_locations = ListBuilder().build();
}
if (_locations.length > 0) {
_strokes = (_strokes.toBuilder()..add(_stroke)).build();
_strokesOut.add(_strokes);
_locations = ListBuilder().build();
}
}

@override
Expand Down
26 changes: 19 additions & 7 deletions test/bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ import 'package:drawapp/models/touch_location.dart';
import 'package:drawapp/models/stroke.dart';

void main() {
test('PainterBloc', () {
test('Simple stroke', () {
final painterBloc = PainterBloc();
var _strokes = BuiltList<Stroke>();
painterBloc.strokes.listen((strokes) {
_strokes = strokes;
});
expect(_strokes.length, 0);
painterBloc.touchLocation.add(TouchLocation((builder) {
builder
..x = 1
Expand All @@ -22,6 +17,23 @@ void main() {
..x = 5
..y = 6;
}));
expect(_strokes.length, 1);
painterBloc.endTouch.add(null);
var callCount = 0;
painterBloc.strokes.listen(expectAsync1(
(strokes) {
callCount++;
if (callCount > 1) {
expect(strokes.length, 1);
expect(strokes[0].locations.length, 2);
expect(strokes[0].locations[0].x, 1);
expect(strokes[0].locations[0].y, 2);
expect(strokes[0].locations[1].x, 5);
expect(strokes[0].locations[1].y, 6);
}
},
count: 3,
));
});

test('Change color splits strokes', () {});
}

0 comments on commit b544351

Please sign in to comment.