From c80e30303ad1a840dcadb95b65d13a34a8321e52 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 20 Jun 2023 16:57:10 -0700 Subject: [PATCH] Create paging based multi webview layout --- .../frost/compose/webview/FrostWebCompose.kt | 5 +-- .../frost/main/MainScreenWebView.kt | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app-compose/src/main/kotlin/com/pitchedapps/frost/compose/webview/FrostWebCompose.kt b/app-compose/src/main/kotlin/com/pitchedapps/frost/compose/webview/FrostWebCompose.kt index cb24703c1..4af7739d3 100644 --- a/app-compose/src/main/kotlin/com/pitchedapps/frost/compose/webview/FrostWebCompose.kt +++ b/app-compose/src/main/kotlin/com/pitchedapps/frost/compose/webview/FrostWebCompose.kt @@ -28,7 +28,6 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.viewinterop.AndroidView -import androidx.core.view.children import androidx.core.widget.NestedScrollView import com.google.common.flogger.FluentLogger import com.pitchedapps.frost.ext.WebTargetId @@ -85,6 +84,8 @@ class FrostWebCompose( var webView by remember { mutableStateOf(null) } + logger.atInfo().log("Webview %s %s", tabId, webView?.hashCode()) + webView?.let { wv -> val lifecycleOwner = LocalLifecycleOwner.current @@ -164,7 +165,7 @@ class FrostWebCompose( }, modifier = modifier, onRelease = { parentFrame -> - val wv = parentFrame.children.first() as WebView + val wv = parentFrame.getChildAt(0) as WebView onDispose(wv) logger.atInfo().log("Released webview for %s", tabId) }, diff --git a/app-compose/src/main/kotlin/com/pitchedapps/frost/main/MainScreenWebView.kt b/app-compose/src/main/kotlin/com/pitchedapps/frost/main/MainScreenWebView.kt index e2c2f7935..c9da5835c 100644 --- a/app-compose/src/main/kotlin/com/pitchedapps/frost/main/MainScreenWebView.kt +++ b/app-compose/src/main/kotlin/com/pitchedapps/frost/main/MainScreenWebView.kt @@ -16,8 +16,11 @@ */ package com.pitchedapps.frost.main +import androidx.compose.foundation.ExperimentalFoundationApi 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.material.ExperimentalMaterialApi import androidx.compose.material.pullrefresh.PullRefreshIndicator import androidx.compose.material.pullrefresh.pullRefresh @@ -30,6 +33,7 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -38,6 +42,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.lifecycle.viewmodel.compose.viewModel +import com.pitchedapps.frost.compose.webview.FrostWebCompose import com.pitchedapps.frost.ext.WebTargetId import com.pitchedapps.frost.web.state.FrostWebStore import com.pitchedapps.frost.web.state.TabListAction.SelectHomeTab @@ -111,7 +116,34 @@ private fun MainScreenWebContainer( modifier = modifier, store = store, ) { - homeTabComposables.find { it.tabId == selectedTab }?.WebView() + MainPager(selectedTab, items = homeTabComposables) + // homeTabComposables.find { it.tabId == selectedTab }?.WebView() + + // MultiViewContainer(store = store) + + // SampleContainer(selectedTab = selectedTab, items = items) + } +} + +@OptIn(ExperimentalFoundationApi::class) +@Composable +fun MainPager(selectedTab: WebTargetId?, items: List) { + + val pagerState = rememberPagerState { items.size } + + LaunchedEffect(selectedTab, items) { + val i = items.indexOfFirst { it.tabId == selectedTab } + if (i != -1) { + pagerState.scrollToPage(i) + } + } + + HorizontalPager( + state = pagerState, + userScrollEnabled = false, + beyondBoundsPageCount = 10, // Do not allow view release + ) { page -> + items[page].WebView() } }