Skip to content

Commit

Permalink
Add some subjective questions
Browse files Browse the repository at this point in the history
  • Loading branch information
ferndot committed Mar 19, 2020
1 parent 1880ed2 commit f82a161
Show file tree
Hide file tree
Showing 19 changed files with 554 additions and 243 deletions.
11 changes: 10 additions & 1 deletion lib/src/app.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'blocs/preferences/preferences.dart';
import 'package:coronavirus_diary/src/blocs/preferences/preferences.dart';
import 'package:coronavirus_diary/src/blocs/questions/questions.dart';
import 'package:coronavirus_diary/src/data/repositories/questions.dart';
import 'ui/router.dart';

class DiaryApp extends StatelessWidget {
Expand All @@ -14,6 +16,13 @@ class DiaryApp extends StatelessWidget {
return PreferencesBloc();
},
),
BlocProvider<QuestionsBloc>(
create: (context) {
return QuestionsBloc(
questionsRepository: QuestionsRepository(),
);
},
),
],
child: BlocBuilder<PreferencesBloc, PreferencesState>(
builder: (context, state) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/blocs/questions/questions.dart
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';
41 changes: 41 additions & 0 deletions lib/src/blocs/questions/questions_bloc.dart
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);
}
}
}
10 changes: 10 additions & 0 deletions lib/src/blocs/questions/questions_event.dart
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 {}
32 changes: 32 additions & 0 deletions lib/src/blocs/questions/questions_state.dart
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 }';
}
77 changes: 77 additions & 0 deletions lib/src/data/models/questions.dart
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);
}
49 changes: 49 additions & 0 deletions lib/src/data/models/questions.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions lib/src/data/repositories/questions.dart
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.",
},
),
];
}
Loading

0 comments on commit f82a161

Please sign in to comment.