.unaryPlus() = intent.putExtras(
218 | com.skydoves.bundler.bundleOf(this)
219 | )
220 |
221 | /**
222 | * Removes a previous extra.
223 | *
224 | * ```
225 | * -key
226 | * ```
227 | **/
228 | operator fun String.unaryMinus() = intent.removeExtra(this)
229 | }
230 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | Bundler
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | 🎁 Android Intent & Bundle extensions that insert and retrieve values elegantly.
15 |
16 |
17 |
18 |
19 |
20 |
21 | ## Including in your project
22 | [](https://search.maven.org/search?q=g:%22com.github.skydoves%22%20AND%20a:%22bundler%22) [](https://jitpack.io/#skydoves/bundler)
23 | ### Gradle
24 | Add below codes to your **root** `build.gradle` file (not your module build.gradle file).
25 | ```gradle
26 | allprojects {
27 | repositories {
28 | mavenCentral()
29 | }
30 | }
31 | ```
32 | And add a dependency code to your **module**'s `build.gradle` file.
33 | ```gradle
34 | dependencies {
35 | implementation "com.github.skydoves:bundler:1.0.4"
36 | }
37 | ```
38 | ## SNAPSHOT
39 | [](https://oss.sonatype.org/content/repositories/snapshots/com/github/skydoves/bundler/)
40 | 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/).
41 | ```Gradle
42 | repositories {
43 | maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
44 | }
45 | ```
46 |
47 | ## Usage
48 | ### Intent
49 | `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.
50 | ```kotlin
51 | val intent = intentOf {
52 | putExtra("posterId", poster.id) // put a Long type 'posterId' value.
53 | putExtra("posterName" to poster.name) // put a String type 'posterName' value.
54 | putExtra("poster", poster) // put a Parcelable type 'poster' value.
55 |
56 | +("id" to userInfo.id) // put a Long type 'id' value.
57 | +("name" to userInfo.nickname) // put a String type 'name' value.
58 |
59 | -"name" // remove a String type 'name' value.
60 | }
61 | ```
62 | ### StartActivity
63 | We can start activities using the `intentOf` expression like below.
64 | ```kotlin
65 | intentOf {
66 | putExtra("id" to userInfo.id)
67 | putExtra("name" to userInfo.nickname)
68 | putExtra("poster", poster)
69 | startActivity(this@MainActivity)
70 | }
71 | ```
72 | We can also use other options for creating an intent.
73 | ```kotlin
74 | intentOf {
75 | setAction(Intent.ACTION_MAIN)
76 | addCategory(Intent.CATEGORY_APP_MUSIC)
77 | setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
78 | startActivity(this@MainActivity)
79 | }
80 | ```
81 | ### bundle
82 | `bundle` is an expression for initializing lazily extra values from the intent.
83 | ```kotlin
84 | class SecondActivity : AppCompatActivity() {
85 |
86 | private val id: Long by bundle("id", -1) // initializes a Long extra value lazily.
87 | private val name: String by bundle("name", "") // initializes a String extra value lazily.
88 | private val poster: Poster? by bundle("poster") // initializes a Parcelable extra value lazily.
89 |
90 | // -- stubs -- //
91 | ```
92 | We can initialize a Parcelable value with a defaut value.
93 | ```kotlin
94 | private val poster: Poster? by bundle("poster") { Poster.create() }
95 | ```
96 | Also, we can initialize type of Array and ArrayList using `bundleArray` and `bundleArrayList` expression.
97 | ```kotlin
98 | // initialize lazily without default values.
99 | private val posterArray by bundleArray("posterArray")
100 | private val posterListArray by bundleArrayList("posterArrayList")
101 |
102 | or
103 |
104 | // initialize lazily with default values.
105 | private val posterArray by bundleArray("posterArray") { arrayOf() }
106 | private val posterListArray by bundleArrayList("posterArrayList") { arrayListOf() }
107 | ```
108 | ### bundle in Fragment
109 | The below example shows setting arguments using the `intentOf` expression.
110 | ```kotlin
111 | arguments = intentOf {
112 | +("id" to userInfo.id)
113 | +("name" to userInfo.nickname)
114 | +("poster" to poster)
115 | }.extras
116 | ```
117 | We can initialize argument values lazily in Fragments using the `bundle` expression like below.
118 | ```diff
119 | - val id: Long = arguments?.getLong("id", -1) ?: -1
120 | + val id: Long by bundle("id", -1)
121 | - val poster: Poster? = arguments?.getParcelable("poster")
122 | + val poster: Poster? by bundle("poster")
123 | ```
124 |
125 | ### bundleNonNull
126 | 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.
127 | ```diff
128 | - val poster: Poster? by bundle("poster")
129 | + val poster: Poster by bundleNotNull("poster")
130 | ```
131 |
132 | ### observeBundle
133 | 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.
134 | ```kotlin
135 | private val id: LiveData by observeBundle("id", -1L)
136 | private val poster: LiveData by observeBundle("poster")
137 |
138 | id.observe(this) {
139 | vm.id = it
140 | }
141 |
142 | poster.observe(this) {
143 | binding.name = it.name
144 | }
145 | ```
146 |
147 | ### bundleValue
148 | We can also retrieve intent & arguments extra values from Activity and Fragment immediately. We can use `bundleValue`, `bundleNonNullValue`, `bundleArrayValue`, `bundleArrayListValue`.
149 |
150 | ```kotlin
151 | val id = bundleValue("id", 100L)
152 | val name = bundleValue("name", "")
153 | val poster = bundleValue("poster")
154 | ```
155 |
156 | ## Find this library useful? :heart:
157 | Support it by joining __[stargazers](https://github.com/skydoves/bundler/stargazers)__ for this repository. :star:
158 | And __[follow](https://github.com/skydoves)__ me for my next creations! 🤩
159 |
160 | # License
161 | ```xml
162 | Copyright 2020 skydoves (Jaewoong Eum)
163 |
164 | Licensed under the Apache License, Version 2.0 (the "License");
165 | you may not use this file except in compliance with the License.
166 | You may obtain a copy of the License at
167 |
168 | http://www.apache.org/licenses/LICENSE-2.0
169 |
170 | Unless required by applicable law or agreed to in writing, software
171 | distributed under the License is distributed on an "AS IS" BASIS,
172 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
173 | See the License for the specific language governing permissions and
174 | limitations under the License.
175 | ```
176 |
--------------------------------------------------------------------------------
/bundler/src/main/java/com/skydoves/bundler/FragmentBundleLazy.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Designed and developed by 2020 skydoves (Jaewoong Eum)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | @file:Suppress("UNCHECKED_CAST", "unused")
18 |
19 | package com.skydoves.bundler
20 |
21 | import android.os.Bundle
22 | import android.os.Parcelable
23 | import androidx.fragment.app.Fragment
24 | import java.io.Serializable
25 |
26 | /**
27 | * @author skydoves (Jaewoong Eum)
28 | *
29 | * Retrieves a primitive type of extended data from arguments lazily.
30 | *
31 | * @param key The name of the desired item.
32 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
33 | *
34 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
35 | */
36 | @JvmSynthetic
37 | @InlineBundleDsl
38 | inline fun Fragment.bundle(key: String, defaultValue: T): Lazy {
39 | return fragmentVariableBundler(defaultValue) {
40 | when (defaultValue) {
41 | is Boolean -> intent.getBooleanExtra(key, defaultValue)
42 | is Byte -> intent.getByteExtra(key, defaultValue)
43 | is Char -> intent.getCharExtra(key, defaultValue)
44 | is Double -> intent.getDoubleExtra(key, defaultValue)
45 | is Float -> intent.getFloatExtra(key, defaultValue)
46 | is Int -> intent.getIntExtra(key, defaultValue)
47 | is Long -> intent.getLongExtra(key, defaultValue)
48 | is Short -> intent.getShortExtra(key, defaultValue)
49 | is CharSequence -> intent.getStringExtra(key)
50 |
51 | else -> throw IllegalArgumentException(
52 | "Illegal value type ${defaultValue.javaClass} for key \"$key\""
53 | )
54 | } as? T
55 | }
56 | }
57 |
58 | /**
59 | * @author skydoves (Jaewoong Eum)
60 | *
61 | * Retrieves a references type of extended data from arguments lazily.
62 | *
63 | * @param key The name of the desired item.
64 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
65 | *
66 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
67 | */
68 | @JvmSynthetic
69 | @InlineBundleDsl
70 | inline fun Fragment.bundle(
71 | key: String,
72 | crossinline defaultValue: () -> T? = { null }
73 | ): Lazy {
74 | val objectType = T::class.javaObjectType
75 | return fragmentTypedBundler(defaultValue) {
76 | when {
77 | // references
78 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as? T
79 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(key) as? T
80 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
81 | key
82 | ) as? T
83 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
84 | key
85 | ) as? T
86 |
87 | // scalar arrays
88 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
89 | key
90 | ) as? T
91 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as? T
92 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as? T
93 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as? T
94 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as? T
95 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as? T
96 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as? T
97 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as? T
98 |
99 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
100 | }
101 | }
102 | }
103 |
104 | /**
105 | * @author skydoves (Jaewoong Eum)
106 | *
107 | * Retrieves a references type of extended data from arguments lazily.
108 | *
109 | * @param key The name of the desired item.
110 | *
111 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
112 | * @exception NullPointerException When there is no desired value from the arguments.
113 | */
114 | @JvmSynthetic
115 | @InlineBundleDsl
116 | inline fun Fragment.bundleNonNull(
117 | key: String
118 | ): Lazy {
119 | val objectType = T::class.javaObjectType
120 | return fragmentNonNullTypedBundler {
121 | when {
122 | // references
123 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as T
124 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(key) as T
125 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
126 | key
127 | ) as T
128 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
129 | key
130 | ) as T
131 |
132 | // scalar arrays
133 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
134 | key
135 | ) as T
136 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as T
137 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as T
138 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as T
139 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as T
140 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as T
141 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as T
142 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as T
143 |
144 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
145 | }
146 | }
147 | }
148 |
149 | /**
150 | * @author skydoves (Jaewoong Eum)
151 | *
152 | * Retrieves a references array type of extended data from arguments lazily.
153 | *
154 | * @param key The name of the desired item.
155 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
156 | *
157 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
158 | */
159 | @JvmSynthetic
160 | @InlineBundleDsl
161 | inline fun Fragment.bundleArray(
162 | key: String,
163 | crossinline defaultValue: () -> Array? = { null }
164 | ): Lazy?> {
165 | val javaObjectType = T::class.javaObjectType
166 | return fragmentArrayBundler(defaultValue) {
167 | (
168 | when {
169 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayExtra(key)
170 | CharSequence::class.java.isAssignableFrom(javaObjectType) -> intent.getCharSequenceArrayExtra(key)
171 | Parcelable::class.java.isAssignableFrom(javaObjectType) -> intent.getParcelableArrayExtra(key)
172 |
173 | else -> throw IllegalArgumentException("Illegal value type $javaObjectType for key \"$key\"")
174 | }
175 | )?.filterIsInstance()?.toTypedArray()
176 | }
177 | }
178 |
179 | /**
180 | * @author skydoves (Jaewoong Eum)
181 | *
182 | * Retrieves a references array list type of extended data from arguments lazily.
183 | *
184 | * @param key The name of the desired item.
185 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
186 | *
187 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
188 | */
189 | @JvmSynthetic
190 | @InlineBundleDsl
191 | inline fun Fragment.bundleArrayList(
192 | key: String,
193 | crossinline defaultValue: () -> ArrayList? = { null }
194 | ): Lazy?> {
195 | val javaObjectType = T::class.javaObjectType
196 | return fragmentArrayListBundler(defaultValue) {
197 | when {
198 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayListExtra(key)
199 | CharSequence::class.java.isAssignableFrom(
200 | javaObjectType
201 | ) -> intent.getCharSequenceArrayListExtra(key)
202 | Parcelable::class.java.isAssignableFrom(
203 | javaObjectType
204 | ) -> intent.getParcelableArrayListExtra(key)
205 |
206 | else -> throw IllegalArgumentException("Illegal value type $javaObjectType for key \"$key\"")
207 | } as? ArrayList
208 | }
209 | }
210 |
--------------------------------------------------------------------------------
/bundler/src/main/java/com/skydoves/bundler/ActivityBundleLazy.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Designed and developed by 2020 skydoves (Jaewoong Eum)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | @file:Suppress("UNCHECKED_CAST", "unused")
18 |
19 | package com.skydoves.bundler
20 |
21 | import android.app.Activity
22 | import android.os.Bundle
23 | import android.os.Parcelable
24 | import java.io.Serializable
25 |
26 | /**
27 | * @author skydoves (Jaewoong Eum)
28 | *
29 | * Retrieves a primitive type of extended data from the Intent lazily.
30 | *
31 | * @param key The name of the desired item.
32 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
33 | *
34 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
35 | */
36 | @JvmSynthetic
37 | @InlineBundleDsl
38 | inline fun Activity.bundle(key: String, defaultValue: T): Lazy {
39 | return activityVariableBundler(defaultValue) {
40 | when (defaultValue) {
41 | is Boolean -> intent.getBooleanExtra(key, defaultValue)
42 | is Byte -> intent.getByteExtra(key, defaultValue)
43 | is Char -> intent.getCharExtra(key, defaultValue)
44 | is Double -> intent.getDoubleExtra(key, defaultValue)
45 | is Float -> intent.getFloatExtra(key, defaultValue)
46 | is Int -> intent.getIntExtra(key, defaultValue)
47 | is Long -> intent.getLongExtra(key, defaultValue)
48 | is Short -> intent.getShortExtra(key, defaultValue)
49 | is CharSequence -> intent.getStringExtra(key)
50 |
51 | else -> throw IllegalArgumentException(
52 | "Illegal value type ${defaultValue.javaClass} for key \"$key\""
53 | )
54 | } as? T
55 | }
56 | }
57 |
58 | /**
59 | * @author skydoves (Jaewoong Eum)
60 | *
61 | * Retrieves a references type of extended data from the Intent lazily.
62 | *
63 | * @param key The name of the desired item.
64 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
65 | *
66 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
67 | */
68 | @JvmSynthetic
69 | @InlineBundleDsl
70 | inline fun Activity.bundle(
71 | key: String,
72 | crossinline defaultValue: () -> T? = { null }
73 | ): Lazy {
74 | val objectType = T::class.javaObjectType
75 | return activityTypedBundler(defaultValue) {
76 | when {
77 | // references
78 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as? T
79 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(key) as? T
80 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
81 | key
82 | ) as? T
83 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
84 | key
85 | ) as? T
86 |
87 | // scalar arrays
88 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
89 | key
90 | ) as? T
91 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as? T
92 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as? T
93 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as? T
94 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as? T
95 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as? T
96 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as? T
97 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as? T
98 |
99 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
100 | }
101 | }
102 | }
103 |
104 | /**
105 | * @author skydoves (Jaewoong Eum)
106 | *
107 | * Retrieves a non-null references type of extended data from the Intent lazily.
108 | *
109 | * @param key The name of the desired item.
110 | *
111 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle] or null.
112 | * @exception NullPointerException When there is no desired value from the Intent.
113 | */
114 | @JvmSynthetic
115 | @InlineBundleDsl
116 | inline fun Activity.bundleNonNull(
117 | key: String
118 | ): Lazy {
119 | val objectType = T::class.javaObjectType
120 | return activityNonNullTypedBundler {
121 | when {
122 | // references
123 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as T
124 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(key) as T
125 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
126 | key
127 | ) as T
128 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
129 | key
130 | ) as T
131 |
132 | // scalar arrays
133 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
134 | key
135 | ) as T
136 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as T
137 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as T
138 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as T
139 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as T
140 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as T
141 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as T
142 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as T
143 |
144 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
145 | }
146 | }
147 | }
148 |
149 | /**
150 | * @author skydoves (Jaewoong Eum)
151 | *
152 | * Retrieves a references array type of extended data from the Intent lazily.
153 | *
154 | * @param key The name of the desired item.
155 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
156 | *
157 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
158 | */
159 | @JvmSynthetic
160 | @InlineBundleDsl
161 | inline fun Activity.bundleArray(
162 | key: String,
163 | crossinline defaultValue: () -> Array? = { null }
164 | ): Lazy?> {
165 | val javaObjectType = T::class.javaObjectType
166 | return activityArrayBundler(defaultValue) {
167 | (
168 | when {
169 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayExtra(key)
170 | CharSequence::class.java.isAssignableFrom(javaObjectType) -> intent.getCharSequenceArrayExtra(key)
171 | Parcelable::class.java.isAssignableFrom(javaObjectType) -> intent.getParcelableArrayExtra(key)
172 |
173 | else -> throw IllegalArgumentException("Illegal value type $javaObjectType for key \"$key\"")
174 | } as? Array<*>
175 | )?.filterIsInstance()?.toTypedArray()
176 | }
177 | }
178 |
179 | /**
180 | * @author skydoves (Jaewoong Eum)
181 | *
182 | * Retrieves a references array list type of extended data from the Intent lazily.
183 | *
184 | * @param key The name of the desired item.
185 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
186 | *
187 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
188 | */
189 | @JvmSynthetic
190 | @InlineBundleDsl
191 | inline fun Activity.bundleArrayList(
192 | key: String,
193 | crossinline defaultValue: () -> ArrayList? = { null }
194 | ): Lazy?> {
195 | val javaObjectType = T::class.javaObjectType
196 | return activityArrayListBundler(defaultValue) {
197 | when {
198 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayListExtra(key)
199 | CharSequence::class.java.isAssignableFrom(
200 | javaObjectType
201 | ) -> intent.getCharSequenceArrayListExtra(key)
202 | Parcelable::class.java.isAssignableFrom(
203 | javaObjectType
204 | ) -> intent.getParcelableArrayListExtra(key)
205 |
206 | else -> throw IllegalArgumentException("Illegal value type $javaObjectType for key \"$key\"")
207 | } as ArrayList?
208 | }
209 | }
210 |
--------------------------------------------------------------------------------
/bundler/src/main/java/com/skydoves/bundler/FragmentBundleValue.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Designed and developed by 2020 skydoves (Jaewoong Eum)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | @file:Suppress("UNCHECKED_CAST", "unused")
18 |
19 | package com.skydoves.bundler
20 |
21 | import android.os.Bundle
22 | import android.os.Parcelable
23 | import androidx.fragment.app.Fragment
24 | import java.io.Serializable
25 |
26 | /**
27 | * @author skydoves (Jaewoong Eum)
28 | *
29 | * Retrieves a primitive type of extended data from arguments immediately.
30 | *
31 | * @param key The name of the desired item.
32 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
33 | *
34 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
35 | */
36 | @JvmSynthetic
37 | @InlineBundleDsl
38 | inline fun Fragment.bundleValue(key: String, defaultValue: T): T {
39 | return fragmentVariableBundlerValue(defaultValue) {
40 | when (defaultValue) {
41 | is Boolean -> intent.getBooleanExtra(key, defaultValue)
42 | is Byte -> intent.getByteExtra(key, defaultValue)
43 | is Char -> intent.getCharExtra(key, defaultValue)
44 | is Double -> intent.getDoubleExtra(key, defaultValue)
45 | is Float -> intent.getFloatExtra(key, defaultValue)
46 | is Int -> intent.getIntExtra(key, defaultValue)
47 | is Long -> intent.getLongExtra(key, defaultValue)
48 | is Short -> intent.getShortExtra(key, defaultValue)
49 | is CharSequence -> intent.getStringExtra(key)
50 |
51 | else -> throw IllegalArgumentException(
52 | "Illegal value type ${defaultValue.javaClass} for key \"$key\""
53 | )
54 | } as? T
55 | }
56 | }
57 |
58 | /**
59 | * @author skydoves (Jaewoong Eum)
60 | *
61 | * Retrieves a references type of extended data from arguments immediately.
62 | *
63 | * @param key The name of the desired item.
64 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
65 | *
66 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
67 | */
68 | @JvmSynthetic
69 | @InlineBundleDsl
70 | inline fun Fragment.bundleValue(
71 | key: String,
72 | crossinline defaultValue: () -> T? = { null }
73 | ): T? {
74 | val objectType = T::class.javaObjectType
75 | return fragmentTypedBundlerValue(defaultValue) {
76 | when {
77 | // references
78 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as? T
79 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(
80 | key
81 | ) as? T
82 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
83 | key
84 | ) as? T
85 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
86 | key
87 | ) as? T
88 |
89 | // scalar arrays
90 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
91 | key
92 | ) as? T
93 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as? T
94 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as? T
95 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as? T
96 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as? T
97 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as? T
98 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as? T
99 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as? T
100 |
101 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
102 | }
103 | }
104 | }
105 |
106 | /**
107 | * @author skydoves (Jaewoong Eum)
108 | *
109 | * Retrieves a references type of extended data from arguments immediately.
110 | *
111 | * @param key The name of the desired item.
112 | *
113 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
114 | * @exception NullPointerException When there is no desired value from the arguments.
115 | */
116 | @JvmSynthetic
117 | @InlineBundleDsl
118 | inline fun Fragment.bundleNonNullValue(
119 | key: String
120 | ): T {
121 | val objectType = T::class.javaObjectType
122 | return fragmentNonNullTypedBundlerValue {
123 | when {
124 | // references
125 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as T
126 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(key) as T
127 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
128 | key
129 | ) as T
130 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
131 | key
132 | ) as T
133 |
134 | // scalar arrays
135 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
136 | key
137 | ) as T
138 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as T
139 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as T
140 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as T
141 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as T
142 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as T
143 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as T
144 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as T
145 |
146 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
147 | }
148 | }
149 | }
150 |
151 | /**
152 | * @author skydoves (Jaewoong Eum)
153 | *
154 | * Retrieves a references array type of extended data from arguments immediately.
155 | *
156 | * @param key The name of the desired item.
157 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
158 | *
159 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
160 | */
161 | @JvmSynthetic
162 | @InlineBundleDsl
163 | inline fun Fragment.bundleArrayValue(
164 | key: String,
165 | crossinline defaultValue: () -> Array? = { null }
166 | ): Array? {
167 | val javaObjectType = T::class.javaObjectType
168 | return fragmentArrayBundlerValue(defaultValue) {
169 | (
170 | when {
171 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayExtra(key)
172 | CharSequence::class.java.isAssignableFrom(
173 | javaObjectType
174 | ) -> intent.getCharSequenceArrayExtra(key)
175 | Parcelable::class.java.isAssignableFrom(javaObjectType) -> intent.getParcelableArrayExtra(
176 | key
177 | )
178 |
179 | else -> throw IllegalArgumentException(
180 | "Illegal value type $javaObjectType for key \"$key\""
181 | )
182 | }
183 | )?.filterIsInstance()?.toTypedArray()
184 | }
185 | }
186 |
187 | /**
188 | * @author skydoves (Jaewoong Eum)
189 | *
190 | * Retrieves a references array list type of extended data from arguments immediately.
191 | *
192 | * @param key The name of the desired item.
193 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
194 | *
195 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
196 | */
197 | @JvmSynthetic
198 | @InlineBundleDsl
199 | inline fun Fragment.bundleArrayListValue(
200 | key: String,
201 | crossinline defaultValue: () -> ArrayList? = { null }
202 | ): ArrayList? {
203 | val javaObjectType = T::class.javaObjectType
204 | return fragmentArrayListBundlerValue(defaultValue) {
205 | when {
206 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayListExtra(key)
207 | CharSequence::class.java.isAssignableFrom(
208 | javaObjectType
209 | ) -> intent.getCharSequenceArrayListExtra(key)
210 | Parcelable::class.java.isAssignableFrom(
211 | javaObjectType
212 | ) -> intent.getParcelableArrayListExtra(key)
213 |
214 | else -> throw IllegalArgumentException("Illegal value type $javaObjectType for key \"$key\"")
215 | } as? ArrayList
216 | }
217 | }
218 |
--------------------------------------------------------------------------------
/bundler/src/main/java/com/skydoves/bundler/ActivityBundleValue.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Designed and developed by 2020 skydoves (Jaewoong Eum)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | @file:Suppress("UNCHECKED_CAST")
18 |
19 | package com.skydoves.bundler
20 |
21 | import android.app.Activity
22 | import android.os.Bundle
23 | import android.os.Parcelable
24 | import java.io.Serializable
25 |
26 | /**
27 | * @author skydoves (Jaewoong Eum)
28 | *
29 | * Retrieves a primitive type of extended data from the Intent immediately.
30 | *
31 | * @param key The name of the desired item.
32 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
33 | *
34 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
35 | */
36 | @JvmSynthetic
37 | @InlineBundleDsl
38 | inline fun Activity.bundleValue(key: String, defaultValue: T): T {
39 | return activityVariableBundlerValue(defaultValue) {
40 | when (defaultValue) {
41 | is Boolean -> intent.getBooleanExtra(key, defaultValue)
42 | is Byte -> intent.getByteExtra(key, defaultValue)
43 | is Char -> intent.getCharExtra(key, defaultValue)
44 | is Double -> intent.getDoubleExtra(key, defaultValue)
45 | is Float -> intent.getFloatExtra(key, defaultValue)
46 | is Int -> intent.getIntExtra(key, defaultValue)
47 | is Long -> intent.getLongExtra(key, defaultValue)
48 | is Short -> intent.getShortExtra(key, defaultValue)
49 | is CharSequence -> intent.getStringExtra(key)
50 |
51 | else -> throw IllegalArgumentException(
52 | "Illegal value type ${defaultValue.javaClass} for key \"$key\""
53 | )
54 | } as T?
55 | }
56 | }
57 |
58 | /**
59 | * @author skydoves (Jaewoong Eum)
60 | *
61 | * Retrieves a references type of extended data from the Intent immediately.
62 | *
63 | * @param key The name of the desired item.
64 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
65 | *
66 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
67 | */
68 | @JvmSynthetic
69 | @InlineBundleDsl
70 | inline fun Activity.bundleValue(
71 | key: String,
72 | crossinline defaultValue: () -> T? = { null }
73 | ): T? {
74 | val objectType = T::class.javaObjectType
75 | return activityTypedBundlerValue(defaultValue) {
76 | when {
77 | // references
78 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as? T
79 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(
80 | key
81 | ) as? T
82 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
83 | key
84 | ) as? T
85 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
86 | key
87 | ) as? T
88 |
89 | // scalar arrays
90 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
91 | key
92 | ) as? T
93 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as? T
94 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as? T
95 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as? T
96 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as? T
97 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as? T
98 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as? T
99 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as? T
100 |
101 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
102 | }
103 | }
104 | }
105 |
106 | /**
107 | * @author skydoves (Jaewoong Eum)
108 | *
109 | * Retrieves a non-null references type of extended data from the Intent immediately.
110 | *
111 | * @param key The name of the desired item.
112 | *
113 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle] or null.
114 | * @exception NullPointerException When there is no desired value from the Intent.
115 | */
116 | @JvmSynthetic
117 | @InlineBundleDsl
118 | inline fun Activity.bundleNonNullValue(
119 | key: String
120 | ): T {
121 | val objectType = T::class.javaObjectType
122 | return activityNonNullTypedBundlerValue {
123 | when {
124 | // references
125 | Bundle::class.java.isAssignableFrom(objectType) -> intent.getBundleExtra(key) as T
126 | CharSequence::class.java.isAssignableFrom(objectType) -> intent.getCharSequenceExtra(key) as T
127 | Parcelable::class.java.isAssignableFrom(objectType) -> intent.getParcelableExtra(
128 | key
129 | ) as T
130 | Serializable::class.java.isAssignableFrom(objectType) -> intent.getSerializableExtra(
131 | key
132 | ) as T
133 |
134 | // scalar arrays
135 | BooleanArray::class.java.isAssignableFrom(objectType) -> intent.getBooleanArrayExtra(
136 | key
137 | ) as T
138 | ByteArray::class.java.isAssignableFrom(objectType) -> intent.getByteArrayExtra(key) as T
139 | CharArray::class.java.isAssignableFrom(objectType) -> intent.getCharArrayExtra(key) as T
140 | DoubleArray::class.java.isAssignableFrom(objectType) -> intent.getDoubleArrayExtra(key) as T
141 | FloatArray::class.java.isAssignableFrom(objectType) -> intent.getFloatArrayExtra(key) as T
142 | IntArray::class.java.isAssignableFrom(objectType) -> intent.getIntArrayExtra(key) as T
143 | LongArray::class.java.isAssignableFrom(objectType) -> intent.getLongArrayExtra(key) as T
144 | ShortArray::class.java.isAssignableFrom(objectType) -> intent.getShortArrayExtra(key) as T
145 |
146 | else -> throw IllegalArgumentException("Illegal value type $objectType for key \"$key\"")
147 | }
148 | }
149 | }
150 |
151 | /**
152 | * @author skydoves (Jaewoong Eum)
153 | *
154 | * Retrieves a references array type of extended data from the Intent immediately.
155 | *
156 | * @param key The name of the desired item.
157 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
158 | *
159 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
160 | */
161 | @JvmSynthetic
162 | @InlineBundleDsl
163 | inline fun Activity.bundleArrayValue(
164 | key: String,
165 | crossinline defaultValue: () -> Array? = { null }
166 | ): Array? {
167 | val javaObjectType = T::class.javaObjectType
168 | return activityArrayBundlerValue(defaultValue) {
169 | (
170 | when {
171 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayExtra(key)
172 | CharSequence::class.java.isAssignableFrom(
173 | javaObjectType
174 | ) -> intent.getCharSequenceArrayExtra(key)
175 | Parcelable::class.java.isAssignableFrom(javaObjectType) -> intent.getParcelableArrayExtra(
176 | key
177 | )
178 |
179 | else -> throw IllegalArgumentException(
180 | "Illegal value type $javaObjectType for key \"$key\""
181 | )
182 | } as? Array<*>
183 | )?.filterIsInstance()?.toTypedArray()
184 | }
185 | }
186 |
187 | /**
188 | * @author skydoves (Jaewoong Eum)
189 | *
190 | * Retrieves a references array list type of extended data from the Intent immediately.
191 | *
192 | * @param key The name of the desired item.
193 | * @param defaultValue The value to be returned if no value of the desired type is stored with the given name.
194 | *
195 | * @throws IllegalArgumentException When a value is not a supported type of [Bundle].
196 | */
197 | @JvmSynthetic
198 | @InlineBundleDsl
199 | inline fun Activity.bundleArrayListValue(
200 | key: String,
201 | crossinline defaultValue: () -> ArrayList? = { null }
202 | ): ArrayList? {
203 | val javaObjectType = T::class.javaObjectType
204 | return activityArrayListBundlerValue(defaultValue) {
205 | when {
206 | String::class.java.isAssignableFrom(javaObjectType) -> intent.getStringArrayListExtra(key)
207 | CharSequence::class.java.isAssignableFrom(
208 | javaObjectType
209 | ) -> intent.getCharSequenceArrayListExtra(key)
210 | Parcelable::class.java.isAssignableFrom(
211 | javaObjectType
212 | ) -> intent.getParcelableArrayListExtra(key)
213 |
214 | else -> throw IllegalArgumentException("Illegal value type $javaObjectType for key \"$key\"")
215 | } as ArrayList?
216 | }
217 | }
218 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/bundler/src/test/java/com/skydoves/bundler/FragmentBundleLazyTest.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Designed and developed by 2020 skydoves (Jaewoong Eum)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.skydoves.bundler
18 |
19 | import android.os.Bundle
20 | import com.skydoves.bundler.data.Poster
21 | import com.skydoves.bundler.data.PosterSerializable
22 | import com.skydoves.bundler.data.UserInfo
23 | import org.hamcrest.MatcherAssert.assertThat
24 | import org.hamcrest.core.Is.`is`
25 | import org.junit.Test
26 | import org.junit.runner.RunWith
27 | import org.robolectric.RobolectricTestRunner
28 | import org.robolectric.annotation.Config
29 |
30 | @Config(sdk = [21])
31 | @RunWith(RobolectricTestRunner::class)
32 | class FragmentBundleLazyTest {
33 |
34 | @Test
35 | fun bundleLazyTest() {
36 | val fragment = TestFragment()
37 | fragment.arguments = intentOf {
38 | putExtra("boolean", true)
39 | putExtra("byte", Byte.MAX_VALUE)
40 | putExtra("char", 'a')
41 | putExtra("double", 1.23)
42 | putExtra("float", 1.23f)
43 | putExtra("int", 123)
44 | putExtra("long", 123L)
45 | putExtra("short", 123.toShort())
46 | putExtra("charSequence", "skydoves")
47 | }.extras
48 |
49 | val boolean by fragment.bundle("boolean", false)
50 | assertThat(boolean, `is`(true))
51 |
52 | val byte: Byte by fragment.bundle("byte", 0.toByte())
53 | assertThat(byte, `is`(Byte.MAX_VALUE))
54 |
55 | val char: Char by fragment.bundle("char", 'b')
56 | assertThat(char, `is`('a'))
57 |
58 | val double: Double by fragment.bundle("double", 0.0)
59 | assertThat(double, `is`(1.23))
60 |
61 | val float: Float by fragment.bundle("float", 0.0f)
62 | assertThat(float, `is`(1.23f))
63 |
64 | val int: Int by fragment.bundle("int", 0)
65 | assertThat(int, `is`(123))
66 |
67 | val long: Long by fragment.bundle("long", 0L)
68 | assertThat(long, `is`(123L))
69 |
70 | val short: Short by fragment.bundle("short", 0.toShort())
71 | assertThat(short, `is`(123.toShort()))
72 |
73 | val charSequence: String by fragment.bundle("charSequence", "")
74 | assertThat(charSequence, `is`("skydoves"))
75 | }
76 |
77 | @Test
78 | fun bundleLazyDefaultValueTest() {
79 | val fragment = TestFragment()
80 |
81 | val boolean by fragment.bundle("boolean", true)
82 | assertThat(boolean, `is`(true))
83 |
84 | val byte: Byte by fragment.bundle("byte", Byte.MAX_VALUE)
85 | assertThat(byte, `is`(Byte.MAX_VALUE))
86 |
87 | val char: Char by fragment.bundle("char", 'a')
88 | assertThat(char, `is`('a'))
89 |
90 | val double: Double by fragment.bundle("double", 1.23)
91 | assertThat(double, `is`(1.23))
92 |
93 | val float: Float by fragment.bundle("float", 1.23f)
94 | assertThat(float, `is`(1.23f))
95 |
96 | val int: Int by fragment.bundle("int", 123)
97 | assertThat(int, `is`(123))
98 |
99 | val long: Long by fragment.bundle("long", 123L)
100 | assertThat(long, `is`(123L))
101 |
102 | val short: Short by fragment.bundle("short", 123.toShort())
103 | assertThat(short, `is`(123.toShort()))
104 |
105 | val charSequence: String by fragment.bundle("charSequence", "skydoves")
106 | assertThat(charSequence, `is`("skydoves"))
107 | }
108 |
109 | @Test(expected = IllegalArgumentException::class)
110 | fun bundleLazyWrongTypeExceptionTest() {
111 | val mock = Poster.create()
112 | val fragment = TestFragment()
113 | fragment.arguments = intentOf {
114 | putExtra("poster", mock)
115 | }.extras
116 |
117 | val poster: Poster by fragment.bundle("poster", mock)
118 | poster.id
119 | }
120 |
121 | @Test
122 | fun bundleReferenceLazyTest() {
123 | val fragment = TestFragment()
124 | fragment.arguments = intentOf {
125 | val poster = Poster.create()
126 | val serializablePoster = PosterSerializable.create()
127 |
128 | putExtra("bundle", Bundle().apply { putString("bundleString", "skydoves") })
129 | putExtra("charSequence", "skydoves")
130 | putExtra("parcelable", poster)
131 | putExtra("serializable", serializablePoster)
132 | putExtra("booleanArray", booleanArrayOf(true, false, true))
133 | putExtra("byteArray", byteArrayOf(0.toByte(), 1.toByte(), 2.toByte()))
134 | putExtra("doubleArray", doubleArrayOf(0.0, 1.0, 2.0))
135 | putExtra("floatArray", floatArrayOf(0f, 1f, 2f))
136 | putExtra("intArray", intArrayOf(0, 1, 2))
137 | putExtra("longArray", longArrayOf(0L, 1L, 2L))
138 | putExtra("shortArray", shortArrayOf(0, 1, 2))
139 | }.extras
140 |
141 | val bundle: Bundle? by fragment.bundle("bundle")
142 | assertThat(bundle?.getString("bundleString"), `is`("skydoves"))
143 |
144 | val charSequence: CharSequence? by fragment.bundle("charSequence")
145 | assertThat(charSequence, `is`("skydoves"))
146 |
147 | val parcelable: Poster? by fragment.bundle("parcelable")
148 | assertThat(parcelable, `is`(Poster.create()))
149 |
150 | val serializable: PosterSerializable? by fragment.bundle("serializable")
151 | assertThat(serializable, `is`(PosterSerializable.create()))
152 |
153 | val booleanArray: BooleanArray? by fragment.bundle("booleanArray")
154 | assertThat(booleanArray, `is`(booleanArrayOf(true, false, true)))
155 |
156 | val byteArray: ByteArray? by fragment.bundle("byteArray")
157 | assertThat(byteArray, `is`(byteArrayOf(0.toByte(), 1.toByte(), 2.toByte())))
158 |
159 | val doubleArray: DoubleArray? by fragment.bundle("doubleArray")
160 | assertThat(doubleArray, `is`(doubleArrayOf(0.0, 1.0, 2.0)))
161 |
162 | val floatArray: FloatArray? by fragment.bundle("floatArray")
163 | assertThat(floatArray, `is`(floatArrayOf(0f, 1f, 2f)))
164 |
165 | val intArray: IntArray? by fragment.bundle("intArray")
166 | assertThat(intArray, `is`(intArrayOf(0, 1, 2)))
167 |
168 | val longArray: LongArray? by fragment.bundle("longArray")
169 | assertThat(longArray, `is`(longArrayOf(0L, 1L, 2L)))
170 |
171 | val shortArray: ShortArray? by fragment.bundle("shortArray")
172 | assertThat(shortArray, `is`(shortArrayOf(0, 1, 2)))
173 | }
174 |
175 | @Test
176 | fun bundleReferenceLazyDefaultValueTest() {
177 | val fragment = TestFragment()
178 | val poster = Poster.create()
179 | val serializablePoster = PosterSerializable.create()
180 |
181 | val bundle: Bundle? by fragment.bundle("bundle") { Bundle().apply { putString("bundleString", "skydoves") } }
182 | assertThat(bundle?.getString("bundleString"), `is`("skydoves"))
183 |
184 | val charSequence: CharSequence? by fragment.bundle("charSequence") { "skydoves" }
185 | assertThat(charSequence, `is`("skydoves"))
186 |
187 | val parcelable: Poster? by fragment.bundle("parcelable") { poster }
188 | assertThat(parcelable, `is`(Poster.create()))
189 |
190 | val serializable: PosterSerializable? by fragment.bundle("serializable") { serializablePoster }
191 | assertThat(serializable, `is`(PosterSerializable.create()))
192 |
193 | val booleanArray: BooleanArray? by fragment.bundle("booleanArray") { booleanArrayOf(true, false, true) }
194 | assertThat(booleanArray, `is`(booleanArrayOf(true, false, true)))
195 |
196 | val byteArray: ByteArray? by fragment.bundle("byteArray") { byteArrayOf(0.toByte(), 1.toByte(), 2.toByte()) }
197 | assertThat(byteArray, `is`(byteArrayOf(0.toByte(), 1.toByte(), 2.toByte())))
198 |
199 | val doubleArray: DoubleArray? by fragment.bundle("doubleArray") { doubleArrayOf(0.0, 1.0, 2.0) }
200 | assertThat(doubleArray, `is`(doubleArrayOf(0.0, 1.0, 2.0)))
201 |
202 | val floatArray: FloatArray? by fragment.bundle("floatArray") { floatArrayOf(0f, 1f, 2f) }
203 | assertThat(floatArray, `is`(floatArrayOf(0f, 1f, 2f)))
204 |
205 | val intArray: IntArray? by fragment.bundle("intArray") { intArrayOf(0, 1, 2) }
206 | assertThat(intArray, `is`(intArrayOf(0, 1, 2)))
207 |
208 | val longArray: LongArray? by fragment.bundle("longArray") { longArrayOf(0L, 1L, 2L) }
209 | assertThat(longArray, `is`(longArrayOf(0L, 1L, 2L)))
210 |
211 | val shortArray: ShortArray? by fragment.bundle("shortArray") { shortArrayOf(0, 1, 2) }
212 | assertThat(shortArray, `is`(shortArrayOf(0, 1, 2)))
213 | }
214 |
215 | @Test(expected = IllegalArgumentException::class)
216 | fun bundleReferenceLazyWrongTypeExceptionTest() {
217 | val fragment = TestFragment()
218 |
219 | val userInfo: UserInfo? by fragment.bundle("userInfo") { UserInfo.create() }
220 | userInfo?.nickname
221 | }
222 |
223 | @Test
224 | fun bundleNonNullReferenceLazyTest() {
225 | val fragment = TestFragment()
226 | fragment.arguments = intentOf {
227 | val poster = Poster.create()
228 | val serializablePoster = PosterSerializable.create()
229 |
230 | putExtra("bundle", Bundle().apply { putString("bundleString", "skydoves") })
231 | putExtra("charSequence", "skydoves")
232 | putExtra("parcelable", poster)
233 | putExtra("serializable", serializablePoster)
234 | putExtra("booleanArray", booleanArrayOf(true, false, true))
235 | putExtra("byteArray", byteArrayOf(0.toByte(), 1.toByte(), 2.toByte()))
236 | putExtra("doubleArray", doubleArrayOf(0.0, 1.0, 2.0))
237 | putExtra("floatArray", floatArrayOf(0f, 1f, 2f))
238 | putExtra("intArray", intArrayOf(0, 1, 2))
239 | putExtra("longArray", longArrayOf(0L, 1L, 2L))
240 | putExtra("shortArray", shortArrayOf(0, 1, 2))
241 | }.extras
242 |
243 | val bundle: Bundle by fragment.bundleNonNull("bundle")
244 | assertThat(bundle.getString("bundleString"), `is`("skydoves"))
245 |
246 | val charSequence: CharSequence by fragment.bundleNonNull("charSequence")
247 | assertThat(charSequence, `is`("skydoves"))
248 |
249 | val parcelable: Poster by fragment.bundleNonNull("parcelable")
250 | assertThat(parcelable, `is`(Poster.create()))
251 |
252 | val serializable: PosterSerializable by fragment.bundleNonNull("serializable")
253 | assertThat(serializable, `is`(PosterSerializable.create()))
254 |
255 | val booleanArray: BooleanArray by fragment.bundleNonNull("booleanArray")
256 | assertThat(booleanArray, `is`(booleanArrayOf(true, false, true)))
257 |
258 | val byteArray: ByteArray by fragment.bundleNonNull("byteArray")
259 | assertThat(byteArray, `is`(byteArrayOf(0.toByte(), 1.toByte(), 2.toByte())))
260 |
261 | val doubleArray: DoubleArray by fragment.bundleNonNull("doubleArray")
262 | assertThat(doubleArray, `is`(doubleArrayOf(0.0, 1.0, 2.0)))
263 |
264 | val floatArray: FloatArray by fragment.bundleNonNull("floatArray")
265 | assertThat(floatArray, `is`(floatArrayOf(0f, 1f, 2f)))
266 |
267 | val intArray: IntArray by fragment.bundleNonNull("intArray")
268 | assertThat(intArray, `is`(intArrayOf(0, 1, 2)))
269 |
270 | val longArray: LongArray by fragment.bundleNonNull("longArray")
271 | assertThat(longArray, `is`(longArrayOf(0L, 1L, 2L)))
272 |
273 | val shortArray: ShortArray by fragment.bundleNonNull("shortArray")
274 | assertThat(shortArray, `is`(shortArrayOf(0, 1, 2)))
275 | }
276 |
277 | @Test(expected = java.lang.IllegalArgumentException::class)
278 | fun bundleNonNullReferenceLazyWrongTypeExceptionTest() {
279 | val fragment = TestFragment()
280 |
281 | val userInfo: UserInfo by fragment.bundleNonNull("userInfo")
282 | userInfo.nickname
283 | }
284 |
285 | @Test(expected = NullPointerException::class)
286 | fun bundleNonNullReferenceLazyNoValueIncludedExceptionTest() {
287 | val fragment = TestFragment()
288 |
289 | val poster: Poster by fragment.bundleNonNull("poster")
290 | poster.name
291 | }
292 |
293 | @Test
294 | fun bundleArrayLazyTest() {
295 | val fragment = TestFragment()
296 | val poster = Poster.create()
297 | fragment.arguments = intentOf {
298 | putExtra("stringArray", arrayOf("skydoves0", "skydoves1", "skydoves2"))
299 | putExtra("charSequenceArray", arrayOf("skydoves0", "skydoves1", "skydoves2"))
300 | putExtra("parcelableArray", arrayOf(poster, poster, poster))
301 | }.extras
302 |
303 | val stringArray by fragment.bundleArray("stringArray")
304 | assertThat(stringArray?.size, `is`(3))
305 | assertThat(stringArray?.get(0), `is`("skydoves0"))
306 |
307 | val charSequenceArray by fragment.bundleArray("charSequenceArray")
308 | assertThat(charSequenceArray?.size, `is`(3))
309 | assertThat(charSequenceArray?.get(0), `is`("skydoves0"))
310 |
311 | val parcelableArray by fragment.bundleArray("parcelableArray")
312 | assertThat(parcelableArray?.size, `is`(3))
313 | assertThat(parcelableArray?.get(0), `is`(poster))
314 | }
315 |
316 | @Test
317 | fun bundleArrayLazyDefaultValueTest() {
318 | val fragment = TestFragment()
319 | val poster = Poster.create()
320 |
321 | val stringArray by fragment.bundleArray("stringArray") {
322 | arrayOf("skydoves0", "skydoves1", "skydoves2")
323 | }
324 | assertThat(stringArray?.size, `is`(3))
325 | assertThat(stringArray?.get(0), `is`("skydoves0"))
326 |
327 | val charSequenceArray by fragment.bundleArray("charSequenceArray") {
328 | arrayOf("skydoves0", "skydoves1", "skydoves2")
329 | }
330 | assertThat(charSequenceArray?.size, `is`(3))
331 | assertThat(charSequenceArray?.get(0), `is`("skydoves0"))
332 |
333 | val parcelableArray by fragment.bundleArray("parcelableArray") {
334 | arrayOf(poster, poster, poster)
335 | }
336 | assertThat(parcelableArray?.size, `is`(3))
337 | assertThat(parcelableArray?.get(0), `is`(poster))
338 | }
339 |
340 | @Test(expected = IllegalArgumentException::class)
341 | fun bundleArrayLazyWrongTypeExceptionTest() {
342 | val fragment = TestFragment()
343 |
344 | val userInfo by fragment.bundleArray("userInfo") { arrayOf(UserInfo.create()) }
345 | userInfo?.get(0)
346 | }
347 |
348 | @Test
349 | fun bundleArrayListLazyTest() {
350 | val fragment = TestFragment()
351 | val poster = Poster.create()
352 | fragment.arguments = intentOf {
353 | putExtra("stringArrayList", arrayListOf("skydoves0", "skydoves1", "skydoves2"))
354 | putExtra("charSequenceArrayList", arrayListOf("skydoves0", "skydoves1", "skydoves2"))
355 | putExtra("parcelableArrayList", arrayListOf(poster, poster, poster))
356 | }.extras
357 |
358 | val stringArrayList by fragment.bundleArrayList("stringArrayList")
359 | assertThat(stringArrayList?.size, `is`(3))
360 | assertThat(stringArrayList?.get(0), `is`("skydoves0"))
361 |
362 | val charSequenceArrayList by fragment.bundleArrayList("charSequenceArrayList")
363 | assertThat(charSequenceArrayList?.size, `is`(3))
364 | assertThat(charSequenceArrayList?.get(0), `is`("skydoves0"))
365 |
366 | val parcelableArrayList by fragment.bundleArrayList("parcelableArrayList")
367 | assertThat(parcelableArrayList?.size, `is`(3))
368 | assertThat(parcelableArrayList?.get(0), `is`(poster))
369 | }
370 |
371 | @Test
372 | fun bundleArrayLsitLazyDefaultValueTest() {
373 | val fragment = TestFragment()
374 | val poster = Poster.create()
375 |
376 | val stringArrayList by fragment.bundleArrayList("stringArrayList") {
377 | arrayListOf("skydoves0", "skydoves1", "skydoves2")
378 | }
379 | assertThat(stringArrayList?.size, `is`(3))
380 | assertThat(stringArrayList?.get(0), `is`("skydoves0"))
381 |
382 | val charSequenceArrayList by fragment.bundleArrayList("charSequenceArrayList") {
383 | arrayListOf("skydoves0", "skydoves1", "skydoves2")
384 | }
385 | assertThat(charSequenceArrayList?.size, `is`(3))
386 | assertThat(charSequenceArrayList?.get(0), `is`("skydoves0"))
387 |
388 | val parcelableArrayList by fragment.bundleArrayList("parcelableArrayList") {
389 | arrayListOf(poster, poster, poster)
390 | }
391 | assertThat(parcelableArrayList?.size, `is`(3))
392 | assertThat(parcelableArrayList?.get(0), `is`(poster))
393 | }
394 |
395 | @Test(expected = IllegalArgumentException::class)
396 | fun bundleArrayListLazyWrongTypeExceptionTest() {
397 | val fragment = TestFragment()
398 |
399 | val userInfo by fragment.bundleArrayList("userInfo") { arrayListOf(UserInfo.create()) }
400 | userInfo?.get(0)
401 | }
402 | }
403 |
--------------------------------------------------------------------------------