Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ fun <T, E> Answer<T, E>.onFailure(
is Answer.Failure -> also { function(value) }
is Answer.Success -> this
}

@Suppress("UNCHECKED_CAST")
fun <T1, T2, R, E> Answer<T1, E>.combine(
other: Answer<T2, E>,
transform: (T1, T2) -> R
): Answer<R, E> = when (this) {
is Answer.Failure -> this as Answer<R, E>
is Answer.Success -> when (other) {
is Answer.Failure -> other as Answer<R, E>
is Answer.Success -> Answer.Success(transform(this.value, other.value))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,47 @@ internal class AnswerExtensionTest {
assertThat(result).isEqualTo(Answer.Failure("originalValue"))
}

@Test
fun `use combine when both are success`() {
val answer1 = Answer.Success(5)
val answer2 = Answer.Success(10)

val result = answer1.combine(answer2) { a, b -> a + b }

assertTrue(result.isSuccess)
assertThat(result).isEqualTo(Answer.Success(15))
}

@Test
fun `use combine when first is failure`() {
val answer1: Answer<Int, String> = Answer.Failure("error1")
val answer2 = Answer.Success(10)

val result = answer1.combine(answer2) { a, b -> a + b }

assertTrue(result.isFailure)
assertThat(result).isEqualTo(Answer.Failure("error1"))
}

@Test
fun `use combine when second is failure`() {
val answer1 = Answer.Success(5)
val answer2: Answer<Int, String> = Answer.Failure("error2")

val result = answer1.combine(answer2) { a, b -> a + b }

assertTrue(result.isFailure)
assertThat(result).isEqualTo(Answer.Failure("error2"))
}

@Test
fun `use combine when both are failure`() {
val answer1: Answer<Int, String> = Answer.Failure("error1")
val answer2: Answer<Int, String> = Answer.Failure("error2")

val result = answer1.combine(answer2) { a, b -> a + b }

assertTrue(result.isFailure)
assertThat(result).isEqualTo(Answer.Failure("error1"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,17 @@ fun <T, R, E> Flow<Answer<T, E>>.thenFailure(
map { result -> result.coThenFailure { function(it) } }

fun <T> Flow<T>.toAnswer() =
map { stations -> Answer.Success(stations) }
map { data -> Answer.Success(data) }
.catch { error -> Answer.Failure(error) }

@Suppress("UNCHECKED_CAST")
suspend fun <T1, T2, R, E> Answer<T1, E>.coCombine(
other: Answer<T2, E>,
transform: suspend (T1, T2) -> R
): Answer<R, E> = when (this) {
is Answer.Failure -> this as Answer<R, E>
is Answer.Success -> when (other) {
is Answer.Failure -> other as Answer<R, E>
is Answer.Success -> Answer.Success(transform(this.value, other.value))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,48 @@ internal class AnswerExtensionTest {
awaitComplete()
}
}

@Test
fun `use coCombine when both are success`() = runTest {
val answer1 = Answer.Success(5)
val answer2 = Answer.Success(10)

val result = answer1.coCombine(answer2) { a, b -> a + b }

assertTrue(result.isSuccess)
assertThat(result).isEqualTo(Answer.Success(15))
}

@Test
fun `use coCombine when first is failure`() = runTest {
val answer1: Answer<Int, String> = Answer.Failure("error1")
val answer2 = Answer.Success(10)

val result = answer1.coCombine(answer2) { a, b -> a + b }

assertTrue(result.isFailure)
assertThat(result).isEqualTo(Answer.Failure("error1"))
}

@Test
fun `use coCombine when second is failure`() = runTest {
val answer1 = Answer.Success(5)
val answer2: Answer<Int, String> = Answer.Failure("error2")

val result = answer1.coCombine(answer2) { a, b -> a + b }

assertTrue(result.isFailure)
assertThat(result).isEqualTo(Answer.Failure("error2"))
}

@Test
fun `use coCombine when both are failure`() = runTest {
val answer1: Answer<Int, String> = Answer.Failure("error1")
val answer2: Answer<Int, String> = Answer.Failure("error2")

val result = answer1.coCombine(answer2) { a, b -> a + b }

assertTrue(result.isFailure)
assertThat(result).isEqualTo(Answer.Failure("error1"))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.raxdenstudios.commons.permissions.model

import android.Manifest
import android.os.Build
import androidx.annotation.RequiresApi

sealed class Permission(
val value: String,
Expand Down Expand Up @@ -41,6 +43,11 @@ sealed class Permission(
value = Manifest.permission.WRITE_EXTERNAL_STORAGE,
)

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
data object PostNotifications : Permission(
value = Manifest.permission.POST_NOTIFICATIONS,
)

data class Other(
val permission: String
) : Permission(
Expand All @@ -55,6 +62,7 @@ sealed class Permission(
* @param value
* @return
*/
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun fromValue(value: String): Permission = when (value) {
Manifest.permission.CAMERA -> Camera
Manifest.permission.ACCESS_FINE_LOCATION -> AccessFineLocation
Expand All @@ -65,6 +73,7 @@ sealed class Permission(
Manifest.permission.CALL_PHONE -> CallPhone
Manifest.permission.READ_EXTERNAL_STORAGE -> ReadExternalStorage
Manifest.permission.WRITE_EXTERNAL_STORAGE -> WriteExternalStorage
Manifest.permission.POST_NOTIFICATIONS -> PostNotifications
else -> Other(value)
}
}
Expand Down
Loading