Skip to content

Commit

Permalink
update ios: tested on a physical device
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-jiao committed Mar 27, 2024
1 parent 93075f8 commit 48eb549
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 52 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ Download *.dmg from [releases].
### iOS
Download *.ipa from [releases] and install by AltStore or other tools.

In fact, I have no Apple development experience at all, and I don’t even know the Swift language. The iOS and macOS native code was completed with the following links as references: [Writing custom platform-specific code](https://docs.flutter.dev/platform-integration/platform-channels?tab=type-mappings-swift-tab#type-mappings-swift-tab), [Providing access to directories](https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories), [juanmartin/renamerApp-ios](https://github.com/juanmartin/renamerApp-ios). Therefore, if there are any errors in the swift code, please fell free to point them out by opening an issue or a pull request. I'll be very grateful to you.

## todo:
- ~~Duplicate name check~~.(Done.)
- ~~Convert, including case convert, Chinese simp/trad/pinyin convert, Latin/Cyrillic script transliteration~~.(Done.)
- ~~Incremental renaming: for example, RenamerFile-1, RenamerFile-2, RenamerFile-3, RenamerFile-4, ...~~.(Done.)
- Rules re-editing.
- ~~Implement iOS renamer with specific code and Platform channel~~.(Done.) In fact, I have no Apple development experience at all, and I don’t even know the Swift language. The iOS and macOS native code was completed with the following links as references: [Writing custom platform-specific code](https://docs.flutter.dev/platform-integration/platform-channels?tab=type-mappings-swift-tab#type-mappings-swift-tab), [Providing access to directories](https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories), [juanmartin/renamerApp-ios](https://github.com/juanmartin/renamerApp-ios). Therefore, if there are any errors in the swift code, please fell free to point them out by opening an issue or a pull request. I'll be very grateful to you.
- ~~Implement iOS renamer with specific code and Platform channel~~.(Done.)

# Screenshots
## Desktop
Expand Down
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
DKImagePickerController: b512c28220a2b8ac7419f21c491fc8534b7601ac
DKPhotoGallery: fdfad5125a9fdda9cc57df834d49df790dbb4179
file_picker: 15fd9539e4eb735dc54bae8c0534a7a9511a03de
file_picker: 09aa5ec1ab24135ccd7a1621c46c84134bfd6655
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
fluttertoast: 31b00dabfa7fb7bacd9e7dbee580d7a2ff4bf265
package_info_plus: 115f4ad11e0698c8c1c5d8a689390df880f47e85
package_info_plus: 58f0028419748fad15bf008b270aaa8e54380b1c
path_provider_foundation: 3784922295ac71e43754bd15e0653ccfd36a147c
permission_handler_apple: 036b856153a2b1f61f21030ff725f3e6fece2b78
SDWebImage: a3ba0b8faac7228c3c8eadd1a55c9c9fe5e16457
Expand Down
29 changes: 12 additions & 17 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import UniformTypeIdentifiers
let startPath = args["startPath"] as! String

self!.fileAccess(startPath: startPath, result: result)
} else if call.method == "renameFile" {
} else if call.method == "changeScopedAccess" {
guard let args = call.arguments as? [String : Any] else {return}
let oldPath = args["oldPath"] as! String
let newPath = args["newPath"] as! String
self!.renameFile(oldPath: oldPath, newPath: newPath, result: result)
let targetPath = args["targetPath"] as! String
let startOrEnd = args["startOrEnd"] as! Bool

self!.changeScopedAccess(targetPath: targetPath, startOrEnd: startOrEnd, result: result)
} else {
result(FlutterMethodNotImplemented)
}
Expand Down Expand Up @@ -55,28 +55,23 @@ import UniformTypeIdentifiers
window?.rootViewController?.present(documentPicker, animated: true, completion: nil)
}

private func renameFile(oldPath: String, newPath: String, result: FlutterResult) {
let oldUrl = URL(fileURLWithPath: oldPath)
let dirUrl = oldUrl.deletingLastPathComponent()
let newUrl = URL(fileURLWithPath: newPath)
private func changeScopedAccess(targetPath: String, startOrEnd: Bool, result: FlutterResult) {
let targetUrl = URL(fileURLWithPath: targetPath)

do {
let access = oldUrl.startAccessingSecurityScopedResource()
let folderAccess = dirUrl.startAccessingSecurityScopedResource()
try FileManager.default.moveItem(at: oldUrl, to: newUrl)
dirUrl.stopAccessingSecurityScopedResource()
oldUrl.stopAccessingSecurityScopedResource()
if (startOrEnd) {
let access = targetUrl.startAccessingSecurityScopedResource()
} else {
targetUrl.stopAccessingSecurityScopedResource()
}
result(NSNumber(value: true))
return
} catch let error as NSError {
result(FlutterError(code: error.description, message: error.localizedDescription, details: nil))
} catch let error {
result(FlutterError(code: error.localizedDescription, message: error.localizedDescription, details: nil))
}

result(NSNumber(value: false))
dirUrl.stopAccessingSecurityScopedResource()
oldUrl.stopAccessingSecurityScopedResource()
}
}

