Skip to content

Commit

Permalink
fix: parcel error for nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
Axelen123 committed Nov 15, 2023
1 parent ac561e7 commit 2bd8463
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))
}

Expand Down
18 changes: 10 additions & 8 deletions app/src/main/java/app/revanced/manager/util/saver/NullableSaver.kt
Original file line number Diff line number Diff line change
@@ -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)
}
)

0 comments on commit 2bd8463

Please sign in to comment.