ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - nsk-mironov/kotlin-jetpack: A collection of useful extension methods for Android
A collection of useful extension methods for Android - nsk-mironov/kotlin-jetpack
Visit Site

GitHub - nsk-mironov/kotlin-jetpack: A collection of useful extension methods for Android

GitHub - nsk-mironov/kotlin-jetpack: A collection of useful extension methods for Android

Kotlin Jetpack Android Arsenal

A collection of useful extension methods for Android

Arguments Bindings

public class ArgumentsFragment : Fragment() {
  public companion object {
    public fun newInstance(): ArgumentsFragment = ArgumentsFragment().apply {
      arguments = Bundle().apply {
        putBoolean("extra_boolean", true)
      }
    }
  }
  
  // Required binding without default value
  val booleanOrThrow by bindArgument<Boolean>("extra_boolean")

  // Required binding with default value
  val booleanOrDefault by bindArgument<Boolean>("extra_boolean", false)

  // Optional binding
  val booleanOrNull by bindOptionalArgument<Boolean>("extra_boolean")
}

These methods can be used with Activity, Fragment, and support library Fragment subclasses. You can also implement ArgumentsAware interface to provide a custom arguments source. Full list of supported bindings:

  • bindArgument<Boolean> / bindOptionalArgument<Boolean>
  • bindArgument<Double> / bindOptionalArgument<Double>
  • bindArgument<Int> / bindOptionalArgument<Int>
  • bindArgument<Long> / bindOptionalArgument<Long>
  • bindArgument<String> / bindOptionalArgument<String>
  • bindArgument<CharSequence> / bindOptionalArgument<CharSequence>
  • bindArgument<Float> / bindOptionalArgument<Float>
  • bindArgument<Enum> / bindOptionalArgument<Enum>
  • bindArgument<Parcelable> / bindOptionalArgument<Parcelable>
  • bindArgument<Serializable> / bindOptionalArgument<Serializable>

Every bindXXXArgument returns a ReadWriteProperty so they can be used with var's as well. In this case you don't have to deal with Bundle at all. No explicit Bundle creation, no silly EXTRA_XXX constants, no annoying Bundle.putXXX and Bundle.getXXX calls. Everything just works:

public class UserProfileFragment : Fragment() {
  public companion object {
    public fun newInstance(): UserProfileFragment = UserProfileFragment().apply {
      this.firstName = "Vladimir"
      this.lastName = "Mironov"
    }
  }
  
  // extra name is automatically inferred from property name ("firstName" in this case)
  var firstName by bindArgument<String>()

  // you can also provide a default value using "default" named argument
  var lastName by bindArgument<String>(default = "")
}

Gradle dependency:

compile "com.github.vmironov.jetpack:jetpack-bindings-arguments:0.14.2"

Preferences Bindings

public class PreferencesFragment : Fragment() {
  // Boolean preference
  var boolean by bindPreference<Boolean>("boolean", false)

  // Float preference
  var float by bindPreference<Float>("float", 0.0f)

  // Integer preference
  var integer by bindPreference<Int>("integer", 1)

  // Long preference
  var long by bindPreference<Long>("long", 1L)

  // String preference
  var string by bindPreference<String>("string", "default")
}

These methods can be used with Context, Fragment, support library Fragment, View, and ViewHolder subclasses. The example above uses a default SharedPreferences instance. You can always provide a custom one by implementing PreferencesAware interface:

public class PreferencesFragment : Fragment() {
  val preferences = PreferencesAware {
    activity.getSharedPreferences("CustomSharedPreferences", Context.MODE_PRIVATE)
  }
  
  var boolean by preferences.bindPreference<Boolean>("boolean", false)
  var float by preferences.bindPreference<Float>("float", 0.0f)
  var integer by preferences.bindPreference<Int>("integer", 1)
  var long by preferences.bindPreference<Long>("long", 1L)
  var string by preferences.bindPreference<String>("string", "default")
  
  // Optional preferences are supported as well
  var optionalLong by preferences.bindOptionalPreference<Long>()
  var optionalString by preferences.bindOptionalPreference<String>()
}

Although only Boolean, Float, Int, Long and String preferences are supported by default, the library can be easily extented to support custom type of preference. Adapter interface can be implemented in order to convert any type to a supported one. Here is an example how to imlement json-based preferences using Gson:

public inline fun <reified E : Any> Any.bindGsonPreference(default: E, key: String? = null): ReadWriteProperty<Any, E> {
  return bindPreference(default, GsonPreferenceAdapter(E::class.java), key)
}

public inline fun <reified E : Any> Any.bindGsonPreference(noinline default: () -> E, key: String? = null): ReadWriteProperty<Any, E> {
  return bindPreference(default, GsonPreferenceAdapter(E::class.java), key)
}

public class GsonPreferenceAdapter<T>(val clazz: Class<T>, val gson: Gson = GsonPreferenceAdapter.GSON) : Adapter<T, String> {
  override fun type(): Class<String> = String::class.java
  override fun fromPreference(preference: String): T = gson.fromJson(preference, clazz)
  override fun toPreference(value: T): String = gson.toJson(value)

  public companion object {
    public val GSON = Gson()
  }
}

Usage:

public data class Profile(val firstName: String? = null, val lastName: String? = null)

public class ProfileManager(val context: Context) {
  public var profile by bindGsonPreference(Profile())
}

Gradle dependency:

compile "com.github.vmironov.jetpack:jetpack-bindings-preferences:0.14.2"

Resources Bindings

public class ResourcesFragment : Fragment() {
  // Boolean resource binding
  val boolean by bindResource<Boolean>(R.boolean.boolean_resource)

  // Color resource binding
  val color by bindResource<Int>(R.color.color_resource)

  // Drawable resource binding #1
  val bitmap by bindResource<BitmapDrawable>(R.drawable.drawable_bitmap)
  
  // Drawable resource binding #2
  val vector by bindResource<VectorDrawable>(R.drawable.drawable_vector)

  // Dimension resource binding
  val dimension by bindResource<Int>(R.dimen.dimen_resource)

  // String resource binding
  val string by bindResource<String>(R.string.string_resource)
}

These methods can be used with Activity, Context, Fragment, support library Fragment, View, and ViewHolder subclasses. You can also implement ResourcesAware interface to provide a custom resources source. Full list of supported bindings:

  • bindResource<Boolean>(R.boolean.boolean_resource)
  • bindResource<Int>(R.integer.integer_resource)
  • bindResource<Int>(R.color.color_resource)
  • bindResource<ColorStateList>(R.color.color_resource)
  • bindResource<Drawable>(R.drawable.drawable_resource)
  • bindResource<Int>(R.dimen.dimen_resource)
  • bindResource<Float>(R.dimen.dimen_resource)
  • bindResource<String>(R.string.string_resource)
  • bindResource<CharSequence>(R.string.string_resource)
  • bindResource<IntArray>(R.array.array_resource)
  • bindResource<Array<String>>(R.array.array_resource)
  • bindResource<Array<CharSequence>>(R.array.array_resource)

Gradle dependency:

compile "com.github.vmironov.jetpack:jetpack-bindings-resources:0.14.2"

License

Copyright 2015 Vladimir Mironov

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory