Skip to content

Commit 742c355

Browse files
added README & ResultOf
1 parent 8d064c5 commit 742c355

File tree

6 files changed

+291
-1
lines changed

6 files changed

+291
-1
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# AndroidCommons
2+
3+
Android library with common utilities required when building Android apps.
4+
5+
---
6+
7+
## Usage
8+
9+
Step 1: Add JitPack in your **root build.gradle** :
10+
11+
```groovy
12+
allprojects {
13+
repositories {
14+
...
15+
maven { url 'https://jitpack.io' }
16+
}
17+
}
18+
```
19+
20+
Step 2: Add AndroidCommons dependency to your **app build.gradle**:
21+
22+
```groovy
23+
dependencies {
24+
implementation 'com.github.The-Streamliners:AndroidCommons:0.1.0'
25+
}
26+
```
27+
28+
---
29+
30+
## Utilities
31+
32+
### TasksInParallelHelper
33+
34+
TasksInParallelHelper is a helper class that performs task in parallel. Any input type (I) & output type (O) is supported. By Implementing task class, you can perform any task on I to generate O, whether it be a long IO operation or a network call.
35+
36+
[View examples](docs/TasksInParallelHelper.md)
37+
38+
---
39+
40+
### ResultOf<T>
41+
42+
ResultOf is a resource wrapper class. While performing an IO / Network operation, there are 2 possibilities:
43+
44+
- Task will be successful & we will get some data T
45+
46+
- Task may fail producing some exception
47+
48+
49+
ResultOf<T> class makes it easier to wrap these 2 possibilities in one.
50+
51+
[View examples & features](docs/ResultOf.md)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.streamliners.commons
2+
3+
sealed class ResultOf<T>(val data: T? = null, val message: String? = null) {
4+
5+
class Success<T>(data: T) : ResultOf<T>(data)
6+
7+
class Error<T>(message: String) : ResultOf<T>(message = message)
8+
9+
fun onSuccess(handler: (T) -> Unit): ResultOf<T> {
10+
if(this is Success)
11+
data?.let(handler)
12+
return this
13+
}
14+
15+
fun onFailure(handler: (String) -> Unit): ResultOf<T> {
16+
if(this is Error)
17+
message?.let(handler)
18+
return this
19+
}
20+
21+
fun finally(handler: () -> Unit): ResultOf<T> {
22+
handler()
23+
return this
24+
}
25+
26+
fun await(): T {
27+
if(this is Success)
28+
data?.let {
29+
return it
30+
} ?: throw Exception("Something went wrong")
31+
else
32+
message?.let {
33+
throw Exception(it)
34+
} ?: throw Exception("Something went wrong")
35+
}
36+
37+
}

