From df695e8f4754a437d4c4551a53ff311850367516 Mon Sep 17 00:00:00 2001 From: Radek Kozak Date: Sun, 19 Jul 2020 12:58:54 +0200 Subject: [PATCH] Fix: multi-window mode insets problems (#236) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Takes care of few WindowInsets problems with HomeActivity layout: - toolbar not being correctly padded when returning from multi-window mode (bottom position) - toolbar not being correctly laid out when back to multi-window after activity was “hidden” with second activity Resolves: #236 --- .../main/java/io/plaidapp/ui/HomeActivity.kt | 71 ++------ app/src/main/res/layout/activity_home.xml | 157 ++++++++++-------- 2 files changed, 97 insertions(+), 131 deletions(-) diff --git a/app/src/main/java/io/plaidapp/ui/HomeActivity.kt b/app/src/main/java/io/plaidapp/ui/HomeActivity.kt index 38403a53a..42ff4f15d 100644 --- a/app/src/main/java/io/plaidapp/ui/HomeActivity.kt +++ b/app/src/main/java/io/plaidapp/ui/HomeActivity.kt @@ -35,10 +35,7 @@ import android.transition.TransitionManager import android.view.Menu import android.view.MenuItem import android.view.View -import android.view.ViewGroup import android.view.ViewStub -import android.view.WindowInsets -import android.widget.FrameLayout import android.widget.ImageView import android.widget.ProgressBar import android.widget.TextView @@ -74,11 +71,11 @@ import io.plaidapp.core.ui.recyclerview.InfiniteScrollListener import io.plaidapp.core.util.Activities import io.plaidapp.core.util.AnimUtils import io.plaidapp.core.util.ColorUtils -import io.plaidapp.core.util.ViewUtils import io.plaidapp.core.util.drawableToBitmap import io.plaidapp.core.util.event.Event import io.plaidapp.core.util.intentTo import io.plaidapp.dagger.inject +import io.plaidapp.databinding.ActivityHomeBinding import io.plaidapp.ui.recyclerview.FilterTouchHelperCallback import io.plaidapp.ui.recyclerview.GridItemDividerDecoration import javax.inject.Inject @@ -101,6 +98,8 @@ class HomeActivity : AppCompatActivity() { private lateinit var feedAdapter: FeedAdapter private lateinit var filtersList: RecyclerView + private lateinit var binding: ActivityHomeBinding + // data @Inject lateinit var sourcesRepository: SourcesRepository @@ -186,7 +185,8 @@ class HomeActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_home) + binding = ActivityHomeBinding.inflate(layoutInflater) + setContentView(binding.root) bindResources() inject(this) @@ -219,12 +219,6 @@ class HomeActivity : AppCompatActivity() { setupGrid() - // drawer layout treats fitsSystemWindows specially so we have to handle insets ourselves - drawer.setOnApplyWindowInsetsListener { _, insets -> - handleDrawerInsets(insets) - insets.consumeSystemWindowInsets() - } - setupTaskDescription() filtersList.apply { @@ -266,11 +260,11 @@ class HomeActivity : AppCompatActivity() { } private fun bindResources() { - drawer = findViewById(R.id.drawer) - toolbar = findViewById(R.id.toolbar) - grid = findViewById(R.id.grid) - filtersList = findViewById(R.id.filters) - loading = findViewById(android.R.id.empty) + drawer = binding.drawer + toolbar = binding.toolbar + grid = binding.grid + filtersList = binding.filters + loading = binding.empty columns = resources.getInteger(R.integer.num_columns) } @@ -320,51 +314,6 @@ class HomeActivity : AppCompatActivity() { } } - private fun handleDrawerInsets(insets: WindowInsets) { - // inset the toolbar down by the status bar height - val lpToolbar = (toolbar.layoutParams as ViewGroup.MarginLayoutParams).apply { - topMargin += insets.systemWindowInsetTop - leftMargin += insets.systemWindowInsetLeft - rightMargin += insets.systemWindowInsetRight - } - toolbar.layoutParams = lpToolbar - - // inset the grid top by statusbar+toolbar & the bottom by the navbar (don't clip) - grid.setPadding( - grid.paddingLeft + insets.systemWindowInsetLeft, // landscape - insets.systemWindowInsetTop + ViewUtils.getActionBarSize(this@HomeActivity), - grid.paddingRight + insets.systemWindowInsetRight, // landscape - grid.paddingBottom + insets.systemWindowInsetBottom - ) - - // we place a background behind the status bar to combine with it's semi-transparent - // color to get the desired appearance. Set it's height to the status bar height - val statusBarBackground = findViewById(R.id.status_bar_background) - val lpStatus = (statusBarBackground.layoutParams as FrameLayout.LayoutParams).apply { - height = insets.systemWindowInsetTop - } - statusBarBackground.layoutParams = lpStatus - - // inset the filters list for the status bar / navbar - // need to set the padding end for landscape case - val ltr = filtersList.layoutDirection == View.LAYOUT_DIRECTION_LTR - with(filtersList) { - setPaddingRelative( - paddingStart, - paddingTop + insets.systemWindowInsetTop, - paddingEnd + if (ltr) { - insets.systemWindowInsetRight - } else { - 0 - }, - paddingBottom + insets.systemWindowInsetBottom - ) - } - - // clear this listener so insets aren't re-applied - drawer.setOnApplyWindowInsetsListener(null) - } - override fun onActivityReenter(resultCode: Int, data: Intent?) { if (data == null || resultCode != Activity.RESULT_OK || !data.hasExtra(Activities.Dribbble.Shot.RESULT_EXTRA_SHOT_ID) diff --git a/app/src/main/res/layout/activity_home.xml b/app/src/main/res/layout/activity_home.xml index 487d6ae4a..9240b67fb 100644 --- a/app/src/main/res/layout/activity_home.xml +++ b/app/src/main/res/layout/activity_home.xml @@ -15,83 +15,100 @@ limitations under the License. --> - - - + + + android:layout_height="match_parent" + android:clipToPadding="false" + tools:context=".ui.HomeActivity"> - + android:layout_height="match_parent"> + + + + + + + + + + + + + + + + android:scrollbars="vertical" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + app:paddingTopSystemWindowInsets="@{true}" + app:paddingBottomSystemWindowInsets="@{true}" + app:paddingLeftSystemWindowInsets="@{true}" + app:paddingRightSystemWindowInsets="@{true}" + tools:listitem="@layout/filter_item" /> - - - - - - - - - - - - - - + + +