Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
zsmb13 committed May 11, 2024
1 parent b43ef8f commit 5f7705a
Showing 1 changed file with 53 additions and 35 deletions.
88 changes: 53 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
# requireKTX

**requireKTX** is a collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android, using the same idea as [`requireContext`](https://developer.android.com/reference/androidx/fragment/app/Fragment.html#requireContext()), [`requireArguments`](https://developer.android.com/reference/androidx/fragment/app/Fragment.html#requireArguments()), and other similar Android SDK methods.
**requireKTX** is a collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android and Kotlin Multiplatform, in the style of [`requireContext`](https://developer.android.com/reference/androidx/fragment/app/Fragment.html#requireContext()), [`requireArguments`](https://developer.android.com/reference/androidx/fragment/app/Fragment.html#requireArguments()), and other similar Android SDK methods.

Types that requireKTX provides extensions for:

- [Bundle](#bundle)
- [Intent](#intent)
- [NavBackStackEntry](#navbackstackentry)
- [WorkManager Data](#workmanager-data)
- [Bundle](#bundle) (Kotlin Multiplatform)
- [NavBackStackEntry](#navbackstackentry) (Kotlin Multiplatform)
- [Intent](#intent) (Android)
- [WorkManager Data](#workmanager-data) (Android)

## Why?

Take the example of grabbing a Fragment argument bundle and reading a String ID from it that should always be there: you have two choices, and none of them are great:
Take the example of grabbing a Bundle and reading a String ID from it that should always be there: the Bundle APIs give you a nullable result, which means you'll have to do some kind of null handling.

```kotlin
// Providing no default value
// Results in a nullable return type, caller has to do null handling
val id: String = requireArguments().getString("user_id")!!

// Providing a (meaningless) default value
// Results in a platform return type, caller has to explicitly type as non-null
val id: String = requireArguments().getString("user_id", "")
if (id == "") { ... } // ... and check to avoid accidentally using the default value
val id: String = argumentBundle.getString("user_id")!!
```

requireKTX provides methods such as `requireString` so that you can demand that a value be there, or otherwise get an exception:
The exception potentially thrown by this code also won't be too helpful in tracking down the problem, as it won't tell you details such as whether the value was missing, or if it was the wrong type for the request.

Instead of using these methods, requireKTX provides extensions such as `requireString`, which you can use to *require* a value that must always be there. These methods give you non-nullable return types.

```kotlin
val id: String = requireArguments().requireString("user_id")
val id: String = argumentBundle.requireString("user_id")
```

These methods in the library will throw meaningful exceptions based on the error that occurred - see the method docs for details.
If the value isn't available, these methods throw meaningful exceptions based on the error that occurred - see the method docs for details.

### getOrNull

To make the nullable case more obvious and explicit, requireKTX **also includes `getOrNull` style methods for everything that it covers with `require` style methods**. These match the conventions of the Kotlin Standard Library, and can make it clearer that `null` is returned if a value for a key couldn't be fetched.
requireKTX **also includes `getOrNull` style methods for everything that it covers with `require` style methods**,to make the nullable case more obvious and explicit. These match the conventions of the Kotlin Standard Library, and can make it clearer that `null` is returned if a value for a key couldn't be fetched.

```kotlin
val userId: String? = requireArguments().getStringOrNull("user_id")
Expand All @@ -44,30 +39,35 @@ val userId: String? = requireArguments().getStringOrNull("user_id")

requireKTX is available from MavenCentral.

```groovy
```kotlin
repositories {
mavenCentral()
}
```

It's available in several artifacts which you can import depending on which types you want to get extensions for - see the module descriptions below for more info:
It's available in several artifacts which you can import depending on which types you want to get extensions for - see the module descriptions below for more info.

```groovy
```kotlin
dependencies {
implementation "co.zsmb:requirektx-bundle:1.2.0"
implementation "co.zsmb:requirektx-intent:1.2.0"
implementation "co.zsmb:requirektx-navigation:1.2.0"
implementation "co.zsmb:requirektx-work:1.2.0"
// commonMain or Android
implementation("co.zsmb:requirektx-bundle:2.0.0-alpha01")
implementation("co.zsmb:requirektx-navigation:2.0.0-alpha01")

// Android only
implementation("co.zsmb:requirektx-intent:2.0.0-alpha01")
implementation("co.zsmb:requirektx-work:2.0.0-alpha01")
}
```

## Available modules and extensions

### Bundle

*The `requirektx-bundle` artifact works with the `androidx.core.bundle.Bundle` type, available on Android and other Kotlin Multiplatform targets from `org.jetbrains.androidx.core:core-bundle`.*

Given a `Bundle`, you can require the following types of values:

```
```kotlin
// Primitives (examples)
bundle.requireBoolean()
bundle.requireByte()
Expand All @@ -92,9 +92,33 @@ bundle.requireFloatArray()

... and many more!

### NavBackStackEntry

*The `requirektx-navigation` artifact works with the `androidx.navigation.NavBackStackEntry` type, available on Android and other Kotlin Multiplatform targets from `org.jetbrains.androidx.navigation:navigation-runtime`.*

*This is compatible with both the [Jetpack Navigaton component on Android](https://developer.android.com/guide/navigation) (with or without Compose) and the [Compose Multiplatform navigation library](https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-navigation-routing.html).*

To get

```kotlin
val args: Bundle = navBackStackEntry.requireArguments()
```

An example of using this with Compose, in combination with the [Bundle extensions](#bundle):

```kotlin
composable("detail/{objectId}") { backStackEntry ->
val args = backStackEntry.requireArguments()
val objectId = args.requireInt("objectId")
DetailScreen(navController, objectId)
}
```

### Intent

Given a `Intent`, you can require its extras `Bundle` (and then require values from it as seen above):
*The `requirektx-intent` artifact works with the [`android.content.Intent`](https://developer.android.com/reference/android/content/Intent) type, available on Android only.*

Given an `Intent`, you can require its extras `Bundle` (and then require values from it as seen above):

```kotlin
val extras: Bundle = intent.requireExtras()
Expand Down Expand Up @@ -127,16 +151,10 @@ intent.requireFloatArrayExtra()

... and many more!

### NavBackStackEntry

The navigation module provides an extension to require the arguments of a `NavBackStackEntry`, same as you could do for a `Fragment`:

```kotlin
val args: Bundle = navBackStackEntry.requireArguments()
```

### WorkManager Data

*The `requirektx-work` artifact provides extensions for the [`androidx.work.Data`](https://developer.android.com/reference/androidx/work/Data) type, available on Android only.*

Given a WorkManager `Data` object (such as `inputData` inside a worker), you can require the following types of values:

```kotlin
Expand Down

0 comments on commit 5f7705a

Please sign in to comment.