Skip to content

Commit 396e56b

Browse files
committed
Merge pull request #152 from samontab/preserve-imported-notes-modification-date
Preserve modified date when importing notes with date info
2 parents a56a80a + feb20ca commit 396e56b

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

app/src/main/java/com/farmerbb/notepad/usecase/ArtVandelay.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import org.koin.dsl.module
3737

3838
interface ArtVandelay {
3939
fun importNotes(
40-
saveImportedNote: (InputStream) -> Unit,
40+
saveImportedNote: (InputStream, String) -> Unit,
4141
onComplete: (Int) -> Unit
4242
)
4343

@@ -62,7 +62,7 @@ private class ArtVandelayImpl(
6262
private val fileManager: FileManager
6363
): ArtVandelay {
6464
override fun importNotes(
65-
saveImportedNote: (InputStream) -> Unit,
65+
saveImportedNote: (InputStream, String) -> Unit,
6666
onComplete: (Int) -> Unit
6767
) = fileChooser.openChooseMultiSelectFileDialog(
6868
importCallback(saveImportedNote, onComplete)
@@ -97,13 +97,19 @@ private class ArtVandelayImpl(
9797
private val registeredBaseDirs = mutableListOf<Uri>()
9898

9999
private fun importCallback(
100-
saveImportedNote: (InputStream) -> Unit,
100+
saveImportedNote: (InputStream, String) -> Unit,
101101
onComplete: (Int) -> Unit
102-
) = object: FileMultiSelectChooserCallback() {
102+
) = object : FileMultiSelectChooserCallback() {
103103
override fun onResult(uris: List<Uri>) {
104104
with(fileManager) {
105105
for (uri in uris) {
106-
fromUri(uri)?.let(::getInputStream)?.let(saveImportedNote)
106+
fromUri(uri)?.let { file ->
107+
val inputStream = getInputStream(file)
108+
val filename = file.getFullPath() // Extract filename from the file
109+
if (inputStream != null) {
110+
saveImportedNote(inputStream, filename)
111+
}
112+
}
107113
}
108114

109115
onComplete(uris.size)
@@ -113,6 +119,7 @@ private class ArtVandelayImpl(
113119
override fun onCancel(reason: String) = Unit // no-op
114120
}
115121

122+
116123
private fun exportFolderCallback(
117124
hydratedNotes: List<Note>,
118125
filenameFormat: FilenameFormat,

app/src/main/java/com/farmerbb/notepad/viewmodel/NotepadViewModel.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ import okio.source
6161
import org.koin.android.ext.koin.androidApplication
6262
import org.koin.androidx.viewmodel.dsl.viewModel
6363
import org.koin.dsl.module
64+
import java.text.SimpleDateFormat
65+
import java.util.Locale
66+
import java.util.Date
6467

6568
class NotepadViewModel(
6669
private val context: Application,
@@ -372,13 +375,36 @@ class NotepadViewModel(
372375
}
373376
}
374377

378+
379+
private fun parseDateFromFileName(filePath: String): Date? {
380+
// Extracting the filename from the full path
381+
val fileName = filePath.substring(filePath.lastIndexOf('/') + 1)
382+
383+
// Pattern to match the date in the filename
384+
val datePattern = Regex("\\d{4}-\\d{2}-\\d{2}-\\d{2}-\\d{2}")
385+
386+
// Trying to find the date in the filename
387+
val matchResult = datePattern.find(fileName)
388+
return matchResult?.value?.let { dateString ->
389+
try {
390+
SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.getDefault()).parse(dateString)
391+
} catch (e: Exception) {
392+
null // Return null if the date cannot be parsed
393+
}
394+
}
395+
}
396+
375397
private fun saveImportedNote(
376-
input: InputStream
398+
input: InputStream,
399+
filePath: String = ""
377400
) = viewModelScope.launch(Dispatchers.IO) {
378401
input.source().buffer().use {
379402
val text = it.readUtf8()
380403
if (text.isNotEmpty()) {
381-
repo.saveNote(text = text)
404+
val modifiedDate = parseDateFromFileName(filePath)
405+
//if the modifiedDate couldn't be parsed, use current date
406+
val nonNullModifiedDate: Date = modifiedDate ?: Date()
407+
repo.saveNote(text = text, date = nonNullModifiedDate)
382408
}
383409
}
384410
}

0 commit comments

Comments
 (0)