Skip to content

Commit 5e1c356

Browse files
Support for PopupWindow
1 parent 1276c58 commit 5e1c356

File tree

5 files changed

+130
-14
lines changed

5 files changed

+130
-14
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2020. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("unused")
18+
19+
package com.dylanc.viewbinding
20+
21+
import android.app.Activity
22+
import android.view.ViewGroup
23+
import android.widget.PopupWindow
24+
import androidx.fragment.app.Fragment
25+
import androidx.viewbinding.ViewBinding
26+
27+
inline fun <reified VB : ViewBinding> Activity.popupWindow(
28+
width: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
29+
height: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
30+
focusable: Boolean = false,
31+
crossinline block: VB.() -> Unit
32+
) =
33+
lazy {
34+
PopupWindow(inflateBinding<VB>(layoutInflater).apply(block).root, width, height, focusable)
35+
}
36+
37+
inline fun <reified VB : ViewBinding> Fragment.popupWindow(
38+
width: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
39+
height: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
40+
focusable: Boolean = false,
41+
crossinline block: VB.() -> Unit
42+
) =
43+
lazy {
44+
PopupWindow(inflateBinding<VB>(layoutInflater).apply(block).root, width, height, focusable)
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2020. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.dylanc.viewbinding
18+
19+
import android.view.View
20+
import androidx.viewbinding.ViewBinding
21+
22+
inline fun <reified VB : ViewBinding> View.getBinding() = getBinding(VB::class.java)
23+
24+
@Suppress("UNCHECKED_CAST")
25+
fun <VB : ViewBinding> View.getBinding(clazz: Class<VB>) =
26+
bindingCache as? VB ?: (clazz.getMethod("bind", View::class.java)
27+
.invoke(null, this) as VB)
28+
.also { binding -> bindingCache = binding }
29+
30+
private var View.bindingCache: Any?
31+
get() = getTag(Int.MIN_VALUE)
32+
set(value) = setTag(Int.MIN_VALUE, value)

viewbinding-ktx/src/main/java/com/dylanc/viewbinding/ViewBinding.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@ package com.dylanc.viewbinding
2121
import android.view.LayoutInflater
2222
import android.view.View
2323
import android.view.ViewGroup
24-
import androidx.databinding.ViewDataBinding
25-
import androidx.lifecycle.LifecycleOwner
2624
import androidx.viewbinding.ViewBinding
2725

28-
private var View.binding: Any?
29-
get() = getTag(Int.MIN_VALUE)
30-
set(value) = setTag(Int.MIN_VALUE, value)
31-
3226
inline fun <reified VB : ViewBinding> inflateBinding(layoutInflater: LayoutInflater) =
3327
VB::class.java.getMethod("inflate", LayoutInflater::class.java).invoke(null, layoutInflater) as VB
3428

@@ -40,10 +34,3 @@ inline fun <reified VB : ViewBinding> inflateBinding(
4034
) =
4135
VB::class.java.getMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.java)
4236
.invoke(null, layoutInflater, parent, attachToParent) as VB
43-
44-
inline fun <reified VB : ViewBinding> View.getBinding() = getBinding(VB::class.java)
45-
46-
@Suppress("UNCHECKED_CAST")
47-
fun <VB : ViewBinding> View.getBinding(clazz: Class<VB>) =
48-
binding as? VB ?: (clazz.getMethod("bind", View::class.java).invoke(null, this) as VB).also { binding = it }
49-
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2020. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("unused")
18+
19+
package com.dylanc.viewbinding.nonreflection
20+
21+
import android.app.Activity
22+
import android.view.LayoutInflater
23+
import android.view.ViewGroup
24+
import android.widget.PopupWindow
25+
import androidx.fragment.app.Fragment
26+
import androidx.viewbinding.ViewBinding
27+
28+
fun <VB : ViewBinding> Activity.popupWindow(
29+
inflate: (LayoutInflater) -> VB,
30+
width: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
31+
height: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
32+
focusable: Boolean = false,
33+
block: VB.() -> Unit
34+
) =
35+
lazy {
36+
PopupWindow(inflate(layoutInflater).apply(block).root, width, height, focusable)
37+
}
38+
39+
fun <VB : ViewBinding> Fragment.popupWindow(
40+
inflate: (LayoutInflater) -> VB,
41+
width: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
42+
height: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
43+
focusable: Boolean = false,
44+
block: VB.() -> Unit
45+
) =
46+
lazy {
47+
PopupWindow(inflate(layoutInflater).apply(block).root, width, height, focusable)
48+
}

viewbinding-nonreflection-ktx/src/main/java/com/dylanc/viewbinding/nonreflection/View.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ import androidx.viewbinding.ViewBinding
2525

2626
@Suppress("UNCHECKED_CAST")
2727
fun <VB : ViewBinding> View.getBinding(bind: (View) -> VB): VB =
28-
(getTag(Int.MIN_VALUE) as? VB) ?: bind(this).also { binding -> setTag(Int.MIN_VALUE, binding) }
28+
(bindingCache as? VB) ?: bind(this).also { binding -> bindingCache = binding }
29+
30+
private var View.bindingCache: Any?
31+
get() = getTag(Int.MIN_VALUE)
32+
set(value) = setTag(Int.MIN_VALUE, value)

0 commit comments

Comments
 (0)