diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt index fd7fcc88..87f9a0e6 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt @@ -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 @@ -84,10 +84,6 @@ class TextNotesRepo @Inject constructor( private suspend fun readStorage(): List = 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( @@ -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 + ) + } } } } diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt b/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt index 0d72fa09..f2d5e95a 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt @@ -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) { @@ -56,3 +57,11 @@ fun Path.listFiles(extension: String, process: (Path) -> R): List = Files.list(this).toList().filter { it.extension == extension }.map { process(it) } + +fun Path.readLines(useLines: (String) -> R): R { + val dataBuilder = StringBuilder() + forEachLine { + dataBuilder.appendLine(it) + } + return useLines(dataBuilder.removeSuffix("\n").toString()) +} \ No newline at end of file