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

Overview

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:

    companion object {
        private const val TEXT_FIELD = "text"
        private const val PASSWORD_FIELD = "password"
    }

    val text: MutableLiveData<String> = savedStateHandle.getLiveData(TEXT_FIELD, "Hello World")
    
    val password: String
        get() = savedStateHandle[PASSWORD_FIELD]

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

    @SaveState
    val text: MutableLiveData<String> = 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. Also, all these types can be specified as a generic param in LiveData or MutableLiveData

4 generated methods

For example takes this variable

    private val savedStateHandle: SavedStateHandle
    
    @SaveState
    val text: LiveData<String>

1. getTextValue()

It method return value from SavedStateHandle. Inside it:

  private fun getTextValue(): String? {
    return savedStateHandle.get<String>(key = "text")
  }

2. getTextLiveData()

It method returns LiveData from SavedStateHandle. Inside it:

  private fun getTextLiveData(default: String? = null): MutableLiveData<String> {
    return savedStateHandle.getLiveData<String>(key = "text", initialValue = default)
  }

3. getIdentifierText()

It method returns the key witch uses for set and get value in SavedStateHandle. Inside it:

  private fun getIdentifierText(): String {
    return "text"
  }

4. setTextValue()

It method sets value in SavedStateHandle. Inside it:

  private fun setTextValue(text: String?) {
    savedStateHandle.set<String?>(key = "text", value = text)
  }

One more things

The plugin also allows:

1.Type checking. Supports all types from the documentation and indicates an error if, for example, class did not implement parcelable

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

  1. 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:
buildscript {
    repositories {
        mavenCentral()
        // Or
        gradlePluginPortal()
    }
    dependencies {
        classpath "io.github.toxa2033.saved-state:gradle-plugin:1.0.5"
    }  
}
  • In module-level build.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'
}
dependencies {
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
}

IDE Support

  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.
You might also like...
IntelliJ-based IDEs Protobuf Language Plugin that provides Protobuf language support.
IntelliJ-based IDEs Protobuf Language Plugin that provides Protobuf language support.

IntelliJ Protobuf Language Plugin Reference Inspired by protobuf-jetbrains-plugin and intellij-protobuf-editor. Descriptor IntelliJ-based IDEs Protobu

✈️ IDE plugin for the IntelliJ platform which adds GitHub Copilot support. (VERY WIP)
✈️ IDE plugin for the IntelliJ platform which adds GitHub Copilot support. (VERY WIP)

JetBrains Copilot GitHub Copilot support for the IntellIJ Platform. Installation Download the latest release. Select the Install Plugin from Disk opti

A plugin that adds support for rs2asm files to IntelliJ.

Rs2Asm This plugin adds some simple features when interacting with rs2asm files. Features Syntax highlighting Autocompletion for label names and instr

A Candid language plugin that provide a complete support to efficiently edit .did files.

IntelliJ Candid Language Plugin A Candid language plugin that provide a complete support to efficiently edit .did files. Candid is an interface descri

BuildPlots-Plugin - PaperMC-Plugin for build contests written in Kotlin.

BuildPlotsPlugin PaperMC-Plugin for build contests. This is my first time using Kotlin and the first plugin I've written after a long time. It is stil

eventbus-intellij-plugin 3.8 0.0 L1 Java Plugin to navigate between events posted by EventBus.
eventbus-intellij-plugin 3.8 0.0 L1 Java Plugin to navigate between events posted by EventBus.

eventbus-intellij-plugin Plugin to navigate between events posted by EventBus. Post to onEvent and onEvent to Post Install There are two ways. Prefere

IntelliJ Idea Astor Plugin is a plugin that integrates Astor in Intellij Idea

IntelliJ Idea Astor Plugin IntelliJ Idea Astor Plugin is a plugin that integrates Astor in Intellij Idea. It communicates with a local/remote program

Intellij-platform-plugin-template - IntelliJ Platform Plugin Template
Intellij-platform-plugin-template - IntelliJ Platform Plugin Template

IntelliJ Platform Plugin Template TL;DR: Click the Use this template button and

K6-intellij-plugin - IntelliJ-based Plugin to run k6 tests locally or in the k6 Cloud from your IntelliJ IDE
K6-intellij-plugin - IntelliJ-based Plugin to run k6 tests locally or in the k6 Cloud from your IntelliJ IDE

IntelliJ-based Plugin to run k6 tests locally or in the k6 Cloud from your Intel

Releases(v1.0.5)
Owner
Anton Nazarov
Anton Nazarov
Android Studio plugin which automatically generates drawable selectors from appropriately named resources.

SelectorChapek for Android This Android Studio plugin automatically generates drawable selectors from appropriately named Android resources. How to in

Inmite s.r.o. 1.6k Dec 29, 2022
Social share - Social Sharing Plugin For Private Use only (Code without error checking)

Flutter Social Share plugin for sharing file to media with multiple (More option

MP IT SPACE 0 Mar 19, 2022
A Kotlin compiler plugin implementation of AutoService

auto-service-kt A Kotlin compiler plugin implementation of AutoService. Usage Simply add the auto-service-kt Gradle Plugin. plugins { id("dev.zacswe

Zac Sweers 5 Dec 29, 2022
Kotlin Native's Clang Compiler Plugin

Kotlin Native's Clang Compiler Plugin This plugin makes possible to use konon clang compiler. Support targets: Target Name Host compatibility linux_x6

null 3 May 8, 2022
UTBotJava generates test cases by code, trying to cover maximum statements and execution paths.

UTBotJava generates test cases by code, trying to cover maximum statements and execution paths. We treat source code as source of truth assuming that behavior is correct and corresponds to initial user demand. Generated tests are placed in so-called regression suite

null 60 Jan 3, 2023
gradle-android-scala-plugin adds scala language support to official gradle android plugin

gradle-android-scala-plugin gradle-android-scala-plugin adds scala language support to official gradle android plugin. See also sample projects at htt

saturday06 345 Dec 10, 2022
StringFuck - Yet Another String Obfuscator for Android

StringFuck Yet Another String Obfuscator for Android 一个字符串混淆器,旨在防止被jadx、MT等工具一键解

Nullptr 57 Dec 17, 2022
A webserver interface to the same methods and code that Cordova plugins install.

cordova-plugin-webserver Cordova plugin for localhost web server written in Kotlin and Ktor Install plugin cordova plugin add https://github.com/Qbix/

Qbix 0 May 5, 2022
A Gradle plugin to support the Groovy language for building Android apps

Groovy language support for Android Deprecated: This plugin has been deprecated in favor of Kotlin which has the full support of JetBrains and Google.

Groovy programming language 853 Dec 21, 2022
IntelliJ plugin that provides some useful utilities to support the daily work with Gradle.

IntelliJ Gradle Utilities Plugin This IntelliJ plugin provides some useful utilities to support the daily work with Gradle. It's available on the offi

Marcel Kliemannel 6 Jul 29, 2022