Skip to content

Commit

Permalink
Add version of VBPD without reflection in API
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed Sep 9, 2020
1 parent 485431c commit 5927173
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 110 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ android {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation project(':library')

implementation project(':vbpd')
}
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,17 @@ task clean(type: Delete) {
}

ext {
compileSdkVersion = 30
buildToolsVersion = "30.0.3"
minSdkVersion = 14
targetSdkVersion = 29

versionName = "1.2.0"

dependencies = [:]
dependencies.kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib"
dependencies.coreKtx = 'androidx.core:core-ktx:1.3.1'
dependencies.fragmentKtx = 'androidx.fragment:fragment-ktx:1.2.5'
dependencies.viewBinding = 'androidx.databinding:viewbinding:4.0.1'
dependencies.lifecycleCommonJava8 = 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
}
2 changes: 0 additions & 2 deletions library/src/main/AndroidManifest.xml

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion library/publishing.gradle → publishing.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ afterEvaluate {
artifact sourceJar

groupId = 'com.kirich1409.viewbindingpropertydelegate'
artifactId = 'viewbindingpropertydelegate'
artifactId = project.ext.artifactId
version = rootProject.ext.versionName

pom {
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include ':app'
include ':library'
include ':vbpd'
include ':vbpd-noreflection'
rootProject.name = "View Binding Delegate"
File renamed without changes.
50 changes: 50 additions & 0 deletions vbpd-noreflection/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
}

ext {
artifactId = "vbpd-noreflection"
}

apply from: rootProject.file('publishing.gradle')

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

// For Kotlin projects
kotlinOptions {
jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs +=
['-module-name', "com.github.kirich1409.ViewBindingPropertyDelegate.noreflection"]
}

buildFeatures {
buildConfig = false
viewBinding = false
dataBinding = false
aidl = false
renderScript = false
resValues = false
shaders = false
}
}

dependencies {
implementation rootProject.ext.dependencies.coreKtx
implementation rootProject.ext.dependencies.fragmentKtx
implementation rootProject.ext.dependencies.kotlinStdlib
implementation rootProject.ext.dependencies.viewBinding
implementation rootProject.ext.dependencies.lifecycleCommonJava8
}
1 change: 1 addition & 0 deletions vbpd-noreflection/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="by.kirich1409.viewbindingdelegate.noreflection" />
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import android.view.View
import androidx.annotation.IdRes
import androidx.core.app.ComponentActivity
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.internal.ActivityViewBinder
import by.kirich1409.viewbindingdelegate.internal.requireViewByIdCompat

