From f196ab85ff8aaab92fb32a4a310a91921ab8fb83 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 29 Dec 2023 11:23:33 -0700 Subject: [PATCH] Move the progress bar to the bottom when using the bottom app bar. https://redmine.stoutner.com/issues/1070 --- .../activities/MainWebViewActivity.kt | 2 +- .../adapters/WebViewStateAdapter.kt | 6 +-- .../fragments/WebViewTabFragment.kt | 54 +++++++++---------- ... => webview_framelayout_bottom_appbar.xml} | 12 ++--- .../layout/webview_framelayout_top_appbar.xml | 49 +++++++++++++++++ 5 files changed, 84 insertions(+), 39 deletions(-) rename app/src/main/res/layout/{webview_framelayout.xml => webview_framelayout_bottom_appbar.xml} (84%) create mode 100644 app/src/main/res/layout/webview_framelayout_top_appbar.xml diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt index 3e6acce2..f43e66d1 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -644,7 +644,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook drawerLayout.visibility = View.GONE // Initialize the WebView state adapter. - webViewStateAdapter = WebViewStateAdapter(this) + webViewStateAdapter = WebViewStateAdapter(this, bottomAppBar) // Set the pager adapter on the web view pager. webViewViewPager2.adapter = webViewStateAdapter diff --git a/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewStateAdapter.kt b/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewStateAdapter.kt index 7a7913a5..d1663630 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewStateAdapter.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewStateAdapter.kt @@ -34,7 +34,7 @@ import com.stoutner.privacybrowser.views.NestedScrollWebView import java.util.LinkedList -class WebViewStateAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) { +class WebViewStateAdapter(fragmentActivity: FragmentActivity, private val bottomAppBar: Boolean) : FragmentStateAdapter(fragmentActivity) { // Define the class variables. private val webViewFragmentsList = LinkedList() @@ -84,7 +84,7 @@ class WebViewStateAdapter(fragmentActivity: FragmentActivity) : FragmentStateAda fun addPage(pagePosition: Int, url: String) { // Add a new page. - webViewFragmentsList.add(pagePosition, WebViewTabFragment.createPage(pagePosition, url)) + webViewFragmentsList.add(pagePosition, WebViewTabFragment.createPage(pagePosition, url, bottomAppBar)) // Update the view pager. notifyItemInserted(pagePosition) @@ -156,7 +156,7 @@ class WebViewStateAdapter(fragmentActivity: FragmentActivity) : FragmentStateAda fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle) { // Restore the page. - webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState)) + webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState, bottomAppBar)) // Update the view pager. The position is zero indexed. notifyItemInserted(webViewFragmentsList.size - 1) diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.kt b/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.kt index 34568cb5..2dae2419 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.kt @@ -36,10 +36,11 @@ import java.util.Calendar // Define the class constants. private const val CREATE_NEW_PAGE = "A" -private const val PAGE_POSITION = "B" -private const val URL = "C" -private const val SAVED_STATE = "D" -private const val SAVED_NESTED_SCROLL_WEBVIEW_STATE = "E" +private const val BOTTOM_APP_BAR = "B" +private const val PAGE_POSITION = "C" +private const val SAVED_NESTED_SCROLL_WEBVIEW_STATE = "D" +private const val SAVED_STATE = "E" +private const val URL = "F" class WebViewTabFragment : Fragment() { // Define the public variables. @@ -58,7 +59,7 @@ class WebViewTabFragment : Fragment() { private lateinit var nestedScrollWebView: NestedScrollWebView companion object { - fun createPage(pageNumber: Int, url: String?): WebViewTabFragment { + fun createPage(pageNumber: Int, url: String?, bottomAppBar: Boolean): WebViewTabFragment { // Create an arguments bundle. val argumentsBundle = Bundle() @@ -66,6 +67,7 @@ class WebViewTabFragment : Fragment() { argumentsBundle.putBoolean(CREATE_NEW_PAGE, true) argumentsBundle.putInt(PAGE_POSITION, pageNumber) argumentsBundle.putString(URL, url) + argumentsBundle.putBoolean(BOTTOM_APP_BAR, bottomAppBar) // Create a new instance of the WebView tab fragment. val webViewTabFragment = WebViewTabFragment() @@ -77,13 +79,14 @@ class WebViewTabFragment : Fragment() { return webViewTabFragment } - fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle): WebViewTabFragment { + fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle, bottomAppBar: Boolean): WebViewTabFragment { // Create an arguments bundle val argumentsBundle = Bundle() // Store the saved states in the arguments bundle. argumentsBundle.putBundle(SAVED_STATE, savedState) argumentsBundle.putBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE, savedNestedScrollWebViewState) + argumentsBundle.putBoolean(BOTTOM_APP_BAR, bottomAppBar) // Create a new instance of the WebView tab fragment. val webViewTabFragment = WebViewTabFragment() @@ -105,6 +108,23 @@ class WebViewTabFragment : Fragment() { } override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + // Get the bottom app bar status from the arguments. + val bottomAppBar = requireArguments().getBoolean(BOTTOM_APP_BAR) + + // Inflate the tab's WebView according to the app bar position. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container. + // The fragment will take care of attaching the root automatically. + val newPageView = if (bottomAppBar) + layoutInflater.inflate(R.layout.webview_framelayout_bottom_appbar, container, false) + else + layoutInflater.inflate(R.layout.webview_framelayout_top_appbar, container, false) + + // Get handles for the views. + nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview) + val progressBar = newPageView.findViewById(R.id.progress_bar) + + // Store the WebView fragment ID in the nested scroll WebView. + nestedScrollWebView.webViewFragmentId = fragmentId + // Check to see if the fragment is being restarted without the app being killed. return if (savedInstanceState == null) { // The fragment is not being restarted. It is either new or is being restored after the app was killed. // Check to see if a new page is being created. @@ -113,17 +133,6 @@ class WebViewTabFragment : Fragment() { val pagePosition = requireArguments().getInt(PAGE_POSITION) val url = requireArguments().getString(URL)!! - // Inflate the tab's WebView. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container. - // The fragment will take care of attaching the root automatically. - val newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false) - - // Get handles for the views. - nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview) - val progressBar = newPageView.findViewById(R.id.progress_bar) - - // Store the WebView fragment ID in the nested scroll WebView. - nestedScrollWebView.webViewFragmentId = fragmentId - // Request the main activity initialize the WebView. newTabListener.initializeWebView(nestedScrollWebView, pagePosition, progressBar, url, false) @@ -134,17 +143,6 @@ class WebViewTabFragment : Fragment() { val savedState = requireArguments().getBundle(SAVED_STATE)!! val savedNestedScrollWebViewState = requireArguments().getBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE)!! - // Inflate the tab's WebView. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container. - // The fragment will take care of attaching the root automatically. - val newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false) - - // Get handles for the views. - nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview) - val progressBar = newPageView.findViewById(R.id.progress_bar) - - // Store the WebView fragment ID in the nested scroll WebView. - nestedScrollWebView.webViewFragmentId = fragmentId - // Restore the nested scroll WebView state. nestedScrollWebView.restoreNestedScrollWebViewState(savedNestedScrollWebViewState) diff --git a/app/src/main/res/layout/webview_framelayout.xml b/app/src/main/res/layout/webview_framelayout_bottom_appbar.xml similarity index 84% rename from app/src/main/res/layout/webview_framelayout.xml rename to app/src/main/res/layout/webview_framelayout_bottom_appbar.xml index 42b40954..16ece121 100644 --- a/app/src/main/res/layout/webview_framelayout.xml +++ b/app/src/main/res/layout/webview_framelayout_bottom_appbar.xml @@ -1,7 +1,7 @@ @@ -37,16 +36,15 @@ android:visibility="invisible" /> + `android:layout_height="2dp"` works best for API >= 23, but `3dp` is required for visibility on API <= 22.--> - \ No newline at end of file + android:visibility="gone" /> + diff --git a/app/src/main/res/layout/webview_framelayout_top_appbar.xml b/app/src/main/res/layout/webview_framelayout_top_appbar.xml new file mode 100644 index 00000000..a404f304 --- /dev/null +++ b/app/src/main/res/layout/webview_framelayout_top_appbar.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + -- 2.45.2