One AlertDialog To Rule Them All

William Gouvea
ProAndroidDev
Published in
3 min readApr 23, 2021

--

One Ring to rule them all, and in the darkness get him suspended

After some time developing I can tell you that throughout the lifecycle of the whole product there will be a couple of features that expect to show a modal dialog. Until that nothing is wrong right? Once it compounds the UX mechanics of many design systems and products in the wild.

So sometimes a not harmful piece of code starts to replicate fast, appears in many places simultaneously, and creates a lot of antigens in your system.

Kotlin has brought a lot of good tools to help us out get rid of boilerplate, be meaningful in terms of abstraction, and also improve the maintainability of the code since it becomes easier to read and understand, decreasing cognitive penalty.

So our code needs to be generic enough to adapt over time, but also simple enough for anyone in the team to be able to read and understand it, afterwards, they are who gonna maintain and fix bugs on the solution, so those steps must be a team decision.

Material Design to the Rescue

Dialogs inform users about a task and can contain critical information, require decisions, or involve multiple tasks.

How?

In cognitive psychology, cognitive load refers to the total amount of mental effort being used in the working memory

The idea behind the below code is to make code more readable and decrease the cognitive load to new entrants and staff developers

Yet making a layer of abstraction could make our life better but we should avoid BDUF and YAGNI principles when taking the decision to move ahead and build such a component.

There are a couple of extension functions to provide readability and fluency to the code.

DialogThemeColor is an attempt to provide an easy way to programmatically overrides themes without huge modifications.

Usage of the DSL

We could go further and overload the options to receive xmls ids and even use data binding to hide the model.saveNotificationPreferences call

Conclusion

We can create some classes to help out removing impedance making the code documents themselves.

We also take a look at some Kotlin superpowers that allow making the day by the day of the whole team easier.

It’s noteworthy that with Jetpack Compose most of what is been shown here is practically obsolete.

Thanks for reading.

Checkout out the complete code in Github:
https://github.com/amsterdatech/AlertDialogDsl

References

https://developer.android.com/jetpack/compose/documentation

--

--

Dad, Husband, Curious, Pet Lover and also Android Specialist - Reverse Engineer Specialist - Senior Backend Developer.