Skip to content

Instantly share code, notes, and snippets.

@arkivanov
Last active November 10, 2022 19:03
Show Gist options
  • Save arkivanov/34bb84e73e56c22a4e7c752421d5f02c to your computer and use it in GitHub Desktop.
Save arkivanov/34bb84e73e56c22a4e7c752421d5f02c to your computer and use it in GitHub Desktop.
IDEA Live Templates for MVIKotlin Store creation
This IDEA live template generates a dummy Store factory. The name of the Store is
Installation:
1. Open IDEA preferences -> Editor -> Live Templates
2. Create a new group using the "+" button or select an existing group
3. Copy the content of the template below and paste it under the selected group (Ctrl+C/Ctrl+V)
Usage:
1. Create a new Kotlin file with the name "FooStoreFactory.kt", where "Foo" is the name of the Store
2. Type "mvisf" and press Enter
<template name="mvisf" value="import com.arkivanov.mvikotlin.core.store.Reducer&#10;import com.arkivanov.mvikotlin.core.store.Store&#10;import com.arkivanov.mvikotlin.core.store.StoreFactory&#10;import com.arkivanov.mvikotlin.extensions.reaktive.ReaktiveBootstrapper&#10;import com.arkivanov.mvikotlin.extensions.reaktive.ReaktiveExecutor&#10;import example.todo.common.main.$name$Store.Intent&#10;import example.todo.common.main.$name$Store.Label&#10;import example.todo.common.main.$name$Store.State&#10;&#10;internal class $name$StoreFactory(&#10; private val storeFactory: StoreFactory&#10;) {&#10;&#10; fun create(): $name$Store =&#10; object : $name$Store, Store&lt;Intent, State, Label&gt; by storeFactory.create(&#10; name = &quot;$name$Store&quot;,&#10; initialState = State(),&#10; bootstrapper = BootstrapperImpl(),&#10; executorFactory = ::ExecutorImpl,&#10; reducer = ReducerImpl&#10; ) {}&#10;&#10; private sealed interface Action {&#10; }&#10;&#10; private sealed interface Result {&#10; }&#10;&#10; private class BootstrapperImpl : ReaktiveBootstrapper&lt;Action&gt;() {&#10; }&#10;&#10; private class ExecutorImpl : ReaktiveExecutor&lt;Intent, Action, State, Result, Label&gt;() {&#10; }&#10;&#10; private object ReducerImpl : Reducer&lt;State, Result&gt; {&#10; override fun State.reduce(result: Result): State =&#10; when (result) {&#10; }&#10; }&#10;}&#10;" description="MVIKotlin Store factory" toReformat="true" toShortenFQNames="true">
<variable name="name" expression="groovyScript(&quot;if (_1.endsWith('StoreFactory')) return _1.substring(0, _1.length() - 12) else return _1&quot;, fileNameWithoutExtension())" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_TOPLEVEL" value="true" />
</context>
</template>
This IDEA live template generates a dummy Store interface.
Installation:
1. Open IDEA preferences -> Editor -> Live Templates
2. Create a new group using the "+" button or select an existing group
3. Copy the content of the template below and paste it under the selected group (Ctrl+C/Ctrl+V)
Usage:
1. Create a new Kotlin file with the required name
2. Type "mvisi" and press Enter
<template name="mvisi" value="import com.arkivanov.mvikotlin.core.store.Store&#10;import example.todo.common.main.$name$Store.Intent&#10;import example.todo.common.main.$name$Store.Label&#10;import example.todo.common.main.$name$Store.State&#10;&#10;internal interface $name$Store : Store&lt;Intent, State, Label&gt; {&#10;&#10; sealed interface Intent {&#10; }&#10;&#10; data class State(&#10; )&#10;&#10; sealed interface Label {&#10; }&#10;}&#10;" description="MVIKotlin Store interface" toReformat="true" toShortenFQNames="true">
<variable name="name" expression="fileNameWithoutExtension()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_TOPLEVEL" value="true" />
</context>
</template>
This IDEA live template generates both a dummy Store interface and its factory.
Installation:
1. Open IDEA preferences -> Editor -> Live Templates
2. Create a new group using the "+" button or select an existing group
3. Copy the content of the template below and paste it under the selected group (Ctrl+C/Ctrl+V)
Usage:
1. Create a new Kotlin file with the required name
2. Type "mvis" and press Enter
<template name="mvis" value="import com.arkivanov.mvikotlin.core.store.Reducer&#10;import com.arkivanov.mvikotlin.core.store.Store&#10;import com.arkivanov.mvikotlin.core.store.StoreFactory&#10;import com.arkivanov.mvikotlin.extensions.reaktive.ReaktiveBootstrapper&#10;import com.arkivanov.mvikotlin.extensions.reaktive.ReaktiveExecutor&#10;import example.todo.common.main.$name$Store.Intent&#10;import example.todo.common.main.$name$Store.Label&#10;import example.todo.common.main.$name$Store.State&#10;&#10;internal interface $name$Store : Store&lt;Intent, State, Label&gt; {&#10;&#10; sealed interface Intent {&#10; }&#10;&#10; data class State(&#10; )&#10;&#10; sealed interface Label {&#10; }&#10;}&#10;&#10;internal class SomeStoreFactory(&#10; private val storeFactory: StoreFactory&#10;) {&#10;&#10; fun create(): $name$Store =&#10; object : $name$Store, Store&lt;Intent, State, Label&gt; by storeFactory.create(&#10; name = &quot;$name$Store&quot;,&#10; initialState = State(),&#10; bootstrapper = BootstrapperImpl(),&#10; executorFactory = ::ExecutorImpl,&#10; reducer = ReducerImpl&#10; ) {}&#10;&#10; private sealed interface Action {&#10; }&#10;&#10; private sealed interface Result {&#10; }&#10;&#10; private class BootstrapperImpl : ReaktiveBootstrapper&lt;Action&gt;() {&#10; }&#10;&#10; private class ExecutorImpl : ReaktiveExecutor&lt;Intent, Action, State, Result, Label&gt;() {&#10; }&#10;&#10; private object ReducerImpl : Reducer&lt;State, Result&gt; {&#10; override fun State.reduce(result: Result): State =&#10; when (result) {&#10; }&#10; }&#10;}&#10;" description="MVIKotlin Store factory" toReformat="true" toShortenFQNames="true">
<variable name="name" expression="fileNameWithoutExtension()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_TOPLEVEL" value="true" />
</context>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment