Skip to content

Commit

Permalink
SNAPSHOT: second draft presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
carltonwhitehead committed May 31, 2024
1 parent 5e90d2e commit 44f6803
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 29 deletions.
4 changes: 4 additions & 0 deletions presentation/presentation-library-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<groupId>com.willowtreeapps.assertk</groupId>
<artifactId>assertk-jvm</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-test-jvm</artifactId>
</dependency>
</dependencies>

<build>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tech.coner.trailer.presentation.library.presenter

import tech.coner.trailer.presentation.library.adapter.LoadableItemAdapter
import tech.coner.trailer.presentation.library.model.BaseItemModel
import tech.coner.trailer.presentation.library.state.LoadableItem
import tech.coner.trailer.presentation.library.state.LoadableItemState
import tech.coner.trailer.toolkit.konstraints.CompositeConstraint

class FooPresenter : LoadableItemPresenter<
Unit,
FooState,
Foo,
Unit,
FooModel
>() {
override val argument = Unit
override val initialState = FooState(LoadableItem.Empty())
override val adapter = FooAdapter()
}

data class Foo(val value: Int)

data class FooState(override val loadable: LoadableItem<Foo>) : LoadableItemState<Foo>

class FooModel(override val original: Foo) : BaseItemModel<Foo, FooConstraint>() {
override val constraints = FooConstraint()
}

class FooConstraint : CompositeConstraint<Foo>() {

val valueIsInRange = propertyConstraint(
property = Foo::value,
assessFn = {
when (it.value) {
in 0..4 -> true
else -> false
}
},
violationMessageFn = { "Value is out of range" }
)
}

class FooAdapter : LoadableItemAdapter<
Unit,
FooState,
Foo,
Unit,
FooModel
>() {
override val argumentModelAdapter = null
override val partialItemAdapter = null
override val itemAdapter: (Foo) -> FooModel = ::FooModel
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tech.coner.trailer.presentation.library.presenter

import assertk.assertThat
import assertk.assertions.isEqualTo
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import tech.coner.trailer.presentation.library.model.LoadableModel

class LoadableItemPresenterTest {


@Test
fun itsModelFlowShouldBeAdaptedFromInitialState() = runTest {
val presenter = FooPresenter()

val actual = presenter.modelFlow.first()

assertThat(actual).isEqualTo(LoadableModel.Empty(null))
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
package tech.coner.trailer.presentation.library.testsupport

import assertk.Assert
import assertk.assertions.prop
import tech.coner.trailer.presentation.library.model.ItemModel
import tech.coner.trailer.presentation.library.presenter.LoadableItemPresenter
import tech.coner.trailer.presentation.library.state.LoadableItemState


fun <S : LoadableItemState<I>, I, IM : ItemModel<I>> Assert<LoadableItemPresenter<S, I, IM>>.itemModelFlow() = prop(LoadableItemPresenter<S, I, IM>::itemModelFlow)
fun <S : LoadableItemState<I>, I, IM : ItemModel<I>> Assert<LoadableItemPresenter<S, I, IM>>.itemModel() = prop(LoadableItemPresenter<S, I, IM>::itemModel)
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package tech.coner.trailer.presentation.library.presenter

import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import tech.coner.trailer.presentation.library.adapter.LoadableItemAdapter
import tech.coner.trailer.presentation.library.model.ItemModel
import tech.coner.trailer.presentation.library.model.LoadableModel
import tech.coner.trailer.presentation.library.state.LoadableItemState

abstract class LoadableItemPresenter<ARGUMENT, STATE, ITEM, ARGUMENT_MODEL, ITEM_MODEL>
abstract class LoadableItemPresenter<
ARGUMENT,
STATE,
ITEM,
ARGUMENT_MODEL,
ITEM_MODEL
>
where STATE : LoadableItemState<ITEM>,
ITEM_MODEL : ItemModel<ITEM> {

Expand All @@ -18,7 +28,8 @@ abstract class LoadableItemPresenter<ARGUMENT, STATE, ITEM, ARGUMENT_MODEL, ITEM
private val _stateFlow: MutableStateFlow<STATE> by lazy { MutableStateFlow(initialState) }
val stateFlow: StateFlow<STATE> by lazy { _stateFlow.asStateFlow() }

private val modelFlow: Flow<LoadableModel<ARGUMENT_MODEL, ITEM_MODEL>> by lazy {
stateFlow.map { state -> adapter(argument, state) }
val modelFlow: Flow<LoadableModel<ARGUMENT_MODEL, ITEM_MODEL>> by lazy {
stateFlow
.map { state -> adapter(argument, state) }
}
}

0 comments on commit 44f6803

Please sign in to comment.