Skip to content

Commit

Permalink
Improve performance of saving files
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 28, 2023
1 parent 7c5daa2 commit 0e62299
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 32 deletions.
2 changes: 2 additions & 0 deletions api/lib/src/models/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ sealed class AssetLocation with _$AssetLocation {

const AssetLocation._();

static const empty = AssetLocation(path: '');

bool get isRemote => remote.isNotEmpty;

String get identifier =>
Expand Down
11 changes: 6 additions & 5 deletions app/lib/api/file_system/file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ abstract class DocumentFileSystem extends GeneralFileSystem {

Future<AppDocumentDirectory> createDirectory(String path);

Future<AppDocumentFile> updateFile(String path, List<int> data);
Future<void> updateFile(String path, List<int> data);

Future<String> findAvailableName(String path) =>
_findAvailableName(path, hasAsset);

Future<AppDocumentFile> createFile(String path, List<int> data) async =>
updateFile(await findAvailableName(path), data);
Future<AppDocumentFile?> createFile(String path, List<int> data) async =>
updateFile(await findAvailableName(path), data)
.then((_) => getAppDocumentFile(AssetLocation.local(path), data));

Future<bool> hasAsset(String path);

Expand Down Expand Up @@ -227,10 +228,10 @@ abstract class DocumentFileSystem extends GeneralFileSystem {

Future<void> saveAbsolute(String path, Uint8List bytes) => Future.value();

Future<AppDocumentFile> updateDocument(String path, NoteData document) =>
Future<void> updateDocument(String path, NoteData document) =>
updateFile(path, document.save());

Future<AppDocumentFile> importDocument(NoteData document,
Future<AppDocumentFile?> importDocument(NoteData document,
{String path = '/'}) {
if (path.endsWith('/')) {
path = path.substring(0, path.length - 1);
Expand Down
12 changes: 5 additions & 7 deletions app/lib/api/file_system/file_system_dav.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,10 @@ class DavRemoteDocumentFileSystem extends DocumentRemoteSystem {
}

@override
Future<AppDocumentFile> updateFile(String path, List<int> data,
Future<void> updateFile(String path, List<int> data,
{bool forceSync = false}) async {
if (!forceSync && remote.hasDocumentCached(path)) {
cacheContent(path, data);
return getAppDocumentFile(
AssetLocation(remote: remote.identifier, path: path), data);
}
// Create directory if not exists
final directoryPath = path.substring(0, path.lastIndexOf('/'));
Expand All @@ -206,12 +204,10 @@ class DavRemoteDocumentFileSystem extends DocumentRemoteSystem {
throw Exception(
'Failed to update document: ${response.statusCode} ${response.reasonPhrase}');
}
return getAppDocumentFile(
AssetLocation(remote: remote.identifier, path: path), data);
}

@override
Future<AppDocumentFile> updateDocument(String path, NoteData document,
Future<void> updateDocument(String path, NoteData document,
{bool forceSync = false}) =>
updateFile(path, document.save(), forceSync: forceSync);

Expand All @@ -228,7 +224,9 @@ class DavRemoteDocumentFileSystem extends DocumentRemoteSystem {
@override
Future<AppDocumentFile> createFile(String path, List<int> data,
{bool forceSync = false}) async =>
updateFile(await findAvailableName(path), data);
updateFile(await findAvailableName(path), data).then((_) =>
getAppDocumentFile(
AssetLocation(remote: remote.identifier, path: path), data));
}

class DavRemoteTemplateFileSystem extends TemplateFileSystem with RemoteSystem {
Expand Down
4 changes: 2 additions & 2 deletions app/lib/api/file_system/file_system_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class WebDocumentFileSystem extends DocumentFileSystem {
}

@override
Future<AppDocumentFile> updateFile(String path, List<int> data) async {
Future<bool> updateFile(String path, List<int> data) async {
// Remove trailing slash
if (path.endsWith('/')) {
path = path.substring(0, path.length - 1);
Expand All @@ -243,7 +243,7 @@ class WebDocumentFileSystem extends DocumentFileSystem {
final dataStore = txn.objectStore('documents-data');
await dataStore.put(data, path);
await txn.completed;
return getAppDocumentFile(AssetLocation.local(path), data);
return true;
}

@override
Expand Down
2 changes: 1 addition & 1 deletion app/lib/api/file_system/file_system_html_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class WebDocumentFileSystem extends DocumentFileSystem {
}

@override
Future<AppDocumentFile> updateFile(String path, List<int> data) {
Future<bool> updateFile(String path, List<int> data) {
throw UnimplementedError();
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/lib/api/file_system/file_system_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ class IODocumentFileSystem extends DocumentFileSystem {
}

@override
Future<AppDocumentFile> updateFile(String path, List<int> data) async {
Future<bool> updateFile(String path, List<int> data) async {
var file = File(await getAbsolutePath(path));
if (!(await file.exists())) {
await file.create(recursive: true);
}
await file.writeAsBytes(data, flush: true);

return getAppDocumentFile(AssetLocation.local(path), data);
return true;
}

@override
Expand Down
2 changes: 1 addition & 1 deletion app/lib/api/file_system/file_system_remote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ abstract class DocumentRemoteSystem extends DocumentFileSystem
}

@override
Future<AppDocumentFile> updateFile(String path, List<int> data,
Future<void> updateFile(String path, List<int> data,
{bool forceSync = false});

Future<void> cache(String path) async {
Expand Down
28 changes: 14 additions & 14 deletions app/lib/bloc/document_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,27 @@ class DocumentLoadSuccess extends DocumentLoaded {
?.hasDocumentCached(location.path) ??
false)));

Future<AssetLocation> save() {
Future<AssetLocation> save() async {
currentIndexCubit.setSaveState(saved: SaveState.saving);
final currentData = saveData();
final storage = getRemoteStorage();
if (embedding != null) return Future.value(AssetLocation.local(''));
if (embedding != null) return AssetLocation.empty;
if (!location.path.endsWith('.bfly') ||
location.absolute ||
location.fileType != AssetFileType.note) {
return DocumentFileSystem.fromPlatform(remote: storage)
.importDocument(currentData)
.then((value) => value.location)
..then(settingsCubit.addRecentHistory)
..then((value) => currentIndexCubit.setSaveState(
location: value, saved: SaveState.saved));
final document = await DocumentFileSystem.fromPlatform(remote: storage)
.importDocument(currentData);
if (document == null) return AssetLocation.empty;
await settingsCubit.addRecentHistory(document.location);
currentIndexCubit.setSaveState(
location: document.location, saved: SaveState.saved);
return document.location;
}
return DocumentFileSystem.fromPlatform(remote: storage)
.updateDocument(location.path, currentData)
.then((value) => value.location)
..then(settingsCubit.addRecentHistory)
..then((value) => currentIndexCubit.setSaveState(
location: value, saved: SaveState.saved));
await DocumentFileSystem.fromPlatform(remote: storage)
.updateDocument(location.path, currentData);
settingsCubit.addRecentHistory(location);
currentIndexCubit.setSaveState(location: location, saved: SaveState.saved);
return location;
}

ExternalStorage? getRemoteStorage() => location.remote.isEmpty
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/77.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* Allow moving tools when selected
* Readd recent files ([#512](https://github.com/LinwoodDev/Butterfly/issues/512))
* Improve responsiveness in home page
* Improve performance of saving files
* Fix tool indicator alignment if toolbar is in column mode
* Fix moving issues when painting
* Fix painting issues when using gestures
Expand Down

0 comments on commit 0e62299

Please sign in to comment.