@PublishedApi
internal class ActivityViewBindingProperty<A : ComponentActivity, T : ViewBinding>(
private class ActivityViewBindingProperty<A : ComponentActivity, T : ViewBinding>(
viewBinder: (A) -> T
) : ViewBindingProperty<A, T>(viewBinder) {

Expand All @@ -28,20 +25,6 @@ public fun <A : ComponentActivity, T : ViewBinding> ComponentActivity.viewBindin
return ActivityViewBindingProperty(viewBinder)
}

/**
* Create new [ViewBinding] associated with the [Activity][ComponentActivity]
*
* @param viewBindingRootId Root view's id that will be used as root for the view binding
*/
@JvmName("viewBindingActivity")
public inline fun <reified T : ViewBinding> ComponentActivity.viewBinding(
@IdRes viewBindingRootId: Int
): ViewBindingProperty<ComponentActivity, T> {
val activityViewBinder =
ActivityViewBinder(T::class.java) { it.requireViewByIdCompat(viewBindingRootId) }
return viewBinding(activityViewBinder::bind)
}

/**
* Create new [ViewBinding] associated with the [Activity][ComponentActivity] and allow customize how
* a [View] will be bounded to the view binding.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import androidx.annotation.IdRes
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.internal.DialogFragmentViewBinder


@PublishedApi
internal class DialogFragmentViewBindingProperty<F : DialogFragment, T : ViewBinding>(
private class DialogFragmentViewBindingProperty<F : DialogFragment, T : ViewBinding>(
viewBinder: (F) -> T
) : ViewBindingProperty<F, T>(viewBinder) {

Expand All @@ -30,18 +27,6 @@ public fun <F : DialogFragment, T : ViewBinding> DialogFragment.dialogViewBindin
return DialogFragmentViewBindingProperty(viewBinder)
}

/**
* Create new [ViewBinding] associated with the [DialogFragment]'s view
*
* @param viewBindingRootId Id of the root view from your custom view
*/
@JvmName("viewBindingDialogFragment")
public inline fun <reified T : ViewBinding> DialogFragment.dialogViewBinding(
@IdRes viewBindingRootId: Int
): ViewBindingProperty<DialogFragment, T> {
return dialogViewBinding(DialogFragmentViewBinder(T::class.java, viewBindingRootId)::bind)
}

/**
* Create new [ViewBinding] associated with the [DialogFragment]
*
Expand All @@ -52,7 +37,7 @@ public inline fun <F : DialogFragment, T : ViewBinding> DialogFragment.dialogVie
crossinline vbFactory: (View) -> T,
crossinline viewProvider: (F) -> View
): ViewBindingProperty<F, T> {
return DialogFragmentViewBindingProperty { fragment -> vbFactory(viewProvider(fragment)) }
return dialogViewBinding { fragment -> vbFactory(viewProvider(fragment)) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import android.view.View
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.internal.FragmentViewBinder

@PublishedApi
internal class FragmentViewBindingProperty<F : Fragment, T : ViewBinding>(
private class FragmentViewBindingProperty<F : Fragment, T : ViewBinding>(
viewBinder: (F) -> T
) : ViewBindingProperty<F, T>(viewBinder) {

Expand All @@ -24,14 +22,6 @@ public fun <F : Fragment, T : ViewBinding> Fragment.viewBinding(viewBinder: (F)
return FragmentViewBindingProperty(viewBinder)
}

/**
* Create new [ViewBinding] associated with the [Fragment]
*/
@JvmName("viewBindingFragment")
public inline fun <reified T : ViewBinding> Fragment.viewBinding(): ViewBindingProperty<Fragment, T> {
return viewBinding(FragmentViewBinder(T::class.java)::bind)
}

/**
* Create new [ViewBinding] associated with the [Fragment]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ public abstract class ViewBindingProperty<in R : Any, T : ViewBinding>(

private val mainHandler = Handler(Looper.getMainLooper())
}
}
}
1 change: 1 addition & 0 deletions vbpd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 15 additions & 11 deletions library/build.gradle → vbpd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ plugins {
id 'kotlin-android'
}

apply from: 'publishing.gradle'
ext {
artifactId = "viewbindingpropertydelegate"
}

apply from: rootProject.file('publishing.gradle')

android {
compileSdkVersion 29
buildToolsVersion "30.0.3"
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
minSdkVersion 14
targetSdkVersion 29
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion

consumerProguardFiles 'proguard-rules.pro'
}
Expand Down Expand Up @@ -40,9 +44,9 @@ android {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.fragment:fragment-ktx:1.2.5'
implementation 'androidx.databinding:viewbinding:4.0.1'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
}
implementation rootProject.ext.dependencies.coreKtx
implementation rootProject.ext.dependencies.fragmentKtx
implementation rootProject.ext.dependencies.kotlinStdlib
implementation rootProject.ext.dependencies.viewBinding
api project(":vbpd-noreflection")
}
File renamed without changes.
1 change: 1 addition & 0 deletions vbpd/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="by.kirich1409.viewbindingdelegate" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@file:Suppress("RedundantVisibilityModifier", "unused")

package by.kirich1409.viewbindingdelegate

import android.app.Activity
import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.RestrictTo
import androidx.core.app.ActivityCompat
import androidx.core.app.ComponentActivity
import androidx.viewbinding.ViewBinding

@RestrictTo(RestrictTo.Scope.LIBRARY)
@PublishedApi
internal class ActivityViewBinder<T : ViewBinding>(
private val viewBindingClass: Class<T>,
private val viewProvider: (Activity) -> View
) {

/**
* Cache static method `ViewBinding.bind(View)`
*/
private val bindViewMethod by lazy(LazyThreadSafetyMode.NONE) {
viewBindingClass.getMethod("bind", View::class.java)
}

/**
* Create new [ViewBinding] instance
*/
@Suppress("UNCHECKED_CAST")
fun bind(activity: Activity): T {
val view = viewProvider(activity)
return bindViewMethod(null, view) as T
}
}

/**
* Create new [ViewBinding] associated with the [Activity][ComponentActivity]
*
* @param viewBindingRootId Root view's id that will be used as root for the view binding
*/
@JvmName("viewBindingActivity")
public inline fun <reified T : ViewBinding> ComponentActivity.viewBinding(
@IdRes viewBindingRootId: Int
): ViewBindingProperty<ComponentActivity, T> {
val activityViewBinder =
ActivityViewBinder(T::class.java) { ActivityCompat.requireViewById(this, viewBindingRootId) }
return viewBinding(activityViewBinder::bind)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package by.kirich1409.viewbindingdelegate.internal
@file:Suppress("RedundantVisibilityModifier", "unused")

package by.kirich1409.viewbindingdelegate

import android.view.View
import androidx.annotation.IdRes
Expand Down Expand Up @@ -38,3 +40,15 @@ internal class DialogFragmentViewBinder<T : ViewBinding>(
}
}
}

/**
* Create new [ViewBinding] associated with the [DialogFragment]'s view
*
* @param viewBindingRootId Id of the root view from your custom view
*/
@JvmName("viewBindingDialogFragment")
public inline fun <reified T : ViewBinding> DialogFragment.dialogViewBinding(
@IdRes viewBindingRootId: Int
): ViewBindingProperty<DialogFragment, T> {
return dialogViewBinding(DialogFragmentViewBinder(T::class.java, viewBindingRootId)::bind)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package by.kirich1409.viewbindingdelegate.internal
@file:Suppress("RedundantVisibilityModifier", "unused")

package by.kirich1409.viewbindingdelegate

import android.view.View
import androidx.annotation.RestrictTo
Expand All @@ -24,3 +26,11 @@ internal class FragmentViewBinder<T : ViewBinding>(private val viewBindingClass:
return bindViewMethod(null, fragment.requireView()) as T
}
}

/**
* Create new [ViewBinding] associated with the [Fragment]
*/
@JvmName("viewBindingFragment")
public inline fun <reified T : ViewBinding> Fragment.viewBinding(): ViewBindingProperty<Fragment, T> {
return viewBinding(FragmentViewBinder(T::class.java)::bind)
}

0 comments on commit 5927173

Please sign in to comment.