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) + } +}