Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Kotlin Delegation by Inception

Delegating to delegates with a functional twist

 

 
TJ
Bloke.
Published: January 11, 2021
Tweet
Share
 

Android Matryoshka Dolls

 

I think Kotlin delegates are underused; they are the best implementation of the “favor composition over inheritance” refrain.

 

A class that is also a list by virtue of it having a reference to one

 

In the above, the ListContainer is a List that can be iterated through by delegating to the backing List within itself. Delegates are so much more than that however; fundamentally a Delegate allows for a property to have its read and/or write semantics implemented by an arbitrary bit of code. One of the most underutilized, and therefore the least learned from in my opinion, is the map() Delegate.

 

A class whose properties delegate to and therefore mutate the backing map passed to it

 

In the above, read/writes to the User instance are delegated to the backing map; changes in the fields are reflected in the map immediately. Although a neat example, most Android apps don’t have data blobs marshaled in maps, they have it in Bundles. A more useful Delegate Android wise therefore would be:

 

A Delegate for reading/writing from and to an Android Bundle

 

This would let you write the following expressions to let you read/write from a Bundle without having to declare extra string constants all over the place, the key is simply the property name:

 

 

The above is nice, but not very flexible as it’s an extension on the Bundletype itself. The more interesting usages of Bundles in Android tend to be via proxy; Intent extras, Activity deep link params and Fragment arguments all internally delegate to a Bundle instance. What would be really nice is if we could write a Delegate that itself delegated to something else that provides a type we already know how to delegate to; or delegation by inception as I like to call it:

 

A Delegate whose implementation delegates to another Delegate via a mapper transform

 

With the above, we can compose Delegates to arbitrarily read and write to any type, provided the type has a reference to another type that has a ready to use Delegate. So for Intents, Activities and Fragments we can write:

 

Delegates for various Android types that use Bundles to marshal data

 

With this Bundles become so much more convenient to work with:

 

Examples of Android Delegates that all rely on a Bundle

 

With this, a full User edit flow using the new FragmentResult API may look something like this:

 

Example integration flow of Bundle delegates

 

In the above, the edited user has their name set to “Blake”, wile keeping the existing user’s last name and age. Also in UserEditFragment, the user data will survive process death since it is stored in the arguments bundle; all this with no bundle.getParcelable(“propertyKey”), or fragment.arguments.getParcelable(“userKey”) in sight.

 

Image for post

We need to go deeper

 

Why stop there though? I wrote recently on how ViewBinding makes it really easy to express Views as a function of their State. In situations like that, it often is very helpful if the View could remember the last bit of state it was bound to; typically to memoize animations. Now all Android Viewinstances let you save arbitrary bits of data in them via their setTag and getTag methods with unique integer resource ids. If this is making you start thinking of a map like Delegate for a View that took full advantage of this, you’re in luck:

 

A delegate for a View to store arbitrary types via its tags

 

Much like with Bundles above, if we have any class that has a reference to a View, we can write delegates for it that internally delegate to it:

 

Delegates for a View, ViewBinding and even a RecyclerView ViewHolder

 

Again, just like before with Intents, Activities and Fragments; any ViewBinding or RecyclerView.ViewHolder instance can have any arbitrary property. This is especially useful for:

  1. RecyclerView.ViewHolder instances, because it means you don’t ever need to subclass it again; just add private var properties that delegate to the type you want. This is the whole foundation for declarative RecyclerViews covered here.
  2. ViewBinding or View instances needing a custom animator. In the below, a TextView has a custom ObjectAnimator tightly coupled with it without needing to subclass it.

 

Using a private delegate to strongly couple an ObjectAnimator with a TextView

 

The possibilities are quite endless. JSON deserialization is one of the most common things an app has to do and libraries often use reflection at runtime to help with it. With the right Delegate however, you could declare fields in a class and read the value from a common JSON type without the need for reflection.

In summary, Kotlin delegates are one of the best features of the language, and seem to be woefully underused. In your codebase there’s probably some utility function that does a transform that a Delegate is better suited for. Why not add replacing it with a custom Delegate to your New Year’s resolutions?

All the aforementioned Delegates are bundled (pun intended) in the following dependency, and more reading about how Kotlin Delegatesunder the hood can be found here.

 

tunjid/Android-Extensions

Install 1/2: Add this to pom.xml: Learn more about Maven or Gradle com.tunjid.androidx core 1.3.0-alpha01 Install 2/2…

github.com

 

 

 

Tags: Android, Kotlin, Delegation, Composition, Functional Programming

 

View original article at: 


 

Originally published: December 15, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One