Skip to content

Commit

Permalink
Fix extra line feed in text content
Browse files Browse the repository at this point in the history
  • Loading branch information
shubertm committed Dec 4, 2023
1 parent a709241 commit 2de93c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.repo.NotesRepoHelper
import dev.arkbuilders.arkmemo.utils.listFiles
import dev.arkbuilders.arkmemo.utils.readLines
import java.nio.file.Path
import javax.inject.Inject
import javax.inject.Named
import kotlin.io.path.extension
import kotlin.io.path.fileSize
import kotlin.io.path.forEachLine
import kotlin.io.path.getLastModifiedTime
import kotlin.io.path.name
import kotlin.io.path.writeLines
Expand Down Expand Up @@ -84,10 +84,6 @@ class TextNotesRepo @Inject constructor(

private suspend fun readStorage(): List<TextNote> = withContext(iODispatcher) {
root.listFiles(NOTE_EXT) { path ->
val data = StringBuilder()
path.forEachLine {
data.appendLine(it)
}
val size = path.fileSize()
val id = computeId(size, path)
val resource = Resource(
Expand All @@ -99,12 +95,14 @@ class TextNotesRepo @Inject constructor(

val userNoteProperties = helper.readProperties(id)

TextNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
text = data.toString(),
resource = resource
)
path.readLines { data ->
TextNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
text = data,
resource = resource
)
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.arkbuilders.arkmemo.ui.views.toast
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.extension
import kotlin.io.path.forEachLine
import kotlin.streams.toList

fun Fragment.observeSaveResult(result: LiveData<SaveNoteResult>) {
Expand Down Expand Up @@ -56,3 +57,11 @@ fun <R> Path.listFiles(extension: String, process: (Path) -> R): List<R> =
Files.list(this).toList().filter { it.extension == extension }.map {
process(it)
}

fun <R> Path.readLines(useLines: (String) -> R): R {
val dataBuilder = StringBuilder()
forEachLine {
dataBuilder.appendLine(it)
}
return useLines(dataBuilder.removeSuffix("\n").toString())
}

0 comments on commit 2de93c3

Please sign in to comment.