forked from ReVanced/revanced-manager
-
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.
fix: parcel error for nullable types
- Loading branch information
Showing
2 changed files
with
12 additions
and
9 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
18 changes: 10 additions & 8 deletions
18
app/src/main/java/app/revanced/manager/util/saver/NullableSaver.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 |
---|---|---|
@@ -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) | ||
} | ||
) |