From 02fdfc746cc7ce576744270ee5aba689150c62b9 Mon Sep 17 00:00:00 2001 From: Faiz Hashmi Date: Thu, 22 Dec 2022 10:04:15 +0300 Subject: [PATCH] Pushing stable (acc to me :p) marathon --- lib/api/marathon/marathon_api_client.dart | 32 ++++++++++++++----- lib/ui/marathon/marathon_provider.dart | 22 +++++-------- lib/ui/marathon/marathon_screen.dart | 14 +++++--- .../widgets/marathon_details_card.dart | 2 +- lib/ui/marathon/widgets/marathon_footer.dart | 2 +- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/lib/api/marathon/marathon_api_client.dart b/lib/api/marathon/marathon_api_client.dart index a2d684f..5b1dfc8 100644 --- a/lib/api/marathon/marathon_api_client.dart +++ b/lib/api/marathon/marathon_api_client.dart @@ -28,6 +28,7 @@ class MarathonApiClient { return await ApiClient().postJsonForObject( (json) { MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json); + AppState().setMarathonToken = marathonModel.data["token"] ?? ""; return marathonModel.data["token"] ?? ""; }, ApiConsts.marathonParticipantLoginUrl, @@ -43,14 +44,17 @@ class MarathonApiClient { }, ApiConsts.marathonProjectGetUrl, {}, - token: AppState().getMarathonToken ?? await getMarathonToken(), + token: AppState().getMarathonToken == null || AppState().getMarathonToken == "" ? await getMarathonToken() : AppState().getMarathonToken, ); } Future getMarathonDetails() async { String payrollString = AppState().postParamsObject?.payrollCodeStr.toString() ?? "CS"; - Response response = await ApiClient().getJsonForResponse(ApiConsts.marathonUpcomingUrl + payrollString, token: AppState().getMarathonToken ?? await getMarathonToken()); + Response response = await ApiClient().getJsonForResponse( + ApiConsts.marathonUpcomingUrl + payrollString, + token: AppState().getMarathonToken == null || AppState().getMarathonToken == "" ? await getMarathonToken() : AppState().getMarathonToken, + ); var json = jsonDecode(response.body); logger.i("json in getMarathonDetails: $json"); @@ -83,7 +87,7 @@ class MarathonApiClient { }, ApiConsts.marathonJoinParticipantUrl, jsonObject, - token: AppState().getMarathonToken ?? await getMarathonToken(), + token: AppState().getMarathonToken == null || AppState().getMarathonToken == "" ? await getMarathonToken() : AppState().getMarathonToken, ); } @@ -114,7 +118,7 @@ class MarathonApiClient { }, ApiConsts.marathonNextQuestionUrl, jsonObject, - token: AppState().getMarathonToken ?? await getMarathonToken(), + token: AppState().getMarathonToken == null || AppState().getMarathonToken == "" ? await getMarathonToken() : AppState().getMarathonToken, ); } @@ -124,17 +128,25 @@ class MarathonApiClient { return await ApiClient().postJsonForObject( (json) { MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json); - return marathonModel.isSuccessful ?? false; + if (marathonModel.data != null) { + bool isOptionCorrect = marathonModel.data["isCorrect"]; + return isOptionCorrect; + } + return false; }, ApiConsts.marathonSubmitAnswerUrl, jsonObject, - token: AppState().getMarathonToken ?? await getMarathonToken(), + token: AppState().getMarathonToken == null || AppState().getMarathonToken == "" ? await getMarathonToken() : AppState().getMarathonToken, ); } Future getQualifiers({required String marathonId}) async { Map params = {"marathonId": marathonId}; - Response response = await ApiClient().getJsonForResponse(ApiConsts.marathonQualifiersUrl, queryParameters: params, token: AppState().getMarathonToken ?? await getMarathonToken()); + Response response = await ApiClient().getJsonForResponse( + ApiConsts.marathonQualifiersUrl, + queryParameters: params, + token: AppState().getMarathonToken == null || AppState().getMarathonToken == "" ? await getMarathonToken() : AppState().getMarathonToken, + ); var json = jsonDecode(response.body); logger.i("json in getQualifiers: $json"); @@ -149,7 +161,11 @@ class MarathonApiClient { Future?> getSelectedWinner({required String marathonId}) async { Map params = {"marathonId": marathonId}; - Response response = await ApiClient().getJsonForResponse(ApiConsts.marathonSelectedWinner, queryParameters: params, token: AppState().getMarathonToken ?? await getMarathonToken()); + Response response = await ApiClient().getJsonForResponse( + ApiConsts.marathonSelectedWinner, + queryParameters: params, + token: AppState().getMarathonToken == null || AppState().getMarathonToken == "" ? await getMarathonToken() : AppState().getMarathonToken, + ); var json = jsonDecode(response.body); logger.i("json in getSelectedWinner: $json"); diff --git a/lib/ui/marathon/marathon_provider.dart b/lib/ui/marathon/marathon_provider.dart index a8dde7b..ebf8af7 100644 --- a/lib/ui/marathon/marathon_provider.dart +++ b/lib/ui/marathon/marathon_provider.dart @@ -172,7 +172,6 @@ class MarathonProvider extends ChangeNotifier { timerForQuestion = Timer.periodic( oneSec, (Timer timer) async { - print("here is the timer: $totalCurrentQuestionTime"); // This 2 is just to show the color of answer tile for 1 and then update card status if (totalCurrentQuestionTime - currentGapTime == 1) { getCorrectAnswerAndUpdateAnswerColor(); @@ -188,9 +187,8 @@ class MarathonProvider extends ChangeNotifier { await callNextQuestionApi(); } else { await callSubmitOptionApi().then((bool value) async { - if (value) { - await callNextQuestionApi(); - } + updateIsUserOutOfGame = !value; + await callNextQuestionApi(); }); } }); @@ -229,9 +227,8 @@ class MarathonProvider extends ChangeNotifier { oneSec, (Timer timer) async { if (totalSecondsToWaitForWinner == 1) { + await callGetSelectedWinnersApi().whenComplete(() => updateQuestionCardStatus(QuestionCardStatus.winnerFound)); timer.cancel(); - callGetSelectedWinnersApi(); - updateQuestionCardStatus(QuestionCardStatus.winnerFound); return; } else if (totalSecondsToWaitForWinner == 15) { totalSecondsToWaitForWinner--; @@ -274,8 +271,6 @@ class MarathonProvider extends ChangeNotifier { } Future callNextQuestionApi() async { - print("Called callNextQuestionApi!!"); - if (currentQuestionNumber < marathonDetailModel.totalQuestions!) { if (currentQuestionNumber == 0) { Utils.showLoading(AppRoutes.navigatorKey.currentContext!); @@ -298,7 +293,6 @@ class MarathonProvider extends ChangeNotifier { } void updateCardData() { - print("Called updateCardData!!"); if (currentQuestionNumber > 0) { swiperController.swipeLeft(); } @@ -337,14 +331,15 @@ class MarathonProvider extends ChangeNotifier { currentQuestion.questionOptions![i].optionStatus = QuestionsOptionStatus.unSelected; } currentQuestion.questionOptions![index].optionStatus = status; + selectedOptionId = currentQuestion.questionOptions![index].id; selectedOptionIndex = index; notifyListeners(); } void updateQuestionCardStatus(QuestionCardStatus status) { - if (status == QuestionCardStatus.wrongAnswer || status == QuestionCardStatus.skippedAnswer) { - updateIsUserOutOfGame = true; - } + // if (status == QuestionCardStatus.wrongAnswer || status == QuestionCardStatus.skippedAnswer) { + // updateIsUserOutOfGame = true; + // } questionCardStatus = status; notifyListeners(); } @@ -378,8 +373,6 @@ class MarathonProvider extends ChangeNotifier { } } - void resetProgressColorValues() {} - void resetValues() async { _currentQuestionNumber = 0; cardContentList.clear(); @@ -392,6 +385,7 @@ class MarathonProvider extends ChangeNotifier { totalCurrentQuestionTime = 0; sponsorsSecondsCounter = 0; totalSponsorVideoSeconds = 0; + totalSecondsToWaitForWinner = 30; totalSecondsToWaitForMarathon = 0; currentGapTime = 0; currentQuestion = QuestionModel(); diff --git a/lib/ui/marathon/marathon_screen.dart b/lib/ui/marathon/marathon_screen.dart index 3a257c7..dffc4b8 100644 --- a/lib/ui/marathon/marathon_screen.dart +++ b/lib/ui/marathon/marathon_screen.dart @@ -36,11 +36,11 @@ class MarathonScreen extends StatelessWidget { } return gapText.toText18(color: MyColors.darkTextColor, isCentered: true); } - if (gapType == 2) { + if (gapType == 0) { if (gapImage == null) { return Image.asset(MyLottieConsts.congratsGif, height: 200); } - return Image.network(gapImage, height: 200); + return Image.network(ApiConsts.marathonBaseUrlServices + gapImage, height: 200); } return Image.asset(MyLottieConsts.congratsGif, height: 200); } @@ -50,7 +50,7 @@ class MarathonScreen extends StatelessWidget { width: double.infinity, decoration: MyDecorations.shadowDecoration, padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 20), - child: provider.selectedWinners == null || provider.selectedWinners!.isEmpty + child: provider.selectedWinners == null || (provider.selectedWinners!.isEmpty && !provider.iAmWinner) //TODO: WE WILL UPDATE THE DESIGN WHEN THERE IS NO WINNER!! ? Center( child: "Sad! No one won Today. What you guys are doing?".toText20( @@ -73,7 +73,11 @@ class MarathonScreen extends StatelessWidget { ), Align( alignment: Alignment.center, - child: LocaleKeys.winners.tr().toText32(color: MyColors.white, isBold: true, isCentered: true).paddingOnly(top: 07), + child: ((provider.selectedWinners!.length == 1 && !provider.iAmWinner) || (provider.selectedWinners!.isEmpty && provider.iAmWinner) + ? LocaleKeys.winner.tr() + : LocaleKeys.winners.tr()) + .toText32(color: MyColors.white, isBold: true, isCentered: true) + .paddingOnly(top: 07), ) ], ), @@ -126,7 +130,7 @@ class MarathonScreen extends StatelessWidget { ), 5.height, Image.network( - ApiConsts.marathonBaseUrlServices + provider.marathonDetailModel.sponsors!.first.image!, + ApiConsts.marathonBaseUrlServices + provider.marathonDetailModel.sponsors!.first.logo!, height: 50, width: 150, fit: BoxFit.contain, diff --git a/lib/ui/marathon/widgets/marathon_details_card.dart b/lib/ui/marathon/widgets/marathon_details_card.dart index 3b686ef..b888915 100644 --- a/lib/ui/marathon/widgets/marathon_details_card.dart +++ b/lib/ui/marathon/widgets/marathon_details_card.dart @@ -59,7 +59,7 @@ class MarathonDetailsCard extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ Image.network( - ApiConsts.marathonBaseUrlServices + provider.marathonDetailModel.sponsors!.first.image!, + ApiConsts.marathonBaseUrlServices + provider.marathonDetailModel.sponsors!.first.logo!, height: 50, width: 150, fit: BoxFit.contain, diff --git a/lib/ui/marathon/widgets/marathon_footer.dart b/lib/ui/marathon/widgets/marathon_footer.dart index cd23ce1..fea076e 100644 --- a/lib/ui/marathon/widgets/marathon_footer.dart +++ b/lib/ui/marathon/widgets/marathon_footer.dart @@ -43,7 +43,7 @@ class MarathonFooter extends StatelessWidget { Widget build(BuildContext context) { return DefaultButton( LocaleKeys.joinMarathon.tr(), - provider.itsMarathonTime ? () => provider.onJoinMarathonPressed(context) : null, + !provider.itsMarathonTime ? () => provider.onJoinMarathonPressed(context) : null, ).insideContainer; } }