Skip to content

Commit

Permalink
Fix back button closes app on mobile, closes #551
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 16, 2023
1 parent 18ac95c commit f0ff2e3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 43 deletions.
14 changes: 10 additions & 4 deletions app/lib/actions/new.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class NewAction extends Action<NewIntent> {
final template = await templateSystem.getDefaultTemplate(
templateSystem.remote?.defaultTemplate ?? settings.defaultTemplate,
);
openNewDocument(context, template);
openNewDocument(context, true, template);
}
}

void openNewDocument(BuildContext context,
Future<void> openNewDocument(BuildContext context, bool replace,
[NoteData? template, String? remote]) {
NoteData? document;
String? path;
Expand All @@ -49,6 +49,12 @@ void openNewDocument(BuildContext context,
path = metadata.directory;
}
}
GoRouter.of(context).pushReplacementNamed('new',
queryParameters: {'path': path, 'remote': remote}, extra: document);
final queryParams = {'path': path, 'remote': remote};
if (replace) {
return GoRouter.of(context).pushReplacementNamed('new',
queryParameters: queryParams, extra: document);
} else {
return GoRouter.of(context)
.pushNamed('new', queryParameters: queryParams, extra: document);
}
}
53 changes: 34 additions & 19 deletions app/lib/api/open.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,40 @@ Future<(Uint8List?, String?)> openSupported(
return (e.bytes, e.extension);
}

void openFile(BuildContext context, AssetLocation location, [Object? data]) {
Future<void> openFile(
BuildContext context, bool replace, AssetLocation location,
[Object? data]) {
if (location.isRemote) {
GoRouter.of(context).pushReplacementNamed('remote',
pathParameters: {
'remote': location.remote,
'path': location.pathWithoutLeadingSlash,
},
queryParameters: {
'type': location.fileType?.name,
},
extra: data);
return;
final pathParams = {
'remote': location.remote,
'path': location.pathWithoutLeadingSlash,
};
final queryParams = {
'type': location.fileType?.name,
};
if (replace) {
return GoRouter.of(context).pushReplacementNamed('remote',
pathParameters: pathParams,
queryParameters: queryParams,
extra: data);
} else {
return GoRouter.of(context).pushNamed('remote',
pathParameters: pathParams,
queryParameters: queryParams,
extra: data);
}
}
final pathParams = {
'path': location.pathWithoutLeadingSlash,
};
final queryParams = {
'type': location.fileType?.name,
};
if (replace) {
return GoRouter.of(context).pushReplacementNamed('local',
pathParameters: pathParams, queryParameters: queryParams, extra: data);
} else {
return GoRouter.of(context).pushNamed('local',
pathParameters: pathParams, queryParameters: queryParams, extra: data);
}
GoRouter.of(context).pushReplacementNamed('local',
pathParameters: {
'path': location.pathWithoutLeadingSlash,
},
queryParameters: {
'type': location.fileType?.name,
},
extra: data);
}
8 changes: 6 additions & 2 deletions app/lib/dialogs/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class _TemplateDialogState extends State<TemplateDialog> {
return _TemplateItem(
template: template,
fileSystem: _fileSystem,
replace: widget.bloc != null,
onChanged: () {
load();
setState(() {});
Expand Down Expand Up @@ -227,10 +228,13 @@ class _TemplateItem extends StatelessWidget {
final NoteData template;
final TemplateFileSystem fileSystem;
final VoidCallback onChanged;
final bool replace;

const _TemplateItem({
required this.template,
required this.fileSystem,
required this.onChanged,
required this.replace,
});

@override
Expand Down Expand Up @@ -294,8 +298,8 @@ class _TemplateItem extends StatelessWidget {
},
),
],
onTap: () =>
openNewDocument(context, template, fileSystem.remote?.identifier),
onTap: () => openNewDocument(
context, replace, template, fileSystem.remote?.identifier),
);
}
}
2 changes: 1 addition & 1 deletion app/lib/views/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class _MainPopupMenu extends StatelessWidget {
menuChildren: settings.history
.map((e) => MenuItemButton(
child: Text(e.identifier),
onPressed: () => openFile(context, e),
onPressed: () => openFile(context, true, e),
))
.toList(),
leadingIcon: const PhosphorIcon(PhosphorIconsLight.clock),
Expand Down
37 changes: 23 additions & 14 deletions app/lib/views/files/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class FilesView extends StatefulWidget {
});

@override
State<FilesView> createState() => _FilesViewState();
State<FilesView> createState() => FilesViewState();
}

