-
-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: save patch options and selected patches in bundle (#50)
- Loading branch information
Showing
5 changed files
with
147 additions
and
39 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
13 changes: 13 additions & 0 deletions
13
app/src/main/java/app/revanced/manager/util/saver/PathSaver.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,13 @@ | ||
package app.revanced.manager.util.saver | ||
|
||
import androidx.compose.runtime.saveable.Saver | ||
import java.nio.file.Path | ||
import kotlin.io.path.Path | ||
|
||
/** | ||
* A [Saver] that can save [Path]s. Only works with the default filesystem. | ||
*/ | ||
val PathSaver = Saver<Path, String>( | ||
save = { it.toString() }, | ||
restore = { Path(it) } | ||
) |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/app/revanced/manager/util/saver/SnapshotStateCollectionSavers.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,75 @@ | ||
package app.revanced.manager.util.saver | ||
|
||
import androidx.compose.runtime.mutableStateMapOf | ||
import androidx.compose.runtime.saveable.Saver | ||
import androidx.compose.runtime.snapshots.SnapshotStateList | ||
import androidx.compose.runtime.snapshots.SnapshotStateMap | ||
import androidx.compose.runtime.toMutableStateList | ||
import androidx.compose.runtime.toMutableStateMap | ||
import app.revanced.manager.util.SnapshotStateSet | ||
import app.revanced.manager.util.toMutableStateSet | ||
|
||
/** | ||
* Create a [Saver] for [SnapshotStateList]s. | ||
*/ | ||
fun <T> snapshotStateListSaver() = Saver<SnapshotStateList<T>, List<T>>( | ||
save = { | ||
it.toMutableList() | ||
}, | ||
restore = { | ||
it.toMutableStateList() | ||
} | ||
) | ||
|
||
/** | ||
* Create a [Saver] for [SnapshotStateSet]s. | ||
*/ | ||
fun <T> snapshotStateSetSaver() = Saver<SnapshotStateSet<T>, Set<T>>( | ||
save = { | ||
it.toMutableSet() | ||
}, | ||
restore = { | ||
it.toMutableStateSet() | ||
} | ||
) | ||
|
||
/** | ||
* Create a [Saver] for [SnapshotStateMap]s. | ||
*/ | ||
fun <K, V> snapshotStateMapSaver() = Saver<SnapshotStateMap<K, V>, Map<K, V>>( | ||
save = { | ||
it.toMutableMap() | ||
}, | ||
restore = { | ||
mutableStateMapOf<K, V>().apply { | ||
this.putAll(it) | ||
} | ||
} | ||
) | ||
|
||
/** | ||
* Create a saver for [SnapshotStateMap]s with a custom [Saver] used for the values. | ||
* Null values will not be saved by this [Saver]. | ||
* | ||
* @param valueSaver The [Saver] used for the values of the [Map]. | ||
*/ | ||
fun <K, Original, Saveable : Any> snapshotStateMapSaver( | ||
valueSaver: Saver<Original, Saveable> | ||
) = Saver<SnapshotStateMap<K, Original>, Map<K, Saveable>>( | ||
save = { | ||
buildMap { | ||
it.forEach { (key, value) -> | ||
with(valueSaver) { | ||
save(value)?.let { | ||
this@buildMap[key] = it | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
restore = { | ||
it.mapNotNull { (key, value) -> | ||
valueSaver.restore(value)?.let { restored -> key to restored } | ||
}.toMutableStateMap() | ||
} | ||
) |