From 64a5368e10a3f4c0f05124d4b7efc041dff1262c Mon Sep 17 00:00:00 2001 From: sheikh-20 Date: Mon, 1 Jul 2024 16:25:15 +0530 Subject: [PATCH] home screen paging loader integrated --- app/build.gradle.kts | 6 ++-- .../moviesapp/data/api/NetworkInterceptor.kt | 13 +++++++++ .../remote/MovieNowPlayingPagingSource.kt | 1 + .../application/moviesapp/di/NetworkModule.kt | 9 +++++- .../movienowplaying/NowPlayingMoviesScreen.kt | 28 +++++++++++++++++-- .../NowPlayingSeriesScreen.kt | 27 ++++++++++++++++-- 6 files changed, 74 insertions(+), 10 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 8ad0eb0..30a701f 100755 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -43,7 +43,7 @@ android { create("config") { keyAlias = "moviesapp" keyPassword = "Sheikh" - storeFile = file("/media/sheikh/hdd/AndroidStudioProjects/AndroidStudioProjects/MoviesApp/app/keystore.jks") + storeFile = file("/Users/sheikh/StudioProjects/MoviesApp/app/keystore.jks") storePassword = "Sheikh" enableV4Signing = true } @@ -53,8 +53,8 @@ android { applicationId = "com.application.moviesapp" minSdk = 24 targetSdk = 33 - versionCode = 25 - versionName = "1.0.24" + versionCode = 26 + versionName = "1.0.25" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { diff --git a/app/src/main/java/com/application/moviesapp/data/api/NetworkInterceptor.kt b/app/src/main/java/com/application/moviesapp/data/api/NetworkInterceptor.kt index 9772cf0..73764fc 100755 --- a/app/src/main/java/com/application/moviesapp/data/api/NetworkInterceptor.kt +++ b/app/src/main/java/com/application/moviesapp/data/api/NetworkInterceptor.kt @@ -1,6 +1,7 @@ package com.application.moviesapp.data.api import com.application.moviesapp.BuildConfig +import kotlinx.coroutines.delay import okhttp3.Interceptor import okhttp3.Response import javax.inject.Inject @@ -10,6 +11,12 @@ class NetworkInterceptor @Inject constructor(@Named("movies_api_key") private va override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request().newBuilder() request.addHeader(name = "Authorization", value = "Bearer $apiKey") + + try { + Thread.sleep(2_000L) // Introduce delay + } catch (e: InterruptedException) { + e.printStackTrace() + } return chain.proceed(request.build()) } } @@ -18,6 +25,12 @@ class YoutubeNetworkInterceptor @Inject constructor(@Named("youtube_api_key") pr override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request().url.newBuilder() request.addQueryParameter(name = "key", value = apiKey) + + try { + Thread.sleep(2_000L) // Introduce delay + } catch (e: InterruptedException) { + e.printStackTrace() + } return chain.proceed(chain.request().newBuilder().url(request.build()).build()) } } diff --git a/app/src/main/java/com/application/moviesapp/data/remote/MovieNowPlayingPagingSource.kt b/app/src/main/java/com/application/moviesapp/data/remote/MovieNowPlayingPagingSource.kt index 0631f6e..89f66d2 100755 --- a/app/src/main/java/com/application/moviesapp/data/remote/MovieNowPlayingPagingSource.kt +++ b/app/src/main/java/com/application/moviesapp/data/remote/MovieNowPlayingPagingSource.kt @@ -4,6 +4,7 @@ import androidx.paging.PagingSource import androidx.paging.PagingState import com.application.moviesapp.data.api.MoviesApi import com.application.moviesapp.data.api.response.MovieNowPlayingDto +import kotlinx.coroutines.delay class MovieNowPlayingPagingSource(private val moviesApi: MoviesApi): PagingSource() { override fun getRefreshKey(state: PagingState): Int? { diff --git a/app/src/main/java/com/application/moviesapp/di/NetworkModule.kt b/app/src/main/java/com/application/moviesapp/di/NetworkModule.kt index 78a5d4d..6548fe5 100755 --- a/app/src/main/java/com/application/moviesapp/di/NetworkModule.kt +++ b/app/src/main/java/com/application/moviesapp/di/NetworkModule.kt @@ -20,6 +20,8 @@ import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import retrofit2.Retrofit import retrofit2.converter.scalars.ScalarsConverterFactory +import java.time.Duration +import java.util.concurrent.TimeUnit import javax.inject.Named import javax.inject.Singleton @@ -46,7 +48,12 @@ object NetworkModule { @Singleton @Named("movies_http_client") fun providesOKHttpClient(networkInterceptor: NetworkInterceptor): OkHttpClient { - return OkHttpClient.Builder().addInterceptor(networkInterceptor).build() + return OkHttpClient.Builder() + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .connectTimeout(30, TimeUnit.SECONDS) + .addInterceptor(networkInterceptor) + .build() } @Provides diff --git a/app/src/main/java/com/application/moviesapp/ui/home/movienowplaying/NowPlayingMoviesScreen.kt b/app/src/main/java/com/application/moviesapp/ui/home/movienowplaying/NowPlayingMoviesScreen.kt index 72ae492..0df72bf 100755 --- a/app/src/main/java/com/application/moviesapp/ui/home/movienowplaying/NowPlayingMoviesScreen.kt +++ b/app/src/main/java/com/application/moviesapp/ui/home/movienowplaying/NowPlayingMoviesScreen.kt @@ -104,7 +104,7 @@ fun NowPlayingMoviesScreen(modifier: Modifier = Modifier, top = bottomPadding.calculateTopPadding(), bottom = bottomPadding.calculateBottomPadding() ).pullRefresh(pullRefreshState)) { - Column { + Column(modifier = modifier.fillMaxSize()) { when (moviesFlow.loadState.refresh) { is LoadState.Error -> Column(modifier = modifier .fillMaxSize() @@ -140,7 +140,9 @@ fun NowPlayingMoviesScreen(modifier: Modifier = Modifier, is LoadState.NotLoading -> { if (searchClicked) { - LazyVerticalGrid(columns = GridCells.Fixed(2), + LazyVerticalGrid( + modifier = modifier.weight(1f), + columns = GridCells.Fixed(2), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp), state = lazyGridState, @@ -151,7 +153,9 @@ fun NowPlayingMoviesScreen(modifier: Modifier = Modifier, } } } else { - LazyVerticalGrid(columns = GridCells.Fixed(2), + LazyVerticalGrid( + modifier = modifier.weight(1f), + columns = GridCells.Fixed(2), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp), state = lazyGridState, @@ -164,6 +168,24 @@ fun NowPlayingMoviesScreen(modifier: Modifier = Modifier, } } } + + when (moviesFlow.loadState.append) { + is LoadState.Loading -> { + CircularProgressIndicator(modifier = modifier + .fillMaxWidth() + .wrapContentWidth(align = Alignment.CenterHorizontally) + .padding(16.dp)) + } + is LoadState.NotLoading -> { } + is LoadState.Error -> { + Text(text = if (moviesFlow.loadState.append.endOfPaginationReached) "You have reached the end" else "", + style = MaterialTheme.typography.displayMedium, + color = MaterialTheme.colorScheme.primary, + fontWeight = FontWeight.SemiBold, + modifier = modifier.fillMaxWidth().wrapContentWidth(align = Alignment.CenterHorizontally).padding(16.dp), + textAlign = TextAlign.Center) + } + } } PullRefreshIndicator( diff --git a/app/src/main/java/com/application/moviesapp/ui/home/tvseriesnowplaying/NowPlayingSeriesScreen.kt b/app/src/main/java/com/application/moviesapp/ui/home/tvseriesnowplaying/NowPlayingSeriesScreen.kt index 2ba29ca..50420f6 100755 --- a/app/src/main/java/com/application/moviesapp/ui/home/tvseriesnowplaying/NowPlayingSeriesScreen.kt +++ b/app/src/main/java/com/application/moviesapp/ui/home/tvseriesnowplaying/NowPlayingSeriesScreen.kt @@ -95,7 +95,7 @@ fun NowPlayingSeriesScreen(modifier: Modifier = Modifier, top = bottomPadding.calculateTopPadding(), bottom = bottomPadding.calculateBottomPadding() ).pullRefresh(pullRefreshState)) { - Column { + Column(modifier = modifier.fillMaxSize()) { when (tvSeriesFlow.loadState.refresh) { is LoadState.Error -> Column(modifier = modifier .fillMaxSize() @@ -131,7 +131,9 @@ fun NowPlayingSeriesScreen(modifier: Modifier = Modifier, } is LoadState.NotLoading -> { if (searchClicked) { - LazyVerticalGrid(columns = GridCells.Fixed(2), + LazyVerticalGrid( + modifier = modifier.weight(1f), + columns = GridCells.Fixed(2), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp), state = lazyGridState, @@ -142,7 +144,9 @@ fun NowPlayingSeriesScreen(modifier: Modifier = Modifier, } } } else { - LazyVerticalGrid(columns = GridCells.Fixed(2), + LazyVerticalGrid( + modifier = modifier.weight(1f), + columns = GridCells.Fixed(2), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp), state = lazyGridState, @@ -156,6 +160,23 @@ fun NowPlayingSeriesScreen(modifier: Modifier = Modifier, } } + when (tvSeriesFlow.loadState.append) { + is LoadState.Loading -> { + CircularProgressIndicator(modifier = modifier + .fillMaxWidth() + .wrapContentWidth(align = Alignment.CenterHorizontally) + .padding(16.dp)) + } + is LoadState.NotLoading -> { } + is LoadState.Error -> { + Text(text = if (tvSeriesFlow.loadState.append.endOfPaginationReached) "You have reached the end" else "", + style = MaterialTheme.typography.displayMedium, + color = MaterialTheme.colorScheme.primary, + fontWeight = FontWeight.SemiBold, + modifier = modifier.fillMaxWidth().wrapContentWidth(align = Alignment.CenterHorizontally).padding(16.dp), + textAlign = TextAlign.Center) + } + } } PullRefreshIndicator(