Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v1.1.0 #9

Merged
merged 16 commits into from
Sep 24, 2023
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
28 changes: 28 additions & 0 deletions .github/workflows/test-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: test ci

on:
pull_request:
branches: [ master, develop ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up JDK
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'temurin'

- name: Assemble
run: ./gradlew assemble

- name: Tests
run: ./gradlew test

- name: Publish
run: ./gradlew publishToMavenLocal
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Irineu A. Silva

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Resource [![](https://jitpack.io/v/Irineu333/Resource.svg) ](https://jitpack.io/#Irineu333/Highlight)

Complete solution for handling success, failure, and loading states in Kotlin.

## Loading

Use the sealed class [`Resource`](src/main/kotlin/Resource.kt) with loading states.

``` kotlin
ordersRepository.flow.collect { resource ->
when (resource) {
is Resource.Result.Failure -> //...
is Resource.Result.Success -> //...
Resource.Loading -> //...
}
}
```

## Result

If the possible states are success and error, the ideal class is `Resource.Result`.

``` kotlin
when (ordersRepository.getOrders()) {
is Resource.Result.Failure -> //...
is Resource.Result.Success -> //...
}
```

## Extensions

The library comes with some basi basic [extensions](src/main/kotlin/extension/Resource.kt) to handle and
manipulate the states.

## kotlin.Result

Integration with kotlin Result.

``` kotlin
suspend fun getOrders(): Resource.Result<List<Order>, String> {
val result = runCatching {
service.getOrders()
}.toResource()

return result.mapError {
it.message ?: ""
}.ifFailure {
logger.send("error: $it")
}.ifSuccess {
logger.send("success: $it")
}
}
```

## Releases

Add the jitpack to project in `build.gradle.kts` or `settings.gradle.kts`:

``` kotlin
repositories {

maven { url = uri("https://jitpack.io") }
}
```

Add the dependence to module:

``` kotlin
implementation("com.github.NeoUtils:Resource:{version}")
```

## License
```
Copyright (c) 2023 Irineu A. Silva

This project is licensed under the terms of the MIT License,
a permissive open-source license that allows for the use, modification,
and distribution of the code, provided that copyright notices and
the license statement are included in all copies or modifications.
```
10 changes: 7 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ plugins {
}

group = "com.neo.resource"
version = "1.0"
version = "1.1.0"

repositories {
mavenCentral()
}

dependencies {
testImplementation(kotlin("test"))
testImplementation("io.mockk:mockk:1.13.8")
}

tasks.test {
useJUnitPlatform()
}

kotlin {
jvmToolchain(8)
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
vendor.set(JvmVendorSpec.ORACLE)
}
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = "com.neo.resource"
artifactId = "resource"
version = "1.0"
version = "1.1.0"

from(components["java"])
}
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
20 changes: 16 additions & 4 deletions src/main/kotlin/extension/Resource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ fun <T, E> Resource.Result<T, E>.asResource() = this as Resource<T, E>

// map

inline fun <T, E, T2> Resource<T, E>.mapSuccess(
fun <T, E, T2> Resource<T, E>.mapSuccess(
transform: (T) -> T2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(error)
is Resource.Result.Success -> Resource.Result.Success(transform(data))
Resource.Loading -> Resource.Loading
}

inline fun <T, E, T2> Resource.Result<T, E>.mapSuccess(
fun <T, E, T2> Resource.Result<T, E>.mapSuccess(
transform: (T) -> T2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(error)
is Resource.Result.Success -> Resource.Result.Success(transform(data))
}

inline fun <T, E, E2> Resource<T, E>.mapError(
fun <T, E, E2> Resource<T, E>.mapError(
transform: (E) -> E2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(transform(error))
is Resource.Result.Success -> Resource.Result.Success(data)
Resource.Loading -> Resource.Loading
}

inline fun <T, E, E2> Resource.Result<T, E>.mapError(
fun <T, E, E2> Resource.Result<T, E>.mapError(
transform: (E) -> E2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(transform(error))
Expand All @@ -52,6 +52,12 @@ inline fun <T, E> Resource<T, E>.ifSuccess(
}
}

inline fun <T, E> Resource.Result<T, E>.ifSuccess(
onSuccess: (T) -> Unit,
) = apply {
asResource().ifSuccess(onSuccess)
}

inline fun <T, E> Resource<T, E>.ifFailure(
onError: (E) -> Unit,
) = apply {
Expand All @@ -60,6 +66,12 @@ inline fun <T, E> Resource<T, E>.ifFailure(
}
}

inline fun <T, E> Resource.Result<T, E>.ifFailure(
onError: (E) -> Unit,
) = apply {
asResource().ifFailure(onError)
}

inline fun <T, E> Resource<T, E>.ifLoading(
onLoading: () -> Unit,
) = apply {
Expand Down
32 changes: 0 additions & 32 deletions src/test/kotlin/ResourceTest.kt

This file was deleted.

Loading
Loading