Skip to content

Commit

Permalink
List games
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Jan 19, 2024
1 parent dc1064b commit b3a8bd3
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 89 deletions.
17 changes: 13 additions & 4 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<!-- required from API level 33 -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> <!-- To read images created by other apps -->
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" /> <!-- To read audios created by other apps -->
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /> <!-- To read vidoes created by other apps -->

<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />

<application
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:name="${applicationName}"
android:enableOnBackInvokedCallback="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down
23 changes: 13 additions & 10 deletions lib/app/(public)/config/edit_platform_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,12 @@ class _EditPlatformPageState extends State<EditPlatformPage> {
initialValue: beautifyPath(platform.folder),
readOnly: true,
onTap: () async {
final String? selectedDirectory =
await getDirectoryPath();
final selectedDirectory = await getDirectoryPath();

if (selectedDirectory != null) {
setState(() {
platform = platform.copyWith(
folder: Directory.fromUri(
Uri.directory(selectedDirectory)),
folder:selectedDirectory,
);
});
}
Expand All @@ -326,7 +324,12 @@ class _EditPlatformPageState extends State<EditPlatformPage> {
if (platform.id != -1)
FloatingActionButton(
heroTag: 'delete',
onPressed: () {},
onPressed: () {
deletePlatform(platform);
if (context.mounted) {
Routefly.pop(context);
}
},
child: const Icon(Icons.delete),
),
if (platform.id != -1) const Gap(17),
Expand All @@ -338,9 +341,9 @@ class _EditPlatformPageState extends State<EditPlatformPage> {
} else {
createPlatform(platform);
}
// if (context.mounted) {
// Routefly.pop(context);
// }
if (context.mounted) {
Routefly.pop(context);
}
},
child: const Icon(Icons.save),
),
Expand All @@ -349,8 +352,8 @@ class _EditPlatformPageState extends State<EditPlatformPage> {
);
}

