Skip to content

Better, type safe version of creating bundle.

License

Apache-2.0, Apache-2.0 licenses found

Licenses found

Apache-2.0
LICENSE
Apache-2.0
LICENSE.md
Notifications You must be signed in to change notification settings

hanibalsk/kbundle

License This project is licensed under Apache 2.0 license.

kBundle (ANDROID)

Android CI Maven Central

Better, type safe version of creating bundle.

Gradle

implementation "sk.32bit:kbundle:0.2.0"

Usage

Bundle creation

val bundle = bundle {
  "IntKey" to 4
  "LongKey" to 7L
  "StringKey" to "Some text"
  putAll(existingBundle) // Adds values from existing bundle
}
val bundle = bundle(existingBundle) {
  "IntKey" to 4
  "LongKey" to 7L
  "StringKey" to "Some text"
}

Persistable bundle creation

val persistableBundle = persistableBundle(existingBundle) {
  "IntKey" to 4
  "LongKey" to 7L
  "StringKey" to "Some text"
}

Fragment with bundle arguments

class CustomFragment : Fragment() {

  private val intParameter by requireArgument<Int>()
  private val stringWithDefaultParameter by requireArgument<String>("default text")
  private var longOptionalParameter by instanceArgument<Long>()

  // Error throw, when missing
  private val parcelableParameter by requireArgument<Rect>("parcelableParameter parameter is required")

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    println("intParameter is $intParameter")
    println("stringWithDefaultParameter is $stringWithDefaultParameter")
    println("longOptionalParameter is $longOptionalParameter")
    println("parcelableParameter is $parcelableParameter")

    // Parameter can be changed
    longOptionalParameter = 22L
  }

  companion object {

    fun newInstance(
      intParameter: Int,
      stringWithDefaultParameter: String,
      parcelableParameter: Rect,
      longOptionalParameter: Long? = null,
    ) = CustomFragment().also { fragment ->
      fragment.arguments = bundle {
        fragment::intParameter.name to intParameter
        fragment::stringWithDefaultParameter.name to stringWithDefaultParameter
        fragment::longOptionalParameter.name to longOptionalParameter
        fragment::parcelableParameter.name to parcelableParameter
      }
    }
  }
}

Code Formatting

./gradlew spotlessApply

Make sure you update spotless.license.kt and LICENSE.md to reflect your own license and author info! Other settings for this plugin can be tweaked in gradle/spotless_plugin_config.gradle.

Check if Dependencies Are Up-to-Date

./gradlew dependencyUpdates

Settings can be tweaked for this plugin within gradle/versions_plugin_config.gradle.

Publishing the Library to Bintray -> jCenter

This repository is setup to automatically publish to Bintray when you create a new release from GitHub.

But there are some pre-requisites:

  1. Update library_info.gradle to contain your library's information (group, description, etc.).
  2. From your GitHub repository settings, add secrets for BINTRAY_ORG, BINTRAY_USER, and BINTRAY_API_KEY. These will be used by the Bintray Release GitHub Actions workflow.
  3. With each release, update the version name and code in library_info.gradle.
  4. Once you've deployed to Bintray for the first time, you can link your Bintray repository to jCenter so people can depend on your library from Android projects without having to add any special Maven repositories.

How does releasing work?

  1. Automatically when you create a Release in your GitHub repository.
  2. OR if you set the three environment variables mentioned above, and invoke ./gradlew bintrayUpload.

About

Better, type safe version of creating bundle.

Resources

License

Apache-2.0, Apache-2.0 licenses found

Licenses found

Apache-2.0
LICENSE
Apache-2.0
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Languages