-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Jintin/feature/revert_reflection
revert to reflection
- Loading branch information
Showing
9 changed files
with
126 additions
and
43 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
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
7 changes: 2 additions & 5 deletions
7
lib/src/main/java/com/jintin/bindingextension/BindingActivity.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 |
---|---|---|
@@ -1,19 +1,16 @@ | ||
package com.jintin.bindingextension | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.viewbinding.ViewBinding | ||
|
||
open class BindingActivity<V : ViewBinding>( | ||
private val bindingProvider: (LayoutInflater) -> V | ||
) : AppCompatActivity() { | ||
open class BindingActivity<V : ViewBinding> : AppCompatActivity() { | ||
|
||
lateinit var binding: V | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = bindingProvider.invoke(layoutInflater) | ||
binding = getBinding() | ||
setContentView(binding.root) | ||
} | ||
} |
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
17 changes: 0 additions & 17 deletions
17
lib/src/main/java/com/jintin/bindingextension/BindingHolder.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.jintin.bindingextension | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.viewbinding.ViewBinding | ||
import java.lang.reflect.ParameterizedType | ||
|
||
internal fun <V : ViewBinding> Class<*>.getBinding(layoutInflater: LayoutInflater): V { | ||
return try { | ||
@Suppress("UNCHECKED_CAST") | ||
getMethod( | ||
"inflate", | ||
LayoutInflater::class.java | ||
).invoke(null, layoutInflater) as V | ||
} catch (ex: Exception) { | ||
throw RuntimeException("The ViewBinding inflate function has been changed.", ex) | ||
} | ||
} | ||
|
||
internal fun <V : ViewBinding> Class<*>.getBinding( | ||
layoutInflater: LayoutInflater, | ||
container: ViewGroup? | ||
): V { | ||
return try { | ||
@Suppress("UNCHECKED_CAST") | ||
getMethod( | ||
"inflate", | ||
LayoutInflater::class.java, | ||
ViewGroup::class.java, | ||
Boolean::class.java | ||
).invoke(null, layoutInflater, container, false) as V | ||
} catch (ex: Exception) { | ||
throw RuntimeException("The ViewBinding inflate function has been changed.", ex) | ||
} | ||
} | ||
|
||
internal fun Class<*>.checkMethod(): Boolean { | ||
return try { | ||
getMethod( | ||
"inflate", | ||
LayoutInflater::class.java | ||
) | ||
true | ||
} catch (ex: Exception) { | ||
false | ||
} | ||
} | ||
|
||
internal fun Any.findClass(): Class<*> { | ||
var javaClass: Class<*> = this.javaClass | ||
var result: Class<*>? = null | ||
while (result == null || !result.checkMethod()) { | ||
result = (javaClass | ||
.genericSuperclass as? ParameterizedType) | ||
?.actualTypeArguments?.firstOrNull { | ||
if (it is Class<*>) { | ||
it.checkMethod() | ||
} else { | ||
false | ||
} | ||
} as? Class<*> | ||
javaClass = javaClass.superclass | ||
} | ||
return result | ||
} | ||
|
||
inline fun <reified V : ViewBinding> ViewGroup.toBinding(): V { | ||
return V::class.java.getMethod( | ||
"inflate", | ||
LayoutInflater::class.java, | ||
ViewGroup::class.java, | ||
Boolean::class.java | ||
).invoke(null, LayoutInflater.from(context), this, false) as V | ||
} | ||
|
||
internal fun <V : ViewBinding> BindingActivity<V>.getBinding(): V { | ||
return findClass().getBinding(layoutInflater) | ||
} | ||
|
||
internal fun <V : ViewBinding> BindingFragment<V>.getBinding( | ||
inflater: LayoutInflater, | ||
container: ViewGroup? | ||
): V { | ||
return findClass().getBinding(inflater, container) | ||
} |