forked from coronavirus-diary/coronavirus-diary
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
554 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'questions_bloc.dart'; | ||
export 'questions_event.dart'; | ||
export 'questions_state.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'dart:async'; | ||
import 'dart:developer' as developer; | ||
|
||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import 'package:coronavirus_diary/src/data/repositories/questions.dart'; | ||
import 'questions.dart'; | ||
|
||
class QuestionsBloc extends Bloc<QuestionsEvent, QuestionsState> { | ||
final QuestionsRepository questionsRepository; | ||
|
||
QuestionsBloc({@required this.questionsRepository}); | ||
|
||
@override | ||
QuestionsState get initialState => QuestionsStateNotLoaded(); | ||
|
||
@override | ||
Stream<QuestionsState> mapEventToState(QuestionsEvent event) async* { | ||
switch (event.runtimeType) { | ||
case LoadQuestions: | ||
yield* _mapLoadQuestionsToState(event); | ||
break; | ||
} | ||
} | ||
|
||
Stream<QuestionsState> _mapLoadQuestionsToState(LoadQuestions event) async* { | ||
yield QuestionsStateLoading(); | ||
|
||
try { | ||
final questions = await questionsRepository.listQuestions(); | ||
yield QuestionsStateLoaded(questions); | ||
} catch (exception) { | ||
developer.log( | ||
'Could not load questions list', | ||
error: exception, | ||
); | ||
yield QuestionsStateLoadingFailed(exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
abstract class QuestionsEvent extends Equatable { | ||
const QuestionsEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class LoadQuestions extends QuestionsEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
import 'package:coronavirus_diary/src/data/models/questions.dart'; | ||
|
||
abstract class QuestionsState extends Equatable { | ||
const QuestionsState(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class QuestionsStateNotLoaded extends QuestionsState {} | ||
|
||
class QuestionsStateLoading extends QuestionsState {} | ||
|
||
class QuestionsStateLoaded extends QuestionsState { | ||
final List<Question> questions; | ||
|
||
const QuestionsStateLoaded(this.questions); | ||
|
||
@override | ||
List<Object> get props => [questions]; | ||
} | ||
|
||
class QuestionsStateLoadingFailed extends QuestionsState { | ||
final Exception exception; | ||
|
||
const QuestionsStateLoadingFailed([this.exception]); | ||
|
||
@override | ||
String toString() => 'CourseListError { exception: $exception }'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'questions.g.dart'; | ||
|
||
abstract class Question { | ||
final String id; | ||
final String title; | ||
final String subtitle; | ||
|
||
Question({ | ||
this.id, | ||
this.title, | ||
this.subtitle, | ||
}); | ||
|
||
factory Question.fromJson(Map<String, dynamic> json) { | ||
switch (json['type']) { | ||
case SliderQuestion.TYPE: | ||
return SliderQuestion.fromJson(json); | ||
default: | ||
return UnknownQuestion.fromJson(json); | ||
} | ||
} | ||
Map<String, dynamic> toJson() => throw UnimplementedError(); | ||
} | ||
|
||
@JsonSerializable() | ||
class SliderQuestion extends Question { | ||
static const TYPE = 'slider'; | ||
|
||
final double min; | ||
final double max; | ||
final double initialValue; | ||
final Map<String, String> labels; | ||
|
||
SliderQuestion({ | ||
id, | ||
title, | ||
subtitle, | ||
this.min, | ||
this.max, | ||
this.initialValue, | ||
this.labels, | ||
}) : super( | ||
id: id, | ||
title: title, | ||
subtitle: subtitle, | ||
); | ||
|
||
factory SliderQuestion.fromJson(Map<String, dynamic> json) => | ||
_$SliderQuestionFromJson(json); | ||
Map<String, dynamic> toJson() { | ||
Map<String, dynamic> json = _$SliderQuestionToJson(this); | ||
json['type'] = TYPE; | ||
return json; | ||
} | ||
} | ||
|
||
@JsonSerializable() | ||
class UnknownQuestion extends Question { | ||
final String type; | ||
|
||
UnknownQuestion({ | ||
id, | ||
title, | ||
subtitle, | ||
this.type, | ||
}) : super( | ||
id: id, | ||
title: title, | ||
subtitle: subtitle, | ||
); | ||
|
||
factory UnknownQuestion.fromJson(Map<String, dynamic> json) => | ||
_$UnknownQuestionFromJson(json); | ||
Map<String, dynamic> toJson() => _$UnknownQuestionToJson(this); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:coronavirus_diary/src/data/models/questions.dart'; | ||
|
||
class QuestionsRepository { | ||
Future<List<Question>> listQuestions() async => [ | ||
SliderQuestion( | ||
title: 'Are you experiencing shortness of breath?', | ||
subtitle: "Do you feel like you can't get enough air?", | ||
min: 0, | ||
max: 4, | ||
initialValue: 0, | ||
), | ||
SliderQuestion( | ||
title: 'Do you have a cough?', | ||
min: 0, | ||
max: 4, | ||
initialValue: 0, | ||
labels: { | ||
'0': 'Never', | ||
'4': 'Constantly', | ||
}, | ||
), | ||
SliderQuestion( | ||
title: "Have you felt like you've had a fever?", | ||
min: 0, | ||
max: 4, | ||
initialValue: 0, | ||
labels: { | ||
'0': "No", | ||
'2': "Maybe?", | ||
'4': "I'm burning up.", | ||
}, | ||
), | ||
]; | ||
} |
Oops, something went wrong.