在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):nsk-mironov/kotlin-jetpack开源软件地址(OpenSource Url):https://github.com/nsk-mironov/kotlin-jetpack开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):Kotlin JetpackA collection of useful extension methods for Android Arguments Bindingspublic 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
Every 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 Bindingspublic 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 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 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 Bindingspublic 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
Gradle dependency: compile "com.github.vmironov.jetpack:jetpack-bindings-resources:0.14.2" License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论