Repository: skyline-emu/skyline
File: app/src/main/java/emu/skyline/AppDialog.kt (Kotlin)

Code before:
            lastZipCreated = outputZipFile
        } catch (e : Exception) {
            return false
        }
        return true
    }

    private val startForResultExportSave = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { _ ->
        lastZipCreated?.delete()
    }

    /**
     * Exports the save file located in the given folder path by creating a zip file and sharing it via intent.
     * @param saveFolderPath The path to the folder containing the save file(s) to export.
     */
    private fun exportSave(saveFolderPath : String) {
        CoroutineScope(Dispatchers.IO).launch {
            if (!zipSave(saveFolderPath)) {
                withContext(Dispatchers.Main) {
                    Snackbar.make(binding.root, R.string.error, Snackbar.LENGTH_LONG).show()
                }
                return@launch
            }

            withContext(Dispatchers.Main) {
                val file = DocumentFile.fromSingleUri(requireContext(), DocumentsContract.buildDocumentUri(DocumentsProvider.AUTHORITY, "${DocumentsProvider.ROOT_ID}/switch/nand/user/save/0000000000000000/00000000000000000000000000000001/${lastZipCreated!!.name}"))!!
                val intent = Intent(Intent.ACTION_SEND)
                    .setDataAndType(file.uri, "application/zip")
                    .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                    .putExtra(Intent.EXTRA_STREAM, file.uri)
                startForResultExportSave.launch(Intent.createChooser(intent, "Share save file"))
            }
        }
    }

    /**
     * Imports the save files contained in the zip file, and replaces any existing ones with the new save file.
     * @param zipUri The Uri of the zip file containing the save file(s) to import.
     */
    private fun importSave(zipUri : Uri) {
        val inputZip = requireContext().contentResolver.openInputStream(zipUri)
        // A zip needs to have at least one subfolder named after a TitleId in order to be considered valid.
        var validZip = false
        val savesFolder = File(savesFolderRoot)
        val cacheSaveDir = File("${requireContext().cacheDir.path}/saves/")
        cacheSaveDir.mkdir()

        if (inputZip == null) {
            Snackbar.make(binding.root, getString(R.string.error), Snackbar.LENGTH_LONG).show()
            return
        }

Review comment (suggestion):
`lastZipCreated!!` doesn't look too good. Introduce a local variable of type `File` (non-nullable), where you store a temporary copy of `lastZipCreated`. You assign it above if `zipSave` returns `true`.

See the above suggestion, use `lastZipFile` in here instead of `lastZipCreated`.

Code after:
        }
        return true
    }

    private val startForResultExportSave = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        lastZipCreated?.delete()
    }

    /**
     * Exports the save file located in the given folder path by creating a zip file and sharing it via intent.
     * @param saveFolderPath The path to the folder containing the save file(s) to export.
     */
    private fun exportSave(saveFolderPath : String) {
        CoroutineScope(Dispatchers.IO).launch {
            val wasZipCreated = zipSave(saveFolderPath)
            val lastZipFile = lastZipCreated
            if (!wasZipCreated || lastZipFile == null) {
                withContext(Dispatchers.Main) {
                    Snackbar.make(binding.root, R.string.error, Snackbar.LENGTH_LONG).show()
                }
                return@launch
            }

            withContext(Dispatchers.Main) {
                val file = DocumentFile.fromSingleUri(requireContext(), DocumentsContract.buildDocumentUri(DocumentsProvider.AUTHORITY, "${DocumentsProvider.ROOT_ID}/switch/nand/user/save/0000000000000000/00000000000000000000000000000001/${lastZipFile.name}"))!!
                val intent = Intent(Intent.ACTION_SEND)
                    .setDataAndType(file.uri, "application/zip")
                    .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                    .putExtra(Intent.EXTRA_STREAM, file.uri)
                startForResultExportSave.launch(Intent.createChooser(intent, "Share save file"))
            }
        }
    }

    /**
     * Imports the save files contained in the zip file, and replaces any existing ones with the new save file.
     * @param zipUri The Uri of the zip file containing the save file(s) to import.
     */
    private fun importSave(zipUri : Uri) {
        val inputZip = requireContext().contentResolver.openInputStream(zipUri)
        // A zip needs to have at least one subfolder named after a TitleId in order to be considered valid.
        var validZip = false
        val savesFolder = File(savesFolderRoot)
        val cacheSaveDir = File("${requireContext().cacheDir.path}/saves/")
        cacheSaveDir.mkdir()

        if (inputZip == null) {
            Snackbar.make(binding.root, getString(R.string.error), Snackbar.LENGTH_LONG).show()
            return
        }