Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/flavioarfaria/Catalog

Generate type-safe, user-friendly extensions to resolve Android resources.
https://github.com/flavioarfaria/Catalog

android gradle-plugin kotlin

Last synced: 9 days ago
JSON representation

Generate type-safe, user-friendly extensions to resolve Android resources.

Awesome Lists containing this project

README

        

# Catalog

_Just like View Binding, but for resources._

Catalog is a Gradle plugin that generates type-safe, user-friendly extensions to resolve Android resources.

Let's see how the following string resource gets resolved without and with Catalog:

```xml
Good morning, %1$s! It’s %2$d°C outside.
```

### Without Catalog:

Android resource resolution is overly verbose. It also uses snake case for resource identifiers,
which diverts away from Java/Kotlin naming conventions. In addition, parametrized strings and
plurals are not type safe. Android Studio comes with a very loose lint check for argument types,
but it treats all arguments as optional and it won't flag if you miss one.

Here's how we resolve string resources without Catalog:

### With Catalog:

image

You can also use Catalog to access the resource id directly:

image

Catalog also works with plurals:

```xml

You have %1$d unread message
You have %1$d unread messages

```

image

string arrays:

```xml

Spring
Summer
Fall
Winter

```

image

and simple color resources:

```xml
#FFFF0000
```

image

In the future, other resource types like integer arrays, dimensions, etc. will also be supported.

### Comment support

Resource comments are also carried over to extension properties and methods:

```xml

Catalog
```

### Compose support

If you're using compose, `@Composable` extensions will also be generated. In order to avoid
signature clashes, Compose extensions are extension methods of
`com.flaviofaria.catalog.runtime.compose.[String, Plurals, StringArray]` whereas standard extensions
are extension methods of `com.flaviofaria.catalog.runtime.resources.[String, Plurals, StringArray]`,
so make sure you're importing the right class.

## How it works

Catalog generates `Context` and `Fragment` extensions using [context receivers](https://blog.jetbrains.com/kotlin/2022/02/kotlin-1-6-20-m1-released/#prototype-of-context-receivers-for-kotlin-jvm).

Screen Shot 2022-09-27 at 11 14 14 PM

Since these extensions are `inline`, there will be no increase in your app method count or any
significant impact on runtime performance.

## Setup and configuration

To use Catalog, just apply the plugin to your module:

```groovy
plugins {
id 'com.flaviofaria.catalog' version '0.2.1'
}
```

By default, Catalog generates non-Compose extensions only. Compose extensions will also be generated
if it detects Compose among your module dependencies. If your project is 100% written in Compose,
you can explicitly turn off non-Compose extensions by adding:

```groovy
catalog {
generateResourcesExtensions = false
}
```

Similarly, you can also turn off Compose extensions:

```groovy
catalog {
generateComposeExtensions = false
}
```