From fb41aeb883cee2f4036b32b8ebba9f05a92c6cf3 Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Sun, 23 May 2021 22:54:59 +0200 Subject: [PATCH] Workaround for coroutines crash suspending functions returning inline/value classes cause a crash in the coroutines library. When replacing the kotlin.util.Result class with a primitive custom alternative that isn't a value class, this crash doesn't happen anymore. --- .../jellyfin/mobile/player/source/Result.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 app/src/main/java/org/jellyfin/mobile/player/source/Result.kt diff --git a/app/src/main/java/org/jellyfin/mobile/player/source/Result.kt b/app/src/main/java/org/jellyfin/mobile/player/source/Result.kt new file mode 100644 index 000000000..63c387144 --- /dev/null +++ b/app/src/main/java/org/jellyfin/mobile/player/source/Result.kt @@ -0,0 +1,26 @@ +package org.jellyfin.mobile.player.source + +class Result private constructor( + val value: T?, + val error: Throwable?, +) { + @Suppress("NOTHING_TO_INLINE") + inline fun getOrNull(): T? = value + + @Suppress("UNCHECKED_CAST") + inline fun onSuccess(action: (T) -> Unit): Result { + if (error == null) action(value as T) + return this + } + + inline fun onFailure(action: (Throwable) -> Unit): Result { + if (error != null) action(error) + return this + } + + companion object { + fun success(value: T) = Result(value, null) + + fun failure(error: Throwable) = Result(null, error) + } +}