diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/QuoteStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/QuoteStub.kt index 36e2909..2282ce9 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/QuoteStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/QuoteStub.kt @@ -7,7 +7,7 @@ import com.example.what_the_fantasy.data.model.SrcType object QuoteStub { val quotes: MutableList = mutableListOf( - Quote(1,"All we have to decide is what to do with the time that is given us.",466,SrcLanguage.vo,CharacterStub.gandalf.name,"The Lord of the Rings: The Fellowship of the Ring",CharacterStub.gandalf.imgUrl,SrcType.Movie,2000), + Quote(1, "All we have to decide is what to do with the time that is given us.", 466, SrcLanguage.vo, CharacterStub.gandalf.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), Quote(2, "A wizard is never late, nor is he early, he arrives precisely when he means to.", 467, SrcLanguage.vo, CharacterStub.gandalf.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), Quote(3, "Even the smallest person can change the course of the future.", 466, SrcLanguage.vo, CharacterStub.galadriel.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.galadriel.imgUrl, SrcType.Movie, 2000), Quote(4, "I would rather share one lifetime with you than face all the ages of this world alone.", 120, SrcLanguage.vo, CharacterStub.arwen.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.arwen.imgUrl, SrcType.Movie, 2000), @@ -26,6 +26,47 @@ object QuoteStub { Quote(17, "Le courage n'est pas l'absence de peur, mais la capacité de vaincre ce qui fait peur.", 300, SrcLanguage.vf, CharacterStub.boromir.name, "Nelson Mandela", CharacterStub.boromir.imgUrl, SrcType.Movie, 2000), Quote(18, "La folie, c'est de faire toujours la même chose et de s'attendre à un résultat différent.", 400, SrcLanguage.vf, CharacterStub.eowyn.name, "Albert Einstein", CharacterStub.eowyn.imgUrl, SrcType.Movie, 2000), Quote(19, "Le bonheur n'est pas quelque chose de tout fait. Il vient de vos propres actions.", 500, SrcLanguage.vo, CharacterStub.saruman.name, "Dalaï Lama", CharacterStub.saruman.imgUrl, SrcType.Movie, 2000), - Quote(20, "La vie est un mystère qu'il faut vivre, et non un problème à résoudre.", 600, SrcLanguage.vo, CharacterStub.samwiseGamgee.name, "Gandhi", CharacterStub.samwiseGamgee.imgUrl, SrcType.Movie, 2000) + Quote(20, "La vie est un mystère qu'il faut vivre, et non un problème à résoudre.", 600, SrcLanguage.vo, CharacterStub.samwiseGamgee.name, "Gandhi", CharacterStub.samwiseGamgee.imgUrl, SrcType.Movie, 2000), + Quote(21, "All we have to decide is what to do with the time that is given us.", 466, SrcLanguage.vf, CharacterStub.gandalf.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(22, "A wizard is never late, nor is he early, he arrives precisely when he means to.", 467, SrcLanguage.vf, CharacterStub.gandalf.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(23, "Even the smallest person can change the course of the future.", 466, SrcLanguage.vf, CharacterStub.galadriel.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.galadriel.imgUrl, SrcType.Movie, 2000), + Quote(24, "I would rather share one lifetime with you than face all the ages of this world alone.", 120, SrcLanguage.vf, CharacterStub.arwen.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.arwen.imgUrl, SrcType.Movie, 2000), + Quote(25, "Faithless is he that says farewell when the road darkens.", 150, SrcLanguage.vf, CharacterStub.gimli.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gimli.imgUrl, SrcType.Movie, 2000), + Quote(26, "It's a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there's no knowing where you might be swept off to.", 200, SrcLanguage.vf, CharacterStub.frodoBaggins.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.frodoBaggins.imgUrl, SrcType.Movie, 2000), + Quote(27, "I am no man.", 300, SrcLanguage.vf, CharacterStub.eowyn.name, "The Lord of the Rings: The Return of the King", CharacterStub.eowyn.imgUrl, SrcType.Movie, 2000), + Quote(28, "The world is changed. I feel it in the water. I feel it in the earth. I smell it in the air.", 400, SrcLanguage.vf, CharacterStub.treebeard.name, "The Lord of the Rings: The Two Towers", CharacterStub.treebeard.imgUrl, SrcType.Movie, 2000), + Quote(29, "We wants it, we needs it. Must have the precious.", 500, SrcLanguage.vf, CharacterStub.gollum.name, "The Lord of the Rings: The Two Towers", CharacterStub.gollum.imgUrl, SrcType.Movie, 2000), + Quote(30, "The board is set, the pieces are moving. We come to it at last, the great battle of our time.", 600, SrcLanguage.vf, CharacterStub.gandalf.name, "The Lord of the Rings: The Return of the King", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(31, "Un grand pouvoir implique de grandes responsabilités.", 466, SrcLanguage.vo, CharacterStub.aragorn.name, "Spider-Man", CharacterStub.aragorn.imgUrl, SrcType.Series, 2000), + Quote(32, "Que la Force soit avec toi.", 467, SrcLanguage.vo, CharacterStub.legolas.name, "Star Wars", CharacterStub.legolas.imgUrl, SrcType.Movie, 2000), + Quote(33, "La magie est partout. Il suffit de savoir où la trouver.", 466, SrcLanguage.vo, CharacterStub.gandalf.name, "Harry Potter", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(34, "Le monde est plein de choses magiques, patientant que nos sens s'aiguisent.", 120, SrcLanguage.vo, CharacterStub.frodoBaggins.name, "Le Seigneur des Anneaux", CharacterStub.frodoBaggins.imgUrl, SrcType.Movie, 2000), + Quote(35, "La peur mène à la colère, la colère mène à la haine, la haine mène à la souffrance.", 150, SrcLanguage.vo, CharacterStub.gimli.name, "Star Wars", CharacterStub.gimli.imgUrl, SrcType.Movie, 2000), + Quote(36, "La vie est une aventure audacieuse ou rien du tout.", 200, SrcLanguage.vo, CharacterStub.galadriel.name, "Helen Keller", CharacterStub.galadriel.imgUrl, SrcType.Movie, 2000), + Quote(37, "Le courage n'est pas l'absence de peur, mais la capacité de vaincre ce qui fait peur.", 300, SrcLanguage.vo, CharacterStub.boromir.name, "Nelson Mandela", CharacterStub.boromir.imgUrl, SrcType.Movie, 2000), + Quote(38, "La folie, c'est de faire toujours la même chose et de s'attendre à un résultat différent.", 400, SrcLanguage.vo, CharacterStub.eowyn.name, "Albert Einstein", CharacterStub.eowyn.imgUrl, SrcType.Movie, 2000), + Quote(39, "Le bonheur n'est pas quelque chose de tout fait. Il vient de vos propres actions.", 500, SrcLanguage.vf, CharacterStub.saruman.name, "Dalaï Lama", CharacterStub.saruman.imgUrl, SrcType.Movie, 2000), + Quote(40, "La vie est un mystère qu'il faut vivre, et non un problème à résoudre.", 600, SrcLanguage.vf, CharacterStub.samwiseGamgee.name, "Gandhi", CharacterStub.samwiseGamgee.imgUrl, SrcType.Movie, 2000), + Quote(41, "All we have to decide is what to do with the time that is given us.", 466, SrcLanguage.vo, CharacterStub.gandalf.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(42, "A wizard is never late, nor is he early, he arrives precisely when he means to.", 467, SrcLanguage.vo, CharacterStub.gandalf.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(43, "Even the smallest person can change the course of the future.", 466, SrcLanguage.vo, CharacterStub.galadriel.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.galadriel.imgUrl, SrcType.Movie, 2000), + Quote(44, "I would rather share one lifetime with you than face all the ages of this world alone.", 120, SrcLanguage.vo, CharacterStub.arwen.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.arwen.imgUrl, SrcType.Movie, 2000), + Quote(45, "Faithless is he that says farewell when the road darkens.", 150, SrcLanguage.vo, CharacterStub.gimli.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.gimli.imgUrl, SrcType.Movie, 2000), + Quote(46, "It's a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there's no knowing where you might be swept off to.", 200, SrcLanguage.vo, CharacterStub.frodoBaggins.name, "The Lord of the Rings: The Fellowship of the Ring", CharacterStub.frodoBaggins.imgUrl, SrcType.Movie, 2000), + Quote(47, "I am no man.", 300, SrcLanguage.vo, CharacterStub.eowyn.name, "The Lord of the Rings: The Return of the King", CharacterStub.eowyn.imgUrl, SrcType.Movie, 2000), + Quote(48, "The world is changed. I feel it in the water. I feel it in the earth. I smell it in the air.", 400, SrcLanguage.vo, CharacterStub.treebeard.name, "The Lord of the Rings: The Two Towers", CharacterStub.treebeard.imgUrl, SrcType.Movie, 2000), + Quote(49, "We wants it, we needs it. Must have the precious.", 500, SrcLanguage.vo, CharacterStub.gollum.name, "The Lord of the Rings: The Two Towers", CharacterStub.gollum.imgUrl, SrcType.Movie, 2000), + Quote(50, "The board is set, the pieces are moving. We come to it at last, the great battle of our time.", 600, SrcLanguage.vo, CharacterStub.gandalf.name, "The Lord of the Rings: The Return of the King", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(51, "Un grand pouvoir implique de grandes responsabilités.", 466, SrcLanguage.vf, CharacterStub.aragorn.name, "Spider-Man", CharacterStub.aragorn.imgUrl, SrcType.Series, 2000), + Quote(52, "Que la Force soit avec toi.", 467, SrcLanguage.vf, CharacterStub.legolas.name, "Star Wars", CharacterStub.legolas.imgUrl, SrcType.Movie, 2000), + Quote(53, "La magie est partout. Il suffit de savoir où la trouver.", 466, SrcLanguage.vf, CharacterStub.gandalf.name, "Harry Potter", CharacterStub.gandalf.imgUrl, SrcType.Movie, 2000), + Quote(54, "Le monde est plein de choses magiques, patientant que nos sens s'aiguisent.", 120, SrcLanguage.vf, CharacterStub.frodoBaggins.name, "Le Seigneur des Anneaux", CharacterStub.frodoBaggins.imgUrl, SrcType.Movie, 2000), + Quote(55, "La peur mène à la colère, la colère mène à la haine, la haine mène à la souffrance.", 150, SrcLanguage.vf, CharacterStub.gimli.name, "Star Wars", CharacterStub.gimli.imgUrl, SrcType.Movie, 2000), + Quote(56, "La vie est une aventure audacieuse ou rien du tout.", 200, SrcLanguage.vf, CharacterStub.galadriel.name, "Helen Keller", CharacterStub.galadriel.imgUrl, SrcType.Movie, 2000), + Quote(57, "Le courage n'est pas l'absence de peur, mais la capacité de vaincre ce qui fait peur.", 300, SrcLanguage.vf, CharacterStub.boromir.name, "Nelson Mandela", CharacterStub.boromir.imgUrl, SrcType.Movie, 2000), + Quote(58, "La folie, c'est de faire toujours la même chose et de s'attendre à un résultat différent.", 400, SrcLanguage.vf, CharacterStub.eowyn.name, "Albert Einstein", CharacterStub.eowyn.imgUrl, SrcType.Movie, 2000), + Quote(59, "Le bonheur n'est pas quelque chose de tout fait. Il vient de vos propres actions.", 500, SrcLanguage.vo, CharacterStub.saruman.name, "Dalaï Lama", CharacterStub.saruman.imgUrl, SrcType.Movie, 2000), + Quote(60, "La vie est un mystère qu'il faut vivre, et non un problème à résoudre.", 600, SrcLanguage.vo, CharacterStub.samwiseGamgee.name, "Gandhi", CharacterStub.samwiseGamgee.imgUrl, SrcType.Movie, 2000) ) -} \ No newline at end of file +} + diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index afa5cd6..6804fbc 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -24,4 +24,5 @@ interface IServices { fun isFavorite(id : Int): Boolean fun getAllFavorite(): List fun getAllQuote(): List + fun getSomeQuotes(nb: Int, page: Int) : MutableList } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 7e8eab0..1b5e0b7 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -103,6 +103,15 @@ class ServicesStub : IServices { return (quotes.find { it.id == id+1 }) } + override fun getSomeQuotes(nb: Int, page: Int): MutableList { + val fromIndex = (page - 1) * nb + val toIndex = minOf(page * nb, quotes.size) + + if (fromIndex >= quotes.size) return mutableListOf() + + return quotes.subList(fromIndex, toIndex).toMutableList() + } + override fun isFavorite(id: Int): Boolean { TODO("Not yet implemented") } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt index 6161a81..f2c1d71 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt @@ -5,6 +5,7 @@ import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text @@ -18,10 +19,12 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.what_the_fantasy.R import com.example.what_the_fantasy.data.local.DailyQuoteStub +import com.example.what_the_fantasy.data.model.Quote import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.QuoteLittle import com.example.what_the_fantasy.ui.theme.colorBackground +import kotlinx.coroutines.delay @Composable fun AccueilPage( @@ -31,15 +34,41 @@ fun AccueilPage( navProfil: (Int) -> Unit, navQuote: (Int) -> Unit, services: IServices - ) { - var itemCount by remember { mutableStateOf(15) } val dailyQuote = DailyQuoteStub.dailyQuote - val quotes = services.getAllQuote().take(itemCount) val user = services.getUserById(index) ?: return val titleDalyQuote = stringResource(R.string.TitleHomeDailyQuote) val titleSuggestion = stringResource(R.string.TitleHomeSuggestion) + + val page = remember { mutableStateOf(1) } + val quotes = remember { mutableStateListOf() } + val state = rememberLazyListState() + val layoutInfo = state.layoutInfo + val isLoading = remember { mutableStateOf(false) } + val visibleItemsInfo = layoutInfo.visibleItemsInfo + val fullyVisibleItemsInfo = visibleItemsInfo.toMutableList() + val lastItem = if (fullyVisibleItemsInfo.isNotEmpty()) fullyVisibleItemsInfo.last() else null + + + LaunchedEffect(page.value) { + if (!isLoading.value) { + isLoading.value = true + delay(500) + val newQuotes = services.getSomeQuotes(15, page.value) + val uniqueQuotes = newQuotes.filterNot { new -> quotes.any { it.id == new.id } } + + if (uniqueQuotes.isNotEmpty()) { + quotes.addAll(uniqueQuotes) + } else { + println("Aucune nouvelle quote à la page ${page.value}, stop pagination.") + } + isLoading.value = false + } + } + + + NavBar( onAccueil = true, index = index, @@ -53,9 +82,9 @@ fun AccueilPage( .fillMaxSize() .background(MaterialTheme.colorScheme.background) ) { - LazyColumn(modifier = Modifier.weight(1f)) { - item{ - Column { + LazyColumn(modifier = Modifier.weight(1f), state = state) { + item { + Column(Modifier.clickable { navQuote(dailyQuote.id - 1) }) { Text( text = titleDalyQuote, color = MaterialTheme.colorScheme.onBackground, @@ -65,9 +94,9 @@ fun AccueilPage( .padding(16.dp), textAlign = TextAlign.Center ) - QuoteLittle(dailyQuote) } + Text( text = titleSuggestion, color = MaterialTheme.colorScheme.onBackground, @@ -78,21 +107,18 @@ fun AccueilPage( textAlign = TextAlign.Center ) } - items(quotes) { quote -> - Column(Modifier.clickable {navQuote(quote.id)} - ) { - if(quote.language == user.langage){ + items(quotes) { quote -> + if (quote.language == user.langage) { + Column(Modifier.clickable { navQuote(quote.id - 1) }) { QuoteLittle(quote) Spacer(modifier = Modifier.height(16.dp)) } } } - if (itemCount < quotes.size) { - item { - LaunchedEffect(itemCount) { - itemCount += 15 - } + + item { + if (lastItem?.index == quotes.size) { Box( modifier = Modifier .fillMaxWidth() @@ -101,6 +127,15 @@ fun AccueilPage( ) { CircularProgressIndicator() } + } else { + LaunchedEffect(state.firstVisibleItemIndex, state.layoutInfo.totalItemsCount) { + if (!isLoading.value && state.layoutInfo.visibleItemsInfo.isNotEmpty()) { + val lastVisibleItem = state.layoutInfo.visibleItemsInfo.last() + if (lastVisibleItem.index >= state.layoutInfo.totalItemsCount - 1) { + page.value++ + } + } + } } } }