forked from ReVanced/revanced-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Patch Options * refactor: remove delegate property * feat: List patch options * refactor: add setter example to PatchOptionsUsage.kt * docs: add docs for PatchOptions * docs: tidy docs
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 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
100 changes: 100 additions & 0 deletions
100
src/main/kotlin/app/revanced/patcher/patch/PatchOption.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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package app.revanced.patcher.patch | ||
|
||
/** | ||
* A [Patch] option. | ||
* @param key Unique identifier of the option. Example: _`settings.microg.enabled`_ | ||
* @param default The default value of the option. | ||
* @param title A human-readable title of the option. Example: _MicroG Settings_ | ||
* @param description A human-readable description of the option. Example: _Settings integration for MicroG._ | ||
* @param required Whether the option is required. | ||
*/ | ||
@Suppress("MemberVisibilityCanBePrivate") | ||
sealed class PatchOption<T>( | ||
val key: String, | ||
default: T?, | ||
val title: String, | ||
val description: String, | ||
val required: Boolean | ||
) { | ||
var value: T? = default | ||
|
||
/** | ||
* A [PatchOption] representing a [String]. | ||
* @see PatchOption | ||
*/ | ||
class StringOption( | ||
key: String, | ||
default: String?, | ||
title: String, | ||
description: String, | ||
required: Boolean = false | ||
) : PatchOption<String>( | ||
key, default, title, description, required | ||
) | ||
|
||
/** | ||
* A [PatchOption] representing a [Boolean]. | ||
* @see PatchOption | ||
*/ | ||
class BooleanOption( | ||
key: String, | ||
default: Boolean?, | ||
title: String, | ||
description: String, | ||
required: Boolean = false | ||
) : PatchOption<Boolean>( | ||
key, default, title, description, required | ||
) | ||
|
||
/** | ||
* A [PatchOption] with a list of allowed options. | ||
* @param options A list of allowed options for the [ListOption]. | ||
* @see PatchOption | ||
*/ | ||
sealed class ListOption<E>( | ||
key: String, | ||
default: E?, | ||
val options: Iterable<E>, | ||
title: String, | ||
description: String, | ||
required: Boolean = false | ||
) : PatchOption<E>( | ||
key, default, title, description, required | ||
) { | ||
init { | ||
if (default !in options) { | ||
throw IllegalStateException("Default option must be an allowed options") | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A [ListOption] of type [String]. | ||
* @see ListOption | ||
*/ | ||
class StringListOption( | ||
key: String, | ||
default: String?, | ||
options: Iterable<String>, | ||
title: String, | ||
description: String, | ||
required: Boolean = false | ||
) : ListOption<String>( | ||
key, default, options, title, description, required | ||
) | ||
|
||
/** | ||
* A [ListOption] of type [Int]. | ||
* @see ListOption | ||
*/ | ||
class IntListOption( | ||
key: String, | ||
default: Int?, | ||
options: Iterable<Int>, | ||
title: String, | ||
description: String, | ||
required: Boolean = false | ||
) : ListOption<Int>( | ||
key, default, options, title, description, required | ||
) | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/kotlin/app/revanced/patcher/usage/PatchOptionsUsage.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package app.revanced.patcher.usage | ||
|
||
import app.revanced.patcher.patch.PatchOption | ||
import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch | ||
|
||
fun patchOptionsUsage() { | ||
val options = ExampleBytecodePatch().options | ||
for (option in options) { | ||
when (option) { | ||
is PatchOption.StringOption -> { | ||
option.value = "Hello World" | ||
} | ||
is PatchOption.BooleanOption -> { | ||
option.value = false | ||
} | ||
is PatchOption.StringListOption -> { | ||
option.value = option.options.first() | ||
for (choice in option.options) { | ||
println(choice) | ||
} | ||
} | ||
is PatchOption.IntListOption -> { | ||
option.value = option.options.first() | ||
for (choice in option.options) { | ||
println(choice) | ||
} | ||
} | ||
} | ||
} | ||
} |
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