Use coroutines

This commit is contained in:
PabloG02 2023-04-07 23:17:08 +02:00 committed by Billy Laws
parent c57d572936
commit 5c4973e141

View File

@ -28,7 +28,12 @@ import emu.skyline.databinding.AppDialogBinding
import emu.skyline.loader.LoaderResult import emu.skyline.loader.LoaderResult
import emu.skyline.provider.DocumentsProvider import emu.skyline.provider.DocumentsProvider
import emu.skyline.settings.SettingsActivity import emu.skyline.settings.SettingsActivity
import emu.skyline.utils.ZipUtils
import emu.skyline.utils.serializable import emu.skyline.utils.serializable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -64,9 +69,13 @@ class AppDialog : BottomSheetDialogFragment() {
val saveFolder = File(savesFolderRoot + item.titleId) val saveFolder = File(savesFolderRoot + item.titleId)
val inputZip = requireContext().contentResolver.openInputStream(uri) val inputZip = requireContext().contentResolver.openInputStream(uri)
if (inputZip != null) { if (inputZip != null) {
emu.skyline.utils.ZipUtils.Companion.unzip(inputZip, saveFolder) CoroutineScope(Dispatchers.IO).launch {
binding.deleteSave.isEnabled = true ZipUtils.unzip(inputZip, saveFolder)
binding.exportSave.isEnabled = true withContext(Dispatchers.Main){
binding.deleteSave.isEnabled = true
binding.exportSave.isEnabled = true
}
}
} }
} else { } else {
Snackbar.make(binding.root, "Zip file must have as name the TitleID of the game", Snackbar.LENGTH_LONG).show() Snackbar.make(binding.root, "Zip file must have as name the TitleID of the game", Snackbar.LENGTH_LONG).show()
@ -152,30 +161,32 @@ class AppDialog : BottomSheetDialogFragment() {
binding.exportSave.isEnabled = saveExists binding.exportSave.isEnabled = saveExists
binding.exportSave.setOnClickListener { binding.exportSave.setOnClickListener {
val saveFolder = File(saveFolderPath) CoroutineScope(Dispatchers.IO).launch {
//val outputZipFile = File.createTempFile("out", ".zip") val saveFolder = File(saveFolderPath)
val outputZipFile = File("$saveFolderPath.zip") //val outputZipFile = File.createTempFile("out", ".zip")
if (outputZipFile.exists()) outputZipFile.delete() val outputZipFile = File("$saveFolderPath.zip")
outputZipFile.createNewFile() if (outputZipFile.exists()) outputZipFile.delete()
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputZipFile))).use { zos -> outputZipFile.createNewFile()
saveFolder.walkTopDown().forEach { file -> ZipOutputStream(BufferedOutputStream(FileOutputStream(outputZipFile))).use { zos ->
val zipFileName = file.absolutePath.removePrefix(saveFolder.absolutePath).removePrefix("/") saveFolder.walkTopDown().forEach { file ->
if (zipFileName == "") return@forEach val zipFileName = file.absolutePath.removePrefix(saveFolder.absolutePath).removePrefix("/")
val entry = ZipEntry("$zipFileName${(if (file.isDirectory) "/" else "")}") if (zipFileName == "") return@forEach
zos.putNextEntry(entry) val entry = ZipEntry("$zipFileName${(if (file.isDirectory) "/" else "")}")
if (file.isFile) { zos.putNextEntry(entry)
file.inputStream().use { fis -> fis.copyTo(zos) } if (file.isFile) {
file.inputStream().use { fis -> fis.copyTo(zos) }
}
} }
} }
withContext(Dispatchers.Main) {
val file = DocumentFile.fromSingleUri(requireContext(), DocumentsContract.buildDocumentUri(DocumentsProvider.AUTHORITY, "${DocumentsProvider.ROOT_ID}/switch/nand/user/save/0000000000000000/00000000000000000000000000000001/${item.titleId}.zip"))!!
val intent = Intent(Intent.ACTION_SEND)
.setDataAndType(file.uri, "application/zip")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra(Intent.EXTRA_STREAM, file.uri)
startActivity(Intent.createChooser(intent, "Share save file"))
}
} }
val file = DocumentFile.fromSingleUri(requireContext(), DocumentsContract.buildDocumentUri(DocumentsProvider.AUTHORITY, "${DocumentsProvider.ROOT_ID}/switch/nand/user/save/0000000000000000/00000000000000000000000000000001/${item.titleId}.zip"))!!
val intent = Intent(Intent.ACTION_SEND)
.setDataAndType(file.uri, "application/zip")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra(Intent.EXTRA_STREAM, file.uri)
startActivity(Intent.createChooser(intent, "Share save file"))
//outputZipFile.deleteOnExit()
} }
binding.gameTitleId.setOnLongClickListener { binding.gameTitleId.setOnLongClickListener {