-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
70 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/pavelrekun/rekado/services/extensions/BindingExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.pavelrekun.rekado.services.extensions | ||
|
||
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<T : ViewBinding>( | ||
val fragment: Fragment, | ||
val viewBindingFactory: (View) -> T | ||
) : ReadOnlyProperty<Fragment, T> { | ||
private var binding: T? = null | ||
|
||
init { | ||
fragment.lifecycle.addObserver(object : DefaultLifecycleObserver { | ||
override fun onCreate(owner: LifecycleOwner) { | ||
fragment.viewLifecycleOwnerLiveData.observe(fragment, Observer { | ||
it.lifecycle.addObserver(object : DefaultLifecycleObserver { | ||
override fun onDestroy(owner: LifecycleOwner) { | ||
binding = null | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
override fun getValue(thisRef: Fragment, property: KProperty<*>): T { | ||
val binding = binding | ||
if (binding != null) { | ||
return binding | ||
} | ||
|
||
val lifecycle = fragment.viewLifecycleOwner.lifecycle | ||
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) { | ||
throw IllegalStateException("Should not attempt to get bindings when Fragment views are destroyed.") | ||
} | ||
|
||
return viewBindingFactory(thisRef.requireView()).also { this@FragmentViewBindingDelegate.binding = it } | ||
} | ||
} | ||
|
||
fun <T : ViewBinding> Fragment.viewBinding(viewBindingFactory: (View) -> T) = | ||
FragmentViewBindingDelegate(this, viewBindingFactory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters