Basic functionality for export/import playlists#112
Basic functionality for export/import playlists#112m-tki wants to merge 1 commit intobrahmkshatriya:mainfrom
Conversation
brahmkshatriya
left a comment
There was a problem hiding this comment.
Thanks for the PR, I hope you can make the changes I requested
There was a problem hiding this comment.
Is this required, thought you could just use setOnPreferenceClickedListener or something
| private fun exportPlaylists(client: ExtensionClient) { | ||
| val context = preferenceManager.context | ||
| if (client is PlaylistEditClient) { | ||
| PickerActivity.openFolder(context) { uri -> | ||
| val directory = DocumentFile.fromTreeUri(context, uri) ?: run { | ||
| throw Exception("Failed to get directory from URI") | ||
| } | ||
|
|
||
| val file = directory.createFile("application/json", "playlists") ?: run { | ||
| throw Exception("Failed to create file") | ||
| } | ||
|
|
||
| viewModel.viewModelScope.launch { | ||
| try { | ||
| context.contentResolver.openOutputStream(file.uri)?.use { outputStream -> | ||
| outputStream.write(client.serializePlaylists().toByteArray()) | ||
| } | ||
| } catch (e: Exception) { | ||
| file.delete() | ||
| throw e | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun importPlaylists(client: ExtensionClient) { | ||
| val context = preferenceManager.context | ||
| if (client is PlaylistEditClient) { | ||
| PickerActivity.openFile(context) { uri -> | ||
| context.contentResolver.openInputStream(uri)?.use { inputStream -> | ||
| inputStream.bufferedReader().use { reader -> | ||
| val text = reader.readText() | ||
| viewModel.viewModelScope.launch { | ||
| client.deserializePlaylists(text) | ||
| } | ||
| } | ||
| } ?: throw Exception("Cannot open input stream for $uri") | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Please move all this code to the viewmodel itself, and instead of passing the client, use the extension from extensionFlow
| sealed interface PickerType { | ||
| object Folder : PickerType | ||
| object File : PickerType | ||
| } |
There was a problem hiding this comment.
Use an enum class instead
| this.callback = callback | ||
| this.pickerType = pickerType |
There was a problem hiding this comment.
you should put the picker type data inside a Bundle,
and the callback should removed
| private val pickerLauncher = registerForActivityResult( | ||
| ActivityResultContracts.StartActivityForResult() | ||
| ) { result -> | ||
| try { | ||
| when { | ||
| result.resultCode == RESULT_OK && result.data?.data != null -> { | ||
| callback?.onSelected(result.data!!.data!!) | ||
| } | ||
| else -> callback?.onCancelled() | ||
| } | ||
| } finally { | ||
| callback = null | ||
| pickerType = null | ||
| finish() | ||
| } | ||
| } |
There was a problem hiding this comment.
This should be created inside open function, and use runCatching instead of try, catch, finally
There was a problem hiding this comment.
I tried to put the code in open function but AI gives me the following warning
registerForActivityResult must be called during the component's initialization phase (before or during onCreate), not from a static context like the companion object. The current implementation will likely crash because we're trying to register for activity result from a non-activity context.
There was a problem hiding this comment.
Try using this
| override fun onDestroy() { | ||
| callback?.onCancelled() | ||
| callback = null | ||
| pickerType = null | ||
| super.onDestroy() | ||
| } |
There was a problem hiding this comment.
wont be required after the pickerLaucher changes
| screen.addPreference(infoPreference) | ||
|
|
||
| val client = extension.instance.value().getOrThrow() | ||
| if (extension.id == UnifiedExtension.metadata.id) { |
There was a problem hiding this comment.
Why limit it to unified extension?
| ) | ||
| screen.addPreference(infoPreference) | ||
|
|
||
| val client = extension.instance.value().getOrThrow() |
There was a problem hiding this comment.
Please remove, client is not needed here
This pr adds basic functionality for export/import playlists in Unified extension (export/import all playlists without selecting)