Skip to content

Commit 5b5be50

Browse files
authored
Merge pull request #9 from NeoUtils/release/v1.1.0
Release/v1.1.0
2 parents 1b376e1 + 6993116 commit 5b5be50

File tree

11 files changed

+482
-178
lines changed

11 files changed

+482
-178
lines changed

.github/workflows/test-ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: test ci
2+
3+
on:
4+
pull_request:
5+
branches: [ master, develop ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
15+
- name: Set up JDK
16+
uses: actions/setup-java@v2
17+
with:
18+
java-version: '11'
19+
distribution: 'temurin'
20+
21+
- name: Assemble
22+
run: ./gradlew assemble
23+
24+
- name: Tests
25+
run: ./gradlew test
26+
27+
- name: Publish
28+
run: ./gradlew publishToMavenLocal

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Irineu A. Silva
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Resource [![](https://jitpack.io/v/Irineu333/Resource.svg) ](https://jitpack.io/#Irineu333/Highlight)
2+
3+
Complete solution for handling success, failure, and loading states in Kotlin.
4+
5+
## Loading
6+
7+
Use the sealed class [`Resource`](src/main/kotlin/Resource.kt) with loading states.
8+
9+
``` kotlin
10+
ordersRepository.flow.collect { resource ->
11+
when (resource) {
12+
is Resource.Result.Failure -> //...
13+
is Resource.Result.Success -> //...
14+
Resource.Loading -> //...
15+
}
16+
}
17+
```
18+
19+
## Result
20+
21+
If the possible states are success and error, the ideal class is `Resource.Result`.
22+
23+
``` kotlin
24+
when (ordersRepository.getOrders()) {
25+
is Resource.Result.Failure -> //...
26+
is Resource.Result.Success -> //...
27+
}
28+
```
29+
30+
## Extensions
31+
32+
The library comes with some basi basic [extensions](src/main/kotlin/extension/Resource.kt) to handle and
33+
manipulate the states.
34+
35+
## kotlin.Result
36+
37+
Integration with kotlin Result.
38+
39+
``` kotlin
40+
suspend fun getOrders(): Resource.Result<List<Order>, String> {
41+
val result = runCatching {
42+
service.getOrders()
43+
}.toResource()
44+
45+
return result.mapError {
46+
it.message ?: ""
47+
}.ifFailure {
48+
logger.send("error: $it")
49+
}.ifSuccess {
50+
logger.send("success: $it")
51+
}
52+
}
53+
```
54+
55+
## Releases
56+
57+
Add the jitpack to project in `build.gradle.kts` or `settings.gradle.kts`:
58+
59+
``` kotlin
60+
repositories {
61+
62+
maven { url = uri("https://jitpack.io") }
63+
}
64+
```
65+
66+
Add the dependence to module:
67+
68+
``` kotlin
69+
implementation("com.github.NeoUtils:Resource:{version}")
70+
```
71+
72+
## License
73+
```
74+
Copyright (c) 2023 Irineu A. Silva
75+
76+
This project is licensed under the terms of the MIT License,
77+
a permissive open-source license that allows for the use, modification,
78+
and distribution of the code, provided that copyright notices and
79+
the license statement are included in all copies or modifications.
80+
```

build.gradle.kts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,34 @@ plugins {
55
}
66

77
group = "com.neo.resource"
8-
version = "1.0"
8+
version = "1.1.0"
99

1010
repositories {
1111
mavenCentral()
1212
}
1313

1414
dependencies {
1515
testImplementation(kotlin("test"))
16+
testImplementation("io.mockk:mockk:1.13.8")
1617
}
1718

1819
tasks.test {
1920
useJUnitPlatform()
2021
}
2122

2223
kotlin {
23-
jvmToolchain(8)
24+
jvmToolchain {
25+
languageVersion.set(JavaLanguageVersion.of(11))
26+
vendor.set(JvmVendorSpec.ORACLE)
27+
}
2428
}
2529

2630
publishing {
2731
publications {
2832
create<MavenPublication>("maven") {
2933
groupId = "com.neo.resource"
3034
artifactId = "resource"
31-
version = "1.0"
35+
version = "1.1.0"
3236

3337
from(components["java"])
3438
}

gradlew

100644100755
File mode changed.

src/main/kotlin/extension/Resource.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ fun <T, E> Resource.Result<T, E>.asResource() = this as Resource<T, E>
1212

1313
// map
1414

15-
inline fun <T, E, T2> Resource<T, E>.mapSuccess(
15+
fun <T, E, T2> Resource<T, E>.mapSuccess(
1616
transform: (T) -> T2
1717
) = when (this) {
1818
is Resource.Result.Failure -> Resource.Result.Failure(error)
1919
is Resource.Result.Success -> Resource.Result.Success(transform(data))
2020
Resource.Loading -> Resource.Loading
2121
}
2222

23-
inline fun <T, E, T2> Resource.Result<T, E>.mapSuccess(
23+
fun <T, E, T2> Resource.Result<T, E>.mapSuccess(
2424
transform: (T) -> T2
2525
) = when (this) {
2626
is Resource.Result.Failure -> Resource.Result.Failure(error)
2727
is Resource.Result.Success -> Resource.Result.Success(transform(data))
2828
}
2929

30-
inline fun <T, E, E2> Resource<T, E>.mapError(
30+
fun <T, E, E2> Resource<T, E>.mapError(
3131
transform: (E) -> E2
3232
) = when (this) {
3333
is Resource.Result.Failure -> Resource.Result.Failure(transform(error))
3434
is Resource.Result.Success -> Resource.Result.Success(data)
3535
Resource.Loading -> Resource.Loading
3636
}
3737

38-
inline fun <T, E, E2> Resource.Result<T, E>.mapError(
38+
fun <T, E, E2> Resource.Result<T, E>.mapError(
3939
transform: (E) -> E2
4040
) = when (this) {
4141
is Resource.Result.Failure -> Resource.Result.Failure(transform(error))
@@ -52,6 +52,12 @@ inline fun <T, E> Resource<T, E>.ifSuccess(
5252
}
5353
}
5454

55+
inline fun <T, E> Resource.Result<T, E>.ifSuccess(
56+
onSuccess: (T) -> Unit,
57+
) = apply {
58+
asResource().ifSuccess(onSuccess)
59+
}
60+
5561
inline fun <T, E> Resource<T, E>.ifFailure(
5662
onError: (E) -> Unit,
5763
) = apply {
@@ -60,6 +66,12 @@ inline fun <T, E> Resource<T, E>.ifFailure(
6066
}
6167
}
6268

69+
inline fun <T, E> Resource.Result<T, E>.ifFailure(
70+
onError: (E) -> Unit,
71+
) = apply {
72+
asResource().ifFailure(onError)
73+
}
74+
6375
inline fun <T, E> Resource<T, E>.ifLoading(
6476
onLoading: () -> Unit,
6577
) = apply {

src/test/kotlin/ResourceTest.kt

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)