fix: parcel error for nullable types

This commit is contained in:
Ax333l 2023-11-15 21:32:54 +01:00
parent ac561e7aca
commit 2bd84636d6
No known key found for this signature in database
GPG key ID: D2B4D85271127D23
2 changed files with 12 additions and 9 deletions

View file

@ -25,6 +25,7 @@ import app.revanced.manager.ui.model.BundleInfo.Extensions.toPatchSelection
import app.revanced.manager.ui.model.SelectedApp
import app.revanced.manager.util.Options
import app.revanced.manager.util.PatchesSelection
import app.revanced.manager.util.saver.Nullable
import app.revanced.manager.util.saver.nullableSaver
import app.revanced.manager.util.saver.persistentMapSaver
import app.revanced.manager.util.saver.persistentSetSaver
@ -210,7 +211,7 @@ class PatchesSelectorViewModel(input: Params) : ViewModel(), KoinComponent {
)
)
private val patchesSaver: Saver<PersistentPatchesSelection?, Optional<PatchesSelection>> =
private val patchesSaver: Saver<PersistentPatchesSelection?, Nullable<PatchesSelection>> =
nullableSaver(persistentMapSaver(valueSaver = persistentSetSaver()))
}

View file

@ -1,22 +1,24 @@
package app.revanced.manager.util.saver
import android.os.Parcelable
import androidx.compose.runtime.saveable.Saver
import java.util.Optional
import kotlin.jvm.optionals.getOrNull
import kotlinx.parcelize.Parcelize
import kotlinx.parcelize.RawValue
@Parcelize
class Nullable<T>(val inner: @RawValue T?) : Parcelable
/**
* Creates a saver that can save nullable versions of types that have custom savers.
*/
fun <Original : Any, Saveable : Any> nullableSaver(baseSaver: Saver<Original, Saveable>): Saver<Original?, Optional<Saveable>> =
fun <Original : Any, Saveable : Any> nullableSaver(baseSaver: Saver<Original, Saveable>): Saver<Original?, Nullable<Saveable>> =
Saver(
save = { value ->
with(baseSaver) {
save(value ?: return@Saver Optional.empty())
}?.let {
Optional.of(it)
}
save(value ?: return@Saver Nullable(null))
}?.let(::Nullable)
},
restore = {
it.getOrNull()?.let(baseSaver::restore)
it.inner?.let(baseSaver::restore)
}
)