Skip to content

Commit

Permalink
saveModel integrated into dewitmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
vextorspace committed Jul 28, 2024
1 parent b16648f commit 5e189c4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composeApp/src/commonMain/kotlin/model/SaveItem.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package model

import kotlinx.serialization.Serializable
import viewmodel.ViewItem

@Serializable
data class SaveItem(val id: String, val subItems: List<SaveItem>) {
companion object {
fun from(item: Item): SaveItem {
fun from(item: ViewItem): SaveItem {
return SaveItem(item.id, item.subItems.map { from(it) })
}
}
Expand Down
13 changes: 9 additions & 4 deletions composeApp/src/commonMain/kotlin/model/SaveModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package model

import viewmodel.DewItViewModel

class SaveModel(viewModel: DewItViewModel) {
val items = viewModel.allItems()
val model = makeModel(viewModel)
class SaveModel(val viewModel: DewItViewModel) {
fun items(): List<Item> {
return viewModel.allItems()
}

fun model(): SaveItem {
return makeModel(viewModel)
}

private fun makeModel(viewModel: DewItViewModel): SaveItem {
return SaveItem.from(viewModel.item.item)
return SaveItem.from(viewModel.item)
}
}
8 changes: 8 additions & 0 deletions composeApp/src/commonMain/kotlin/viewmodel/DewItViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class DewItViewModel(val item: ViewItem = ViewItem()) {
return item.allItems()
}

fun findById(id: String): ViewItem? {
return allViewItems().firstOrNull { it.id == id }
}

fun allViewItems(): List<ViewItem> {
return item.allViewItems()
}

companion object {
val encoder = Json { ignoreUnknownKeys = true }

Expand Down
4 changes: 4 additions & 0 deletions composeApp/src/commonMain/kotlin/viewmodel/ViewItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ data class ViewItem(
fun allItems(): List<Item> {
return subItems.flatMap { it.allItems() } + item
}

fun allViewItems(): List<ViewItem> {
return subItems.flatMap { it.allViewItems() } + this
}
}
30 changes: 26 additions & 4 deletions composeApp/src/commonTest/kotlin/model/SaveModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,33 @@ class SaveModelTest {
val saveModel = viewModel.saveModel

// Then
saveModel.items.shouldContainExactlyInAnyOrder(viewModel.item.item, item1, item2, child1)
saveModel.items().shouldContainExactlyInAnyOrder(viewModel.item.item, item1, item2, child1)

saveModel.model().id shouldBe viewModel.item.item.id
saveModel.model().subItems.map { it.id } shouldBe listOf(item1.id, item2.id)
saveModel.model().subItems.filter { it.subItems.isNotEmpty() }.map { it.subItems.first().id } shouldBe listOf(child1.id)
}

@Test
fun `is correct even after changes to model`() {
// Given
val item1 = Item("Item 1",id="::ITEM 1")
val item2 = Item("Item 2", id="::ITEM 2")
val items = listOf(item1, item2)
val viewModel = DewItViewModel(items)
val saveModel = viewModel.saveModel

// When
val child1 = Item("Child 1")
val viewItem1 = viewModel.findById("::ITEM 1")!!
viewItem1.add(child1)
// Then
saveModel.items().shouldContainExactlyInAnyOrder(viewModel.item.item, item1, item2, child1)

saveModel.model().id shouldBe viewModel.item.item.id
saveModel.model().subItems.map { it.id } shouldBe listOf(item1.id, item2.id)
saveModel.model().subItems.filter { it.subItems.isNotEmpty() }.map { it.subItems.first().id } shouldBe listOf(child1.id)

saveModel.model.id shouldBe viewModel.item.item.id
saveModel.model.subItems.map { it.id } shouldBe listOf(item1.id, item2.id)
saveModel.model.subItems.filter { it.subItems.isNotEmpty() }.map { it.subItems.first().id } shouldBe listOf(child1.id)
}

}

0 comments on commit 5e189c4

Please sign in to comment.