Skip to content

Instantly share code, notes, and snippets.

@Fbalashov
Last active December 6, 2022 12:08
Show Gist options
  • Save Fbalashov/405a21d11e0c76f65b688a5d48ed78a6 to your computer and use it in GitHub Desktop.
Save Fbalashov/405a21d11e0c76f65b688a5d48ed78a6 to your computer and use it in GitHub Desktop.
A property delegate to remove boilerplate when updating views

License

Oh Woe is Me! 😱

Are you an Android Developer, and tired of having to explicitly redraw your views every time you change a property? Are you looking for a more "Kotlin" approach to managing your views? Make use of the InvalidatesView delegate to automatically redraw your views when you set a property!

Without InvalidatesView With InvalidatesView

Usage

5 easy steps to get you started.

  1. Copy InvalidatesView.kt into your project.
  2. Update the package name at the top of InvalidatesView.kt.
  3. Identify the properties that should trigger an invalidation of the view when changed.
  4. Use InvalidatesView to initialize the value of those View properties.
  5. Make sure to pass in an intial value to InvalidatesView (Note that InvalidatesView cannot, and should not be used outside of a view!
(some View class)
var arcStyle: Int = 1
--->
var arcStyle: Int by InvalidatesView(1)

Be Aware

As per this discussion thread there are some costs to using this approach. Each field/property with a delegated property will create a new object. Evaluate whether InvalidatesView is the right option for you, based on how many instances of your view will exist, and how many delegated properties it has.

That's all folks! 🎉

Please leave comments with any feedback and "star" if this was helpful to you!

How it Works 🤔

InvalidatesView.kt is a Kotlin delegate. If you apply a delegate to a property, it will handle any gets or sets of the property's value. We are extending ReadWriteProperty to create our delegate for convenience. Delegates can also hold a reference to the object they are used in (that is the thisRef that you see in InvalidatesView. In the case of InvalidatesView we are using this to intercept the set'ing of the property's value and call thisRef.invalidate() to redreaw the view. Hungry for more Kotlin? Take a bite into this free and interactive course on kotlin.

Distributed under the Apache 2.0 license.

🎈 float free

package *****
import android.view.View
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* Copyright 6/24/2017 Fuad Balashov
*
* 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.
*/
class InvalidatesView<T>(var value: T) : ReadWriteProperty<View, T> {
override fun getValue(thisRef: View, property: KProperty<*>): T {
return value
}
override fun setValue(thisRef: View, property: KProperty<*>, value: T) {
if (this.value == value) {
return
}
this.value = value
thisRef.invalidate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment