Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/main/kotlin/gg/essential/vigilance/Vigilant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,38 @@ abstract class Vigilant @JvmOverloads constructor(
error("Dependency must be a boolean PropertyType!")
}

property.dependsOn = dependency
property.dependsOn = { dependency.getAsBoolean() }
dependency.hasDependants = true
}

/**
* If the dependency is based on another property, make sure to also call setAsDependency
*/
fun addCustomDependency(dependingPropertyName: String, stateProvider: () -> Boolean) {
propertyCollector.getProperty(dependingPropertyName)!!.dependsOn = stateProvider
}

/**
* If the dependency is based on another property, make sure to also call setAsDependency
*/
fun addCustomDependency(dependingField: Field, stateProvider: () -> Boolean) {
propertyCollector.getProperty(dependingField)!!.dependsOn = stateProvider
}

/**
* Should be used when a property has a custom dependency based on this property
*/
fun setAsDependency(dependencyPropertyName: String) {
propertyCollector.getProperty(dependencyPropertyName)!!.hasDependants = true
}

/**
* Should be used when a property has a custom dependency based on this property
*/
fun setAsDependency(dependencyField: Field) {
propertyCollector.getProperty(dependencyField)!!.hasDependants = true
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of having separate methods for creating the dependency and then stating which field/property it depends on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you add a custom dependency, it doesn't need to be based on a property. For example, you could make a property be displayed only when you're connected to a certain server.
setAsDependency tells Vigilance to re-evaluate the visibility when the dependency property has changed. If setAsDependency hasn't been called, Vigilance will assume that the dependency doesn't care about any other properties.

Now that I think more about it, it would probably also be possible for addCustomDependency functions to have the dependency field as an optional argument.


@Deprecated(
message = "Due to startup performance penalties due to KReflect, we advise against using this.",
replaceWith = ReplaceWith("hidePropertyIf(javaField!!, condition())", "kotlin.reflect.jvm.javaField"),
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/gg/essential/vigilance/data/PropertyData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlin.reflect.KMutableProperty0

data class PropertyData(val attributes: PropertyAttributes, val value: PropertyValue, val instance: Vigilant) {
var action: ((Any?) -> Unit)? = null
var dependsOn: PropertyData? = null
var dependsOn: (() -> Boolean)? = null
var hasDependants: Boolean = false

fun getDataType() = attributes.type
Expand All @@ -23,7 +23,7 @@ data class PropertyData(val attributes: PropertyAttributes, val value: PropertyV

fun <T> getAs(clazz: Class<T>) = clazz.cast(getAsAny())

fun isHidden(): Boolean = if (dependsOn == null) false else !dependsOn!!.getAsBoolean()
fun isHidden(): Boolean = if (dependsOn == null) false else !dependsOn!!()

fun setValue(value: Any?) {
if (value == null) {
Expand Down