From 14e76b405a350043e9e8bd19f81ef6f453c2ae06 Mon Sep 17 00:00:00 2001 From: wada811 Date: Sun, 7 Mar 2021 23:15:32 +0900 Subject: [PATCH 1/3] Delete sample of ViewBinding --- .../viewbinding/ViewBindingActivity.kt | 34 ------------------- .../viewbinding/ViewBindingFragment.kt | 32 ----------------- sample/src/main/res/layout/view_stub.xml | 13 ------- 3 files changed, 79 deletions(-) delete mode 100644 sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingActivity.kt delete mode 100644 sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingFragment.kt delete mode 100644 sample/src/main/res/layout/view_stub.xml diff --git a/sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingActivity.kt b/sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingActivity.kt deleted file mode 100644 index eaeb060..0000000 --- a/sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingActivity.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.wada811.databindingktx.viewbinding - -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import com.wada811.databindingktx.R -import com.wada811.databindingktx.databinding.ViewBindingActivityBinding -import com.wada811.viewbinding.viewBinding - -class ViewBindingActivity : AppCompatActivity(R.layout.view_binding_activity) { - private val binding by viewBinding(ViewBindingActivityBinding::bind) - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - binding.button.setOnClickListener { - val fragment = supportFragmentManager.findFragmentById(R.id.fragment)!! - if (fragment.isDetached) { - binding.button.text = "Detach" - supportFragmentManager.beginTransaction() - .attach(fragment) - .commit() - } else { - binding.button.text = "Attach" - supportFragmentManager.beginTransaction() - .detach(fragment) - .commit() - } - } - if (savedInstanceState == null) { - val text = "Fragment is attached." - supportFragmentManager.beginTransaction() - .add(R.id.fragment, ViewBindingFragment.newInstance(text), text) - .commit() - } - } -} diff --git a/sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingFragment.kt b/sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingFragment.kt deleted file mode 100644 index 3c65114..0000000 --- a/sample/src/main/java/com/wada811/databindingktx/viewbinding/ViewBindingFragment.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.wada811.databindingktx.viewbinding - -import android.os.Bundle -import android.view.View -import androidx.fragment.app.Fragment -import com.wada811.databindingktx.R -import com.wada811.databindingktx.databinding.ViewBindingFragmentBinding -import com.wada811.databindingktx.databinding.ViewStubBinding -import com.wada811.viewbinding.viewBinding - -class ViewBindingFragment : Fragment(R.layout.view_stub) { - private val viewStubBinding by viewBinding(ViewStubBinding::bind) - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - viewStubBinding.viewStub.setOnInflateListener { stub, inflated -> - val binding = ViewBindingFragmentBinding.bind(inflated) - binding.text.text = requireArguments().getString(EXTRA_TEXT) - } - if (!viewStubBinding.viewStub.isInflated) { - viewStubBinding.viewStub.viewStub?.inflate() - } - } - - companion object { - private const val EXTRA_TEXT = "text" - fun newInstance(text: String) = ViewBindingFragment().also { fragment -> - fragment.arguments = Bundle().also { bundle -> - bundle.putString(EXTRA_TEXT, text) - } - } - } -} diff --git a/sample/src/main/res/layout/view_stub.xml b/sample/src/main/res/layout/view_stub.xml deleted file mode 100644 index 32cafcb..0000000 --- a/sample/src/main/res/layout/view_stub.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - \ No newline at end of file From 5c65ceb3220929c8595d41e3b870d9e127981d26 Mon Sep 17 00:00:00 2001 From: wada811 Date: Sun, 7 Mar 2021 23:20:09 +0900 Subject: [PATCH 2/3] Update libraries --- build.gradle | 4 +-- gradle/wrapper/gradle-wrapper.properties | 6 ++--- sample/build.gradle | 32 +++++++++++++----------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/build.gradle b/build.gradle index 12f7574..9dd3365 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,11 @@ buildscript { - ext.kotlin_version = '1.3.72' + ext.kotlin_version = '1.4.31' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.0.1' + classpath 'com.android.tools.build:gradle:4.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1b6ab2e..9acc5ff 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sat Apr 25 16:47:20 JST 2020 +#Sat Feb 27 23:00:16 JST 2021 distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip +zipStoreBase=GRADLE_USER_HOME diff --git a/sample/build.gradle b/sample/build.gradle index 24bc44e..16843ce 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -31,26 +31,28 @@ android { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.appcompat:appcompat:1.2.0' implementation project(':DataBinding-ktx') - implementation 'com.squareup.leakcanary:leakcanary-android:2.2' + debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.6' + releaseImplementation 'com.squareup.leakcanary:leakcanary-android-release:2.6' + // Core library - androidTestImplementation 'androidx.test:core:1.2.0' + androidTestImplementation 'androidx.test:core:1.3.0' // AndroidJUnitRunner and JUnit Rules - androidTestImplementation 'androidx.test:runner:1.2.0' - androidTestImplementation 'androidx.test:rules:1.2.0' + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestImplementation 'androidx.test:rules:1.3.0' // Assertions - androidTestImplementation 'androidx.test.ext:junit:1.1.1' - androidTestImplementation 'androidx.test.ext:truth:1.2.0' - androidTestImplementation 'com.google.truth:truth:1.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.ext:truth:1.3.0' + androidTestImplementation 'com.google.truth:truth:1.0.1' // Espresso dependencies - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-web:3.2.0' - androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-intents:3.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-web:3.3.0' + androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.3.0' // LeakCanary - androidTestImplementation "com.squareup.leakcanary:leakcanary-android-instrumentation:2.2" + androidTestImplementation "com.squareup.leakcanary:leakcanary-android-instrumentation:2.6" } From 6a050b5a4b9d311413f76e905f582d02e4891350 Mon Sep 17 00:00:00 2001 From: wada811 Date: Sun, 7 Mar 2021 23:23:07 +0900 Subject: [PATCH 3/3] Fix memory leak --- .../databinding/ActivityDataBinding.kt | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/DataBinding-ktx/src/main/java/com/wada811/databinding/ActivityDataBinding.kt b/DataBinding-ktx/src/main/java/com/wada811/databinding/ActivityDataBinding.kt index 4ea495c..d29fc0c 100644 --- a/DataBinding-ktx/src/main/java/com/wada811/databinding/ActivityDataBinding.kt +++ b/DataBinding-ktx/src/main/java/com/wada811/databinding/ActivityDataBinding.kt @@ -8,20 +8,15 @@ import androidx.databinding.DataBindingUtil import androidx.databinding.ViewDataBinding import androidx.fragment.app.FragmentActivity -fun FragmentActivity.dataBinding(): Lazy = object : Lazy { - private var binding: T? = null - override fun isInitialized(): Boolean = binding != null - override val value: T - get() = binding ?: bind(getContentView()).also { - it.lifecycleOwner = this@dataBinding - binding = it - } - - private fun FragmentActivity.getContentView(): View { - return checkNotNull(findViewById(android.R.id.content).getChildAt(0)) { - "Call setContentView or Use Activity's secondary constructor passing layout res id." - } +fun FragmentActivity.dataBinding(): Lazy = lazy(LazyThreadSafetyMode.NONE) { + bind(getContentView()).also { + it.lifecycleOwner = this@dataBinding } +} - private fun bind(view: View): T = DataBindingUtil.bind(view)!! +private fun bind(view: View): T = DataBindingUtil.bind(view)!! +private fun FragmentActivity.getContentView(): View { + return checkNotNull(findViewById(android.R.id.content).getChildAt(0)) { + "Call setContentView or Use Activity's secondary constructor passing layout res id." + } }