Skip to content

Instantly share code, notes, and snippets.

@herrbert74
Created May 6, 2022 19:31
Show Gist options
  • Save herrbert74/509fad657708a8e7bb7edfbdb3162605 to your computer and use it in GitHub Desktop.
Save herrbert74/509fad657708a8e7bb7edfbdb3162605 to your computer and use it in GitHub Desktop.
MVIKotlin Store Interface
This IDEA live template generates a dummy Store 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 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.coroutines.CoroutineBootstrapper&#10;import com.arkivanov.mvikotlin.extensions.coroutines.CoroutineExecutor&#10;import $package$.$name$Store.Intent&#10;import $package$.$name$Store.Label&#10;import $package$.$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 : CoroutineBootstrapper&lt;Action&gt;() {&#10; }&#10;&#10; private class ExecutorImpl : CoroutineExecutor&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="package" expression="kotlinPackageName()" defaultValue="" alwaysStopAt="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 $package$.$name$.Intent&#10;import $package$.$name$.Label&#10;import $package$.$name$.State&#10;&#10;internal interface $name$ : 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="package" expression="kotlinPackageName()" defaultValue="" alwaysStopAt="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