Skip to content

Commit 2c8899d

Browse files
Added docs for some functions
1 parent 799ec86 commit 2c8899d

File tree

14 files changed

+418
-86
lines changed

14 files changed

+418
-86
lines changed

core/src/androidMain/kotlin/com/mrboomdev/awery/core/utils/AndroidUtils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import android.app.Activity
44
import android.content.Context
55
import android.content.ContextWrapper
66

7+
/**
8+
* Attempts to retrieve the [Activity] associated with the given [Context].
9+
*
10+
* This function traverses the context hierarchy until it finds an [Activity] or reaches the root context.
11+
* If an [Activity] is found, it is returned. Otherwise, null is returned.
12+
*
13+
* @return The associated [Activity], or null if none is found.
14+
*/
715
fun Context.toActivity(): Activity? {
816
var current = this
917

core/src/androidMain/kotlin/com/mrboomdev/awery/core/utils/PermissionUtils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import android.os.Build
55
import com.mrboomdev.awery.core.Awery
66
import com.mrboomdev.awery.core.context
77

8+
/**
9+
* Checks if the application has the specified permission.
10+
* @param permission The permission to check for.
11+
* @return True if the application has the permission, false otherwise.
12+
*/
813
fun Awery.hasPermission(permission: AppPermission): Boolean {
914
if(permission.constant == null) return true
1015

core/src/commonMain/kotlin/com/mrboomdev/awery/core/Awery.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
package com.mrboomdev.awery.core
22

3-
import io.ktor.client.HttpClient
4-
import io.ktor.client.HttpClientConfig
5-
import io.ktor.client.engine.cio.CIO
6-
import io.ktor.client.plugins.HttpTimeout
7-
import io.ktor.client.plugins.compression.ContentEncoding
3+
import io.ktor.client.*
4+
import io.ktor.client.engine.cio.*
5+
import io.ktor.client.plugins.*
6+
import io.ktor.client.plugins.compression.*
87

98
/**
109
* Main object, that stores all the global variables and functions.
1110
* Also, this object is a bridge to the platform-specific code.
1211
*/
1312
expect object Awery {
13+
/**
14+
* Copies the given text to the clipboard.
15+
*
16+
* @param text The text to copy.
17+
*/
1418
fun copyToClipboard(text: String)
19+
20+
/**
21+
* Opens the given URL in the default browser of the device.
22+
*
23+
* @param url The URL to open.
24+
*/
1525
fun openUrl(url: String)
1626

27+
/**
28+
* Shares the given text to the default sharing app of the device.
29+
*
30+
* @param text The text to share.
31+
*/
1732
fun share(text: String)
1833

1934
/**

core/src/commonMain/kotlin/com/mrboomdev/awery/core/utils/CacheStorage.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ import io.github.vinceglb.filekit.PlatformFile
66
import io.github.vinceglb.filekit.absolutePath
77
import io.github.vinceglb.filekit.readString
88
import io.github.vinceglb.filekit.writeString
9-
import kotlinx.coroutines.runBlocking
109
import kotlinx.serialization.KSerializer
1110
import kotlinx.serialization.json.Json
1211
import kotlinx.serialization.serializer
1312
import java.lang.System.currentTimeMillis
1413

14+
/**
15+
* Creates a new instance of [CacheStorage].
16+
*
17+
* @param directory The directory in which the cache files will be stored.
18+
* @param maxSize The maximum size of the cache in bytes.
19+
* @param strategy The strategy to use when storing and retrieving cache values.
20+
* If not specified, defaults to [CacheStorage.Strategy.LRU].
21+
* @param maxAge The maximum age of a cache value in milliseconds.
22+
* If not specified, defaults to [Long.MAX_VALUE].
23+
*
24+
* @return A new instance of [CacheStorage].
25+
*/
1526
suspend inline fun <reified T> CacheStorage(
1627
directory: PlatformFile,
1728
maxSize: Long,

core/src/commonMain/kotlin/com/mrboomdev/awery/core/utils/CommonUtils.kt

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
package com.mrboomdev.awery.core.utils
22

33
import java.lang.AutoCloseable
4+
import kotlin.Boolean
5+
import kotlin.OptIn
6+
import kotlin.PublishedApi
7+
import kotlin.Suppress
8+
import kotlin.Throwable
9+
import kotlin.Unit
10+
import kotlin.addSuppressed
411
import kotlin.contracts.ExperimentalContracts
512
import kotlin.contracts.InvocationKind
613
import kotlin.contracts.contract
714

15+
/**
16+
* Closes the given [AutoCloseable] instance after executing the given [block].
17+
* If [block] throws an exception, it will be re-thrown after closing the instance.
18+
* If the instance cannot be closed because of an exception, the original exception will be re-thrown.
19+
*
20+
* @param block A lambda function that takes a [T] as an argument and returns a result of type [R].
21+
* The function will be called with the instance of [T] as its argument and will be executed exactly once.
22+
* @return The result of executing [block] with the instance of [T] as its argument.
23+
*/
824
@OptIn(ExperimentalContracts::class)
925
inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
1026
contract {
@@ -22,6 +38,15 @@ inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
2238
}
2339
}
2440

41+
/**
42+
* Closes this [AutoCloseable] instance, if it is not null.
43+
* If the provided [cause] is not null, it will be re-thrown after closing the instance.
44+
* If the instance cannot be closed because of an exception, the original exception will be re-thrown.
45+
* If the instance can be closed successfully, the provided [cause] will be re-thrown.
46+
*
47+
* @param cause The exception to be re-thrown after closing the instance.
48+
* @return Unit
49+
*/
2550
@PublishedApi
2651
internal fun AutoCloseable?.closeFinally(cause: Throwable?): Unit = when {
2752
this == null -> {}
@@ -34,14 +59,6 @@ internal fun AutoCloseable?.closeFinally(cause: Throwable?): Unit = when {
3459
}
3560
}
3661

37-
inline fun <T> T.runIfNull(block: () -> Unit): T {
38-
return apply {
39-
if(this == null) {
40-
block()
41-
}
42-
}
43-
}
44-
4562
/**
4663
* Blocks the current thread until the provided [block] function returns `true`.
4764
*
@@ -53,6 +70,17 @@ inline fun await(block: () -> Boolean) {
5370
while(!block()) {}
5471
}
5572

73+
/**
74+
* Retries the provided [block] function until it returns a non-null result, or until an exception is thrown.
75+
* If an exception is thrown, the provided [onFailure] function will be called with the exception as an argument.
76+
* If [onFailure] is not provided, it defaults to an empty lambda function.
77+
*
78+
* @param onFailure A lambda function that takes a [Throwable] as an argument and returns [Unit].
79+
* The function will be called with the exception as an argument if an exception is thrown by [block].
80+
* @param block A lambda function that returns a result of type [T].
81+
* The function will be repeatedly invoked until it returns a non-null result.
82+
* @return The result of executing [block].
83+
*/
5684
inline fun <T> retryUntilSuccess(onFailure: (Throwable) -> Unit = {}, block: () -> T): T {
5785
var result: T? = null
5886

@@ -67,6 +95,15 @@ inline fun <T> retryUntilSuccess(onFailure: (Throwable) -> Unit = {}, block: ()
6795
return result
6896
}
6997

98+
/**
99+
* Tries to execute the provided [computeValue] block and returns the result if it doesn't throw an exception.
100+
* If the block throws an exception, the provided [fallbackComputeValue] block is executed with the exception as an argument
101+
* and the result of that block is returned.
102+
*
103+
* @param computeValue A lambda function that returns a value of type [T].
104+
* @param fallbackComputeValue A lambda function that takes a [Throwable] as an argument and returns a value of type [T].
105+
* @return The result of executing [computeValue] or [fallbackComputeValue] depending on whether an exception was thrown.
106+
*/
70107
inline fun <T> tryOr(computeValue: () -> T, fallbackComputeValue: (Throwable) -> T): T {
71108
return try {
72109
computeValue()

core/src/commonMain/kotlin/com/mrboomdev/awery/core/utils/CoroutineUtils.kt

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ import kotlinx.coroutines.DelicateCoroutinesApi
77
import kotlinx.coroutines.GlobalScope
88
import kotlinx.coroutines.Job
99
import kotlinx.coroutines.async
10-
import kotlinx.coroutines.delay
1110
import kotlinx.coroutines.launch
1211
import kotlinx.coroutines.supervisorScope
1312
import kotlin.coroutines.CoroutineContext
1413
import kotlin.coroutines.EmptyCoroutineContext
15-
import kotlin.time.Duration.Companion.milliseconds
16-
import kotlin.time.measureTimedValue
1714

1815
/**
1916
* Launches a new coroutine in the [GlobalScope] without blocking the current thread and returns a reference to the coroutine as a [Job].
@@ -38,6 +35,20 @@ fun launchGlobal(
3835
return GlobalScope.launch(context, start, block)
3936
}
4037

38+
/**
39+
* Launches a new coroutine in the current scope and returns a reference to the coroutine as a [Job].
40+
* The coroutine is cancelled when the resulting job is [cancelled][Job.cancel].
41+
*
42+
* If the coroutine throws an exception, it will be caught and passed to the [onCatch] function. The function will be called
43+
* with the exception as its argument.
44+
*
45+
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
46+
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
47+
* @param onCatch the function to call when the coroutine throws an exception. The function will be called
48+
* with the exception as its argument.
49+
* @param block the coroutine code which will be invoked in the context of the provided scope.
50+
* @return A [Job] representing the launched coroutine.
51+
*/
4152
fun CoroutineScope.launchTrying(
4253
context: CoroutineContext = EmptyCoroutineContext,
4354
start: CoroutineStart = CoroutineStart.DEFAULT,
@@ -49,6 +60,16 @@ fun CoroutineScope.launchTrying(
4960
block = block
5061
)
5162

63+
/**
64+
* Launches a new coroutine in the current scope that will be supervised by the [supervisorScope] function.
65+
* This means that if the coroutine throws an exception, it will be propagated to the parent coroutine and
66+
* will not cancel the parent coroutine.
67+
*
68+
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
69+
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
70+
* @param block the coroutine code which will be invoked in the context of the provided scope.
71+
* @return A [Job] representing the launched coroutine.
72+
*/
5273
fun CoroutineScope.launchSupervise(
5374
context: CoroutineContext = EmptyCoroutineContext,
5475
start: CoroutineStart = CoroutineStart.DEFAULT,
@@ -58,7 +79,18 @@ fun CoroutineScope.launchSupervise(
5879
}
5980

6081
/**
61-
* Useful for view models.
82+
* A combination of [launchTrying] and [supervisorScope] that launches a coroutine in the current scope and
83+
* returns its result as a [kotlinx.coroutines.Deferred] value. If the coroutine throws an exception, it will be caught and
84+
* passed to the [onCatch] function.
85+
*
86+
* This function is useful for view models that need to handle exceptions in a centralized way.
87+
*
88+
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
89+
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
90+
* @param onCatch the function to call when the coroutine throws an exception. The function will be called
91+
* with the exception as its argument.
92+
* @param block the coroutine code which will be invoked in the context of the provided scope.
93+
* @return A [kotlinx.coroutines.Deferred] representing the launched coroutine.
6294
*/
6395
fun CoroutineScope.launchTryingSupervise(
6496
context: CoroutineContext = EmptyCoroutineContext,
@@ -69,6 +101,20 @@ fun CoroutineScope.launchTryingSupervise(
69101
supervisorScope(block)
70102
}
71103

104+
/**
105+
* Launches a coroutine that will be cancelled when the resulting [kotlinx.coroutines.Deferred] is cancelled.
106+
* The coroutine is launched in the current scope and returns its result as a [kotlinx.coroutines.Deferred] value.
107+
* If the coroutine throws an exception, it will be caught and passed to the [onCatch] function.
108+
*
109+
* This function is useful for view models that need to handle exceptions in a centralized way.
110+
*
111+
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
112+
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
113+
* @param onCatch the function to call when the coroutine throws an exception. The function will be called
114+
* with the exception as its argument.
115+
* @param block the coroutine code which will be invoked in the context of the provided scope.
116+
* @return A [kotlinx.coroutines.Deferred] representing the launched coroutine.
117+
*/
72118
fun <T> CoroutineScope.asyncTrying(
73119
context: CoroutineContext = EmptyCoroutineContext,
74120
start: CoroutineStart = CoroutineStart.DEFAULT,
@@ -81,7 +127,18 @@ fun <T> CoroutineScope.asyncTrying(
81127
)
82128

83129
/**
84-
* Useful for view models.
130+
* A combination of [asyncTrying] and [supervisorScope] that launches a coroutine in the current scope and
131+
* returns its result as a [kotlinx.coroutines.Deferred] value. If the coroutine throws an exception, it will be caught and
132+
* passed to the [onCatch] function.
133+
*
134+
* This function is useful for view models that need to handle exceptions in a centralized way.
135+
*
136+
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
137+
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
138+
* @param onCatch the function to call when the coroutine throws an exception. The function will be called
139+
* with the exception as its argument.
140+
* @param block the coroutine code which will be invoked in the context of the provided scope.
141+
* @return A [kotlinx.coroutines.Deferred] representing the launched coroutine.
85142
*/
86143
fun <T> CoroutineScope.asyncTryingSupervise(
87144
context: CoroutineContext = EmptyCoroutineContext,
@@ -90,17 +147,4 @@ fun <T> CoroutineScope.asyncTryingSupervise(
90147
block: suspend CoroutineScope.() -> T
91148
) = asyncTrying(context, start, onCatch) {
92149
supervisorScope(block)
93-
}
94-
95-
suspend inline fun <T> runForAtLeast(
96-
block: () -> T,
97-
durationInMillis: Long
98-
): T {
99-
val (result, executionDuration) = measureTimedValue(block)
100-
101-
if(executionDuration >= durationInMillis.milliseconds) {
102-
delay(durationInMillis.milliseconds - executionDuration)
103-
}
104-
105-
return result
106150
}

core/src/commonMain/kotlin/com/mrboomdev/awery/core/utils/ExceptionUtils.kt

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

0 commit comments

Comments
 (0)