From 2f1d038cec66bc4ebb7a1e1c6f3e41cd1efeec31 Mon Sep 17 00:00:00 2001 From: Wing <44992537+wingio@users.noreply.github.com> Date: Wed, 29 Nov 2023 11:46:36 -0500 Subject: [PATCH] Switch to foundation pager --- .../dimett/ui/screens/main/MainScreen.kt | 26 +++++++++---------- .../ui/widgets/attachments/Attachments.kt | 14 +++++----- .../java/xyz/wingio/dimett/utils/NavUtils.kt | 15 ++++++----- gradle/libs.versions.toml | 3 +-- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/xyz/wingio/dimett/ui/screens/main/MainScreen.kt b/app/src/main/java/xyz/wingio/dimett/ui/screens/main/MainScreen.kt index c8db98b..c2ec2e6 100644 --- a/app/src/main/java/xyz/wingio/dimett/ui/screens/main/MainScreen.kt +++ b/app/src/main/java/xyz/wingio/dimett/ui/screens/main/MainScreen.kt @@ -1,17 +1,20 @@ package xyz.wingio.dimett.ui.screens.main import androidx.activity.compose.BackHandler +import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size +import androidx.compose.foundation.pager.HorizontalPager +import androidx.compose.foundation.pager.PagerState +import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Menu import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider -import androidx.compose.runtime.compositionLocalOf import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip @@ -22,14 +25,11 @@ import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.koin.getScreenModel import cafe.adriel.voyager.navigator.tab.Tab import coil.compose.AsyncImage -import com.google.accompanist.pager.ExperimentalPagerApi -import com.google.accompanist.pager.HorizontalPager -import com.google.accompanist.pager.PagerState -import com.google.accompanist.pager.rememberPagerState import kotlinx.coroutines.launch import xyz.wingio.dimett.R import xyz.wingio.dimett.ui.components.Text import xyz.wingio.dimett.ui.viewmodels.main.MainViewModel +import xyz.wingio.dimett.utils.LocalPagerState import xyz.wingio.dimett.utils.RootTab class MainScreen : Screen { @@ -38,12 +38,14 @@ class MainScreen : Screen { override fun Content() = Screen() @Composable - @OptIn(ExperimentalMaterial3Api::class, ExperimentalPagerApi::class) + @OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class) private fun Screen() { val viewModel: MainViewModel = getScreenModel() val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior() - val pagerState = rememberPagerState() val coroutineScope = rememberCoroutineScope() + val pagerState = rememberPagerState { + RootTab.entries.size + } BackHandler( enabled = pagerState.currentPage != 0 @@ -54,14 +56,13 @@ class MainScreen : Screen { } CompositionLocalProvider( - LocalPager provides pagerState + LocalPagerState provides pagerState ) { Scaffold( bottomBar = { TabBar(pagerState, viewModel) }, topBar = { TopBar(RootTab.entries[pagerState.currentPage].tab, scrollBehavior) } ) { pv -> HorizontalPager( - count = RootTab.entries.size, state = pagerState, modifier = Modifier.fillMaxSize() ) { @@ -102,8 +103,8 @@ class MainScreen : Screen { ) } - @OptIn(ExperimentalPagerApi::class) @Composable + @OptIn(ExperimentalFoundationApi::class) private fun TabBar( pagerState: PagerState, viewModel: MainViewModel @@ -146,7 +147,4 @@ class MainScreen : Screen { } } -} - -@OptIn(ExperimentalPagerApi::class) -val LocalPager = compositionLocalOf { null } \ No newline at end of file +} \ No newline at end of file diff --git a/app/src/main/java/xyz/wingio/dimett/ui/widgets/attachments/Attachments.kt b/app/src/main/java/xyz/wingio/dimett/ui/widgets/attachments/Attachments.kt index 7b4c37f..dfe30f7 100644 --- a/app/src/main/java/xyz/wingio/dimett/ui/widgets/attachments/Attachments.kt +++ b/app/src/main/java/xyz/wingio/dimett/ui/widgets/attachments/Attachments.kt @@ -1,8 +1,11 @@ package xyz.wingio.dimett.ui.widgets.attachments +import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.pager.HorizontalPager +import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.foundation.shape.CircleShape import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment @@ -10,13 +13,10 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp -import com.google.accompanist.pager.ExperimentalPagerApi -import com.google.accompanist.pager.HorizontalPager import com.google.accompanist.pager.HorizontalPagerIndicator -import com.google.accompanist.pager.rememberPagerState import xyz.wingio.dimett.rest.dto.post.MediaAttachment -@OptIn(ExperimentalPagerApi::class) +@OptIn(ExperimentalFoundationApi::class) @Composable fun Attachments( attachments: List @@ -26,13 +26,14 @@ fun Attachments( attachment = attachments.first() ) } else if (attachments.size > 1) { - val pagerState = rememberPagerState() + val pagerState = rememberPagerState { + attachments.size + } Box( contentAlignment = Alignment.BottomCenter, ) { HorizontalPager( - count = attachments.size, state = pagerState ) { SingleAttachment(attachment = attachments[it]) @@ -40,6 +41,7 @@ fun Attachments( Box(modifier = Modifier.padding(12.dp)) { HorizontalPagerIndicator( pagerState = pagerState, + pageCount = pagerState.pageCount, activeColor = Color.White, modifier = Modifier .clip(CircleShape) diff --git a/app/src/main/java/xyz/wingio/dimett/utils/NavUtils.kt b/app/src/main/java/xyz/wingio/dimett/utils/NavUtils.kt index 6074d51..471f7a7 100644 --- a/app/src/main/java/xyz/wingio/dimett/utils/NavUtils.kt +++ b/app/src/main/java/xyz/wingio/dimett/utils/NavUtils.kt @@ -2,19 +2,19 @@ package xyz.wingio.dimett.utils import android.annotation.SuppressLint import androidx.annotation.StringRes +import androidx.compose.foundation.ExperimentalFoundationApi +import androidx.compose.foundation.pager.PagerState import androidx.compose.runtime.Composable +import androidx.compose.runtime.compositionLocalOf import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.res.stringResource import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.navigator.Navigator -import cafe.adriel.voyager.navigator.currentOrThrow import cafe.adriel.voyager.navigator.tab.Tab import cafe.adriel.voyager.navigator.tab.TabOptions -import com.google.accompanist.pager.ExperimentalPagerApi import xyz.wingio.dimett.ui.screens.explore.ExploreTab import xyz.wingio.dimett.ui.screens.feed.FeedTab -import xyz.wingio.dimett.ui.screens.main.LocalPager import xyz.wingio.dimett.ui.screens.profile.ProfileTab import xyz.wingio.dimett.ui.screens.notifications.NotificationsTab @@ -33,16 +33,19 @@ tailrec fun Navigator.navigate(screen: Screen) { else parent!!.navigate(screen) } -@OptIn(ExperimentalPagerApi::class) +@OptIn(ExperimentalFoundationApi::class) +val LocalPagerState = compositionLocalOf { error("No PagerState provided") } + @Composable @SuppressLint("ComposableNaming") +@OptIn(ExperimentalFoundationApi::class) fun Tab.TabOptions( @StringRes name: Int, icon: ImageVector, iconSelected: ImageVector ): TabOptions { - val pager = LocalPager.currentOrThrow - val selected = RootTab.entries[pager.currentPage].tab == this + val pagerState = LocalPagerState.current + val selected = RootTab.entries[pagerState.currentPage].tab == this return TabOptions( 0u, diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0ef32e1..06ed49e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,7 +21,6 @@ simpleast-core = "master-SNAPSHOT" voyager = "1.0.0-rc07" [libraries] -accompanist-pager = { group = "com.google.accompanist", name = "accompanist-pager", version.ref = "accompanist" } accompanist-pager-indicators = { group = "com.google.accompanist", name = "accompanist-pager-indicators", version.ref = "accompanist" } accompanist-systemuicontroller = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanist" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity-compose" } @@ -61,7 +60,7 @@ voyager-tab-navigator = { group = "cafe.adriel.voyager", name = "voyager-tab-nav voyager-transitions = { group = "cafe.adriel.voyager", name = "voyager-transitions", version.ref = "voyager" } [bundles] -accompanist = ["accompanist-systemuicontroller", "accompanist-pager", "accompanist-pager-indicators"] +accompanist = ["accompanist-systemuicontroller", "accompanist-pager-indicators"] androidx = ["core-ktx", "androidx-browser", "androidx-lifecycle-runtime-ktx", "androidx-activity-compose", "androidx-paging-compose", "androidx-emoji2"] coil = ["coil-compose", "coil", "coil-gif"] compose = ["compose-ui", "compose-ui-graphics", "compose-ui-tooling-preview", "compose-material3", "compose-material-icons-extended"]