Skip to content

Commit ccf6d43

Browse files
authored
Better propagate nullability for AttributeSet (#86)
These can be nullable everywhere except layout/xml inflation
1 parent 6bea1df commit ccf6d43

File tree

6 files changed

+19
-20
lines changed

6 files changed

+19
-20
lines changed

viewpump/src/main/java/io/github/inflationx/viewpump/FallbackViewCreator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import android.util.AttributeSet
55
import android.view.View
66

77
fun interface FallbackViewCreator {
8-
fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View?
8+
fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet?): View?
99
}

viewpump/src/main/java/io/github/inflationx/viewpump/InflateRequest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data class InflateRequest(
1010
@get:JvmName("context")
1111
val context: Context,
1212
@get:JvmName("attrs")
13-
val attrs: AttributeSet,
13+
val attrs: AttributeSet? = null,
1414
@get:JvmName("parent")
1515
val parent: View? = null,
1616
@get:JvmName("fallbackViewCreator")
@@ -61,7 +61,7 @@ data class InflateRequest(
6161
fun build() =
6262
InflateRequest(name = name ?: throw IllegalStateException("name == null"),
6363
context = context ?: throw IllegalStateException("context == null"),
64-
attrs = attrs ?: throw IllegalStateException("attrs == null"),
64+
attrs = attrs,
6565
parent = parent,
6666
fallbackViewCreator = fallbackViewCreator ?: throw IllegalStateException("fallbackViewCreator == null")
6767
)

viewpump/src/main/java/io/github/inflationx/viewpump/ViewPump.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ViewPump private constructor(
4444
* @param clazz The class of View to be created.
4545
* @return The processed view, which might not necessarily be the same type as clazz.
4646
*/
47-
fun create(context: Context, clazz: Class<out View>, attrs: AttributeSet): View? {
47+
fun create(context: Context, clazz: Class<out View>, attrs: AttributeSet?): View? {
4848
return inflate(InflateRequest(
4949
context = context,
5050
name = clazz.name,
@@ -204,7 +204,7 @@ class ViewPump private constructor(
204204
@Deprecated("Global singletons are bad for testing, scoping, and composition. Use local ViewPump instances instead.")
205205
@JvmName("staticCreateDeprecated")
206206
@JvmStatic
207-
fun create(context: Context, clazz: Class<out View>, attrs: AttributeSet): View? {
207+
fun create(context: Context, clazz: Class<out View>, attrs: AttributeSet?): View? {
208208
@Suppress("DEPRECATION_ERROR")
209209
return get()
210210
.inflate(InflateRequest(

viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ReflectiveFallbackViewCreator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class `-ReflectiveFallbackViewCreator` : FallbackViewCreator {
1717
}
1818

1919
override fun onCreateView(parent: View?, name: String, context: Context,
20-
attrs: AttributeSet): View? {
20+
attrs: AttributeSet?): View? {
2121
try {
2222
val clazz = Class.forName(name).asSubclass(View::class.java)
2323
var constructor: Constructor<out View>

viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpActivityFactory.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ internal interface `-ViewPumpActivityFactory` {
3535
* @see android.view.LayoutInflater.Factory2
3636
*/
3737
fun onActivityCreateView(parent: View?, view: View, name: String, context: Context,
38-
attrs: AttributeSet): View?
38+
attrs: AttributeSet?): View?
3939
}

viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpLayoutInflater.kt

+12-13
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ internal class `-ViewPumpLayoutInflater`(
4040
setUpLayoutFactories(cloned)
4141
}
4242

43-
4443
/**
4544
* We use this for internal cloning to be a little more efficient with memory.
4645
*/
@@ -147,7 +146,7 @@ internal class `-ViewPumpLayoutInflater`(
147146
view: View,
148147
name: String,
149148
context: Context,
150-
attrs: AttributeSet
149+
attrs: AttributeSet?
151150
): View? {
152151
return viewPump
153152
.inflate(
@@ -169,7 +168,7 @@ internal class `-ViewPumpLayoutInflater`(
169168
* BUT only for none CustomViews.
170169
*/
171170
@Throws(ClassNotFoundException::class)
172-
override fun onCreateView(parent: View?, name: String, attrs: AttributeSet): View? {
171+
override fun onCreateView(parent: View?, name: String, attrs: AttributeSet?): View? {
173172
return viewPump
174173
.inflate(
175174
InflateRequest(
@@ -189,7 +188,7 @@ internal class `-ViewPumpLayoutInflater`(
189188
* Basically if this method doesn't inflate the View nothing probably will.
190189
*/
191190
@Throws(ClassNotFoundException::class)
192-
override fun onCreateView(name: String, attrs: AttributeSet): View? {
191+
override fun onCreateView(name: String, attrs: AttributeSet?): View? {
193192
return viewPump
194193
.inflate(
195194
InflateRequest(
@@ -284,7 +283,7 @@ internal class `-ViewPumpLayoutInflater`(
284283
parent: View?,
285284
name: String,
286285
context: Context,
287-
attrs: AttributeSet
286+
attrs: AttributeSet?
288287
): View? {
289288
return inflater.createCustomViewInternal(view, name, context, attrs)
290289
}
@@ -296,7 +295,7 @@ internal class `-ViewPumpLayoutInflater`(
296295

297296
override fun onCreateView(
298297
parent: View?, name: String, context: Context,
299-
attrs: AttributeSet
298+
attrs: AttributeSet?
300299
): View? {
301300
return inflater.superOnCreateView(parent, name, attrs)
302301
}
@@ -310,7 +309,7 @@ internal class `-ViewPumpLayoutInflater`(
310309
parent: View?,
311310
name: String,
312311
context: Context,
313-
attrs: AttributeSet
312+
attrs: AttributeSet?
314313
): View? {
315314
// This mimics the {@code PhoneLayoutInflater} in the way it tries to inflate the base
316315
// classes, if this fails its pretty certain the app will fail at this point.
@@ -367,9 +366,9 @@ internal class `-ViewPumpLayoutInflater`(
367366
parent: View?,
368367
name: String,
369368
context: Context,
370-
attrs: AttributeSet
369+
attrs: AttributeSet?
371370
): View? {
372-
return factory.onCreateView(name, context, attrs)
371+
return attrs?.let { factory.onCreateView(name, context, it) }
373372
}
374373
}
375374

@@ -414,9 +413,9 @@ internal class `-ViewPumpLayoutInflater`(
414413
parent: View?,
415414
name: String,
416415
context: Context,
417-
attrs: AttributeSet
416+
attrs: AttributeSet?
418417
): View? {
419-
return factory2.onCreateView(parent, name, context, attrs)
418+
return attrs?.let { factory2.onCreateView(parent, name, context, it) }
420419
}
421420
}
422421

@@ -460,10 +459,10 @@ internal class `-ViewPumpLayoutInflater`(
460459
parent: View?,
461460
name: String,
462461
context: Context,
463-
attrs: AttributeSet
462+
attrs: AttributeSet?
464463
): View? {
465464
return inflater.createCustomViewInternal(
466-
factory2.onCreateView(parent, name, context, attrs), name, context, attrs
465+
factory2.onCreateView(parent, name, context, checkNotNull(attrs) { "Should never happen!" }), name, context, attrs
467466
)
468467
}
469468
}

0 commit comments

Comments
 (0)