Skip to content

Instantly share code, notes, and snippets.

View arkivanov's full-sized avatar

Arkadii Ivanov arkivanov

View GitHub Profile
@arkivanov
arkivanov / Store factory
Last active November 10, 2022 19:03
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
@arkivanov
arkivanov / Navigator.kt
Last active August 4, 2023 12:29
Compose-like navigation with Decompose
@Composable
fun <T : Parcelable> Navigator(
initialConfiguration: () -> T,
configurationClass: KClass<out T>,
content: @Composable Router<T>.(T) -> Unit
) {
val lifecycle = getLifecycle()
val stateKeeper = getStateKeeper()
val context = remember { DefaultComponentContext(lifecycle = lifecycle, stateKeeper = stateKeeper) }
@arkivanov
arkivanov / DaggerParentChildExample.kt
Created October 14, 2020 15:50
Dagger parent-child example
// Deps
interface Database
interface Network
// Child
interface ChildRepository
class Child(dependencies: Dependencies) {
@arkivanov
arkivanov / UserFragment1.kt
Last active September 5, 2020 21:38
The defective AndroidX FragmentFactory - UserFragment1
class UserFragment : Fragment() {
private lateinit var userId: String
private lateinit var userRepository: UserRepository
}
interface UserRepository {
fun loadUser(id: String): User
}
@arkivanov
arkivanov / 1_FragmentIocIssue.kt
Last active August 29, 2022 17:26
Fragment IoC issue
//region UserFragment module
class UserFragment(dataSource: UserDataSource) : Fragment()
interface UserDataSource {
fun getUser(): User
}
//endregion UserFragment module
@arkivanov
arkivanov / KittenComponentTest4.kt
Created July 28, 2020 11:44
KittenComponentTest4
@Test
fun reloads_images_WHEN_Event_RefreshTriggered() {
dataSource.generateImages()
startComponent()
val newImages = dataSource.generateImages()
view.dispatch(Event.RefreshTriggered)
assertEquals(newImages, view.model.imageUrls)
}
@arkivanov
arkivanov / KittenComponentTest.kt
Last active August 29, 2022 17:26
KittenComponentTest3
@Test
fun loads_and_shows_images_WHEN_created() {
val images = dataSource.generateImages()
startComponent()
assertEquals(images, view.model.imageUrls)
}
@arkivanov
arkivanov / KittenComponentTest.kt
Created July 28, 2020 11:43
KittenComponentTest2
class KittenComponentTest {
private val dataSourceScheduler = TestScheduler()
private val dataSource = TestKittenDataSource(dataSourceScheduler)
private val view = TestKittenView()
private fun startComponent(): KittenComponent =
KittenComponent(dataSource).apply {
onViewCreated(view)
onStart()
}
@arkivanov
arkivanov / KittenComponentTest.kt
Created July 28, 2020 11:42
KittenComponentTest1
class KittenComponentTest {
private val dataSourceScheduler = TestScheduler()
private val dataSource = TestKittenDataSource(dataSourceScheduler)
private val view = TestKittenView()
private fun startComponent(): KittenComponent =
KittenComponent(dataSource).apply {
onViewCreated(view)
onStart()
}
@arkivanov
arkivanov / TestKittenView.kt
Created July 28, 2020 11:40
TestKittenView
class TestKittenView : AbstractMviView<Model, Event>(), KittenView {
lateinit var model: Model
override fun render(model: Model) {
this.model = model
}
}