commons/src/main/java/com/streamliners/commons/tasksInParallel/TasksInParallelHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class Task<I, O> {
2020
/**
2121
* TasksInParallelHelper is a helper class that performs task in parallel.
2222
* Any input type (I) & output type (O) is supported.
23-
* By Implementing task class, you can perform ny task on I to generate O.
23+
* By Implementing task class, you can perform any task on I to generate O.
2424
* Whether it be a long IO operation or a network call.
2525
*/
2626
class TasksInParallelHelper<I, O> {

docs/ResultOf.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# ResultOf<T>
2+
3+
ResultOf is a resource wrapper class. While performing an IO / Network operation, there are 2 possibilities:
4+
5+
- Task will be successful & we will get some data T
6+
7+
- Task may fail producing some exception
8+
9+
10+
ResultOf class makes it easier to wrap these 2 possibilities in one.
11+
12+
---
13+
14+
## Code without ResultOf class
15+
16+
You'll have to use try catch whenever calling a suspending function
17+
18+
```kotlin
19+
try {
20+
val user = userRepository.getUser()
21+
} catch(e: Exception) {
22+
// Handle error
23+
}
24+
25+
26+
OR
27+
28+
29+
coroutineScope.launch(exceptionHandler) {
30+
val user = userRepository.getUser()
31+
}
32+
```
33+
34+
## Using ResultOf class with when block
35+
36+
```kotlin
37+
when(
38+
val result = userRepository.getUser()
39+
) {
40+
is Success -> {
41+
val user = result.data
42+
// Consume result
43+
}
44+
is Error -> {
45+
val error = result.message
46+
// Show error
47+
}
48+
}
49+
```
50+
51+
The code might look longer, but there are 2 functions at rescue :
52+
53+
## Using ResultOf with onSuccess & onFailure
54+
55+
```kotlin
56+
userRepository.getUser()
57+
.onSuccess { user ->
58+
59+
}.onFailure { error ->
60+
61+
}
62+
```
63+
64+
But these functions lead to callback hell when calling multiple suspending functions in a sequence:
65+
66+
## Callback hell
67+
68+
```kotlin
69+
userRepository.getUser()
70+
.onSuccess { user ->
71+
paymentsRepository.getPayments(user)
72+
.onSuccess { payments ->
73+
74+
}.onFailure { error ->
75+
76+
}
77+
}.onFailure { error ->
78+
79+
}
80+
```
81+
82+
Is there a way out? Of course, ResultOf class offers await() method :
83+
84+
## Using ResultOf class with await()
85+
86+
```kotlin
87+
try {
88+
val user = userRepository.getUser().await()
89+
val payments = paymentsRepository.getPayments(user).await()
90+
} catch(e: Exception) {
91+
// Handle error
92+
}
93+
94+
OR
95+
96+
coroutineScope.launch(exceptionHandler) {
97+
val user = userRepository.getUser().await()
98+
val payments = paymentsRepository.getPayments(user).await()
99+
}
100+
```

docs/TasksInParallelHelper.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# TasksInParallelHelper
2+
3+
TasksInParallelHelper is a helper class that performs task in parallel. Any input type (I) & output type (O) is supported. By Implementing task class, you can perform any task on I to generate O, whether it be a long IO operation or a network call.
4+
5+
---
6+
7+
## Kotlin example
8+
9+
Step 1: Prepare a list of inputs of any input type A
10+
11+
```kotlin
12+
val inputs = listOf(A(1), A(2), A(3))
13+
```
14+
15+
Step 2: Implement Task class to transform input type A into output type B
16+
17+
```kotlin
18+
val task: Task<A, B> =
19+
object : Task<A, B>() {
20+
override fun perform(input: A, listener: Listener<B>) {
21+
// Simple stupid task that converts int to String
22+
val output = B(input.x.toString())
23+
listener.onSuccess(output)
24+
}
25+
}
26+
```
27+
28+
Step 3: Implement listener to get outputs of output type B
29+
30+
```kotlin
31+
val listener: TasksListener<B> =
32+
object : TasksListener<B> {
33+
override fun onSuccess(outputs: List<B>) {
34+
// Do something with the outputs
35+
}
36+
37+
override fun onFailure(error: String) {
38+
// Show the error!
39+
}
40+
}
41+
```
42+
43+
Step 4: Finally perform the tasks in parallel!
44+
45+
```kotlin
46+
TasksInParallelHelper<A, B>()
47+
.doTasksInParallel(inputs, task, listener)
48+
```
49+
50+
---
51+
52+
## Java Example
53+
54+
Step 1: Prepare a list of inputs of any input type A
55+
56+
```kotlin
57+
List<A> inputs = Arrays.asList(new A(1), new A(2), new A(3));
58+
```
59+
60+
Step 2: Implement Task class to transform input type A into output type B
61+
62+
```kotlin
63+
Task<A, B> task =
64+
new Task<A, B>() {
65+
@Override
66+
public void perform(A input, @NonNull Listener<B> listener) {
67+
// Simple stupid task that converts int to String
68+
B output = new B(String.valueOf(input.getX()));
69+
listener.onSuccess(output);
70+
}
71+
};
72+
```
73+
74+
Step 3: Implement listener to get outputs of output type B
75+
76+
```kotlin
77+
TasksInParallelHelper.TasksListener<B> listener =
78+
new TasksInParallelHelper.TasksListener<B>() {
79+
@Override
80+
public void onSuccess(@NonNull List<? extends B> outputs) {
81+
// Do something with the outputs
82+
}
83+
84+
@Override
85+
public void onFailure(@NonNull String error) {
86+
// Show the error!
87+
}
88+
};
89+
```
90+
91+
Step 4: Finally perform the tasks in parallel!
92+
93+
```kotlin
94+
new TasksInParallelHelper<A, B>()
95+
.doTasksInParallel(inputs, task, listener);
96+
```

0 commit comments

Comments
 (0)