- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Allow DSiWare data files to be imported and exported
1 parent
32b1721
commit 3374059
Showing
19 changed files
with
491 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/src/main/java/me/magnum/melonds/common/contracts/CreateFileContract.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package me.magnum.melonds.common.contracts | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import androidx.activity.result.contract.ActivityResultContract | ||
|
||
class CreateFileContract : ActivityResultContract<String, Uri?>() { | ||
override fun createIntent(context: Context, input: String): Intent { | ||
return Intent(Intent.ACTION_CREATE_DOCUMENT) | ||
.putExtra(Intent.EXTRA_TITLE, input) | ||
.addCategory(Intent.CATEGORY_OPENABLE) | ||
.setType("application/octet-stream") | ||
} | ||
|
||
override fun parseResult(resultCode: Int, intent: Intent?): Uri? { | ||
return if (intent == null || resultCode != Activity.RESULT_OK) { | ||
null | ||
} else { | ||
intent.data | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/src/main/java/me/magnum/melonds/domain/model/dsinand/DSiWareTitleFileType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.magnum.melonds.domain.model.dsinand | ||
|
||
enum class DSiWareTitleFileType(val fileName: String) { | ||
PUBLIC_SAV("public.sav"), | ||
PRIVATE_SAV("private.sav"), | ||
BANNER_SAV("banner.sav"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/me/magnum/melonds/ui/dsiwaremanager/model/DSiWareItemDropdownMenu.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.magnum.melonds.ui.dsiwaremanager.model | ||
|
||
enum class DSiWareItemDropdownMenu { | ||
NONE, | ||
MAIN, | ||
IMPORT, | ||
EXPORT, | ||
} |
8 changes: 8 additions & 0 deletions
8
.../main/java/me/magnum/melonds/ui/dsiwaremanager/model/ImportExportDSiWareTitleFileEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.magnum.melonds.ui.dsiwaremanager.model | ||
|
||
sealed class ImportExportDSiWareTitleFileEvent { | ||
data class ImportSuccess(val fileName: String) : ImportExportDSiWareTitleFileEvent() | ||
data object ImportError : ImportExportDSiWareTitleFileEvent() | ||
data class ExportSuccess(val fileName: String) : ImportExportDSiWareTitleFileEvent() | ||
data object ExportError : ImportExportDSiWareTitleFileEvent() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
app/src/main/java/me/magnum/melonds/ui/dsiwaremanager/ui/DSiWareTitleFileActions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package me.magnum.melonds.ui.dsiwaremanager.ui | ||
|
||
import android.net.Uri | ||
import androidx.activity.compose.ManagedActivityResultLauncher | ||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import me.magnum.melonds.common.Permission | ||
import me.magnum.melonds.common.contracts.CreateFileContract | ||
import me.magnum.melonds.common.contracts.FilePickerContract | ||
import me.magnum.melonds.domain.model.DSiWareTitle | ||
import me.magnum.melonds.domain.model.dsinand.DSiWareTitleFileType | ||
|
||
@Composable | ||
fun rememberDSiWareTitleImportFilePicker( | ||
onFilePicked: (title: DSiWareTitle, fileType: DSiWareTitleFileType, fileUri: Uri) -> Unit, | ||
): DSiWareTitleFilePickerLauncher { | ||
return rememberDSiWareTitleFilePicker(onFilePicked = onFilePicked, permission = Permission.READ) | ||
} | ||
|
||
@Composable | ||
fun rememberDSiWareTitleExportFilePicker( | ||
onFilePicked: (title: DSiWareTitle, fileType: DSiWareTitleFileType, fileUri: Uri) -> Unit, | ||
): DSiWareTitleNewFilePickerLauncher { | ||
return rememberDSiWareTitleNewFilePicker(onFilePicked) | ||
} | ||
|
||
@Composable | ||
private fun rememberDSiWareTitleFilePicker( | ||
onFilePicked: (title: DSiWareTitle, fileType: DSiWareTitleFileType, fileUri: Uri) -> Unit, | ||
permission: Permission, | ||
): DSiWareTitleFilePickerLauncher { | ||
val onFilePickedCallback = rememberUpdatedState(onFilePicked) | ||
val requestData = remember { | ||
DSiWareTitleFilePickerRequestData() | ||
} | ||
val filePickerLauncher = rememberLauncherForActivityResult( | ||
contract = FilePickerContract(permission), | ||
onResult = { | ||
if (it != null) { | ||
val title = requestData.currentTitle ?: return@rememberLauncherForActivityResult | ||
val fileType = requestData.currentFileType ?: return@rememberLauncherForActivityResult | ||
|
||
onFilePickedCallback.value(title, fileType, it) | ||
} | ||
requestData.currentTitle = null | ||
requestData.currentFileType = null | ||
}, | ||
) | ||
|
||
return remember { | ||
DSiWareTitleFilePickerLauncher( | ||
requestData, | ||
filePickerLauncher, | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun rememberDSiWareTitleNewFilePicker( | ||
onFilePicked: (title: DSiWareTitle, fileType: DSiWareTitleFileType, fileUri: Uri) -> Unit, | ||
): DSiWareTitleNewFilePickerLauncher { | ||
val onFilePickedCallback = rememberUpdatedState(onFilePicked) | ||
val requestData = remember { | ||
DSiWareTitleFilePickerRequestData() | ||
} | ||
val filePickerLauncher = rememberLauncherForActivityResult( | ||
contract = CreateFileContract(), | ||
onResult = { | ||
if (it != null) { | ||
val title = requestData.currentTitle ?: return@rememberLauncherForActivityResult | ||
val fileType = requestData.currentFileType ?: return@rememberLauncherForActivityResult | ||
|
||
onFilePickedCallback.value(title, fileType, it) | ||
} | ||
requestData.currentTitle = null | ||
requestData.currentFileType = null | ||
}, | ||
) | ||
|
||
return remember { | ||
DSiWareTitleNewFilePickerLauncher( | ||
requestData = requestData, | ||
filePickerLauncher = filePickerLauncher, | ||
) | ||
} | ||
} | ||
|
||
internal class DSiWareTitleFilePickerRequestData { | ||
var currentTitle: DSiWareTitle? = null | ||
var currentFileType: DSiWareTitleFileType? = null | ||
} | ||
|
||
class DSiWareTitleFilePickerLauncher internal constructor( | ||
private val requestData: DSiWareTitleFilePickerRequestData, | ||
private val filePickerLauncher: ManagedActivityResultLauncher<Pair<Uri?, Array<String>?>, Uri?>, | ||
) { | ||
|
||
fun launch(title: DSiWareTitle, fileType: DSiWareTitleFileType) { | ||
requestData.currentTitle = title | ||
requestData.currentFileType = fileType | ||
filePickerLauncher.launch(null to null) | ||
} | ||
} | ||
|
||
class DSiWareTitleNewFilePickerLauncher internal constructor( | ||
private val requestData: DSiWareTitleFilePickerRequestData, | ||
private val filePickerLauncher: ManagedActivityResultLauncher<String, Uri?>, | ||
) { | ||
|
||
fun launch(title: DSiWareTitle, fileType: DSiWareTitleFileType) { | ||
requestData.currentTitle = title | ||
requestData.currentFileType = fileType | ||
filePickerLauncher.launch(fileType.fileName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24" | ||
android:tint="@android:color/white"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters