Skip to content

Instantly share code, notes, and snippets.

View CesarValiente's full-sized avatar
🏠
Working from home

Cesar Valiente Gordo CesarValiente

🏠
Working from home
View GitHub Profile
object TestStore : Store(
storeThread = null,
logger = { tag, message -> Log.d(tag, message) }) {
fun clear() {
sideEffects.clear()
stateHandlers.clear()
state = State()
}
}
@Test
fun should_query_by_localId_correctly() {
val item = createPersistenceItem(1)
item.insertOrUpdate(db)
val managedItem = db.queryByLocalId(item.localId)
assertThat(managedItem, iz(notNullValue()))
managedItem!!.assertIsEqualsTo(item)
}
@Test
fun should_reduceItemsCollection_when_CreateItemAction_and_empty_collection() {
val createItemAction = CreateItemAction(
localId = LOCAL_ID,
text = TEXT,
favorite = FAVORITE,
color = COLOR,
position = POSITION)
val itemsReduced = CreationReducer.reduceItemsCollection(
@Test
fun should_delete_Item_and_handle_State() {
val item1 = createItem(1)
val item2 = createItem(2)
val item3 = createItem(3)
val defaultList = listOf(item1, item2, item3)
val state = State(itemsListScreen = ItemsListScreen(defaultList),
navigation = Navigation.ITEMS_LIST)
store.dispatch(state)
import com.cesarvaliente.kunidirectional.persistence.Color as PersistenceColor
import com.cesarvaliente.kunidirectional.store.Color as StoreColor
fun StoreColor.toPersistenceColor(): PersistenceColor =
when (this) {
StoreColor.BLUE -> PersistenceColor.BLUE
StoreColor.GREEN -> PersistenceColor.GREEN
StoreColor.RED -> PersistenceColor.RED
StoreColor.WHITE -> PersistenceColor.WHITE
fun Color.toColorResource(): Int =
//We don't use Presentation models, we enrich the Store models using EF
when (this) {
RED -> R.color.red
YELLOW -> R.color.yellow
GREEN -> R.color.green
BLUE -> R.color.blue
WHITE -> R.color.white
}
@RealmClass
open class Item() : RealmModel {
constructor(localId: String, text: String?, favorite: Boolean = false,
colorEnum: Color = Color.WHITE, position: Long) : this() {
this.localId = localId
this.text = text
this.favorite = favorite
this.color = colorEnum.name
this.position = position
}
enum class Color {
RED, YELLOW, GREEN, BLUE, WHITE
}
data class Item(
val localId: String = generateLocalId(),
val text: String? = null,
val favorite: Boolean = false,
val color: Color = Color.WHITE,
val position: Long = object : PositionsFactory {}.newPosition()) {
dependencies {
compile project(':persistence')
compile project(':store')
........
}
class PersistenceSideEffect(val store: Store,
persistenceThread: ThreadExecutor? = null): SideEffect(persistenceThread) {
init {
store.sideEffects.add(this)
}
override fun handle(action: Action) {
when (action) {
is CreationAction -> CreationHandler.handle(action) { store.dispatch(it) }