Skip to content

Commit

Permalink
Migrate to new file system library
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Aug 15, 2024
1 parent 9e3219b commit 08b066a
Show file tree
Hide file tree
Showing 25 changed files with 98 additions and 88 deletions.
4 changes: 2 additions & 2 deletions api/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ packages:
dependency: "direct main"
description:
path: "packages/lw_file_system_api"
ref: d4c91a63b34f6efc5827a90f5c59cf9421ed6868
resolved-ref: d4c91a63b34f6efc5827a90f5c59cf9421ed6868
ref: dfda6c2e3cef2e29511f97e9470fd4deb8e0c573
resolved-ref: dfda6c2e3cef2e29511f97e9470fd4deb8e0c573
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
git:
url: https://github.com/LinwoodDev/dart_pkgs
path: packages/lw_file_system_api
ref: d4c91a63b34f6efc5827a90f5c59cf9421ed6868
ref: dfda6c2e3cef2e29511f97e9470fd4deb8e0c573

dev_dependencies:
test: ^1.25.3
Expand Down
7 changes: 4 additions & 3 deletions app/lib/api/close_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class WebCloseSubscription extends CloseSubscription {
final StreamSubscription _subscription;

WebCloseSubscription(OnCloseCallback onClose)
: _subscription = EventStreamProvider<BeforeUnloadEvent>('onbeforeunload')
.forTarget(window)
.listen((event) {
: _subscription =
const EventStreamProvider<BeforeUnloadEvent>('onbeforeunload')
.forTarget(window)
.listen((event) {
final message = onClose();
if (message == null) return;
event.returnValue = message;
Expand Down
1 change: 1 addition & 0 deletions app/lib/bloc/document_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
AssetService? assetService,
DocumentPage? page,
String? pageName,
bool absolute = false,
]) : super(DocumentLoadSuccess(
initial,
page: page,
Expand Down
9 changes: 6 additions & 3 deletions app/lib/bloc/document_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ abstract class DocumentLoaded extends DocumentState {
Area? get currentArea => null;

AssetLocation get location => currentIndexCubit.state.location;
bool get absolute => currentIndexCubit.state.absolute;
SaveState get saved =>
location.absolute ? SaveState.unsaved : currentIndexCubit.state.saved;
absolute ? SaveState.unsaved : currentIndexCubit.state.saved;

@override
CurrentIndexCubit get currentIndexCubit;
Expand Down Expand Up @@ -140,13 +141,14 @@ class DocumentLoadSuccess extends DocumentLoaded {
super.metadata,
super.info,
AssetLocation? location,
bool absolute = false,
this.storageType = StorageType.local,
required this.currentIndexCubit,
this.currentAreaName = '',
this.currentLayer = '',
this.invisibleLayers = const []}) {
if (location != null) {
currentIndexCubit.setSaveState(location: location);
currentIndexCubit.setSaveState(location: location, absolute: absolute);
}
}

Expand Down Expand Up @@ -198,6 +200,7 @@ class DocumentLoadSuccess extends DocumentLoaded {
windowCubit: windowCubit,
currentIndexCubit: currentIndexCubit,
location: location,
absolute: absolute,
);

bool isLayerVisible(String layer) => !invisibleLayers.contains(layer);
Expand All @@ -207,7 +210,7 @@ class DocumentLoadSuccess extends DocumentLoaded {
(networkingService.isActive ||
!(embedding?.save ?? true) ||
(!kIsWeb &&
!location.absolute &&
!absolute &&
(location.fileType == AssetFileType.note ||
location.fileType == null) &&
(location.remote.isEmpty ||
Expand Down
15 changes: 10 additions & 5 deletions app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import '../view_painter.dart';

part 'current_index.freezed.dart';

enum SaveState { saved, saving, unsaved }
enum SaveState { saved, saving, unsaved, absoluteRead }

enum HideState { visible, keyboard, touch }

Expand Down Expand Up @@ -79,6 +79,8 @@ class CurrentIndex with _$CurrentIndex {
bool get moveEnabled =>
settingsCubit.state.inputGestures && pointers.length > 1;

bool get absolute => saved == SaveState.absoluteRead;

MouseCursor get currentCursor => temporaryCursor ?? cursor;

UtilitiesState get utilitiesState => cameraViewport.utilities.element;
Expand Down Expand Up @@ -663,11 +665,14 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
cameraViewport: state.cameraViewport.withUnbaked(unbakedElements)));
}

void setSaveState({AssetLocation? location, SaveState? saved}) =>
void setSaveState(
{AssetLocation? location, SaveState? saved, bool absolute = false}) =>
emit(state.copyWith(
location: location ?? state.location,
saved:
saved ?? (location != null ? SaveState.unsaved : state.saved)));
saved: absolute
? SaveState.absoluteRead
: (saved ??
(location != null ? SaveState.unsaved : state.saved))));

Future<pw.Document> renderPDF(
NoteData document,
Expand Down Expand Up @@ -874,7 +879,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
if (currentData == null) return AssetLocation.empty;
if (blocState.embedding != null) return AssetLocation.empty;
if (!location.path.endsWith('.bfly') ||
location.absolute ||
state.absolute ||
location.fileType != AssetFileType.note) {
final document =
await fileSystem.createFile(currentData.name ?? '', currentData);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/cubits/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ class SettingsCubit extends Cubit<ButterflySettings>
final history = state.history.toList();
history.removeWhere((element) =>
element.remote == location.remote &&
element.pathWithLeadingSlash.startsWith(location.pathWithLeadingSlash));
element.path.startsWith(location.path));
emit(state.copyWith(history: history));
return save();
}
Expand Down
6 changes: 2 additions & 4 deletions app/lib/dialogs/file_system/move.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ class _FileSystemAssetMoveDialogState extends State<FileSystemAssetMoveDialog> {
newPath += _nameController.text;
newPath += '.${widget.asset.fileExtension}';
if (duplicate) {
await widget.fileSystem
.duplicateAsset(widget.asset.pathWithLeadingSlash, newPath);
await widget.fileSystem.duplicateAsset(widget.asset.path, newPath);
} else {
await widget.fileSystem
.moveAsset(widget.asset.pathWithLeadingSlash, newPath);
await widget.fileSystem.moveAsset(widget.asset.path, newPath);
}
navigator.pop(newPath);
}
Expand Down
6 changes: 3 additions & 3 deletions app/lib/dialogs/file_system/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FileSystemDirectoryTreeViewState
if (snapshot.hasData) {
var directory = snapshot.data!;
var children = directory.assets.whereType<FileSystemDirectory>();
var name = directory.pathWithLeadingSlash.split('/').last;
var name = directory.path.split('/').last;
if (name.isEmpty) {
name = '/';
}
Expand All @@ -94,7 +94,7 @@ class FileSystemDirectoryTreeViewState
_expanded = true;
_selected = widget.path;
});
widget.onPathSelected(directory.pathWithLeadingSlash);
widget.onPathSelected(directory.path);
}
},
selected: _selected == widget.path,
Expand All @@ -110,7 +110,7 @@ class FileSystemDirectoryTreeViewState
var current = children.elementAt(index);
return FileSystemDirectoryTreeView(
fileSystem: widget.fileSystem,
path: current.pathWithLeadingSlash,
path: current.path,
selectedPath: _selected,
onPathSelected: (value) {
setState(() {
Expand Down
8 changes: 4 additions & 4 deletions app/lib/dialogs/packs/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:butterfly_api/butterfly_api.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:lw_file_system/lw_file_system.dart';
import 'package:material_leap/material_leap.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

Expand Down Expand Up @@ -188,7 +189,7 @@ class _PacksDialogState extends State<PacksDialog>
},
);
}),
FutureBuilder<Map<String, NoteData>>(
FutureBuilder<List<FileSystemFile<NoteData>>>(
future: _packSystem
.initialize()
.then((value) => _packSystem.getFiles()),
Expand All @@ -202,15 +203,14 @@ class _PacksDialogState extends State<PacksDialog>
child: CircularProgressIndicator(),
);
}
final globalPacks = List<NoteData>.from(
snapshot.data?.values ?? <NoteData>[]);
final globalPacks = snapshot.data ?? [];
return StatefulBuilder(
builder: (context, setInnerState) {
return ListView.builder(
shrinkWrap: true,
itemCount: globalPacks.length,
itemBuilder: (context, index) {
final pack = globalPacks[index];
final pack = globalPacks[index].data!;
final metadata = pack.getMetadata();
if (metadata == null) return Container();
return Dismissible(
Expand Down
12 changes: 6 additions & 6 deletions app/lib/dialogs/sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,25 @@ class _RemoteSyncView extends StatelessWidget {
children: [
TextButton(
onPressed: () {
sync.resolve(file.location.pathWithLeadingSlash,
FileSyncStatus.localLatest);
sync.resolve(
file.location.path, FileSyncStatus.localLatest);
},
child: Text(AppLocalizations.of(context).keepLocal),
),
const SizedBox(width: 8),
TextButton(
onPressed: () {
sync.resolve(file.location.pathWithLeadingSlash,
FileSyncStatus.remoteLatest);
sync.resolve(
file.location.path, FileSyncStatus.remoteLatest);
},
child:
Text(AppLocalizations.of(context).keepConnection),
),
const SizedBox(width: 8),
TextButton(
onPressed: () {
sync.resolve(file.location.pathWithLeadingSlash,
FileSyncStatus.conflict);
sync.resolve(
file.location.path, FileSyncStatus.conflict);
},
child: Text(AppLocalizations.of(context).keepBoth),
),
Expand Down
11 changes: 6 additions & 5 deletions app/lib/dialogs/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class _TemplateDialogState extends State<TemplateDialog> {
void load() {
setState(() {
_templatesFuture = _templateSystem.initialize().then((value) async {
var templates = (await _templateSystem.getFiles()).values.toList();
var templates =
(await _templateSystem.getFiles()).map((e) => e.data!).toList();
templates = templates
.where((element) =>
element.name
Expand Down Expand Up @@ -81,11 +82,11 @@ class _TemplateDialogState extends State<TemplateDialog> {
tooltip: AppLocalizations.of(context).export,
onPressed: () async {
final archive = Archive();
for (final template
in (await _templateSystem.getFiles()).entries) {
final data = template.value.save();
for (final template in (await _templateSystem.getFiles())) {
final data = template.data!.save();
archive.addFile(
ArchiveFile('${template.key}.bfly', data.length, data),
ArchiveFile(
'${template.fileName}.bfly', data.length, data),
);
}
final encoder = ZipEncoder();
Expand Down
4 changes: 2 additions & 2 deletions app/lib/embed/action_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ EventListener onEmbedMessage(String type, EmbedMessageHandler callback) {
}

void removeEmbedMessageListener(EventListener listener) {
if (listener is JSFunction)
html.window.removeEventListener('receive', listener);
if (listener is! JSFunction) return;
html.window.removeEventListener('receive', listener);
}
3 changes: 2 additions & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ class ButterflyApp extends StatelessWidget {
final path = state.uri.queryParameters['path'] ?? '';
final data = state.extra;
return ProjectPage(
location: AssetLocation.local(path, true),
location: AssetLocation.local(path),
absolute: true,
type: type,
data: data,
);
Expand Down
11 changes: 4 additions & 7 deletions app/lib/services/sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ class RemoteSync {
for (final file in currentFiles) {
switch (file.status) {
case FileSyncStatus.localLatest:
await remoteSystem
.uploadCachedContent(file.location.pathWithLeadingSlash);
await remoteSystem.uploadCachedContent(file.location.path);
final syncedFile = SyncFile(
isDirectory: file.isDirectory,
location: file.location,
Expand All @@ -166,7 +165,7 @@ class RemoteSync {
break;
case FileSyncStatus.remoteLatest:
if (!hasError) {
await remoteSystem.cache(file.location.pathWithLeadingSlash);
await remoteSystem.cache(file.location.path);
final syncedFile = SyncFile(
isDirectory: file.isDirectory,
location: file.location,
Expand Down Expand Up @@ -224,8 +223,7 @@ class RemoteSync {
final last = List<SyncFile>.from(files ?? []);
final remoteSystem = buildRemoteSystem();
if (remoteSystem == null) return;
last.removeWhere(
(element) => element.location.pathWithLeadingSlash == path);
last.removeWhere((element) => element.location.path == path);
_filesSubject.add(last);
switch (status) {
case FileSyncStatus.localLatest:
Expand All @@ -243,8 +241,7 @@ class RemoteSync {
if (remoteAsset is RawFileSystemFile) {
final doc = remoteAsset.data;
if (doc == null) return;
await remoteSystem.createFile(remoteAsset.pathWithLeadingSlash, doc,
forceSync: true);
await remoteSystem.createFile(remoteAsset.path, doc, forceSync: true);
await remoteSystem.uploadCachedContent(path);
}
break;
Expand Down
8 changes: 5 additions & 3 deletions app/lib/views/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class _AppBarTitle extends StatelessWidget {
Tooltip(
message: currentIndex.location.identifier,
child: Text(
((currentIndex.location.absolute &&
((currentIndex.absolute &&
currentIndex.location.path.isEmpty)
? currentIndex.location.fileType
?.getLocalizedName(context)
Expand All @@ -248,7 +248,9 @@ class _AppBarTitle extends StatelessWidget {
child: Builder(builder: (context) {
Widget icon = PhosphorIcon(switch (currentIndex.saved) {
SaveState.saved => PhosphorIconsFill.floppyDisk,
SaveState.unsaved => PhosphorIconsLight.floppyDisk,
SaveState.unsaved ||
SaveState.absoluteRead =>
PhosphorIconsLight.floppyDisk,
SaveState.saving => PhosphorIconsDuotone.floppyDisk,
});
return IconButton(
Expand All @@ -271,7 +273,7 @@ class _AppBarTitle extends StatelessWidget {
.add(const CurrentAreaChanged(''));
},
),
if (state.location.absolute)
if (state.absolute)
IconButton(
icon: PhosphorIcon(
state.location.fileType.icon(PhosphorIconsStyle.light)),
Expand Down
2 changes: 1 addition & 1 deletion app/lib/views/files/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class _FileEntityItemState extends State<FileEntityItem> {
);

final draggable = LongPressDraggable<String>(
data: widget.entity.pathWithLeadingSlash,
data: widget.entity.path,
feedback: Material(
elevation: 5,
child: SizedBox(
Expand Down
5 changes: 2 additions & 3 deletions app/lib/views/files/grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ class FilesActionMenu extends StatelessWidget {
stream: syncService.getSync(remote!.identifier)?.filesStream,
builder: (context, snapshot) {
final currentStatus = snapshot.data
?.lastWhereOrNull((element) => entity
.location.pathWithLeadingSlash
.startsWith(element.location.pathWithLeadingSlash))
?.lastWhereOrNull((element) =>
entity.location.path.startsWith(element.location.path))
?.status;
return MenuItemButton(
leadingIcon: PhosphorIcon(currentStatus.getIcon(),
Expand Down
5 changes: 2 additions & 3 deletions app/lib/views/files/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ class FileEntityListTile extends StatelessWidget {
builder: (context, snapshot) {
final currentStatus = snapshot.data
?.lastWhereOrNull((element) => entity
.location.pathWithLeadingSlash
.startsWith(element
.location.pathWithLeadingSlash))
.location.path
.startsWith(element.location.path))
?.status;
return IconButton(
icon: PhosphorIcon(currentStatus.getIcon(),
Expand Down
Loading

0 comments on commit 08b066a

Please sign in to comment.