feat: Implement note export functionality

Implemented note export functionality, including a formatting function in the ViewModel and UI integration in MainActivity.
This commit is contained in:
2025-10-13 19:59:39 +02:00
parent 5bedc0f91d
commit c806f9ba49
2 changed files with 41 additions and 0 deletions

View File

@@ -153,6 +153,20 @@ fun AppShell(
}
)
val noteExportLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.CreateDocument("text/plain"),
onResult = { uri ->
uri?.let {
notesViewModel.noteDetails.value.toNote().let { note ->
val content = notesViewModel.formatNoteForExport(note)
context.contentResolver.openOutputStream(it)?.use { outputStream ->
outputStream.write(content.toByteArray())
}
}
}
}
)
ModalNavigationDrawer(
drawerState = drawerState,
drawerContent = {
@@ -296,6 +310,26 @@ fun AppShell(
}
)
}
} else if (currentScreen == Screen.NoteDetail) {
var showMenu by remember { mutableStateOf(false) }
IconButton(onClick = { showMenu = !showMenu }) {
Icon(Icons.Default.MoreVert, contentDescription = "More")
}
DropdownMenu(
expanded = showMenu,
onDismissRequest = { showMenu = false }
) {
DropdownMenuItem(
text = { Text(stringResource(R.string.export)) },
onClick = {
showMenu = false
notesViewModel.noteDetails.value.toNote().let { note ->
val fileName = "${note.title}.txt"
noteExportLauncher.launch(fileName)
}
}
)
}
}
},
colors = TopAppBarDefaults.topAppBarColors(

View File

@@ -100,6 +100,13 @@ class NotesViewModel(private val noteshopRepository: NoteshopRepository) : ViewM
}
}
fun formatNoteForExport(note: Note): String {
val builder = StringBuilder()
builder.appendLine("Title: ${note.title}")
builder.appendLine("Content: ${note.content}")
return builder.toString()
}
companion object {
private const val TIMEOUT_MILLIS = 5_000L
}