Skip to content

Commit

Permalink
Remove unused APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed Jan 12, 2025
1 parent d266cd3 commit 3d33cf7
Show file tree
Hide file tree
Showing 18 changed files with 351 additions and 270 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
## 2.0.0 (In progress)

- Changed under hood tracking of View lifecycle in ViewBindingPropertyDelegate
- Removed ViewBindingPropertyDelegate base on Lifecycle
- Removed Jetpack Lifecycle dependencies
- Remove Strict Modes checks
- Up Min sdk to API Level 21 Android 5.0 Lollipop
- Remove internal checking of possibility to access host's view
- Change artifact from `com.github.kirich1409:viewbindingpropertydelegate-*` to `dev.androidbroadcast.vbpd:vbpd-*`
- Change classes base package from `com.github.kirich1409.viewbindingpropertydelegate` to `dev.androidbroadcast.vbpd`. It allows to use 1.X and 2.X versions together
- Remove onViewDestroyCallback\(\) from ViewBindingPropertyDelegate
- Update Jetpack dependencies. All of them connected as compileOnly dependencies that's why you can use any version of them in project, but with required APIs
- All libraries artifacts distributes as AAR instead of JAR
- Fix bugs working during inter-Fragment's animations
- Minor improvements

### Changed under hood tracking of Fragment View lifecycle
- Fragment: Replace Jetpack Lifecycle with FragmentManager.FragmentLifecycleCallbacks
- Activity: Remove cleaning view after Activity.onDestroy()
- ViewGroup: Will be kept during View instance life
- RecyclerView.ViewHolder: Will be kept during ViewHolder instance life

## 1.5.10
- Bugs fixes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.androidbroadcast.vbpd.sample

import android.graphics.Color
import android.os.Bundle
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
Expand All @@ -11,12 +13,16 @@ import androidx.fragment.app.replace
import dev.androidbroadcast.vbpd.sample.databinding.ActivityMainBinding
import dev.androidbroadcast.vbpd.viewBinding

