Skip to content

Commit

Permalink
Remove questions state from checkup
Browse files Browse the repository at this point in the history
  • Loading branch information
ferndot committed Mar 20, 2020
1 parent a6c4254 commit a6e1158
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 49 deletions.
2 changes: 1 addition & 1 deletion lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DiaryApp extends StatelessWidget {
create: (context) {
return QuestionsBloc(
questionsRepository: QuestionsRepository(),
);
)..add(LoadQuestions());
},
),
],
Expand Down
67 changes: 19 additions & 48 deletions lib/src/ui/screens/checkup/checkup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';

import 'package:coronavirus_diary/src/blocs/checkup/checkup.dart';
import 'package:coronavirus_diary/src/blocs/questions/questions.dart';
import 'package:coronavirus_diary/src/ui/widgets/loading_indicator.dart';
import 'checkup_loaded_body.dart';

Expand Down Expand Up @@ -33,52 +32,33 @@ class _CheckupScreenState extends State<CheckupScreen> {

Widget _getUnloadedBody(
CheckupState checkupState,
QuestionsState questionsState,
) {
if (questionsState is QuestionsStateNotLoaded) {
context.bloc<QuestionsBloc>().add(LoadQuestions());
}
if (checkupState is CheckupStateNotCreated) {
context.bloc<CheckupBloc>().add(StartCheckup());
}
return LoadingIndicator('Loading your health checkup');
}

Widget _getErrorBody(QuestionsState state) {
Widget errorBody;

if (state is QuestionsStateLoaded && state.questions.length == 0) {
errorBody = Text(
'The checkup experience is not currently available. Please try again later.',
textAlign: TextAlign.center,
);
} else {
errorBody = Text(
'There was an error retrieving the checkup experience. Please try again later.',
textAlign: TextAlign.center,
);
}

Widget _getErrorBody() {
return Center(
child: Padding(
padding: EdgeInsets.all(20),
child: errorBody,
child: Text(
'There was an error retrieving the checkup experience. Please try again later.',
textAlign: TextAlign.center,
),
),
);
}

Widget _getBody(CheckupState checkupState, QuestionsState questionsState) {
if (questionsState is QuestionsStateNotLoaded ||
questionsState is QuestionsStateLoading ||
checkupState is CheckupStateNotCreated ||
Widget _getBody(CheckupState checkupState) {
if (checkupState is CheckupStateNotCreated ||
checkupState is CheckupStateCreating) {
return _getUnloadedBody(checkupState, questionsState);
} else if (questionsState is QuestionsStateLoaded &&
questionsState.questions.length > 0 &&
checkupState is CheckupStateInProgress) {
return _getUnloadedBody(checkupState);
} else if (checkupState is CheckupStateInProgress) {
return CheckupLoadedBody();
} else {
return _getErrorBody(questionsState);
return _getErrorBody();
}
}

Expand All @@ -88,24 +68,15 @@ class _CheckupScreenState extends State<CheckupScreen> {
builder: (context, state) {
final CheckupState checkupState = state;

return BlocBuilder<QuestionsBloc, QuestionsState>(
builder: (context, state) {
final QuestionsState questionsState = state;

return ChangeNotifierProvider<PageController>(
create: (context) => _pageController,
child: Scaffold(
appBar: AppBar(
title: Text('Your Health Checkup'),
),
backgroundColor: Theme.of(context).primaryColor,
body: _getBody(
checkupState,
questionsState,
),
),
);
},
return ChangeNotifierProvider<PageController>(
create: (context) => _pageController,
child: Scaffold(
appBar: AppBar(
title: Text('Your Health Checkup'),
),
backgroundColor: Theme.of(context).primaryColor,
body: _getBody(checkupState),
),
);
},
);
Expand Down

0 comments on commit a6e1158

Please sign in to comment.