Expand Down
48 changes: 28 additions & 20 deletions lib/pages/files_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class FilesPageState extends State<FilesPage> {
return;
}

await PlatformFilePicker.changeScopedAccess(dirs.first.toString(), true);

final files = await PlatformFilePicker.fileAccess(dirs.first.toString());
if (files == null) {
return;
Expand Down Expand Up @@ -153,21 +155,27 @@ class FilesPageState extends State<FilesPage> {
.toList();
}

TableCell _rowTextCell(FileSystemEntity file, {bool isNew = false}) => TableCell(
child: isNew
? FutureBuilder(
future: getNewName(file, FileMetadata(file)),
builder: (context, snap) {
if ((snap.connectionState == ConnectionState.active ||
snap.connectionState == ConnectionState.done) &&
(!snap.hasError)) {
return getRowText(file.newName, file.error);
}
return const LinearProgressIndicator();
},
)
: getRowText(file.name, null),
TableCell _rowTextCell(FileSystemEntity file, {bool isNew = false}) {
if (!file.existsSync()) {
return TableCell(
child: getRowText(L10n.current.fileNotExist, null),
);
}

return TableCell(
child: isNew ? FutureBuilder(
future: getNewName(file, FileMetadata(file)),
builder: (context, snap) {
if ((snap.connectionState == ConnectionState.active ||
snap.connectionState == ConnectionState.done) &&
(!snap.hasError)) {
return getRowText(file.newName, file.error);
}
return const LinearProgressIndicator();
},
) : getRowText(file.name, null),
);
}

Widget getRowText(String text, String? error) {
final textWidget = Text(
Expand All @@ -190,6 +198,7 @@ class FilesPageState extends State<FilesPage> {
child: textWidget,
);
}

List<TableRow> _tableRows() {
final filteredList = _filteredList();
final fileListColors = Theme.of(context).extension<FileListColors>()!;
Expand Down Expand Up @@ -347,7 +356,6 @@ class FilesPageState extends State<FilesPage> {
child: DropTarget(
enable: Responsive.isDesktop(context) && !Platform.isIOS,
onDragDone: (detail) {
print('done');
setState(() {
_files.addAll(
detail.files
Expand All @@ -361,20 +369,16 @@ class FilesPageState extends State<FilesPage> {
});
},
onDragEntered: (detail) {
print('enter');
setState(() {
_dragging = true;
});
},
onDragExited: (detail) {
print('exit');
setState(() {
_dragging = false;
});
},
onDragUpdated: (detail) {
print('update ${DateTime.now().microsecondsSinceEpoch}');
},
onDragUpdated: (detail) {},
child: Container(
color: Theme.of(context).extension<FileListColors>()!.primaryColor,
child: Stack(
Expand Down Expand Up @@ -426,6 +430,10 @@ class FilesPageState extends State<FilesPage> {
setState(() {
_files.remove(file);
});

if (!_files.map((e) => e.parent.path).contains(file.parent.path)) {
PlatformFilePicker.changeScopedAccess(file.parent.path, false);
}
} else {
setState(() {
_files[index] = value;
Expand Down
8 changes: 4 additions & 4 deletions lib/tools/ios_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class PlatformFilePicker {
}
}

static Future<bool> renameFile(String oldPath, String newPath) async {
static Future<bool> changeScopedAccess(String targetPath, bool startOrEnd) async {
try {
return await _channel.invokeMethod(
'renameFile',
'changeScopedAccess',
{
'oldPath': oldPath,
'newPath': newPath,
'targetPath': targetPath,
'startOrEnd': startOrEnd,
},
);
} on PlatformException {
Expand Down
8 changes: 0 additions & 8 deletions lib/tools/rename.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ Future<FileSystemEntity?> rename(

try {
file.newName = replaceSpecialCharacters(file.newName);
if (Platform.isIOS) {
final success = await PlatformFilePicker.renameFile(file.path, file.newPath);

if (success) {
return file.newPath.toFileSystemEntity();
}
}

return await file.rename(file.newPath);
} catch (e, s) {
debugPrint(e.toString());
Expand Down

0 comments on commit 48eb549

Please sign in to comment.