Add fragmentviewbindingdelegate-kt library

This commit is contained in:
Adolfo Santiago 2021-11-13 18:40:19 +01:00
parent 6eaf402dfa
commit 9efc706116
No known key found for this signature in database
GPG key ID: 244D6F9A317B4A65
4 changed files with 6 additions and 76 deletions

View file

@ -212,6 +212,7 @@ dependencies {
implementation(ApplicationLibs.bigImageViewer)
implementation(ApplicationLibs.conscryptAndroid)
implementation(ApplicationLibs.filemojiCompat)
implementation(ApplicationLibs.fragmentviewbindingdelegateKt)
implementation(ApplicationLibs.glideImage)
implementation(ApplicationLibs.glideImageViewFactory)
implementation(ApplicationLibs.markdownEdit)

View file

@ -22,7 +22,7 @@ package com.keylesspalace.tusky.core.extensions
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.keylesspalace.tusky.core.ui.fragment.FragmentViewBindingDelegate
import com.zhuinden.fragmentviewbindingdelegatekt.FragmentViewBindingDelegate
fun <VB : ViewBinding> Fragment.viewBinding(viewBindingFactory: (View) -> VB) =
FragmentViewBindingDelegate(this, viewBindingFactory)

View file

@ -1,74 +0,0 @@
/*
* Copyright 2021 Gabor Varadi
*
* 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.keylesspalace.tusky.core.ui.fragment
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class FragmentViewBindingDelegate<VB : ViewBinding>(
private val fragment: Fragment,
private val viewBindingFactory: (View) -> VB
) : ReadOnlyProperty<Fragment, VB> {
private var binding: VB? = null
init {
fragment.lifecycle.addObserver(object : DefaultLifecycleObserver {
val viewLifecycleOwnerDataObserver = Observer<LifecycleOwner?> {
val viewLifecycleOwner = it ?: return@Observer
viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
binding = null
}
})
}
override fun onCreate(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.observeForever(viewLifecycleOwnerDataObserver)
}
override fun onDestroy(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.removeObserver(viewLifecycleOwnerDataObserver)
}
})
}
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
val binding = binding
if(binding != null) {
return binding
}
val lifecycleState = fragment.viewLifecycleOwner.lifecycle.currentState
if(!lifecycleState.isAtLeast(Lifecycle.State.INITIALIZED)) {
throw IllegalStateException(
"Should not attempt to get bindings when Fragment views are destroyed."
)
}
return viewBindingFactory(thisRef.requireView()).also { this.binding = it }
}
}

View file

@ -25,9 +25,10 @@ object ApplicationLibs {
const val filemojiCompat = "1.0.17"
const val flexbox = "2.0.1"
const val fragmentKtx = "1.2.5"
const val fragmentviewbindingdelegateKt = "1.0.0"
const val glide = "4.12.0"
const val glideImage = "1.8.0"
const val lifecycle = "2.4.0"
const val lifecycle = "2.3.1"
const val markdownEdit = "1.0.0"
const val materialDesign = "1.4.0"
const val materialDrawer = "8.2.0"
@ -144,6 +145,8 @@ object ApplicationLibs {
const val bigImageViewer = "com.github.piasy:BigImageViewer:${Versions.bigImageViewer}"
const val conscryptAndroid = "org.conscrypt:conscrypt-android:${Versions.conscryptAndroid}"
const val filemojiCompat = "de.c1710:filemojicompat:${Versions.filemojiCompat}"
const val fragmentviewbindingdelegateKt =
"com.github.Zhuinden:fragmentviewbindingdelegate-kt:${Versions.fragmentviewbindingdelegateKt}"
const val glideImage = "com.github.piasy:GlideImageLoader:${Versions.glideImage}"
const val glideImageViewFactory =
"com.github.piasy:GlideImageViewFactory:${Versions.glideImage}"