Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit f3a9ecb

Browse files
author
Sergej Shafarenka
authored
Merge pull request #3 from beworker/2-decorview
Watch insets on android.id.content instead of the decorView
2 parents 93d7a18 + 1adcd25 commit f3a9ecb

File tree

10 files changed

+98
-37
lines changed

10 files changed

+98
-37
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<category android:name="android.intent.category.LAUNCHER" />
2020
</intent-filter>
2121
</activity>
22+
<activity android:name=".examples.FullScreenActivity" />
2223
</application>
2324

2425
</manifest>

app/src/main/java/de/halfbit/edgetoedge/sample/MainAdapter.kt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package de.halfbit.edgetoedge.sample
22

3+
import android.app.Activity
34
import android.view.LayoutInflater
45
import android.view.View
56
import android.view.ViewGroup
67
import android.widget.TextView
7-
import androidx.annotation.StringRes
88
import androidx.fragment.app.Fragment
99
import androidx.recyclerview.widget.RecyclerView
1010
import de.halfbit.edgetoedge.sample.examples.*
1111

12-
class MainAdapter(private val onItemClicked: OnItemClicked) :
13-
RecyclerView.Adapter<ItemViewHolder>() {
12+
class MainAdapter(
13+
private val onClick: (OnClick) -> Unit
14+
) : RecyclerView.Adapter<ItemViewHolder>() {
1415

1516
private var items = listOf(
16-
Item(R.string.splash) { SplashScreenFragment() },
17-
Item(R.string.toolbar) { ToolbarWithScrollableContentFragment() },
18-
Item(R.string.toolbar_fab) { ToolbarWithScrollableContentAndFabFragment() },
19-
Item(R.string.toolbar_viewpager) { ToolbarWithViewpagerFragment() },
20-
Item(R.string.bottom_sheet_dialog) { BottomSheetDialogFragment() },
21-
Item(R.string.constraint_layout_transitions) { ConstraintLayoutTransitionsFragment() }
17+
Item(R.string.splash, OnClick.CreateFragment { SplashScreenFragment() }),
18+
Item(R.string.toolbar,OnClick.CreateFragment { ToolbarWithScrollableContentFragment() }),
19+
Item(R.string.toolbar_fab, OnClick.CreateFragment { ToolbarWithScrollableContentAndFabFragment() }),
20+
Item(R.string.toolbar_viewpager, OnClick.CreateFragment { ToolbarWithViewpagerFragment() }),
21+
Item(R.string.bottom_sheet_dialog, OnClick.CreateFragment { BottomSheetDialogFragment() }),
22+
Item(R.string.constraint_layout_transitions, OnClick.CreateFragment { ConstraintLayoutTransitionsFragment() }),
23+
Item(R.string.full_screen_activity, OnClick.CreateActivity(FullScreenActivity::class.java))
2224
)
2325

2426
override fun getItemCount(): Int = items.size
@@ -27,7 +29,7 @@ class MainAdapter(private val onItemClicked: OnItemClicked) :
2729
itemView = LayoutInflater.from(parent.context).inflate(
2830
R.layout.fragment_main_item, parent, false
2931
),
30-
onItemClicked = onItemClicked
32+
onClick = onClick
3133
)
3234

3335
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
@@ -36,19 +38,22 @@ class MainAdapter(private val onItemClicked: OnItemClicked) :
3638
}
3739

3840
class Item(
39-
@StringRes val nameId: Int,
40-
val factory: FragmentFactory
41+
val nameId: Int,
42+
val onClick: OnClick
4143
)
4244

45+
sealed class OnClick {
46+
class CreateFragment(val createFragment: () -> Fragment) : OnClick()
47+
class CreateActivity(val activity: Class<out Activity>) : OnClick()
48+
}
49+
4350
class ItemViewHolder(
44-
itemView: View, val onItemClicked: OnItemClicked
51+
itemView: View,
52+
val onClick: (OnClick) -> Unit
4553
) : RecyclerView.ViewHolder(itemView) {
4654
fun bind(item: Item) {
4755
val textView = itemView as TextView
4856
textView.setText(item.nameId)
49-
textView.setOnClickListener { onItemClicked(item.factory) }
57+
textView.setOnClickListener { onClick(item.onClick) }
5058
}
5159
}
52-
53-
typealias FragmentFactory = () -> Fragment
54-
typealias OnItemClicked = (FragmentFactory) -> Unit

app/src/main/java/de/halfbit/edgetoedge/sample/MainFragment.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.halfbit.edgetoedge.sample
22

3+
import android.content.Intent
34
import android.os.Bundle
45
import android.view.LayoutInflater
56
import android.view.View
@@ -22,13 +23,20 @@ class MainFragment : BaseFragment() {
2223
}
2324

