Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/skydoves/bundler

🎁 Android Intent & Bundle extensions that insert and retrieve values elegantly.
https://github.com/skydoves/bundler

activity android bundle bundler fragment intent kotlin kotlin-extensions skydoves

Last synced: 7 minutes ago
JSON representation

🎁 Android Intent & Bundle extensions that insert and retrieve values elegantly.

Awesome Lists containing this project

README

        

Bundler



License
API
Build Status
Android Weekly
KotlinWeekly
Medium
Javadoc


🎁 Android Intent & Bundle extensions that insert and retrieve values elegantly.



## Including in your project
[![Maven Central](https://img.shields.io/maven-central/v/com.github.skydoves/bundler.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.skydoves%22%20AND%20a:%22bundler%22) [![Jitpack](https://jitpack.io/v/skydoves/bundler.svg)](https://jitpack.io/#skydoves/bundler)
### Gradle
Add below codes to your **root** `build.gradle` file (not your module build.gradle file).
```gradle
allprojects {
repositories {
mavenCentral()
}
}
```
And add a dependency code to your **module**'s `build.gradle` file.
```gradle
dependencies {
implementation "com.github.skydoves:bundler:1.0.4"
}
```
## SNAPSHOT
[![Bundler](https://img.shields.io/static/v1?label=snapshot&message=bundler&logo=apache%20maven&color=C71A36)](https://oss.sonatype.org/content/repositories/snapshots/com/github/skydoves/bundler/)

Snapshots of the current development version of Bundler are available, which track [the latest versions](https://oss.sonatype.org/content/repositories/snapshots/com/github/skydoves/bundler/).
```Gradle
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
```

## Usage
### Intent
`intentOf` is an expression for creating an Intent using Kotlin DSL style and we can put extras using the `putExtra` method. Also, we can put extras using the `+` keyword in front of a key/value pair.
```kotlin
val intent = intentOf {
putExtra("posterId", poster.id) // put a Long type 'posterId' value.
putExtra("posterName" to poster.name) // put a String type 'posterName' value.
putExtra("poster", poster) // put a Parcelable type 'poster' value.

+("id" to userInfo.id) // put a Long type 'id' value.
+("name" to userInfo.nickname) // put a String type 'name' value.

-"name" // remove a String type 'name' value.
}
```
### StartActivity
We can start activities using the `intentOf` expression like below.
```kotlin
intentOf {
putExtra("id" to userInfo.id)
putExtra("name" to userInfo.nickname)
putExtra("poster", poster)
startActivity(this@MainActivity)
}
```
We can also use other options for creating an intent.
```kotlin
intentOf {
setAction(Intent.ACTION_MAIN)
addCategory(Intent.CATEGORY_APP_MUSIC)
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(this@MainActivity)
}
```
### bundle
`bundle` is an expression for initializing lazily extra values from the intent.
```kotlin
class SecondActivity : AppCompatActivity() {

private val id: Long by bundle("id", -1) // initializes a Long extra value lazily.
private val name: String by bundle("name", "") // initializes a String extra value lazily.
private val poster: Poster? by bundle("poster") // initializes a Parcelable extra value lazily.

// -- stubs -- //
```
We can initialize a Parcelable value with a defaut value.
```kotlin
private val poster: Poster? by bundle("poster") { Poster.create() }
```
Also, we can initialize type of Array and ArrayList using `bundleArray` and `bundleArrayList` expression.
```kotlin
// initialize lazily without default values.
private val posterArray by bundleArray("posterArray")
private val posterListArray by bundleArrayList("posterArrayList")

or

// initialize lazily with default values.
private val posterArray by bundleArray("posterArray") { arrayOf() }
private val posterListArray by bundleArrayList("posterArrayList") { arrayListOf() }
```
### bundle in Fragment
The below example shows setting arguments using the `intentOf` expression.
```kotlin
arguments = intentOf {
+("id" to userInfo.id)
+("name" to userInfo.nickname)
+("poster" to poster)
}.extras
```
We can initialize argument values lazily in Fragments using the `bundle` expression like below.
```diff
- val id: Long = arguments?.getLong("id", -1) ?: -1
+ val id: Long by bundle("id", -1)
- val poster: Poster? = arguments?.getParcelable("poster")
+ val poster: Poster? by bundle("poster")
```

### bundleNonNull
The `bundle` expression for initializing objects (e.g. Bundle, CharSequence, Parcelable, Serializable, Arrays), the property type must be null-able. But If we want to initialize them non-nullable type, we can initialize them to non-nullable type using the `bundleNonNull` expression.
```diff
- val poster: Poster? by bundle("poster")
+ val poster: Poster by bundleNotNull("poster")
```

### observeBundle
We can observe the bundle data as `LiveData` using the `observeBundle` expression. If there are no extra & arguments in the Activity or Fragment, `null` will be passed to the observers. The `observeBundle` emits data only a single time to a single observer. So We can observe only once using one observer. And the observer will be unregistered from the LiveData after observing data at once.
```kotlin
private val id: LiveData by observeBundle("id", -1L)
private val poster: LiveData by observeBundle("poster")

id.observe(this) {
vm.id = it
}

poster.observe(this) {
binding.name = it.name
}
```

### bundleValue
We can also retrieve intent & arguments extra values from Activity and Fragment immediately. We can use `bundleValue`, `bundleNonNullValue`, `bundleArrayValue`, `bundleArrayListValue`.

```kotlin
val id = bundleValue("id", 100L)
val name = bundleValue("name", "")
val poster = bundleValue("poster")
```

## Find this library useful? :heart:
Support it by joining __[stargazers](https://github.com/skydoves/bundler/stargazers)__ for this repository. :star:

And __[follow](https://github.com/skydoves)__ me for my next creations! 🤩

# License
```xml
Copyright 2020 skydoves (Jaewoong Eum)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```