From 47f719fbc6926770a9b4bf9dbf6afccaa47729a8 Mon Sep 17 00:00:00 2001
From: Nguyen Quang Minh <nguyenquangminh391@gmail.com>
Date: Tue, 17 Dec 2024 08:52:47 +0700
Subject: [PATCH] =?UTF-8?q?fix(study=20set):=20s=E1=BB=ADa=20l=E1=BB=97i?=
 =?UTF-8?q?=20t=C3=ADnh=20progress=20=E1=BB=9F=20c=C3=A1c=20m=C3=A0n=20h?=
 =?UTF-8?q?=C3=ACnh=20h=E1=BB=8Dc=20-=20t=E1=BA=AFt=20loading=20khi=20l?=
 =?UTF-8?q?=E1=BA=A5y=20qu=E1=BA=A3ng=20c=C3=A1o=20l=E1=BB=97i?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../presentation/app/explore/ExploreScreen.kt        | 12 ++++++++----
 .../app/study_set/studies/quiz/LearnByQuizScreen.kt  |  4 ++--
 .../studies/true_false/LearnByTrueFalseScreen.kt     |  4 ++--
 .../study_set/studies/write/LearnByWriteScreen.kt    |  4 ++--
 .../main/java/com/pwhs/quickmem/util/ads/AdsUtil.kt  |  3 ++-
 5 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreScreen.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreScreen.kt
index 1157c104..8663000f 100644
--- a/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreScreen.kt
+++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreScreen.kt
@@ -232,10 +232,14 @@ fun Explore(
                                             isGettingAds = true
                                             AdsUtil.rewardedInterstitialAd(
                                                 context,
-                                            ) {
-                                                onEarnCoins()
-                                                isGettingAds = false
-                                            }
+                                                onAdLoadFailedToLoad = {
+                                                    isGettingAds = false
+                                                },
+                                                onAdWatched = {
+                                                    onEarnCoins()
+                                                    isGettingAds = false
+                                                }
+                                            )
                                         },
                                 )
                             }
diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/quiz/LearnByQuizScreen.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/quiz/LearnByQuizScreen.kt
index 2c562a57..0105e858 100644
--- a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/quiz/LearnByQuizScreen.kt
+++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/quiz/LearnByQuizScreen.kt
@@ -276,13 +276,13 @@ fun LearnByQuiz(
                     .padding(innerPadding)
             ) {
                 val currentProgress by animateFloatAsState(
-                    targetValue = currentCardIndex.toFloat() / flashCardList.size.toFloat()
+                    targetValue = currentCardIndex.toFloat() / flashCardList.size.toFloat().coerceAtLeast(1f)
                 )
                 LinearProgressIndicator(
                     modifier = Modifier.fillMaxWidth(),
                     progress = {
                         when (isEndOfList) {
-                            false -> currentProgress
+                            false -> currentProgress.coerceAtLeast(0f)
                             true -> 1f
                         }
                     },
diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/true_false/LearnByTrueFalseScreen.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/true_false/LearnByTrueFalseScreen.kt
index f1f6fc4a..4186909b 100644
--- a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/true_false/LearnByTrueFalseScreen.kt
+++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/true_false/LearnByTrueFalseScreen.kt
@@ -218,13 +218,13 @@ fun LearnByTrueFalse(
                 horizontalAlignment = Alignment.CenterHorizontally
             ) {
                 val currentProgress by animateFloatAsState(
-                    targetValue = currentCardIndex.toFloat() / flashCardList.size.toFloat()
+                    targetValue = currentCardIndex.toFloat() / flashCardList.size.toFloat().coerceAtLeast(1f)
                 )
                 LinearProgressIndicator(
                     modifier = Modifier.fillMaxWidth(),
                     progress = {
                         when (isEndOfList) {
-                            false -> currentProgress
+                            false -> currentProgress.coerceAtLeast(0f)
                             true -> 1f
                         }
                     },
diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/write/LearnByWriteScreen.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/write/LearnByWriteScreen.kt
index c84864f9..b657a7b4 100644
--- a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/write/LearnByWriteScreen.kt
+++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/studies/write/LearnByWriteScreen.kt
@@ -279,13 +279,13 @@ fun LearnByWrite(
                 horizontalAlignment = Alignment.CenterHorizontally
             ) {
                 val currentProgress by animateFloatAsState(
-                    targetValue = currentCardIndex.toFloat() / flashCardList.size.toFloat()
+                    targetValue = currentCardIndex.toFloat() / flashCardList.size.toFloat().coerceAtLeast(1f),
                 )
                 LinearProgressIndicator(
                     modifier = Modifier.fillMaxWidth(),
                     progress = {
                         when (isEndOfList) {
-                            false -> currentProgress
+                            false -> currentProgress.coerceAtLeast(0f)
                             true -> 1f
                         }
                     },
diff --git a/app/src/main/java/com/pwhs/quickmem/util/ads/AdsUtil.kt b/app/src/main/java/com/pwhs/quickmem/util/ads/AdsUtil.kt
index 53013d68..0e27841f 100644
--- a/app/src/main/java/com/pwhs/quickmem/util/ads/AdsUtil.kt
+++ b/app/src/main/java/com/pwhs/quickmem/util/ads/AdsUtil.kt
@@ -90,7 +90,7 @@ object AdsUtil {
         )
     }
 
-    fun rewardedInterstitialAd(context: Context, onAdWatched: () -> Unit) {
+    fun rewardedInterstitialAd(context: Context, onAdWatched: () -> Unit, onAdLoadFailedToLoad: () -> Unit ) {
         // Load an ad
         RewardedInterstitialAd.load(
             context,
@@ -101,6 +101,7 @@ object AdsUtil {
                     super.onAdFailedToLoad(error)
                     Toast.makeText(context,
                         context.getString(R.string.txt_no_ads_found), Toast.LENGTH_SHORT).show()
+                    onAdLoadFailedToLoad()
                 }
 
                 override fun onAdLoaded(adLoaded: RewardedInterstitialAd) {