Skip to content

Commit

Permalink
Fix: multi-window mode insets problems (#236)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
radekkozak committed Jul 19, 2020
1 parent e703957 commit df695e8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 131 deletions.
71 changes: 10 additions & 61 deletions app/src/main/java/io/plaidapp/ui/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<View>(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)
Expand Down
157 changes: 87 additions & 70 deletions app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,83 +15,100 @@
limitations under the License.
-->

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context=".ui.HomeActivity">

<FrameLayout
xmlns:tools="http://schemas.android.com/tools">

<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context=".ui.HomeActivity">

<View
android:id="@+id/status_bar_background"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/status_bar_back" />
android:layout_height="match_parent">

<View
android:id="@+id/status_bar_background"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/status_bar_back" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:elevation="1dp"
android:paddingTop="?android:actionBarSize"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
app:paddingTopSystemWindowInsets="@{true}"
app:paddingBottomSystemWindowInsets="@{true}"
app:paddingLeftSystemWindowInsets="@{true}"
app:paddingRightSystemWindowInsets="@{true}" />

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/Widget.Plaid.HomeToolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:elevation="1dp"
android:outlineProvider="none"
android:title="@string/app_name"
app:marginTopSystemWindowInsets="@{true}"
app:marginBottomSystemWindowInsets="@{true}"
app:marginLeftSystemWindowInsets="@{true}"
app:marginRightSystemWindowInsets="@{true}" />

<ProgressBar
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateTint="?android:colorAccent"
android:indeterminateTintMode="src_in" />

<ViewStub
android:id="@+id/stub_no_filters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/spacing_huge"
android:layout="@layout/no_filters" />

<ViewStub
android:id="@+id/stub_no_connection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="@layout/no_connection" />

</FrameLayout>

<!-- filter drawer -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/grid"
android:layout_width="match_parent"
android:id="@+id/filters"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/background_dark"
android:clipToPadding="false"
android:elevation="1dp"
android:elevation="@dimen/z_drawer"
android:paddingTop="@dimen/spacing_normal"
android:paddingBottom="@dimen/spacing_normal"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
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" />

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/Widget.Plaid.HomeToolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:elevation="1dp"
android:outlineProvider="none"
android:title="@string/app_name" />

<ProgressBar
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateTint="?android:colorAccent"
android:indeterminateTintMode="src_in" />

<ViewStub
android:id="@+id/stub_no_filters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/spacing_huge"
android:layout="@layout/no_filters" />

<ViewStub
android:id="@+id/stub_no_connection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="@layout/no_connection" />

</FrameLayout>

<!-- filter drawer -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/filters"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/background_dark"
android:clipToPadding="false"
android:elevation="@dimen/z_drawer"
android:paddingTop="@dimen/spacing_normal"
android:paddingBottom="@dimen/spacing_normal"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/filter_item" />

</androidx.drawerlayout.widget.DrawerLayout>
</androidx.drawerlayout.widget.DrawerLayout>

</layout>

1 comment on commit df695e8

@mcdowellheart2020
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.