String beautifyPath(Directory dir) {
final path = convertContentUriToFilePath(dir.path);
String beautifyPath(String dir) {
final path = convertContentUriToFilePath(dir);
return path.replaceAll('/storage/emulated/0', '');
}
}
7 changes: 2 additions & 5 deletions lib/app/(public)/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,8 @@ class _HomePageState extends State<HomePage> {
transitionAnimation: widget.transitionAnimation,
selected: selectedItemIndex == index,
onTap: () {
if (index == selectedItemIndex) {
openGame();
} else {
handlerSelect(index);
}
handlerSelect(index);
openGame();
},
index: index,
gamesLength: games.length,
Expand Down
2 changes: 2 additions & 0 deletions lib/app/data/repositories/apps/android_apps_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class AndroidAppsRepository implements AppsRepository {
action: intent.action,
package: intent.package,
componentName: intent.componentName,
data: intent.data,
type: intent.type,
arguments: intent.arguments,
flags: <int>[
flag.Flag.FLAG_ACTIVITY_NEW_TASK,
Expand Down
4 changes: 2 additions & 2 deletions lib/app/data/repositories/isar/adapters/platform_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class PlatformAdapter {

data.category = model.category.id;
data.games = model.games.map((e) => gameFromModel(e)).toList();
data.folder = model.folder.path;
data.folder = model.folder;
data.lastUpdate = DateTime.now();
data.playerPackageId = model.player?.app.package;
data.playerExtra = model.player?.extra;
Expand All @@ -45,7 +45,7 @@ abstract class PlatformAdapter {
static PlatformModel platformFromData(PlatformData model) {
return PlatformModel(
id: model.id,
folder: Directory.fromUri(Uri.directory(model.folder)),
folder: model.folder,
lastUpdate: model.lastUpdate,
category: categorieState.firstWhere((e) => e.id == model.category),
player: model.playerPackageId == null
Expand Down
39 changes: 24 additions & 15 deletions lib/app/interactor/actions/platform_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:media_store_plus/media_store_plus.dart';
import 'package:yuno/app/interactor/models/platform_model.dart';
import 'package:yuno/app/interactor/repositories/platform_repository.dart';
import 'package:yuno/injector.dart';
Expand Down Expand Up @@ -55,22 +56,29 @@ Future<List<Game>> _getGames(PlatformModel platform) async {
return platform.games;
}



final games = <Game>[];
final directory = Directory(convertContentUriToFilePath(platform.folder.path));
final files = directory //
.listSync()
.whereType<File>()
.where(platform.category.checkFileExtension)
final media = MediaStore();

final documents = await media.getDocumentTree(uriString: platform.folder);

if(documents == null) {
return [];
}


final files = documents //
.children
.where((doc){
return platform.category.checkFileExtension(doc.name ?? '');
})
.toList();

for (var file in files) {
final name = file.path.split('/').last;
games.add(Game(
name: name,
path: addFileInUri(
platform.folder.path,
name,
),
name: file.name ?? '',
path: file.uriString ?? '',
description: '',
image: '',
));
Expand All @@ -79,6 +87,11 @@ Future<List<Game>> _getGames(PlatformModel platform) async {
return games;
}

String cleanName(String name) {
final index = name.indexOf(RegExp(r'[.(\[]')) - 1;
return name.substring(0 , index <= 0 ? name.length : index).trim();
}

Future<void> updatePlatform(PlatformModel platform) async {
final repository = injector<PlatformRepository>();
await repository.updatePlatform(platform);
Expand All @@ -93,10 +106,6 @@ Future<void> deletePlatform(PlatformModel platform) async {

Future<void> syncPlatform(PlatformModel platform) async {}

String addFileInUri(String path, String file) {
String encoded = Uri.encodeComponent('/$file');
return '$path$encoded';
}

String convertContentUriToFilePath(String contentUri) {
Uri uri = Uri.parse(contentUri);
Expand Down
91 changes: 53 additions & 38 deletions lib/app/interactor/actions/player_action.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
import 'package:yuno/app/interactor/models/embeds/player.dart';

import '../models/embeds/game.dart';
import 'platform_action.dart';

typedef IntentFunction = PlayerIntent Function(Player, Game);

PlayerIntent? getAppIntent(Game game, Player player) {
return _defaultAppIntent[player.app.package]?.parse(player, game);
return _defaultAppIntent[player.app.package]?.call(player, game);
}

final _defaultAppIntent = <String, PlayerIntent>{
'xyz.aethersx2.android': PlayerIntent(
action: 'android.intent.action.VIEW',
package: 'xyz.aethersx2.android',
componentName: 'xyz.aethersx2.android.EmulationActivity',
arguments: {
'bootPath': r'${fileGame}',
},
),
'com.retroarch.aarch64': PlayerIntent(
action: 'android.intent.action.MAIN',
package: 'com.retroarch.aarch64',
componentName: 'com.retroarch.browser.retroactivity.RetroActivityFuture',
arguments: {
'ROM': r'${fileGame}',
'LIBRETRO':
r'/data/data/com.retroarch.aarch64/cores/${extra}_libretro_android.so',
},
),
'com.yuzu.android': PlayerIntent(
action: 'android.nfc.action.TECH_DISCOVERED',
package: 'com.yuzu.android',
componentName: 'com.yuzu.android.EmulationActivity',
data: r'${fileGame}',
),
'org.yuzu.yuzu_emu_ea': PlayerIntent(
action: 'android.nfc.action.TECH_DISCOVERED',
package: 'org.yuzu.yuzu_emu_ea',
componentName: 'com.yuzu.android.EmulationActivity',
data: r'${fileGame}',
),
'switch.skyline.emu': PlayerIntent(
action: 'android.intent.action.VIEW',
package: 'switch.skyline.emu',
componentName: 'switch.skyline.emu.EmulationActivity',
data: r'${fileGame}',
),
final _defaultAppIntent = <String, IntentFunction>{
'xyz.aethersx2.android': (p, g) {
return PlayerIntent(
action: 'android.intent.action.VIEW',
package: 'xyz.aethersx2.android',
componentName: 'xyz.aethersx2.android.EmulationActivity',
arguments: {
'bootPath': g.path,
},
);
},
'com.retroarch.aarch64': (p, g) {
return PlayerIntent(
action: 'android.intent.action.MAIN',
package: 'com.retroarch.aarch64',
componentName: 'com.retroarch.browser.retroactivity.RetroActivityFuture',
arguments: {
'ROM': convertContentUriToFilePath(g.path),
'LIBRETRO':
'/data/data/com.retroarch.aarch64/cores/${p.extra}_libretro_android.so',
},
);
},
'org.yuzu.yuzu_emu': (p, g) {
return PlayerIntent(
action: 'android.nfc.action.TECH_DISCOVERED',
package: 'org.yuzu.yuzu_emu',
type: 'application/octet-stream',
componentName: 'org.yuzu.yuzu_emu.activities.EmulationActivity',
data: g.path,
);
},
'org.yuzu.yuzu_emu.ea': (p, g) {
return PlayerIntent(
action: 'android.nfc.action.TECH_DISCOVERED',
package: 'org.yuzu.yuzu_emu.ea',
type: 'application/octet-stream',
componentName: 'org.yuzu.yuzu_emu.activities.EmulationActivity',
data: g.path,
);
},
'switch.skyline.emu': (p, g) {
return PlayerIntent(
action: 'android.intent.action.VIEW',
package: 'switch.skyline.emu',
componentName: 'switch.skyline.emu.EmulationActivity',
data: g.path,
);
},
};
1 change: 1 addition & 0 deletions lib/app/interactor/atoms/game_atom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ List<GameCategory> get categoriesFoSelectState {
final categorieState = <GameCategory>[
GameCategory(name: 'Android', image: img.androidSVG, id: 'android'),
GameCategory(name: 'Nintendo Switch', image: img.switchSVG, id: 'switch'),
GameCategory(name: 'Super Nintendo', image: img.switchSVG, id: 'snes'),
GameCategory(name: 'Playstation 1', image: img.ps1SVG, id: 'ps1'),
GameCategory(name: 'Playstation 2', image: img.ps2SVG, id: 'ps2'),
GameCategory(name: 'Playstation Portable', image: img.pspSVG, id: 'psp'),
Expand Down
2 changes: 1 addition & 1 deletion lib/app/interactor/models/embeds/game_category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GameCategory {
this.extensions = const [],
});

bool checkFileExtension(File element) => true;
bool checkFileExtension(String name) => true;

@override
bool operator ==(covariant GameCategory other) {
Expand Down
33 changes: 24 additions & 9 deletions lib/app/interactor/models/embeds/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class PlayerIntent {
final String package;
final String? componentName;
final String? data;
final String? type;
final Map<String, dynamic>? arguments;

PlayerIntent({
Expand All @@ -55,23 +56,37 @@ class PlayerIntent {
this.componentName,
this.data,
this.arguments,
this.type,
});

PlayerIntent parse(Player player, Game game) {
PlayerIntent parse({String? fileGameUri, String? fileGamePath, String? extra,}) {
if (arguments == null) {
return this;
}
var newArguments = replaceVariables(arguments, 'fileGame', game.path);
newArguments = replaceVariables(
newArguments,
'extra',
player.extra ?? '',
);
var newArguments = arguments;
var newData = data;


if(fileGameUri != null){
newArguments = replaceMap(newArguments, 'fileGameUri', fileGameUri);
newData = replaceVariable(newData, 'fileGameUri', fileGameUri);
newData = replaceVariable(newData, 'fileGame', fileGameUri);
}

if(fileGamePath != null){
newArguments = replaceMap(newArguments, 'fileGamePath', fileGamePath);
}

if(extra != null){
newArguments = replaceMap(newArguments, 'extra', extra);
}

return PlayerIntent(
action: action,
package: package,
componentName: componentName,
data: replaceVariable(data, 'fileGame', game.path),
type: type,
data: newData,
arguments: newArguments,
);
}
Expand All @@ -84,7 +99,7 @@ class PlayerIntent {
return value.replaceAll(regex, replaceValue);
}

Map<String, dynamic> replaceVariables(
Map<String, dynamic> replaceMap(
Map<String, dynamic>? map, String variable, String replaceValue) {
if (map == null) {
return {};
Expand Down
Loading

0 comments on commit b3a8bd3

Please sign in to comment.