Skip to content

Commit

Permalink
Merge pull request #152 from samontab/preserve-imported-notes-modific…
Browse files Browse the repository at this point in the history
…ation-date

Preserve modified date when importing notes with date info
  • Loading branch information
farmerbb committed Sep 14, 2024
2 parents a56a80a + feb20ca commit 396e56b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
17 changes: 12 additions & 5 deletions app/src/main/java/com/farmerbb/notepad/usecase/ArtVandelay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import org.koin.dsl.module

interface ArtVandelay {
fun importNotes(
saveImportedNote: (InputStream) -> Unit,
saveImportedNote: (InputStream, String) -> Unit,
onComplete: (Int) -> Unit
)

Expand All @@ -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)
Expand Down Expand Up @@ -97,13 +97,19 @@ private class ArtVandelayImpl(
private val registeredBaseDirs = mutableListOf<Uri>()

private fun importCallback(
saveImportedNote: (InputStream) -> Unit,
saveImportedNote: (InputStream, String) -> Unit,
onComplete: (Int) -> Unit
) = object: FileMultiSelectChooserCallback() {
) = object : FileMultiSelectChooserCallback() {
override fun onResult(uris: List<Uri>) {
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)
Expand All @@ -113,6 +119,7 @@ private class ArtVandelayImpl(
override fun onCancel(reason: String) = Unit // no-op
}


private fun exportFolderCallback(
hydratedNotes: List<Note>,
filenameFormat: FilenameFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 396e56b

Please sign in to comment.