class MainActivity : AppCompatActivity(R.layout.activity_main), PersonListFragment.OnPersonClickListener {
class MainActivity :
AppCompatActivity(R.layout.activity_main),
PersonListFragment.OnPersonClickListener {

private val viewBinding by viewBinding(ActivityMainBinding::bind)

override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.dark(Color.WHITE)
)
super.onCreate(savedInstanceState)
setSupportActionBar(viewBinding.appbar)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("RedundantVisibilityModifier")

package dev.androidbroadcast.vbpd

import androidx.annotation.CallSuper
Expand All @@ -15,34 +13,18 @@ import kotlin.reflect.KProperty
*/
public interface ViewBindingProperty<in R : Any, out T : ViewBinding> : ReadOnlyProperty<R, T> {

/**
* Clear all cached data. Will be called when own object destroys view
*/
@MainThread
public fun clear()
}

public open class LazyViewBindingProperty<in R : Any, out T : ViewBinding>(
protected val viewBinder: (R) -> T,
) : ViewBindingProperty<R, T> {

protected var viewBinding: Any? = null

@Suppress("UNCHECKED_CAST")
@MainThread
public override fun getValue(
thisRef: R,
property: KProperty<*>,
): T {
return viewBinding as? T ?: viewBinder(thisRef).also { viewBinding ->
this.viewBinding = viewBinding
}
}

@MainThread
@CallSuper
public override fun clear() {
this.viewBinding = null
public fun clear() {
// Do nothing
}
}

/**
* Eager implementation of [ViewBindingProperty] that holds [ViewBinding] instance.
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public open class EagerViewBindingProperty<in R : Any, out T : ViewBinding>(
private val viewBinding: T,
Expand All @@ -53,35 +35,31 @@ public open class EagerViewBindingProperty<in R : Any, out T : ViewBinding>(
thisRef: R,
property: KProperty<*>,
): T = viewBinding

@MainThread
public override fun clear() {
// Do nothing
}
}

/**
* Lazy implementation of [ViewBindingProperty] that creates [ViewBinding] instance on the first access.
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public abstract class BaseViewBindingProperty<in R : Any, T : ViewBinding>(
public open class LazyViewBindingProperty<in R : Any, T : ViewBinding>(
private val viewBinder: (R) -> T,
) : ViewBindingProperty<R, T> {

protected var viewBinding: T? = null
private var viewBinding: T? = null

@MainThread
public override fun getValue(
thisRef: R,
property: KProperty<*>,
): T {
checkMainThread("Access to ViewBinding from non UI (Main) thread forbidden")
viewBinding?.let { return@getValue it }
this.viewBinding = null
return viewBinder(thisRef).also { this.viewBinding = it }
return viewBinding ?: viewBinder(thisRef).also { viewBinding = it }
}

@MainThread
@CallSuper
public override fun clear() {
checkMainThread()
this.viewBinding = null
viewBinding = null
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ package dev.androidbroadcast.vbpd.internal
import android.os.Looper
import androidx.annotation.RestrictTo

/**
* Check if the current thread is the main thread. If not, throw an exception.
*/
@RestrictTo(RestrictTo.Scope.LIBRARY)
internal fun checkMainThread() {
check(Looper.getMainLooper() === Looper.myLooper()) {
check(isMainLooper()) {
"The method must be called on the main thread"
}
}

/**
* Check if the current thread is the main thread. If not, throw an exception with the provided [reason].
*
* @param reason The reason why the method must be called on the main thread.
*/
@RestrictTo(RestrictTo.Scope.LIBRARY)
internal fun checkMainThread(reason: String) {
check(Looper.getMainLooper() === Looper.myLooper()) {
check(isMainLooper()) {
"The method must be called on the main thread. Reason: $reason."
}
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
private fun isMainLooper(): Boolean {
return Looper.getMainLooper() === Looper.myLooper()
}
9 changes: 9 additions & 0 deletions vbpd-reflection/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ plugins {
id("vbpdconfig")
}

android {

buildTypes {
release {
consumerProguardFiles("proguard-rules.pro")
}
}
}

dependencies {
// Use compileOnly dependencies because usage
// ViewBindingPropertyDelegate without adding them in the project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@file:Suppress("RedundantVisibilityModifier", "unused")
@file:JvmName("ReflectionActivityViewBindings")

package dev.androidbroadcast.vbpd
Expand All @@ -8,7 +7,6 @@ import android.view.View
import androidx.annotation.IdRes
import androidx.core.app.ActivityCompat
import androidx.viewbinding.ViewBinding
import dev.androidbroadcast.vbpd.internal.ViewBindingCache
import dev.androidbroadcast.vbpd.internal.findRootView

/**
Expand Down Expand Up @@ -81,7 +79,7 @@ public fun <A : Activity, T : ViewBinding> Activity.viewBinding(
): ViewBindingProperty<A, T> = when (createMethod) {
CreateMethod.BIND -> viewBinding(viewBindingClass, ::findRootView)
CreateMethod.INFLATE -> {
activityViewBinding(viewNeedsInitialization = false) {
ActivityViewBindingProperty {
ViewBindingCache.getInflateWithLayoutInflater(viewBindingClass)
.inflate(layoutInflater, null, false)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
@file:Suppress("RedundantVisibilityModifier", "unused") @file:JvmName("ReflectionFragmentViewBindings")
@file:JvmName("ReflectionFragmentViewBindings")

package dev.androidbroadcast.vbpd

import androidx.annotation.IdRes
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import dev.androidbroadcast.vbpd.internal.ViewBindingCache
import dev.androidbroadcast.vbpd.internal.findRootView
import dev.androidbroadcast.vbpd.internal.requireViewByIdCompat

/**
* Create new [ViewBinding] associated with the [Fragment]
*
* @param viewBindingRootId Root view's id that will be used as a root for the view binding
*
* @return [ViewBindingProperty] that holds [ViewBinding] instance
*/
@JvmName("viewBindingFragment")
public inline fun <reified T : ViewBinding> Fragment.viewBinding(
@IdRes viewBindingRootId: Int,
): ViewBindingProperty<Fragment, T> {
return viewBinding(T::class.java, viewBindingRootId)
}

/**
* Create new [ViewBinding] associated with the [DialogFragment]
*
* @param viewBindingClass Class of expected [ViewBinding]
* @param viewBindingRootId Root view's id that will be used as a root for the view binding
*
* @return [ViewBindingProperty] that holds [ViewBinding] instance
*/
@JvmName("viewBindingFragment")
public fun <T : ViewBinding> DialogFragment.viewBinding(
viewBindingClass: Class<T>,
Expand All @@ -28,6 +42,14 @@ public fun <T : ViewBinding> DialogFragment.viewBinding(
}
}

/**
* Create new [ViewBinding] associated with the [Fragment]
*
* @param viewBindingClass Class of expected [ViewBinding]
* @param viewBindingRootId Root view's id that will be used as a root for the view binding
*
* @return [ViewBindingProperty] that holds [ViewBinding] instance
*/
@JvmName("viewBindingFragment")
public fun <T : ViewBinding> Fragment.viewBinding(
viewBindingClass: Class<T>,
Expand All @@ -42,7 +64,9 @@ public fun <T : ViewBinding> Fragment.viewBinding(
/**
* Create new [ViewBinding] associated with the [Fragment]
*
* @param T Class of expected [ViewBinding] result class
* @param createMethod Method that will be used to create [ViewBinding] instance
*
* @return [ViewBindingProperty] that holds [ViewBinding] instance
*/
@JvmName("viewBindingFragment")
public inline fun <reified T : ViewBinding> Fragment.viewBinding(
Expand All @@ -55,6 +79,9 @@ public inline fun <reified T : ViewBinding> Fragment.viewBinding(
* Create new [ViewBinding] associated with the [Fragment]
*
* @param viewBindingClass Class of expected [ViewBinding] result class
* @param createMethod Method that will be used to create [ViewBinding] instance
*
* @return [ViewBindingProperty] that holds [ViewBinding] instance
*/
@JvmName("viewBindingFragment")
public fun <T : ViewBinding> Fragment.viewBinding(
Expand Down
Loading

0 comments on commit 3d33cf7

Please sign in to comment.