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

Overview

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 Jitpack

Gradle

Add below codes to your root build.gradle file (not your module build.gradle file).

allprojects {
    repositories {
        mavenCentral()
    }
}

And add a dependency code to your module's build.gradle file.

dependencies {
    implementation "com.github.skydoves:bundler:1.0.4"
}

SNAPSHOT

Bundler
Snapshots of the current development version of Bundler are available, which track the latest versions.

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.

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.

intentOf<SecondActivity> {
    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.

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.

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.

private val poster: Poster? by bundle("poster") { Poster.create() }

Also, we can initialize type of Array and ArrayList using bundleArray and bundleArrayList expression.

("posterArrayList") or // initialize lazily with default values. private val posterArray by bundleArray ("posterArray") { arrayOf() } private val posterListArray by bundleArrayList ("posterArrayList") { arrayListOf() } ">
// initialize lazily without default values.
private val posterArray by bundleArray<Poster>("posterArray")
private val posterListArray by bundleArrayList<Poster>("posterArrayList")

or

// initialize lazily with default values.
private val posterArray by bundleArray<Poster>("posterArray") { arrayOf() }
private val posterListArray by bundleArrayList<Poster>("posterArrayList") { arrayListOf() }

bundle in Fragment

The below example shows setting arguments using the intentOf expression.

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.

- 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.

- 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.

by observeBundle("poster") id.observe(this) { vm.id = it } poster.observe(this) { binding.name = it.name } ">
private val id: LiveData<Long> by observeBundle("id", -1L)
private val poster: LiveData<Poster> 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.

("poster") ">
val id = bundleValue("id", 100L)
val name = bundleValue("name", "")
val poster = bundleValue<Poster>("poster")

Find this library useful? ❤️

Support it by joining stargazers for this repository.
And follow me for my next creations! 🤩

License

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.
Comments
  • Cant start intentof from Fragment

    Cant start intentof from Fragment

    Please complete the following information:

    • Library Version 1.0.4

    Describe the Bug: Cant use intentof<> to start Activity on Fragment

                                intentOf<RequestActivity> {
                                    putExtra(Extras.FRAGMENT_INDEX, FragmentIndex.AUTH_REQUEST)
                                    startActivity(requireContext())
                                }
    
    opened by fjr619 2
  • Illegal value type for Enum

    Illegal value type for Enum

    Please complete the following information:

    • Library Version 1.0.3

    Describe the Bug: Cant get bundle for Enum Example : ENUM

    enum class FragmentIndex {
        NONE,
        SIMPLE_REQUEST
    }
    

    Activity 1

                    intentOf<RequestActivity> {
                        +(Extras.FRAGMENT_INDEX to FragmentIndex.SIMPLE_REQUEST)
                        startActivity(this@MainActivity)
                    }
    

    Activity 2

    private val index: FragmentIndex by  bundle(Extras.FRAGMENT_INDEX, FragmentIndex.NONE)
    

    it will crash Illegal value type.....

    Expected Behavior:

    usually it will use getSerializableExtra({key})

    opened by fjr619 1
  • SingleShotLiveData

    SingleShotLiveData

    SingleShotLiveData is an implementation of the [MutableLiveData] that emits [T] data only a single time to a single observer. We can observe only once using one observer.

    opened by skydoves 0
  • New features: Bundler value

    New features: Bundler value

    Purpose

    Getting intent or arguments extra values immediately without initializing them lazily.

    val id = bundleValue("id", 100L)
    val name = bundleValue("name", "")
    val poster = bundleValue<Poster>("poster")
    
    opened by skydoves 0
  • observeBundle for Activity, Fragment

    observeBundle for Activity, Fragment

    observeBundle

    Providing observable instances lazily using the LiveData, so we can observe the bundle data regardless of its nullability.

    For example

    private val id by observeBundle("poster")
    
    poster.observe(this) {
         poster.name .. 
    }
    
    New Feature 
    opened by skydoves 0
  • Add an infix `eq` method to add extras without need of `+`

    Add an infix `eq` method to add extras without need of `+`

    Guidelines

    Adds an eq method to insert extras naturally. For example:

     intentOf<SecondActivity> {
       "nickName" eq userInfo.nickname
        startActivity(this@MainActivity)
     }
    

    Types of changes

    Added a new method to Bundler without changing any existing methods.

    • [ ] Bugfix (non-breaking change which fixes an issue)
    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

    Notes

    Kept the method name as eq so as to avoid conflict with Kotlin stdlib's to.

    Preparing a pull request for review

    Ensure your change is properly formatted by running:

    $ ./gradlew spotlessApply
    

    Please correct any failures before requesting a review.

    opened by arunkumar9t2 0
  • Change `intent` function extensions to use reified type instead of KC…

    Change `intent` function extensions to use reified type instead of KC…

    Just to make it nicer and shorter to write.

    Types of changes

    • [ ] Bugfix (non-breaking change which fixes an issue)
    • [ ] New feature (non-breaking change which adds functionality)
    • [x] Breaking change (fix or feature that would cause existing functionality to not work as expected)
    opened by seanghay 0
  • How to passing ENUM?

    How to passing ENUM?

    with old way it will use put/getSerializable, but with bundler I cant get it work, because it cause error Property delegate must have a getValue...

    example Activity A

                        intentOf<AuthActivity> {
                            putExtra(Extras.AUTH_TYPE to AuthType.LOGIN)
                            startActivity(this@GuestActivity)
                        }
    

    Activity B

    val test: AuthType by bundle(Extras.AUTH_TYPE) --> it will error
    val authType: AuthType = intent.extras?.getSerializable(Extras.AUTH_TYPE) as AuthType --> it works fine
    
    opened by fjr619 0
Releases(1.0.4)
  • 1.0.4(Mar 20, 2021)

    🎉 Released a new version 1.0.4! 🎉

    What's New?

    • Added bundleValue for retrieving intent & arguments extra values immediately from Activity and Fragment. bundleValue, bundleNonNullValue, bundleArrayValue, bundleArrayListValue.
    val id = bundleValue("id", 100L)
    val name = bundleValue("name", "")
    val poster = bundleValue<Poster>("poster")
    
    • Now observeBundle emits data only a single time to a single observer. We can observe only once using one observer. And the observer will be unregistered from the LiveData after observing data at once.
    Source code(tar.gz)
    Source code(zip)
  • 1.0.3(Nov 30, 2020)

    🎉 Released a new version 1.0.3! 🎉

    What's New?

    • Added new expressions bundleNonNull and observeBundle.

    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.

    - 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.

    private val id: LiveData<Long> by observeBundle("id", -1L)
    private val poster: LiveData<Poster> by observeBundle("poster")
    
    id.observe(this) {
      vm.id = it
    }
    
    poster.observe(this) {
      binding.name = it.name
    }
    
    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Nov 6, 2020)

    🎉 Released a new version 1.0.2! 🎉

    What's New?

    • Add an infix eq method to add extras without need of + (#2)
    • Fix type miss matching for the CharSequence. (d491110)
    • Make initialize lazily with an unsafe lazy mode to bundle expressions. (7a3021f1d)
    • Added ActivityBundler and FragmentBundler for creating an instance of the Bundler and initializing the extras data & arguments.
    Source code(tar.gz)
    Source code(zip)
    bundler-1.0.2.aar(71.54 KB)
  • 1.0.1(Nov 1, 2020)

  • 1.0.0(Oct 31, 2020)

Owner
Jaewoong Eum
Android and open source software engineer.❤️ Digital Nomad. Love coffee, music, magic tricks, and writing poems. Coffee Driven Development
Jaewoong Eum
Type safe intent building for services and activities

#IntentBuilder Type safe intent building for services and activities. IntentBuilder is a type safe way of creating intents and populating them with ex

Emil Sjölander 348 Oct 10, 2022
Criminal-Intent - An android app used to record office crimes

What is Criminal Intent? CriminalIntent records the details of “office crimes” –

Mohammad Rashid 1 Feb 11, 2022
Android Shared preference wrapper than encrypts the values of Shared Preferences. It's not bullet proof security but rather a quick win for incrementally making your android app more secure.

Secure-preferences - Deprecated Please use EncryptedSharedPreferences from androidx.security in preferenced to secure-preference. (There are no active

Scott Alexander-Bown 1.5k Dec 24, 2022
Extensions to encrypt DataStore using Tink

encrypted-datastore Extensions to encrypt DataStore using Tink. ⚠️ This tiny library will be maintained until an official solution for DataStore encry

Osip Fatkullin 53 Jan 1, 2023
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 31, 2022
General purpose utilities and hash functions for Android and Java (aka java-common)

Essentials Essentials are a collection of general-purpose classes we found useful in many occasions. Beats standard Java API performance, e.g. LongHas

Markus Junginger 1.4k Dec 29, 2022
Access and process various types of personal data in Android with a set of easy, uniform, and privacy-friendly APIs.

PrivacyStreams PrivacyStreams is an Android library for easy and privacy-friendly personal data access and processing. It offers a functional programm

null 269 Dec 1, 2022
A simple and easy to use stopwatch and timer library for android

TimeIt Now with Timer support! A simple and easy to use stopwatch and timer library for android Introduction A stopwatch can be a very important widge

Yashovardhan Dhanania 35 Dec 10, 2022
Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platform the code is running.

Trail Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platfor

Mauricio Togneri 13 Aug 29, 2022
General purpose utilities and hash functions for Android and Java (aka java-common)

Essentials Essentials are a collection of general-purpose classes we found useful in many occasions. Beats standard Java API performance, e.g. LongHas

Markus Junginger 1.4k Dec 29, 2022
DiskCache - Simple and readable disk cache for kotlin and android applications

DiskCache Simple and readable disk cache for kotlin and android applications (with journaled lru strategy) This is a simple lru disk cache, based on t

Giovanni Corte 14 Dec 2, 2022
A library for fast and safe delivery of parameters for Activities and Fragments.

MorbidMask - 吸血面具 Read this in other languages: 中文, English, Change Log A library for fast and safe delivery of parameters for Activities and Fragment

Season 67 Mar 29, 2022
Matches incoming and/or outgoing text messages against set rules and sends them over to webhook.

Textmatic If you ever wanted a tool to simply push the SMS (or text messages) from your phone to somewhere remote, this is it. This app matches all in

Float 2 Jan 7, 2022
Command framework built around Kord, built to be robust and scalable, following Kord's convention and design patterns.

Command framework built around Kord, built to be robust and scalable, following Kord's convention and design patterns.

ZeroTwo Bot 4 Jun 15, 2022
a simple cache for android and java

ASimpleCache ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。 1、它可以缓存什么东西? 普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 b

Michael Yang 3.7k Dec 14, 2022
A lightning fast, transactional, file-based FIFO for Android and Java.

Tape by Square, Inc. Tape is a collection of queue-related classes for Android and Java. QueueFile is a lightning-fast, transactional, file-based FIFO

Square 2.4k Dec 30, 2022
UPnP/DLNA library for Java and Android

Cling EOL: This project is no longer actively maintained, code may be outdated. If you are interested in maintaining and developing this project, comm

4th Line 1.6k Jan 4, 2023
WebSocket & WAMP in Java for Android and Java 8

Autobahn|Java Client library providing WAMP on Java 8 (Netty) and Android, plus (secure) WebSocket for Android. Autobahn|Java is a subproject of the A

Crossbar.io 1.5k Dec 9, 2022
Collection of source codes, utilities, templates and snippets for Android development.

Android Templates and Utilities [DEPRECATED] Android Templates and Utilities are deprecated. I started with this project in 2012. Android ecosystem ha

Petr Nohejl 1.1k Nov 30, 2022