diff --git a/app/src/main/java/com/farmerbb/notepad/usecase/ArtVandelay.kt b/app/src/main/java/com/farmerbb/notepad/usecase/ArtVandelay.kt index 7a5faa4..f468c23 100644 --- a/app/src/main/java/com/farmerbb/notepad/usecase/ArtVandelay.kt +++ b/app/src/main/java/com/farmerbb/notepad/usecase/ArtVandelay.kt @@ -37,7 +37,7 @@ import org.koin.dsl.module interface ArtVandelay { fun importNotes( - saveImportedNote: (InputStream) -> Unit, + saveImportedNote: (InputStream, String) -> Unit, onComplete: (Int) -> Unit ) @@ -62,7 +62,7 @@ private class ArtVandelayImpl( private val fileManager: FileManager ): ArtVandelay { override fun importNotes( - saveImportedNote: (InputStream) -> Unit, + saveImportedNote: (InputStream, String) -> Unit, onComplete: (Int) -> Unit ) = fileChooser.openChooseMultiSelectFileDialog( importCallback(saveImportedNote, onComplete) @@ -97,13 +97,19 @@ private class ArtVandelayImpl( private val registeredBaseDirs = mutableListOf() private fun importCallback( - saveImportedNote: (InputStream) -> Unit, + saveImportedNote: (InputStream, String) -> Unit, onComplete: (Int) -> Unit - ) = object: FileMultiSelectChooserCallback() { + ) = object : FileMultiSelectChooserCallback() { override fun onResult(uris: List) { with(fileManager) { for (uri in uris) { - fromUri(uri)?.let(::getInputStream)?.let(saveImportedNote) + fromUri(uri)?.let { file -> + val inputStream = getInputStream(file) + val filename = file.getFullPath() // Extract filename from the file + if (inputStream != null) { + saveImportedNote(inputStream, filename) + } + } } onComplete(uris.size) @@ -113,6 +119,7 @@ private class ArtVandelayImpl( override fun onCancel(reason: String) = Unit // no-op } + private fun exportFolderCallback( hydratedNotes: List, filenameFormat: FilenameFormat, diff --git a/app/src/main/java/com/farmerbb/notepad/viewmodel/NotepadViewModel.kt b/app/src/main/java/com/farmerbb/notepad/viewmodel/NotepadViewModel.kt index 71b7cbc..9a6b43f 100644 --- a/app/src/main/java/com/farmerbb/notepad/viewmodel/NotepadViewModel.kt +++ b/app/src/main/java/com/farmerbb/notepad/viewmodel/NotepadViewModel.kt @@ -61,6 +61,9 @@ import okio.source import org.koin.android.ext.koin.androidApplication import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.dsl.module +import java.text.SimpleDateFormat +import java.util.Locale +import java.util.Date class NotepadViewModel( private val context: Application, @@ -372,13 +375,36 @@ class NotepadViewModel( } } + + private fun parseDateFromFileName(filePath: String): Date? { + // Extracting the filename from the full path + val fileName = filePath.substring(filePath.lastIndexOf('/') + 1) + + // Pattern to match the date in the filename + val datePattern = Regex("\\d{4}-\\d{2}-\\d{2}-\\d{2}-\\d{2}") + + // Trying to find the date in the filename + val matchResult = datePattern.find(fileName) + return matchResult?.value?.let { dateString -> + try { + SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.getDefault()).parse(dateString) + } catch (e: Exception) { + null // Return null if the date cannot be parsed + } + } + } + private fun saveImportedNote( - input: InputStream + input: InputStream, + filePath: String = "" ) = viewModelScope.launch(Dispatchers.IO) { input.source().buffer().use { val text = it.readUtf8() if (text.isNotEmpty()) { - repo.saveNote(text = text) + val modifiedDate = parseDateFromFileName(filePath) + //if the modifiedDate couldn't be parsed, use current date + val nonNullModifiedDate: Date = modifiedDate ?: Date() + repo.saveNote(text = text, date = nonNullModifiedDate) } } }