2425
recycler.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
25-
recycler.adapter = MainAdapter { createFragment ->
26-
requireActivity()
27-
.supportFragmentManager
28-
.beginTransaction()
29-
.replace(R.id.container, createFragment())
30-
.addToBackStack(null)
31-
.commit()
26+
recycler.adapter = MainAdapter { onClick ->
27+
when (onClick) {
28+
is OnClick.CreateFragment -> {
29+
requireActivity()
30+
.supportFragmentManager
31+
.beginTransaction()
32+
.replace(R.id.container, onClick.createFragment())
33+
.addToBackStack(null)
34+
.commit()
35+
}
36+
is OnClick.CreateActivity -> {
37+
startActivity(Intent(context, onClick.activity))
38+
}
39+
}
3240
}
3341
}
3442
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.halfbit.edgetoedge.sample.examples
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.appcompat.app.AppCompatActivity
6+
import de.halfbit.edgetoedge.Edge
7+
import de.halfbit.edgetoedge.edgeToEdge
8+
import de.halfbit.edgetoedge.sample.R
9+
10+
class FullScreenActivity : AppCompatActivity() {
11+
override fun onCreate(savedInstanceState: Bundle?) {
12+
super.onCreate(savedInstanceState)
13+
setContentView(R.layout.activity_full_screen)
14+
edgeToEdge {
15+
findViewById<View>(R.id.message).fit { Edge.BottomArc }
16+
}
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/container"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:background="@android:color/holo_blue_dark"
9+
android:paddingStart="24dp"
10+
android:paddingEnd="24dp"
11+
android:paddingBottom="8dp"
12+
tools:context="de.halfbit.edgetoedge.sample.MainActivity">
13+
14+
<TextView
15+
android:id="@+id/message"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:layout_gravity="bottom|center_horizontal"
19+
android:text="@string/full_screen_message"
20+
android:textAlignment="center"
21+
android:textColor="@android:color/white"
22+
android:textSize="16sp" />
23+
24+
</FrameLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<string name="option2">Option 2</string>
1515
<string name="option3">Option 3</string>
1616
<string name="constraint_layout_transitions">ConstraintLayout + Transitions</string>
17+
<string name="full_screen_activity">Full screen activity</string>
18+
<string name="full_screen_message">It is a full screen activity. This long message should not be covered by any of system bars.</string>
1719
<string name="tap_to_expand">Tap to expand</string>
1820
<string name="polar_light">An aurora (plural: auroras or aurorae),[a] sometimes referred to as polar lights, northern lights (aurora borealis), or southern lights (aurora australis), is a natural light display in the Earth\'s sky, predominantly seen in the high-latitude regions (around the Arctic and Antarctic).</string>
1921
</resources>

buildSrc/src/main/kotlin/Pom.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Pom {
66

77
const val group = "de.halfbit"
88
const val artifactId = "edge-to-edge"
9-
const val version = "0.10"
9+
const val version = "1.0-rc1"
1010

1111
object License {
1212
const val name = "The Apache License, Version 2.0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Version {
22
const val kotlin = "1.3.72"
3-
const val agp = "3.5.3"
3+
const val agp = "4.0.0"
44
const val dokka = "0.10.0"
55
const val jvmTarget = "1.8"
66
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Dec 24 12:45:58 CET 2019
1+
#Mon Jun 15 19:40:24 CEST 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

library/src/main/java/de/halfbit/edgetoedge/EdgeToEdgeExtensions.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package de.halfbit.edgetoedge
22

33
import android.app.Activity
44
import android.app.Dialog
5+
import android.view.View
56
import android.view.Window
67
import androidx.fragment.app.Fragment
78

89
/**
910
* Declares a set of view fitting rules and requests [android.view.WindowInsets] to be
1011
* applied to the view hierarchy. The library will fit the views according to their
11-
* fitting rules each time [WindowInsets] are applied. The set is usually declared in
12+
* fitting rules each time [android.view.WindowInsets] are applied. The set is usually declared in
1213
* the `Fragment.onViewCreated()` callback, but it can be re-declared at any time later,
13-
* for instance after applying a [ConstraintSet]. Each declaration adds new or overwrites
14-
* already existing view fitting rules. For removing a fitting rule [EdgeToEdgeBuilder.unfit]
15-
* method can be used.
14+
* for instance after applying a [androidx.constraintlayout.widget.ConstraintSet].
15+
* Each declaration adds new or overwrites already existing view fitting rules. For removing
16+
* a fitting rule [EdgeToEdgeBuilder.unfit] method can be used.
1617
*
1718
* ```
1819
* override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -40,18 +41,20 @@ fun Fragment.fitEdgeToEdge() {
4041

4142
inline fun Dialog.edgeToEdge(block: EdgeToEdgeBuilder.() -> Unit) {
4243
val window = requireNotNull(window) { "Dialog's window must be not null" }
43-
EdgeToEdgeBuilder(window.decorView, window).also(block).build()
44+
val contentView = findViewById<View>(android.R.id.content)
45+
EdgeToEdgeBuilder(contentView, window).also(block).build()
4446
}
4547

4648
inline fun Activity.edgeToEdge(block: EdgeToEdgeBuilder.() -> Unit) {
47-
val window = requireNotNull(window) { "Dialog's window must be not null" }
48-
EdgeToEdgeBuilder(window.decorView, window).also(block).build()
49+
val window = requireNotNull(window) { "Activity's window must be not null" }
50+
val contentView = findViewById<View>(android.R.id.content)
51+
EdgeToEdgeBuilder(contentView, window).also(block).build()
4952
}
5053

5154
fun Window.setEdgeToEdgeFlags() {
5255
with(decorView) {
5356
systemUiVisibility = systemUiVisibility or
54-
android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
55-
android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
57+
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
58+
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
5659
}
5760
}

0 commit comments

Comments
 (0)