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

https://github.com/toxa2033/savedstatecompilerplugin

Kotlin compiler plugin generates support synthetic methods for use SaveStateHandle without constants and string variables.
https://github.com/toxa2033/savedstatecompilerplugin

compiler generated-code gradle-plugin gradle-plugin-kotlin idea-plugin inspection intellij-plugin kotlin kotlin-android kotlin-compiler-plugin kotlin-library savedstatehandle

Last synced: 5 months ago
JSON representation

Kotlin compiler plugin generates support synthetic methods for use SaveStateHandle without constants and string variables.

Awesome Lists containing this project

README

          

# Experimental not stable plugin

# SavedState Compiler Plugin

Kotlin compiler plugin generates support methods for use SaveStateHandle without constants and string variables.

# Example

If need to use SaveStateHandle, you must declare a variable, like that:
```kotlin
companion object {
private const val TEXT_FIELD = "text"
private const val PASSWORD_FIELD = "password"
}

val text: MutableLiveData = savedStateHandle.getLiveData(TEXT_FIELD, "Hello World")

val password: String
get() = savedStateHandle[PASSWORD_FIELD]
```

This plugin eliminates necessity declares a variable. Same example with plugin:

```kotlin
@SaveState
val text: MutableLiveData = getTextLiveData("Hello World")

@SaveState
val password: String
get() = gePasswordValue()

```

# How it use

1. Apply plugin to your module, sync it and you can use annotation `@SaveState`
2. Mark by this annotation fields witch you want to use with SavedStateHandle, and support methods generated in realtime.

**Importance:**
* The SaveStateHandle variable mast be declarated in class.
* As a key for SaveStateHandle plugin uses name of a annotated variable
* Supported variable types are listed [here](https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate#types). Also, all these types can be specified as a generic param in LiveData or MutableLiveData

## 4 generated methods

For example takes this variable

```kotlin
private val savedStateHandle: SavedStateHandle

@SaveState
val text: LiveData
```

### 1. getTextValue()
It method return value from SavedStateHandle. Inside it:
```kotlin
private fun getTextValue(): String? {
return savedStateHandle.get(key = "text")
}
```

### 2. getTextLiveData()
It method returns LiveData from SavedStateHandle. Inside it:
```kotlin
private fun getTextLiveData(default: String? = null): MutableLiveData {
return savedStateHandle.getLiveData(key = "text", initialValue = default)
}
```

### 3. getIdentifierText()
It method returns the key witch uses for set and get value in SavedStateHandle. Inside it:
```kotlin
private fun getIdentifierText(): String {
return "text"
}
```

### 4. setTextValue()
It method sets value in SavedStateHandle. Inside it:
```kotlin
private fun setTextValue(text: String?) {
savedStateHandle.set(key = "text", value = text)
}
```

## One more things
The plugin also allows:

1.Type checking.
Supports all types from the [documentation](https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate#types) and indicates an error if, for example, class did not implement parcelable

Снимок экрана 2022-05-01 в 15 21 35

2. Validates presence of a variable SavedStateHandle in a class. If class contain an annotated variable and the variable SavedStateHandle is not found in the class, then the plugin will notify you
Снимок экрана 2022-05-01 в 15 29 06

## Installation

- In project-level `build.gradle`:

```gradle
buildscript {
repositories {
mavenCentral()
// Or
gradlePluginPortal()
}
dependencies {
classpath "io.github.toxa2033.saved-state:gradle-plugin:1.0.5"
}
}
```

- In module-level `build.gradle`:

```gradle
// For each module that needs to use the annotations
apply plugin: 'io.github.toxa2033.saved-state'
//or
plugins {
id 'io.github.toxa2033.saved-state'
}
```

```gradle
dependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
}
```
### IDE Support

- Install the [IDEA plugin](https://plugins.jetbrains.com/plugin/19096-savedstatehandle-kotlin-compiler)
1. Launch the IDE and open plugin settings (Preferences -> Plugins -> Marketplace)
2. Search for "SavedStateHandle Kotlin Compiler" and click install

## Versions

| Kotlin Version | Plugin Version |
| :------------: | :------------: |
| 1.5.30 - 1.6.21 | 1.0.*

License
-------

Copyright (C) 2022 Toxa2033

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.