Skip to content

Commit 4277a7e

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Change type fix (Writeopia#409)
* Fixing redo of type change of multiple stories * signature for bulk change * adding bulk changes --------- Co-authored-by: Leandro Ferreira <[email protected]>
1 parent dd04dc9 commit 4277a7e

File tree

4 files changed

+69
-27
lines changed

4 files changed

+69
-27
lines changed

writeopia/src/commonMain/kotlin/io/writeopia/sdk/manager/ContentHandler.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,38 @@ class ContentHandler(
8080
position: Int,
8181
commandInfo: CommandInfo?
8282
): StoryState {
83+
val newMap = changeType(currentStory, typeInfo, position, commandInfo)
84+
return StoryState(newMap, LastEdit.Whole, position)
85+
}
86+
87+
fun bulkChangeStoryType(
88+
currentStory: Map<Int, StoryStep>,
89+
change: Iterable<Pair<Int, TypeInfo>>
90+
): StoryState {
91+
val newMap = change.fold(currentStory) { acc, (position, typeInfo) ->
92+
changeType(
93+
currentStory = acc,
94+
typeInfo = typeInfo,
95+
position = position,
96+
commandInfo = null
97+
)
98+
}
99+
100+
return StoryState(newMap, LastEdit.Whole)
101+
}
102+
103+
private fun changeType(
104+
currentStory: Map<Int, StoryStep>,
105+
typeInfo: TypeInfo,
106+
position: Int,
107+
commandInfo: CommandInfo?
108+
): Map<Int, StoryStep> {
83109
val newMap = currentStory.toMutableMap()
84110
val storyStep = newMap[position]
85111
val commandTrigger = commandInfo?.commandTrigger
86112
val commandText = commandInfo?.command?.commandText?.trim() ?: ""
87113

88-
if (storyStep != null) {
114+
return if (storyStep != null) {
89115
val storyText = storyStep.text
90116
val newText = if (
91117
commandTrigger == CommandTrigger.WRITTEN &&
@@ -116,9 +142,10 @@ class ContentHandler(
116142
}
117143

118144
newMap[position] = newCheck
145+
newMap
146+
} else {
147+
currentStory
119148
}
120-
121-
return StoryState(newMap, LastEdit.Whole, position)
122149
}
123150

124151
private fun Set<TagInfo>.merge(tagInfo: Set<TagInfo>): Set<TagInfo> =

writeopia/src/commonMain/kotlin/io/writeopia/sdk/manager/WriteopiaManager.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,25 @@ class WriteopiaManager(
136136
fun changeStoryState(
137137
stateChange: Action.StoryStateChange,
138138
storyState: StoryState
139-
): StoryState? =
139+
): StoryState =
140140
contentHandler.changeStoryStepState(
141141
storyState.stories,
142142
stateChange.storyStep,
143143
stateChange.position
144-
)
144+
) ?: storyState
145+
146+
fun bulkChangeStoryState(
147+
storyState: StoryState,
148+
stateChange: Iterable<Action.StoryStateChange>,
149+
): StoryState {
150+
val mutable = storyState.stories.toMutableMap()
151+
152+
stateChange.forEach { change ->
153+
mutable[change.position] = change.storyStep
154+
}
155+
156+
return StoryState(mutable, lastEdit = LastEdit.Whole)
157+
}
145158

146159
/**
147160
* Changes the story type. The type of a messages changes without changing the content of it.
@@ -165,6 +178,11 @@ class WriteopiaManager(
165178
commandInfo
166179
)
167180

181+
fun bulkChangeStoryType(
182+
storyState: StoryState,
183+
change: Iterable<Pair<Int, TypeInfo>>
184+
): StoryState = contentHandler.bulkChangeStoryType(storyState.stories, change)
185+
168186
/**
169187
* Removes all tags from a story step
170188
*/

writeopia_ui/src/commonMain/kotlin/io/writeopia/ui/manager/WriteopiaStateManager.kt

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,48 +1021,45 @@ class WriteopiaStateManager(
10211021
private fun toggleStateForStories(onEdit: Set<Int>, storyTypes: StoryTypes) {
10221022
val currentStories = currentStory.value.stories
10231023

1024-
onEdit.map { position -> position to currentStories[position] }
1024+
trackState()
1025+
1026+
val change = onEdit.map { position -> position to currentStories[position] }
10251027
.filter { (_, story) -> story != null && !permanentTypes.contains(story.type.number) }
1026-
.forEach { (position, story) ->
1027-
story!!
1028-
val newType = if (story.type == storyTypes.type) {
1028+
.map { (position, story) ->
1029+
val newType = if (story?.type == storyTypes.type) {
10291030
StoryTypes.TEXT
10301031
} else {
10311032
storyTypes
10321033
}
10331034

1034-
changeStoryState(
1035-
Action.StoryStateChange(
1036-
storyStep = story.copy(type = newType.type),
1037-
position = position,
1038-
)
1039-
)
1035+
position to TypeInfo(newType.type)
10401036
}
1037+
1038+
_currentStory.value =
1039+
writeopiaManager.bulkChangeStoryType(_currentStory.value, change = change)
10411040
}
10421041

10431042
private fun toggleTagForStories(
10441043
onEdit: Set<Int>,
10451044
tag: TagInfo,
10461045
currentStories: Map<Int, StoryStep> = currentStory.value.stories
10471046
) {
1048-
onEdit.forEach { position ->
1049-
val story = currentStories[position]
1050-
if (story != null) {
1051-
val currentTags = story.tags
1047+
trackState()
1048+
1049+
val change = onEdit.map { position -> position to currentStories[position] }
1050+
.filter { (_, story) -> story != null && !permanentTypes.contains(story.type.number) }
1051+
.map { (position, story) ->
1052+
val currentTags = story!!.tags
10521053
val newTags = if (currentTags.contains(tag)) {
10531054
currentTags.filterNot { it.tag == tag.tag }.toSet()
10541055
} else {
10551056
currentTags + tag
10561057
}
10571058

1058-
changeStoryState(
1059-
Action.StoryStateChange(
1060-
storyStep = story.copy(tags = newTags),
1061-
position = position,
1062-
)
1063-
)
1059+
Action.StoryStateChange(story.copy(tags = newTags), position)
10641060
}
1065-
}
1061+
1062+
_currentStory.value = writeopiaManager.bulkChangeStoryState(_currentStory.value, change)
10661063
}
10671064

10681065
private fun changeCurrentStoryType(storyTypes: StoryTypes) {

writeopia_ui/src/commonTest/kotlin/io/writeopia/ui/manager/WriteopiaStateManagerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ class WriteopiaStateManagerTest {
523523
}
524524

525525
@Test
526-
@Ignore("This should be fixed later")
526+
@Ignore // This should be fixed later
527527
fun itShouldBePossibleToRevertMove() = runTest {
528528
val now = Clock.System.now()
529529

0 commit comments

Comments
 (0)