Skip to content

Commit c481ce2

Browse files
Optimized interface names
1 parent fd6f24d commit c481ce2

File tree

6 files changed

+102
-44
lines changed

6 files changed

+102
-44
lines changed

sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingActivity.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ package com.dylanc.viewbinding.sample.base.reflection.kotlin
2121
import android.os.Bundle
2222
import androidx.appcompat.app.AppCompatActivity
2323
import androidx.viewbinding.ViewBinding
24-
import com.dylanc.viewbinding.base.BindingOwner
25-
import com.dylanc.viewbinding.base.BindingOwnerDelegate
24+
import com.dylanc.viewbinding.base.ActivityBinding
25+
import com.dylanc.viewbinding.base.ActivityBindingDelegate
2626

2727
/**
2828
* @author Dylan Cai
2929
*/
3030
abstract class BaseBindingActivity<VB : ViewBinding> : AppCompatActivity(),
31-
BindingOwner<VB> by BindingOwnerDelegate() {
31+
ActivityBinding<VB> by ActivityBindingDelegate() {
3232

3333
override fun onCreate(savedInstanceState: Bundle?) {
3434
super.onCreate(savedInstanceState)

sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingFragment.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import android.view.LayoutInflater
2323
import android.view.ViewGroup
2424
import androidx.fragment.app.Fragment
2525
import androidx.viewbinding.ViewBinding
26-
import com.dylanc.viewbinding.base.BindingOwner
27-
import com.dylanc.viewbinding.base.BindingOwnerDelegate
26+
import com.dylanc.viewbinding.base.FragmentBinding
27+
import com.dylanc.viewbinding.base.FragmentBindingDelegate
2828

2929
/**
3030
* @author Dylan Cai
3131
*/
3232
abstract class BaseBindingFragment<VB : ViewBinding> : Fragment(),
33-
BindingOwner<VB> by BindingOwnerDelegate() {
33+
FragmentBinding<VB> by FragmentBindingDelegate() {
3434

3535
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
3636
createViewWithBinding(inflater, container)

viewbinding-base/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ android {
3737

3838
dependencies {
3939
implementation "com.google.android.material:material:$materialVersion"
40+
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
4041
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.base
18+
19+
import android.app.Activity
20+
import android.view.LayoutInflater
21+
import android.view.View
22+
import android.view.ViewGroup
23+
import androidx.fragment.app.Fragment
24+
import androidx.viewbinding.ViewBinding
25+
26+
interface ActivityBinding<VB : ViewBinding> {
27+
val binding: VB
28+
fun Activity.setContentViewWithBinding()
29+
}
30+
31+
class ActivityBindingDelegate<VB : ViewBinding> : ActivityBinding<VB> {
32+
private lateinit var _binding: VB
33+
34+
override val binding: VB get() = _binding
35+
36+
override fun Activity.setContentViewWithBinding() {
37+
_binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater)
38+
setContentView(binding.root)
39+
}
40+
}

viewbinding-base/src/main/java/com/dylanc/viewbinding/base/BindingOwner.kt

-38
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.base
18+
19+
import android.os.Handler
20+
import android.os.Looper
21+
import android.view.LayoutInflater
22+
import android.view.View
23+
import android.view.ViewGroup
24+
import androidx.databinding.ViewDataBinding
25+
import androidx.fragment.app.Fragment
26+
import androidx.lifecycle.DefaultLifecycleObserver
27+
import androidx.lifecycle.LifecycleOwner
28+
import androidx.viewbinding.ViewBinding
29+
import kotlin.properties.ReadOnlyProperty
30+
import kotlin.reflect.KProperty
31+
32+
interface FragmentBinding<VB : ViewBinding> {
33+
val binding: VB
34+
fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View
35+
}
36+
37+
class FragmentBindingDelegate<VB : ViewBinding> : FragmentBinding<VB> {
38+
private var _binding: VB? = null
39+
private val handler by lazy { Handler(Looper.getMainLooper()) }
40+
41+
override val binding: VB
42+
get() = requireNotNull(_binding) { "The property of binding has been destroyed." }
43+
44+
override fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View {
45+
if (_binding == null) {
46+
_binding = ViewBindingUtil.inflateWithGeneric(this, inflater, container, false)
47+
viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
48+
override fun onDestroy(owner: LifecycleOwner) {
49+
handler.post { _binding = null }
50+
}
51+
})
52+
}
53+
return _binding!!.root
54+
}
55+
}

0 commit comments

Comments
 (0)