From 7f6b7075b8e2228b5138b470ac30f7b16830be8f Mon Sep 17 00:00:00 2001 From: D4rK7355608 Date: Wed, 17 Jul 2024 20:49:12 +0300 Subject: [PATCH] pull to refresh --- app/build.gradle.kts | 2 +- .../plus/ui/home/HomeComposable.kt | 188 ++++++++++-------- .../plus/ui/home/HomeViewModel.kt | 4 + 3 files changed, 108 insertions(+), 86 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 0c92e57..b864385 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -16,7 +16,7 @@ android { applicationId = "com.d4rk.englishwithlidia.plus" minSdk = 26 targetSdk = 34 - versionCode = 55 + versionCode = 57 versionName = "5.0.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" resourceConfigurations += listOf( diff --git a/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeComposable.kt b/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeComposable.kt index d7f8238..fba7752 100644 --- a/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeComposable.kt +++ b/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeComposable.kt @@ -21,17 +21,22 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Card import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton import androidx.compose.material3.Text +import androidx.compose.material3.pulltorefresh.PullToRefreshContainer +import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip +import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource @@ -53,6 +58,7 @@ import com.d4rk.englishwithlidia.plus.utils.IntentUtils import com.d4rk.englishwithlidia.plus.utils.compose.bounceClick import com.d4rk.englishwithlidia.plus.utils.drawable.homeBanner +@OptIn(ExperimentalMaterial3Api::class) @Composable fun HomeComposable() { val context = LocalContext.current @@ -61,103 +67,118 @@ fun HomeComposable() { val viewModel: HomeViewModel = viewModel(factory = HomeViewModelFactory(repository)) val lessons by viewModel.lessons.collectAsState() val isLoading by viewModel.isLoading.collectAsState() - LazyColumn( - modifier = Modifier - .fillMaxSize() - ) { - item { - Image( - imageVector = homeBanner(), - contentDescription = null, - modifier = Modifier - .fillMaxWidth() - .wrapContentHeight(), - contentScale = ContentScale.FillWidth - ) - Spacer(modifier = Modifier.height(16.dp)) - } - item { - Row( - modifier = Modifier - .fillMaxWidth() - .wrapContentHeight() - .padding(start = 24.dp, end = 24.dp), - ) { - OutlinedButton(onClick = { - IntentUtils.openUrl(context, "https://sites.google.com/view/englishwithlidia") - }, modifier = Modifier - .weight(1f) - .bounceClick()) { - Icon( - painter = painterResource(id = R.drawable.ic_web), - contentDescription = stringResource(id = R.string.tooltip_website), - modifier = Modifier.size(ButtonDefaults.IconSize) - ) - Spacer(Modifier.size(ButtonDefaults.IconSpacing)) - Text(stringResource(id = R.string.website)) - } + val state = rememberPullToRefreshState() - Spacer(modifier = Modifier.width(24.dp)) + if (state.isRefreshing) { + LaunchedEffect(true) { + viewModel.getLessons() + state.endRefresh() + } + } - OutlinedButton( - onClick = { - IntentUtils.openUrl(context, "https://www.facebook.com/lidia.melinte") - }, + Box(Modifier.nestedScroll(state.nestedScrollConnection)) { + LazyColumn( + modifier = Modifier.fillMaxSize() + ) { + item { + Image( + imageVector = homeBanner(), + contentDescription = null, modifier = Modifier - .weight(1f) - .bounceClick(), + .fillMaxWidth() + .wrapContentHeight(), + contentScale = ContentScale.FillWidth + ) + Spacer(modifier = Modifier.height(16.dp)) + } + item { + Row( + modifier = Modifier + .fillMaxWidth() + .wrapContentHeight() + .padding(start = 24.dp, end = 24.dp), ) { - Icon( - painter = painterResource(id = R.drawable.ic_find_us), - contentDescription = stringResource(id = R.string.tooltip_facebook), - modifier = Modifier.size(ButtonDefaults.IconSize) - ) - Spacer(Modifier.size(ButtonDefaults.IconSpacing)) - Text(stringResource(id = R.string.find_us)) + OutlinedButton( + onClick = { + IntentUtils.openUrl( + context, "https://sites.google.com/view/englishwithlidia" + ) + }, modifier = Modifier + .weight(1f) + .bounceClick() + ) { + Icon( + painter = painterResource(id = R.drawable.ic_web), + contentDescription = stringResource(id = R.string.tooltip_website), + modifier = Modifier.size(ButtonDefaults.IconSize) + ) + Spacer(Modifier.size(ButtonDefaults.IconSpacing)) + Text(stringResource(id = R.string.website)) + } + + Spacer(modifier = Modifier.width(24.dp)) + + OutlinedButton( + onClick = { + IntentUtils.openUrl(context, "https://www.facebook.com/lidia.melinte") + }, + modifier = Modifier + .weight(1f) + .bounceClick(), + ) { + Icon( + painter = painterResource(id = R.drawable.ic_find_us), + contentDescription = stringResource(id = R.string.tooltip_facebook), + modifier = Modifier.size(ButtonDefaults.IconSize) + ) + Spacer(Modifier.size(ButtonDefaults.IconSpacing)) + Text(stringResource(id = R.string.find_us)) + } + } + Spacer(modifier = Modifier.height(16.dp)) + } + if (isLoading) { + item { + Box( + modifier = Modifier + .fillMaxWidth() + .wrapContentHeight() + .padding(16.dp), + contentAlignment = Alignment.Center + ) { + CircularProgressIndicator() + } + } + } else { + items(lessons) { lesson -> + LessonCard(title = lesson.title, imageResource = lesson.banner, onClick = { + val intent = Intent(context, LessonsActivity::class.java).apply { + putExtra("lessonDetails", lesson) + } + context.startActivity(intent) + }) } } - Spacer(modifier = Modifier.height(16.dp)) - } - if (isLoading) { + item { + BannerAdsComposable(modifier = Modifier.fillMaxWidth(), dataStore = dataStore) + } item { Box( modifier = Modifier .fillMaxWidth() .wrapContentHeight() - .padding(16.dp), + .padding(start = 16.dp, end = 16.dp, bottom = 8.dp), contentAlignment = Alignment.Center ) { - CircularProgressIndicator() + LottieAnimation( + composition = rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.anim_learn)).value, + iterations = LottieConstants.IterateForever, + modifier = Modifier.size(260.dp) + ) } } - } else { - items(lessons) { lesson -> - LessonCard(title = lesson.title, imageResource = lesson.banner, onClick = { - val intent = Intent(context, LessonsActivity::class.java).apply { - putExtra("lessonDetails", lesson) - } - context.startActivity(intent) - }) - } - } - item { - BannerAdsComposable(modifier = Modifier.fillMaxWidth(), dataStore = dataStore) - } - item { - Box( - modifier = Modifier - .fillMaxWidth() - .wrapContentHeight() - .padding(start = 16.dp, end = 16.dp, bottom = 8.dp), - contentAlignment = Alignment.Center - ) { - LottieAnimation( - composition = rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.anim_learn)).value, - iterations = LottieConstants.IterateForever, - modifier = Modifier.size(260.dp) - ) - } } + PullToRefreshContainer(state = state, modifier = Modifier.align(Alignment.TopCenter)) } } @@ -178,10 +199,7 @@ fun LessonCard(title: String, imageResource: String, onClick: () -> Unit) { .aspectRatio(2.06f / 1f), ) { AsyncImage( - model = ImageRequest.Builder(context) - .data(imageResource) - .crossfade(true) - .build(), + model = ImageRequest.Builder(context).data(imageResource).crossfade(true).build(), contentDescription = null, modifier = Modifier.fillMaxSize(), contentScale = ContentScale.Crop, diff --git a/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeViewModel.kt b/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeViewModel.kt index f294de4..2507fef 100644 --- a/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeViewModel.kt +++ b/app/src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/home/HomeViewModel.kt @@ -17,6 +17,10 @@ class HomeViewModel(private val repository: LessonRepository) : ViewModel() { val isLoading: StateFlow = _isLoading.asStateFlow() init { + getLessons() + } + + fun getLessons() { viewModelScope.launch { _isLoading.value = true _lessons.value = repository.getLessonData()