Skip to content

Commit

Permalink
Discarting empty notes
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroBorgesFerreira committed Jul 4, 2023
1 parent f632d0a commit c911fc9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.github.leandroborgesferreira.storyteller.model.document.Document
import com.github.leandroborgesferreira.storyteller.model.story.DrawState
import com.github.leandroborgesferreira.storyteller.model.story.StoryState
import com.github.leandroborgesferreira.storyteller.persistence.repository.DocumentRepositoryImpl
import com.github.leandroborgesferreira.storyteller.utils.extensions.noContent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -90,8 +91,13 @@ class NoteDetailsViewModel(
}

fun saveNote() {
_documentState.value?.let { document ->
viewModelScope.launch {
viewModelScope.launch {
val stories = story.value.stories
val document = _documentState.value ?: return@launch

if (stories.noContent()) {
documentRepository.deleteDocument(document)
} else {
documentRepository.saveDocument(
document.copy(
content = story.value.stories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ fun <T> Iterable<T>.associateWithPosition(): Map<Int, T> {

return associateBy { ++acc }
}

fun Map<Int, StoryStep>.noContent(): Boolean =
this.values.any { storyStep ->
storyStep.run {
url.isNullOrBlank() &&
path.isNullOrBlank() &&
text.isNullOrBlank() &&
title.isNullOrBlank() &&
steps.isEmpty()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.leandroborgesferreira.storyteller.utils.extensions

import com.github.leandroborgesferreira.storyteller.model.story.StoryStep
import junit.framework.TestCase.assertTrue
import org.junit.Test

class StoryExtensionsKtTest {

@Test
fun `it should be able to recognize an empty document`() {
val storyStepMap = buildList {
repeat(5) {
add(it to StoryStep(type = "message"))
}
}.toMap()

assertTrue(storyStepMap.noContent())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class DocumentRepositoryImpl(
}
}

suspend fun deleteDocument(document: Document) {
documentDao.deleteDocuments(document.toEntity())
}

override suspend fun save(documentId: String, content: Map<Int, StoryStep>) {
storyUnitDao.deleteDocumentContent(documentId = documentId)
storyUnitDao.insertStoryUnits(*content.toEntity(documentId).toTypedArray())
Expand Down

0 comments on commit c911fc9

Please sign in to comment.