Skip to content

Commit

Permalink
remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanguinho committed Oct 22, 2020
1 parent 247bd33 commit 39de388
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions lib/presentation/presenters/getx_survey_result_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:get/get.dart';
import 'package:meta/meta.dart';

import '../../domain/entities/entities.dart';
import '../../domain/helpers/helpers.dart';
import '../../domain/usecases/usecases.dart';
import '../../ui/helpers/helpers.dart';
Expand All @@ -22,34 +23,17 @@ class GetxSurveyResultPresenter extends GetxController with LoadingManager, Sess
});

Future<void> loadData() async {
try {
isLoading = true;
final surveyResult = await loadSurveyResult.loadBySurvey(surveyId: surveyId);
_surveyResult.value = SurveyResultViewModel(
surveyId: surveyResult.surveyId,
question: surveyResult.question,
answers: surveyResult.answers.map((answer) => SurveyAnswerViewModel(
image: answer.image,
answer: answer.answer,
percent: '${answer.percent}%',
isCurrentAnswer: answer.isCurrentAnswer
)).toList()
);
} on DomainError catch(error) {
if (error == DomainError.accessDenied) {
isSessionExpired = true;
} else {
_surveyResult.subject.addError(UIError.unexpected.description);
}
} finally {
isLoading = false;
}
showResultOnAction(() => loadSurveyResult.loadBySurvey(surveyId: surveyId));
}

Future<void> save({@required String answer}) async {
showResultOnAction(() => saveSurveyResult.save(answer: answer));
}

Future<void> showResultOnAction(Future<SurveyResultEntity> action()) async {
try {
isLoading = true;
final surveyResult = await saveSurveyResult.save(answer: answer);
final surveyResult = await action();
_surveyResult.value = SurveyResultViewModel(
surveyId: surveyResult.surveyId,
question: surveyResult.question,
Expand Down

0 comments on commit 39de388

Please sign in to comment.