Change name of zip to {name} (v{version}) [{titleId}] - {saveTime}

This commit is contained in:
PabloG02 2023-04-19 14:30:17 +02:00 committed by Billy Laws
parent dae5469a61
commit e7cf7c9dae
2 changed files with 11 additions and 6 deletions

View File

@ -38,6 +38,8 @@ import java.io.BufferedInputStream
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream
@ -65,6 +67,7 @@ class AppDialog : BottomSheetDialogFragment() {
private val item by lazy { requireArguments().serializable<AppItem>(AppItemTag)!! } private val item by lazy { requireArguments().serializable<AppItem>(AppItemTag)!! }
private val savesFolderRoot by lazy { "${requireContext().getPublicFilesDir().canonicalPath}/switch/nand/user/save/0000000000000000/00000000000000000000000000000001/" } private val savesFolderRoot by lazy { "${requireContext().getPublicFilesDir().canonicalPath}/switch/nand/user/save/0000000000000000/00000000000000000000000000000001/" }
private var lastZipCreated : File? = null
private val documentPicker = registerForActivityResult(ActivityResultContracts.OpenDocument()) { private val documentPicker = registerForActivityResult(ActivityResultContracts.OpenDocument()) {
it?.let { uri -> it?.let { uri ->
try { try {
@ -85,7 +88,7 @@ class AppDialog : BottomSheetDialogFragment() {
val isSaveFileOfThisGame = File("$savesFolderRoot${item.titleId}").exists() val isSaveFileOfThisGame = File("$savesFolderRoot${item.titleId}").exists()
binding.deleteSave.isEnabled = isSaveFileOfThisGame binding.deleteSave.isEnabled = isSaveFileOfThisGame
binding.exportSave.isEnabled = isSaveFileOfThisGame binding.exportSave.isEnabled = isSaveFileOfThisGame
Snackbar.make(binding.root, "The save file was imported successfully", Snackbar.LENGTH_LONG).show() Snackbar.make(binding.root, R.string.save_file_imported_ok, Snackbar.LENGTH_LONG).show()
} }
} }
} else { } else {
@ -178,8 +181,8 @@ class AppDialog : BottomSheetDialogFragment() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
try { try {
val saveFolder = File(saveFolderPath) val saveFolder = File(saveFolderPath)
val outputZipFile = File("$savesFolderRoot${item.title}.zip") val outputZipFile = File("$savesFolderRoot${item.title} (v${binding.gameVersion.text}) [${item.titleId}] - ${LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))}.zip")
outputZipFile.delete() lastZipCreated?.delete()
outputZipFile.createNewFile() outputZipFile.createNewFile()
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputZipFile))).use { zos -> ZipOutputStream(BufferedOutputStream(FileOutputStream(outputZipFile))).use { zos ->
saveFolder.walkTopDown().forEach { file -> saveFolder.walkTopDown().forEach { file ->
@ -192,6 +195,7 @@ class AppDialog : BottomSheetDialogFragment() {
} }
} }
} }
lastZipCreated = outputZipFile
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Main){ withContext(Dispatchers.Main){
Snackbar.make(binding.root, e.message as CharSequence, Snackbar.LENGTH_LONG).show() Snackbar.make(binding.root, e.message as CharSequence, Snackbar.LENGTH_LONG).show()
@ -200,7 +204,7 @@ class AppDialog : BottomSheetDialogFragment() {
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
val file = DocumentFile.fromSingleUri(requireContext(), DocumentsContract.buildDocumentUri(DocumentsProvider.AUTHORITY, "${DocumentsProvider.ROOT_ID}/switch/nand/user/save/0000000000000000/00000000000000000000000000000001/${item.title}.zip"))!! 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) val intent = Intent(Intent.ACTION_SEND)
.setDataAndType(file.uri, "application/zip") .setDataAndType(file.uri, "application/zip")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
@ -229,6 +233,6 @@ class AppDialog : BottomSheetDialogFragment() {
override fun onDestroyView(){ override fun onDestroyView(){
super.onDestroyView() super.onDestroyView()
File("$savesFolderRoot${item.title}.zip").delete() lastZipCreated?.delete()
} }
} }

View File

@ -283,5 +283,6 @@
<string name="action_irreversible">This action is irreversible</string> <string name="action_irreversible">This action is irreversible</string>
<string name="yes">Yes</string> <string name="yes">Yes</string>
<string name="no">No</string> <string name="no">No</string>
<string name="save_file_invalid_zip_structure">Failed to unzip the provided save file</string> <string name="save_file_imported_ok">The save file was imported successfully</string>
<string name="save_file_invalid_zip_structure">Failed to unzip. First subfolder must be named after the TitleId of the game.</string>
</resources> </resources>