From 3ced79740783265bb92a53eec37ab5bd907cf29a Mon Sep 17 00:00:00 2001 From: zontreck Date: Wed, 24 Jul 2024 17:16:52 -0700 Subject: [PATCH] Fix file saving on Android --- android/app/src/debug/AndroidManifest.xml | 4 +- android/app/src/main/AndroidManifest.xml | 5 ++- android/app/src/profile/AndroidManifest.xml | 4 +- lib/Editor.dart | 41 +++++++++++++-------- lib/main.dart | 4 +- pubspec.yaml | 2 +- 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/android/app/src/debug/AndroidManifest.xml b/android/app/src/debug/AndroidManifest.xml index c7b5675..0fc2d23 100644 --- a/android/app/src/debug/AndroidManifest.xml +++ b/android/app/src/debug/AndroidManifest.xml @@ -4,5 +4,7 @@ to allow setting breakpoints, to provide hot reload, etc. --> - + + + diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index cc8ebdf..b068230 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,8 @@ - + + + + - + + + diff --git a/lib/Editor.dart b/lib/Editor.dart index adb8e8c..b2b0854 100644 --- a/lib/Editor.dart +++ b/lib/Editor.dart @@ -1,7 +1,7 @@ import 'dart:typed_data'; +import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_file_dialog/flutter_file_dialog.dart'; import 'package:flutter_treeview/flutter_treeview.dart'; import 'package:libac_dart/nbt/NbtIo.dart'; import 'package:libac_dart/nbt/SnbtIo.dart'; @@ -89,11 +89,12 @@ class EditorState extends State { leading: const Icon(Icons.folder), subtitle: const Text("Open an existing NBT Document for editing"), onTap: () async { - String? filePath = await FlutterFileDialog.pickFile( - params: const OpenFileDialogParams()); - if (filePath != null) { + String? filePath; + var result = await FilePicker.platform.pickFiles(); + if (result != null) { // Do something with the selected file path print('Selected file path: $filePath'); + filePath = result!.files.first.path; } else { // User canceled the picker print('File selection canceled.'); @@ -150,24 +151,27 @@ class EditorState extends State { ); }); - Uint8List u8l = Uint8List(0); - if (result == null) { - // Save uncompressed - u8l = await NbtIo.writeToStream( - controller.children[0].data as CompoundTag); - } else { - u8l = await NbtIo.writeToStreamCompressed( - controller.children[0].data as CompoundTag); - } // Prompt for where to save print("Begin picking file to save to"); - var filePath = await FlutterFileDialog.saveFile(); + var FSL = await FilePicker.platform.saveFile(bytes: Uint8List(0)); + String? filePath; + + if (FSL != null) { + filePath = FSL; + } if (filePath == null) { print("No file selected"); return; } print(filePath); + CompoundTag tag = controller.children[0].data as CompoundTag; + if (result == null) { + // Save uncompressed + NbtIo.write(filePath, tag); + } else { + NbtIo.writeCompressed(filePath, tag); + } }, ), ListTile( @@ -179,8 +183,13 @@ class EditorState extends State { onTap: () async { // Prompt for where to save print("Begin picking file to save to"); - String? filePath = await FlutterFileDialog.saveFile( - params: SaveFileDialogParams()); + var FSL = await FilePicker.platform.saveFile(bytes: Uint8List(0)); + String? filePath; + + if (FSL != null) { + filePath = FSL; + } + if (filePath == null) { print("No file selected"); return; diff --git a/lib/main.dart b/lib/main.dart index 13c6fd3..bac7650 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -53,7 +53,9 @@ class StartPageState extends State { Navigator.pushReplacementNamed(context, "/perms"); }); } else { - Navigator.pushReplacementNamed(context, "/edit"); + await Future.delayed(Duration(seconds: 2), () { + Navigator.pushReplacementNamed(context, "/edit"); + }); } } diff --git a/pubspec.yaml b/pubspec.yaml index 577a1f4..33390b9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,10 +8,10 @@ environment: sdk: '>=3.1.5 <4.0.0' dependencies: + file_picker: ^8.0.0+1 flutter: sdk: flutter flutter_code_editor: ^0.3.2 - flutter_file_dialog: ^3.0.2 flutter_highlight: ^0.7.0 flutter_treeview: ^1.0.7+1 libac_dart: