DEV Community

Vladislav Kochetov
Vladislav Kochetov

Posted on

#kutilicious: Enhancing Your Kotlin & Android Development Experience

Introduction

In the world of development, efficiency and productivity are paramount. As developers, we strive to create apps that are not only functional but also easy to maintain and extend. To achieve this, we often find ourselves looking for ways to streamline our development process and reduce boilerplate code. The kutilicious is a simple but powerful library that provides a comprehensive set of Kotlin and Android extensions, designed to enhance your development experience and make your coding journey smoother.

As an Android developer, I understand the challenges of repetitive tasks and boilerplate code. That's why I created kutilicious. I collected frequently used code snippets from my own projects and packaged them into a convenient library. By leveraging kutilicious, you can save time and effort by reusing these commonly needed extensions, enabling you to focus on what truly matters—building amazing apps.

Source code: https://github.com/vladleesi/kutilicious

Why You Need kutilicious?

Imagine a scenario where you need to handle SharedPreferences in your Android app. Without kutilicious, you would have to write boilerplate code for editing and retrieving values, and ensure that your changes are committed or applied correctly. With kutilicious, however, you can simplify this process significantly. By using the editSync and editAsync extensions, you can edit SharedPreferences synchronously or asynchronously with just a few lines of code. This saves you time and effort, allowing you to focus on the core functionality of your app.

Examples of kutilicious in Action

kutilicious offers a wide range of extensions that can improve your development experience in various ways. Let's take a look at some examples from different blocks of the README:

Base Extensions:

// Returns the tag (simple name) of the class
val className = MyClass::class.tag()
// or
val className = tag()
Enter fullscreen mode Exit fullscreen mode

The tag() extension from the Base Extensions block allows you to easily retrieve the tag (simple name) of a class. This can be useful when working with logging or debugging.

Boolean Extensions:

if (isEnabled.orFalse()) {
    makeSomething()
}
Enter fullscreen mode Exit fullscreen mode

The orFalse() ensures that the isEnabled value is not null and provides a default value of false if it is.

val isDarkModeEnabled: Boolean? = true
// Execute code block if the Boolean value is true
isDarkModeEnabled.ifTrue {
    // Perform actions when enabled
    setTheme(R.style.DarkTheme)
}
Enter fullscreen mode Exit fullscreen mode

The ifTrue extension simplifies conditional logic based on a Boolean value. In this example, if isDarkModeEnabled is true, the code block will be executed, allowing you to apply the appropriate theme.

Integer Extensions:

val count: Int? = 10
val dividedByTwo: Int = count.orZero() / 2
Enter fullscreen mode Exit fullscreen mode

The orZero() extension ensures that the count value is not null and provides a default value of zero if it is. This allows you to perform calculations without worrying about nullability.

String Extensions:

// Convert the first letter of a string to uppercase
val formattedString = "hello".firstLetterUpperCase()

// Add a prefix to a string if it doesn't already start with it
val prefixedString = "world".addMissingPrefix("hello")
Enter fullscreen mode Exit fullscreen mode
val url: String = "https://www.example.com"
if (url.isUrl()) {
    val queryMap = url.getQueryMap()
    queryMap?.forEach { (key, value) ->
        Log.d("Query Parameter", "$key: $value")
    }
}
Enter fullscreen mode Exit fullscreen mode

The isUrl() extension allows you to check if a string is a valid URL. In this example, if the url is a valid URL, the getQueryMap() extension is used to extract and log the query parameters.

Android Preferences:

val sharedPreferences = context.getSharedPreferences("mySharedPreferences", Context.MODE_PRIVATE)
// Synchronously edit SharedPreferences
sharedPreferences.editSync {
    putString("key", "value")
    putInt("count", 5)
}
Enter fullscreen mode Exit fullscreen mode

The editSync extension simplifies editing SharedPreferences synchronously. In this example, the code block within editSync is used to store string and integer values in the SharedPreferences.

val sharedPreferences = context.getSharedPreferences("mySharedPreferences", Context.MODE_PRIVATE)

// Store a boolean value with the key "isDarkModeEnabled" asynchronously
sharedPreferences.putAsync("isDarkModeEnabled", true)

// Retrieve a boolean value with the key "isDarkModeEnabled", providing a default value of false
val isDarkModeEnabled = sharedPreferences.get("isDarkModeEnabled", false)
Enter fullscreen mode Exit fullscreen mode

Android View:

val view: View = findViewById(R.id.myView)
view.visible() // Sets the visibility of the view to VISIBLE
view.addRipple() // Adds a ripple effect to the background of the view
Enter fullscreen mode Exit fullscreen mode

The visible() extension allows you to easily set the visibility of a view to VISIBLE. Additionally, the addRipple() extension adds a ripple effect to the background of the view, enhancing its visual feedback.

You can find more examples in README file.

Conclusion

By incorporating kutilicious into your Android development workflow, you can enjoy a smoother and more efficient experience. The library's Kotlin and Android extensions empower you to write cleaner and more expressive code, reducing boilerplate and increasing productivity. Whether you need to simplify SharedPreferences handling, manipulate Views with ease, format and style text effortlessly, or perform other common tasks, kutilicious has got you covered.

As many developers have already discovered, kutilicious is a valuable tool that can significantly enhance your Android development journey. Don't take my word for it, try it out yourself and see the positive impact it can have on your projects. Join the growing community of developers who have found value in kutilicious by giving it a star on GitHub. Together, let's make Android development more enjoyable and efficient.

Top comments (0)