You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm-flutter-app/lib/ui/disclosure/disclosure_provider.dart

190 lines
6.8 KiB
Dart

import 'dart:async';
import 'dart:developer';
import 'package:appinio_swiper/appinio_swiper.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/marathon/demo_marathon_repo.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/models/disclosure/disclosure_details_model.dart';
import 'package:mohem_flutter_app/models/disclosure/disclosure_question_model.dart';
import 'package:mohem_flutter_app/ui/disclosure/widgets/disclosure_question_card.dart';
class DisclosureProvider extends ChangeNotifier {
//************************************************ VARIABLES **********************************************************
final AppinioSwiperController swiperController = AppinioSwiperController();
DisclosureQuestionsOptionStatus currentQuestionSelectionStatus = DisclosureQuestionsOptionStatus.unSelected;
DisclosureDetailsModel disclosureDetailsModel = DisclosureDetailsModel();
List<DisclosureCardContent> cardContentList = <DisclosureCardContent>[];
DisclosureQuestionModel currentQuestion = DisclosureQuestionModel();
List<DisclosureQuestionCardStatus> answerStatusesList = <DisclosureQuestionCardStatus>[];
DisclosureQuestionCardStatus questionCardStatus = DisclosureQuestionCardStatus.question;
int? selectedOptionIndex;
String? selectedOptionId;
int? totalQualifiers;
Locale savedLocale = const Locale("en", "US");
bool iAmWinner = false;
bool isGettingQualifiers = false;
bool isPrivilegedWithMarathon = false;
bool _isLoading = false;
bool get isLoading => _isLoading;
set isLoading(bool value) {
_isLoading = value;
notifyListeners();
}
bool _isButtonEnabled = false;
bool get isButtonEnabled => _isButtonEnabled;
set isButtonEnabled(bool value) {
_isButtonEnabled = value;
notifyListeners();
}
int _currentQuestionNumber = 0;
int get currentQuestionNumber => _currentQuestionNumber;
set currentQuestionNumber(int value) {
_currentQuestionNumber = value;
notifyListeners();
}
String currentAdditionalText = "";
void updateCurrentAdditionalText(String value) {
currentAdditionalText = value;
notifyListeners();
}
void updateCurrentSelectionYesNo(DisclosureQuestionsOptionStatus value) {
currentQuestionSelectionStatus = value;
notifyListeners();
}
//************************************************ FUNCTIONS **********************************************************
Future<void> callNextQuestionApi() async {
if (currentQuestionNumber < (disclosureDetailsModel.totalQuestions!)) {
if (currentQuestionNumber == 0) {
for (int i = 1; i <= disclosureDetailsModel.totalQuestions!; i++) {
cardContentList.add(const DisclosureCardContent());
}
currentQuestion = await DisclosureRepo().getDisclosureNextQuestion(currentQuestionNumber: currentQuestionNumber);
updateCardData();
if (Utils.isLoading) {
Utils.hideLoading(AppRoutes.navigatorKey.currentContext!);
}
Navigator.pushReplacementNamed(AppRoutes.navigatorKey.currentContext!, AppRoutes.disclosureScreen);
} else {
currentQuestion = await DisclosureRepo().getDisclosureNextQuestion(currentQuestionNumber: currentQuestionNumber);
updateCardData();
}
notifyListeners();
}
}
Future<void> callPreviousQuestionApi() async {
currentQuestion = await DisclosureRepo().getDisclosureNextQuestion(currentQuestionNumber: currentQuestionNumber - 2);
updateCardData(isForPrevious: true);
notifyListeners();
}
void updateCardData({bool isForPrevious = false}) {
if (isForPrevious) {
selectedOptionIndex = null;
currentQuestionNumber--;
cardContentList.add(const DisclosureCardContent());
cardContentList.add(const DisclosureCardContent());
swiperController.swipeLeft();
questionCardStatus = DisclosureQuestionCardStatus.question;
notifyListeners();
return;
}
if (currentQuestionNumber > 0) {
swiperController.swipeRight();
}
selectedOptionIndex = null;
currentQuestionNumber++;
questionCardStatus = DisclosureQuestionCardStatus.question;
notifyListeners();
}
void populateQuestionStatusesList() {
answerStatusesList.clear();
if (disclosureDetailsModel.totalQuestions != null) {
for (int i = 0; i < disclosureDetailsModel.totalQuestions!; i++) {
answerStatusesList.add(DisclosureQuestionCardStatus.question);
}
notifyListeners();
}
}
void updateCurrentQuestionOptionStatus({required DisclosureQuestionsOptionStatus status, required int selectedOptIndex, required int correctOptionIndex}) {
if (selectedOptIndex == 0) {
updateCurrentSelectionYesNo(DisclosureQuestionsOptionStatus.correct);
} else if (selectedOptIndex == 1) {
updateCurrentSelectionYesNo(DisclosureQuestionsOptionStatus.wrong);
}
for (int i = 0; i < currentQuestion.questionOptions!.length; i++) {
currentQuestion.questionOptions![i].optionStatus = DisclosureQuestionsOptionStatus.unSelected;
}
if (status == DisclosureQuestionsOptionStatus.wrong) {
currentQuestion.questionOptions![correctOptionIndex].optionStatus = DisclosureQuestionsOptionStatus.correct; // if person's answer is wrong we have to show him the actual right answer
}
currentQuestion.questionOptions![selectedOptIndex].optionStatus = status;
selectedOptionId = currentQuestion.questionOptions![selectedOptIndex].id;
selectedOptionIndex = selectedOptIndex;
notifyListeners();
}
void updateQuestionCardStatus(DisclosureQuestionCardStatus status) {
questionCardStatus = status;
notifyListeners();
}
void updateAnswerStatusesList(DisclosureQuestionCardStatus status) {
answerStatusesList[currentQuestionNumber - 1] = status;
notifyListeners();
}
void resetValues() async {
_currentQuestionNumber = 0;
iAmWinner = false;
cardContentList.clear();
isButtonEnabled = false;
currentQuestion = DisclosureQuestionModel();
if (answerStatusesList.isNotEmpty) {
for (int i = 0; i < answerStatusesList.length; i++) {
answerStatusesList[i] = DisclosureQuestionCardStatus.question;
}
}
currentQuestionSelectionStatus = DisclosureQuestionsOptionStatus.unSelected;
AppRoutes.navigatorKey.currentContext!.setLocale(savedLocale);
notifyListeners();
}
Future<void> getDisclosureDetails() async {
isLoading = true;
notifyListeners();
disclosureDetailsModel = await DisclosureRepo().getDisclosureDetails();
populateQuestionStatusesList();
isLoading = false;
notifyListeners();
}
Future<void> onStartDisclosurePressed(BuildContext context) async {
await callNextQuestionApi();
}
}