diff --git a/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingActivity.kt b/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingActivity.kt index 9457425..5dbb8e1 100644 --- a/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingActivity.kt +++ b/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingActivity.kt @@ -21,14 +21,14 @@ package com.dylanc.viewbinding.sample.base.reflection.kotlin import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.viewbinding.ViewBinding -import com.dylanc.viewbinding.base.BindingOwner -import com.dylanc.viewbinding.base.BindingOwnerDelegate +import com.dylanc.viewbinding.base.ActivityBinding +import com.dylanc.viewbinding.base.ActivityBindingDelegate /** * @author Dylan Cai */ abstract class BaseBindingActivity : AppCompatActivity(), - BindingOwner by BindingOwnerDelegate() { + ActivityBinding by ActivityBindingDelegate() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingFragment.kt b/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingFragment.kt index bbb6214..179a66c 100644 --- a/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingFragment.kt +++ b/sample/src/main/java/com/dylanc/viewbinding/sample/base/reflection/kotlin/BaseBindingFragment.kt @@ -23,14 +23,14 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.viewbinding.ViewBinding -import com.dylanc.viewbinding.base.BindingOwner -import com.dylanc.viewbinding.base.BindingOwnerDelegate +import com.dylanc.viewbinding.base.FragmentBinding +import com.dylanc.viewbinding.base.FragmentBindingDelegate /** * @author Dylan Cai */ abstract class BaseBindingFragment : Fragment(), - BindingOwner by BindingOwnerDelegate() { + FragmentBinding by FragmentBindingDelegate() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = createViewWithBinding(inflater, container) diff --git a/viewbinding-base/build.gradle b/viewbinding-base/build.gradle index 49db393..b3e8501 100644 --- a/viewbinding-base/build.gradle +++ b/viewbinding-base/build.gradle @@ -37,4 +37,5 @@ android { dependencies { implementation "com.google.android.material:material:$materialVersion" + implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion" } \ No newline at end of file diff --git a/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/ActivityBinding.kt b/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/ActivityBinding.kt new file mode 100644 index 0000000..4764784 --- /dev/null +++ b/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/ActivityBinding.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020. Dylan Cai + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.dylanc.viewbinding.base + +import android.app.Activity +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.viewbinding.ViewBinding + +interface ActivityBinding { + val binding: VB + fun Activity.setContentViewWithBinding() +} + +class ActivityBindingDelegate : ActivityBinding { + private lateinit var _binding: VB + + override val binding: VB get() = _binding + + override fun Activity.setContentViewWithBinding() { + _binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater) + setContentView(binding.root) + } +} \ No newline at end of file diff --git a/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/BindingOwner.kt b/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/BindingOwner.kt deleted file mode 100644 index 594176d..0000000 --- a/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/BindingOwner.kt +++ /dev/null @@ -1,38 +0,0 @@ -package com.dylanc.viewbinding.base - -import android.app.Activity -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.fragment.app.Fragment -import androidx.viewbinding.ViewBinding - -interface BindingOwner { - val Activity.binding: VB - val Fragment.binding: VB - fun Activity.setContentViewWithBinding() - fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View -} - -class BindingOwnerDelegate : BindingOwner { - private lateinit var _binding: VB - - override val Activity.binding: VB get() = _binding - - @Suppress("UNCHECKED_CAST") - override val Fragment.binding: VB - get() = requireNotNull(view) { "The property of binding has been destroyed." } - .getTag(R.id.tag_view_binding) as VB - - override fun Activity.setContentViewWithBinding() { - _binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater) - setContentView(binding.root) - } - - override fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View = - ViewBindingUtil.inflateWithGeneric(this, inflater, container, false) - .run { - root.setTag(R.id.tag_view_binding, this) - root - } -} \ No newline at end of file diff --git a/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/FragmentBinding.kt b/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/FragmentBinding.kt new file mode 100644 index 0000000..f6164fd --- /dev/null +++ b/viewbinding-base/src/main/java/com/dylanc/viewbinding/base/FragmentBinding.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2020. Dylan Cai + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.dylanc.viewbinding.base + +import android.os.Handler +import android.os.Looper +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.databinding.ViewDataBinding +import androidx.fragment.app.Fragment +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import androidx.viewbinding.ViewBinding +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KProperty + +interface FragmentBinding { + val binding: VB + fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View +} + +class FragmentBindingDelegate : FragmentBinding { + private var _binding: VB? = null + private val handler by lazy { Handler(Looper.getMainLooper()) } + + override val binding: VB + get() = requireNotNull(_binding) { "The property of binding has been destroyed." } + + override fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View { + if (_binding == null) { + _binding = ViewBindingUtil.inflateWithGeneric(this, inflater, container, false) + viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver { + override fun onDestroy(owner: LifecycleOwner) { + handler.post { _binding = null } + } + }) + } + return _binding!!.root + } +} \ No newline at end of file