Skip to content

Commit

Permalink
feat: Add function to reset options to their default value
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Oct 21, 2023
1 parent c7922e9 commit e6de90d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/revanced-patcher.api
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ public abstract class app/revanced/patcher/patch/options/PatchOption {
public final fun getValidator ()Lkotlin/jvm/functions/Function1;
public final fun getValue ()Ljava/lang/Object;
public final fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
public fun reset ()V
public final fun setValue (Ljava/lang/Object;)V
public final fun setValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V
}
Expand Down
20 changes: 15 additions & 5 deletions src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class PatchOption<T>(
/**
* The value of the [PatchOption].
*/
var value: T? = default
var value: T?
/**
* Set the value of the [PatchOption].
*
Expand All @@ -37,7 +37,7 @@ abstract class PatchOption<T>(
assertRequiredButNotNull(value)
assertValid(value)

field = value
uncheckedValue = value
}
/**
* Get the value of the [PatchOption].
Expand All @@ -48,12 +48,22 @@ abstract class PatchOption<T>(
* @throws PatchOptionException.ValueValidationException If the value is invalid.
*/
get() {
assertRequiredButNotNull(field)
assertValid(field)
assertRequiredButNotNull(uncheckedValue)
assertValid(uncheckedValue)

return field
return uncheckedValue
}

// The unchecked value is used to allow setting the value without validation.
private var uncheckedValue = default

/**
* Reset the [PatchOption] to its default value.
*/
open fun reset() {
uncheckedValue = default
}

private fun assertRequiredButNotNull(value: T?) {
if (required && value == null) throw PatchOptionException.ValueRequiredException(this)
}
Expand Down

0 comments on commit e6de90d

Please sign in to comment.