class _FilesViewState extends State<FilesView> {
class FilesViewState extends State<FilesView> {
final TextEditingController _locationController = TextEditingController();
late DocumentFileSystem _fileSystem;
late TemplateFileSystem _templateSystem;
Expand Down Expand Up @@ -81,7 +81,7 @@ class _FilesViewState extends State<FilesView> {
_filesStream = _fileSystem.fetchAsset(_locationController.text);
}

void _reloadFileSystem() {
void reloadFileSystem() {
setState(_setFilesStream);
}

Expand All @@ -101,7 +101,7 @@ class _FilesViewState extends State<FilesView> {
template.createDocument(
name: name,
));
_reloadFileSystem();
reloadFileSystem();
}

void _setRemote(ExternalStorage? remote) {
Expand Down Expand Up @@ -229,7 +229,9 @@ class _FilesViewState extends State<FilesView> {
);
}),
const SizedBox(height: 8),
const _RecentFilesView(),
_RecentFilesView(
replace: widget.collapsed,
),
const SizedBox(height: 16),
LayoutBuilder(builder: (context, constraints) {
final searchBar = SearchBar(
Expand Down Expand Up @@ -258,7 +260,7 @@ class _FilesViewState extends State<FilesView> {
final path = _locationController.text;
final newPath = '$path/$name';
await _fileSystem.createDirectory(newPath);
_reloadFileSystem();
reloadFileSystem();
},
),
MenuItemButton(
Expand Down Expand Up @@ -317,7 +319,7 @@ class _FilesViewState extends State<FilesView> {
const route = '/native?name=document.bfly&type=note';
router.go(route, extra: model.save());
if (!widget.collapsed) {
_reloadFileSystem();
reloadFileSystem();
}
},
child: Text(AppLocalizations.of(context).import),
Expand Down Expand Up @@ -369,7 +371,7 @@ class _FilesViewState extends State<FilesView> {
onAccept: (data) async {
await _fileSystem.moveAsset(
data, '$parent/${data.split('/').last}');
_reloadFileSystem();
reloadFileSystem();
},
),
const SizedBox(width: 8),
Expand All @@ -381,7 +383,7 @@ class _FilesViewState extends State<FilesView> {
filled: true,
),
controller: _locationController,
onFieldSubmitted: (value) => _reloadFileSystem(),
onFieldSubmitted: (value) => reloadFileSystem(),
),
),
],
Expand Down Expand Up @@ -453,7 +455,7 @@ class _FilesViewState extends State<FilesView> {
selected: selected,
collapsed: widget.collapsed,
onTap: () => _onFileTap(e),
onReload: _reloadFileSystem,
onReload: reloadFileSystem,
gridView: true,
);
},
Expand All @@ -473,7 +475,7 @@ class _FilesViewState extends State<FilesView> {
selected: selected,
collapsed: widget.collapsed,
onTap: () => _onFileTap(entity),
onReload: _reloadFileSystem,
onReload: reloadFileSystem,
gridView: false,
isMobile: widget.isMobile,
);
Expand All @@ -494,7 +496,10 @@ class _FilesViewState extends State<FilesView> {
}
final location = entity.location;
final data = entity.data;
openFile(context, location, data);
await openFile(context, widget.collapsed, location, data);
if (!widget.collapsed) {
reloadFileSystem();
}
}

int _sortAssets(AppDocumentEntity a, AppDocumentEntity b) {
Expand Down Expand Up @@ -567,7 +572,10 @@ class _FilesViewState extends State<FilesView> {
}

class _RecentFilesView extends StatefulWidget {
const _RecentFilesView();
final bool replace;
const _RecentFilesView({
required this.replace,
});

@override
State<_RecentFilesView> createState() => _RecentFilesViewState();
Expand Down Expand Up @@ -627,7 +635,8 @@ class _RecentFilesViewState extends State<_RecentFilesView> {
metadata: metadata,
thumbnail: thumbnail,
name: entity.location.identifier,
onTap: () => openFile(context, entity.location, data),
onTap: () => openFile(
context, widget.replace, entity.location, data),
);
},
),
Expand Down
10 changes: 7 additions & 3 deletions app/lib/views/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
final GlobalKey<FilesViewState> _filesViewKey = GlobalKey();
ExternalStorage? _remote;

@override
Expand Down Expand Up @@ -102,7 +103,8 @@ class _HomePageState extends State<HomePage> {
hasNewerVersion);
final quickStart = _QuickstartHomeView(
remote: _remote,
onReload: () => setState(() {}),
onReload: () => setState(
() => _filesViewKey.currentState?.reloadFileSystem()),
);
return Scaffold(
appBar: WindowTitleBar(
Expand Down Expand Up @@ -168,6 +170,7 @@ class _HomePageState extends State<HomePage> {
selectedAsset: widget.selectedAsset,
remote: _remote,
isMobile: true,
key: _filesViewKey,
onRemoteChanged: (value) =>
setState(() => _remote = value),
),
Expand Down Expand Up @@ -510,8 +513,9 @@ class _QuickstartHomeViewState extends State<_QuickstartHomeView> {
metadata: metadata,
thumbnail: thumbnail,
onTap: () async {
openNewDocument(
context, e, widget.remote?.identifier);
await openNewDocument(
context, false, e, widget.remote?.identifier);
widget.onReload();
},
);
},
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/84.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* Change duplicate icon to have a difference to copy icon ([#552](https://github.com/LinwoodDev/Butterfly/issues/552))
* Improve responsiveness of dialogs on mobile devices ([#550](https://github.com/LinwoodDev/Butterfly/issues/550))
* Improve add dialog trailing icon ([#550](https://github.com/LinwoodDev/Butterfly/issues/550))
* Fix back button closes app on mobile ([#551](https://github.com/LinwoodDev/Butterfly/issues/551))

View all changes in the blog: https://linwood.dev/butterfly/2.0.0-rc.3

0 comments on commit f0ff2e3

Please sign in to comment.