refactor: non-inline all layout content for black-box problem

This commit is contained in:
2025-12-14 20:35:26 +08:00
parent 0dd1c5b57d
commit 8e9e1a9632
9 changed files with 139 additions and 78 deletions

View File

@@ -19,10 +19,7 @@
*
* This file is created by fankes on 2025/2/25.
*/
@file:Suppress(
"unused", "FunctionName", "PropertyName", "ConstPropertyName", "UNCHECKED_CAST",
"MemberVisibilityCanBePrivate", "TOPLEVEL_TYPEALIASES_ONLY", "NON_PUBLIC_CALL_FROM_PUBLIC_INLINE"
)
@file:Suppress("unused", "FunctionName", "PropertyName", "ConstPropertyName", "UNCHECKED_CAST", "MemberVisibilityCanBePrivate")
package com.highcapable.hikage.core
@@ -40,6 +37,7 @@ import androidx.annotation.StringRes
import androidx.core.graphics.drawable.toBitmap
import androidx.viewbinding.ViewBinding
import com.highcapable.betterandroid.ui.extension.binding.ViewBinding
import com.highcapable.betterandroid.ui.extension.binding.ViewBindingBuilder
import com.highcapable.betterandroid.ui.extension.component.base.DisplayDensity
import com.highcapable.betterandroid.ui.extension.component.base.getColorCompat
import com.highcapable.betterandroid.ui.extension.component.base.getColorStateListCompat
@@ -57,6 +55,7 @@ import com.highcapable.hikage.core.base.HikageFactory
import com.highcapable.hikage.core.base.HikageFactoryBuilder
import com.highcapable.hikage.core.base.HikagePerformer
import com.highcapable.hikage.core.base.HikageView
import com.highcapable.hikage.core.base.LayoutParamsBody
import com.highcapable.hikage.core.base.PerformerException
import com.highcapable.hikage.core.base.ProvideException
import com.highcapable.hikage.core.extension.ResourcesScope
@@ -119,8 +118,8 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
context: Context,
parent: ViewGroup? = null,
attachToParent: Boolean = parent != null,
factory: HikageFactoryBuilder.() -> Unit = {},
performer: HikagePerformer<LP>
noinline factory: HikageFactoryBuilder.() -> Unit = {},
noinline performer: HikagePerformer<LP>
) = create(classOf<LP>(), context, parent, attachToParent, factory, performer)
/**
@@ -132,7 +131,7 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
* @param performer the performer body.
* @return [Hikage]
*/
inline fun create(
fun create(
context: Context,
parent: ViewGroup? = null,
attachToParent: Boolean = parent != null,
@@ -150,7 +149,7 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
* @param performer the performer body.
* @return [Hikage]
*/
inline fun <LP : ViewGroup.LayoutParams> create(
fun <LP : ViewGroup.LayoutParams> create(
lpClass: Class<LP>,
context: Context,
parent: ViewGroup? = null,
@@ -208,9 +207,6 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
) = Delegate(lpClass, factory, performer)
}
/** The [Hikage] layout params body type. */
private typealias LayoutParamsBody<LP> = LP.() -> Unit
/**
* The [Hikage.Performer] scope interface.
*/
@@ -434,19 +430,6 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
*/
private fun createAttributeSet(context: Context): AttributeSet = XmlBlockBypass.newParser(context)
/**
* Start a new performer [LP].
* @param parent the parent view group.
* @param attachToParent whether to attach the layout to the parent when the [parent] is filled.
* @param context the context, priority is given to [parent]'s context.
* @return [Performer]
*/
private inline fun <reified LP : ViewGroup.LayoutParams> newPerformer(
parent: ViewGroup? = null,
attachToParent: Boolean = parent != null,
context: Context? = null
) = newPerformer(classOf<LP>(), parent, attachToParent, context)
/**
* Start a new performer [LP].
* @param lpClass the layout params type.
@@ -548,6 +531,32 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
/** The count of providing views. */
private var provideCount = 0
/**
* Provide a new [View] instance [V].
* @param viewClass the view class.
* @param lparams the view layout params.
* @param id the view id, generated by default.
* @param init the view initialization body.
* @return [V]
*/
@Hikageable
fun <V : View> View(
viewClass: Class<V>,
lparams: LayoutParams? = null,
id: String? = null,
init: HikageView<V> = {}
): V {
val lpDelegate = LayoutParams.from(current, lpClass, parent, lparams)
val view = createView(viewClass, id, context)
view.layoutParams = lpDelegate.create()
requireNoPerformers(viewClass.name) { view.init() }
startProvide(id, viewClass)
addToParentIfRequired(view)
return view
}
/**
* Provide a new [View] instance [V].
* @param lparams the view layout params.
@@ -560,18 +569,8 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
inline fun <reified V : View> View(
lparams: LayoutParams? = null,
id: String? = null,
init: HikageView<V> = {}
): V {
val lpDelegate = LayoutParams.from(current, lpClass, parent, lparams)
val view = createView(classOf<V>(), id, context)
view.layoutParams = lpDelegate.create()
requireNoPerformers(classOf<V>().name) { view.init() }
startProvide(id, classOf<V>())
addToParentIfRequired(view)
return view
}
noinline init: HikageView<V> = {}
) = View(classOf<V>(), lparams, id, init)
/**
* Provide a new [View] instance.
@@ -581,12 +580,67 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
* @return [View]
*/
@Hikageable
inline fun View(
fun View(
lparams: LayoutParams? = null,
id: String? = null,
init: HikageView<View> = {}
) = View<View>(lparams, id, init)
/**
* Provide a new [ViewGroup] instance [VG].
*
* Provide the new type of [ViewGroup.LayoutParams] down via [LP].
*
* - Note: The [VG] must be inherited from [ViewGroup].
* @param viewClass the view class.
* @param lpClass the layout params class.
* @param lparams the view layout params.
* @param id the view id, generated by default.
* @param init the view initialization body.
* @param performer the performer body.
* @return [VG]
*/
@Hikageable
fun <VG : ViewGroup, LP : ViewGroup.LayoutParams> ViewGroup(
viewClass: Class<VG>,
lpClass: Class<LP>,
lparams: LayoutParams? = null,
id: String? = null,
init: HikageView<VG> = {},
performer: HikagePerformer<LP> = {}
): VG {
val lpDelegate = LayoutParams.from(current, lpClass, parent, lparams)
val view = createView(viewClass, id, context)
view.layoutParams = lpDelegate.create()
requireNoPerformers(viewClass.name) { view.init() }
startProvide(id, viewClass)
addToParentIfRequired(view)
newPerformer(lpClass, view).apply(performer)
return view
}
/**
* Provide a new [ViewGroup] instance [VG].
*
* - Note: The [VG] must be inherited from [ViewGroup].
* @param viewClass the view class.
* @param lparams the view layout params.
* @param id the view id, generated by default.
* @param init the view initialization body.
* @param performer the performer body.
* @return [VG]
*/
@Hikageable
fun <VG : ViewGroup> ViewGroup(
viewClass: Class<VG>,
lparams: LayoutParams? = null,
id: String? = null,
init: HikageView<VG> = {},
performer: HikagePerformer<ViewGroup.LayoutParams> = {}
) = ViewGroup(viewClass, classOf<ViewGroup.LayoutParams>(), lparams, id, init, performer)
/**
* Provide a new [ViewGroup] instance [VG].
*
@@ -604,20 +658,9 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
inline fun <reified VG : ViewGroup, reified LP : ViewGroup.LayoutParams> ViewGroup(
lparams: LayoutParams? = null,
id: String? = null,
init: HikageView<VG> = {},
performer: HikagePerformer<LP> = {}
): VG {
val lpDelegate = LayoutParams.from(current, lpClass, parent, lparams)
val view = createView(classOf<VG>(), id, context)
view.layoutParams = lpDelegate.create()
requireNoPerformers(classOf<VG>().name) { view.init() }
startProvide(id, classOf<VG>())
addToParentIfRequired(view)
newPerformer<LP>(view).apply(performer)
return view
}
noinline init: HikageView<VG> = {},
noinline performer: HikagePerformer<LP> = {}
) = ViewGroup(classOf<VG>(), classOf<LP>(), lparams, id, init, performer)
/**
* Provide a new [ViewGroup] instance [VG].
@@ -633,9 +676,9 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
inline fun <reified VG : ViewGroup> ViewGroup(
lparams: LayoutParams? = null,
id: String? = null,
init: HikageView<VG> = {},
performer: HikagePerformer<ViewGroup.LayoutParams> = {}
) = ViewGroup<VG, ViewGroup.LayoutParams>(lparams, id, init, performer)
noinline init: HikageView<VG> = {},
noinline performer: HikagePerformer<ViewGroup.LayoutParams> = {}
) = ViewGroup(classOf<VG>(), lparams, id, init, performer)
/**
* Provide layout from [resId].
@@ -661,16 +704,18 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
/**
* Provide layout from [ViewBinding].
* @param bindingBuilder the view binding builder.
* @param lparams the view layout params.
* @param id the view id, generated by default.
* @return [VB]
*/
@Hikageable
inline fun <reified VB : ViewBinding> Layout(
fun <VB : ViewBinding> Layout(
bindingBuilder: ViewBindingBuilder<VB>,
lparams: LayoutParams? = null,
id: String? = null
): VB {
val viewBinding = ViewBinding<VB>().inflate(context.layoutInflater, parent, attachToParent = false)
val viewBinding = bindingBuilder.inflate(context.layoutInflater, parent, attachToParent = false)
val view = viewBinding.root
startProvide(id, view.javaClass, view)
@@ -686,6 +731,18 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
return viewBinding
}
/**
* Provide layout from [ViewBinding].
* @param lparams the view layout params.
* @param id the view id, generated by default.
* @return [VB]
*/
@Hikageable
inline fun <reified VB : ViewBinding> Layout(
lparams: LayoutParams? = null,
id: String? = null
) = Layout(ViewBinding<VB>(), lparams, id)
/**
* Provide layout from exists [View].
* @param view the view instance.
@@ -702,7 +759,7 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
if (view.parent != null) throw ProvideException("The view $view already has a parent, cannot be provided.")
startProvide(id, view.javaClass, view)
val lpDelegate = LayoutParams.from(current = this@Hikage, lpClass, parent, lparams, view.layoutParams)
val lpDelegate = LayoutParams.from(current, lpClass, parent, lparams, view.layoutParams)
view.layoutParams = lpDelegate.create()
provideView(view, id)
@@ -984,7 +1041,6 @@ class Hikage private constructor(private val factories: List<HikageFactory>) {
* @param attachToParent whether to attach the layout to the parent when the [parent] is filled.
* @return [Hikage]
*/
fun create(context: Context, parent: ViewGroup? = null, attachToParent: Boolean = parent != null) =
create(lpClass, context, parent, attachToParent, factory, performer)
}

View File

@@ -27,10 +27,12 @@ package com.highcapable.hikage.core.base
* The exception of performing view.
* @param message the exception message.
*/
@PublishedApi
internal class PerformerException(message: String) : Exception(message)
/**
* The exception of providing view.
* @param message the exception message.
*/
@PublishedApi
internal class ProvideException(message: String) : Exception(message)

View File

@@ -19,7 +19,7 @@
*
* This file is created by fankes on 2025/2/25.
*/
@file:Suppress("unused", "FunctionName", "NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
@file:Suppress("unused", "FunctionName")
@file:JvmName("HikageableUtils")
package com.highcapable.hikage.core.base
@@ -38,6 +38,9 @@ typealias HikagePerformer<LP> = Hikage.Performer<LP>.() -> Unit
*/
typealias HikageView<V> = V.() -> Unit
/** The [Hikage] layout params body type. */
internal typealias LayoutParamsBody<LP> = LP.() -> Unit
/**
* Start performing a [Hikage] layout [LP].
* @param context the context to create the layout.
@@ -47,14 +50,13 @@ typealias HikageView<V> = V.() -> Unit
* @param performer the performer body.
* @return [Hikage]
*/
@JvmSynthetic
@JvmName("HikageableTyped")
inline fun <reified LP : ViewGroup.LayoutParams> Hikageable(
context: Context,
parent: ViewGroup? = null,
attachToParent: Boolean = parent != null,
factory: HikageFactoryBuilder.() -> Unit = {},
performer: HikagePerformer<LP>
noinline factory: HikageFactoryBuilder.() -> Unit = {},
noinline performer: HikagePerformer<LP>
) = Hikage.create(context, parent, attachToParent, factory, performer)
/**
@@ -67,7 +69,7 @@ inline fun <reified LP : ViewGroup.LayoutParams> Hikageable(
* @return [Hikage]
*/
@JvmSynthetic
inline fun Hikageable(
fun Hikageable(
context: Context,
parent: ViewGroup? = null,
attachToParent: Boolean = parent != null,
@@ -81,7 +83,6 @@ inline fun Hikageable(
* @param performer the performer body.
* @return [Hikage.Delegate]<[LP]>
*/
@JvmSynthetic
@JvmName("HikageableTyped")
inline fun <reified LP : ViewGroup.LayoutParams> Hikageable(
noinline factory: HikageFactoryBuilder.() -> Unit = {},