Skip to content

Commit

Permalink
Add templates to data settings
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Nov 5, 2023
1 parent 2256deb commit eecb9ef
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 69 deletions.
59 changes: 23 additions & 36 deletions app/lib/actions/new.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly/cubits/settings.dart';
import 'package:butterfly/cubits/transform.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import '../cubits/current_index.dart';
import '../dialogs/template.dart';
import '../models/defaults.dart';

class NewIntent extends Intent {
final BuildContext context;
Expand All @@ -26,42 +23,32 @@ class NewAction extends Action<NewIntent> {
final bloc = context.read<DocumentBloc>();
final settingsCubit = context.read<SettingsCubit>();
final settings = settingsCubit.state;
final transformCubit = context.read<TransformCubit>();
final currentIndexCubit = context.read<CurrentIndexCubit>();
final router = GoRouter.of(context);
var path = '';
var document = DocumentDefaults.createDocument();
NoteData? template;
if (intent.fromTemplate && context.mounted) {
var state = bloc.state;
if (state is DocumentLoadSuccess) document = state.data;
template = await showDialog<NoteData>(
context: context,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider.value(value: bloc),
BlocProvider.value(value: transformCubit),
BlocProvider.value(value: currentIndexCubit),
],
child: TemplateDialog(
currentDocument: document,
),
));
if (template == null) return;
} else {
final templateSystem = settings.getDefaultTemplateFileSystem();
template = await templateSystem.getDefaultTemplate(
templateSystem.remote?.defaultTemplate ?? settings.defaultTemplate,
await showDialog(
context: context,
builder: (context) => TemplateDialog(bloc: bloc),
);
return;
}
if (template != null) {
document = template.createDocument();
final metadata = document.getMetadata();
if (metadata != null) {
path = metadata.directory;
}
final templateSystem = settings.getDefaultTemplateFileSystem();
final template = await templateSystem.getDefaultTemplate(
templateSystem.remote?.defaultTemplate ?? settings.defaultTemplate,
);
openNewDocument(context, template);
}
}

void openNewDocument(BuildContext context,
[NoteData? template, String? remote]) {
NoteData? document;
String? path;
if (template != null) {
document = template.createDocument();
final metadata = document.getMetadata();
if (metadata != null) {
path = metadata.directory;
}
router.pushReplacementNamed('new',
queryParameters: {'path': path}, extra: document);
}
GoRouter.of(context).pushReplacementNamed('new',
queryParameters: {'path': path, 'remote': remote}, extra: document);
}
23 changes: 12 additions & 11 deletions app/lib/dialogs/template.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:butterfly/actions/new.dart';
import 'package:butterfly/cubits/settings.dart';
import 'package:butterfly/widgets/remote_button.dart';
import 'package:butterfly_api/butterfly_api.dart';
Expand All @@ -13,8 +14,8 @@ import '../widgets/editable_list_tile.dart';
import 'delete.dart';

class TemplateDialog extends StatefulWidget {
final NoteData? currentDocument;
const TemplateDialog({super.key, required this.currentDocument});
final DocumentBloc? bloc;
const TemplateDialog({super.key, this.bloc});

@override
State<TemplateDialog> createState() => _TemplateDialogState();
Expand Down Expand Up @@ -113,12 +114,11 @@ class _TemplateDialogState extends State<TemplateDialog> {
);
},
),
...widget.currentDocument == null
...widget.bloc == null
? []
: [
IconButton(
onPressed: () =>
_showCreateDialog(widget.currentDocument!),
onPressed: () => _showCreateDialog(widget.bloc!),
tooltip: AppLocalizations.of(context).create,
icon: const PhosphorIcon(PhosphorIconsLight.plus),
)
Expand Down Expand Up @@ -175,7 +175,7 @@ class _TemplateDialogState extends State<TemplateDialog> {
)));
}

Future<void> _showCreateDialog(NoteData document) {
Future<void> _showCreateDialog(DocumentBloc bloc) {
final directoryController = TextEditingController();
return showDialog<void>(
context: context,
Expand Down Expand Up @@ -207,10 +207,10 @@ class _TemplateDialogState extends State<TemplateDialog> {
ElevatedButton(
child: Text(AppLocalizations.of(context).create),
onPressed: () async {
this.context.read<DocumentBloc>().add(TemplateCreated(
directoryController.text,
_fileSystem.remote?.identifier,
));
bloc.add(TemplateCreated(
directoryController.text,
_fileSystem.remote?.identifier,
));
Navigator.of(context).pop();
load();
setState(() {});
Expand Down Expand Up @@ -293,7 +293,8 @@ class _TemplateItem extends StatelessWidget {
},
),
],
onTap: () => Navigator.of(context).pop(template),
onTap: () =>
openNewDocument(context, template, fileSystem.remote?.identifier),
);
}
}
3 changes: 2 additions & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ class ButterflyApp extends StatelessWidget {
return ProjectPage(
data: state.extra,
location: AssetLocation(
remote: defaultRemote,
remote: state.uri.queryParameters['remote'] ??
defaultRemote,
path: state.uri.queryParameters['path'] ?? '',
),
);
Expand Down
41 changes: 24 additions & 17 deletions app/lib/settings/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:butterfly/api/file_system/file_system_io.dart';
import 'package:butterfly/api/save_data.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly/cubits/settings.dart';
import 'package:butterfly/dialogs/template.dart';
import 'package:butterfly/widgets/window.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -85,27 +86,33 @@ class _DataSettingsPageState extends State<DataSettingsPage> {
)
: null,
),
ListTile(
title: Text(AppLocalizations.of(context).templates),
leading: const PhosphorIcon(PhosphorIconsLight.file),
onTap: () => showDialog(
context: context,
builder: (ctx) => const TemplateDialog(),
),
),
ListTile(
title: Text(AppLocalizations.of(context).packs),
leading:
const PhosphorIcon(PhosphorIconsLight.package),
onTap: () {
showDialog(
context: context,
builder: (ctx) => MultiBlocProvider(
providers: [
BlocProvider.value(
value: context.read<SettingsCubit>()),
BlocProvider(
lazy: false,
create: (ctx) => DocumentBloc.placeholder(
context.read<SettingsCubit>()),
),
],
child: const PacksDialog(globalOnly: true),
),
);
},
onTap: () => showDialog(
context: context,
builder: (ctx) => MultiBlocProvider(
providers: [
BlocProvider.value(
value: context.read<SettingsCubit>()),
BlocProvider(
lazy: false,
create: (ctx) => DocumentBloc.placeholder(
context.read<SettingsCubit>()),
),
],
child: const PacksDialog(globalOnly: true),
),
),
),
ListTile(
title: Text(AppLocalizations.of(context).export),
Expand Down
7 changes: 3 additions & 4 deletions app/lib/views/home.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:butterfly/actions/new.dart';
import 'package:butterfly/actions/settings.dart';
import 'package:butterfly/api/file_system/file_system.dart';
import 'package:butterfly/api/open.dart';
Expand All @@ -8,7 +9,6 @@ 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:go_router/go_router.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

import '../api/open_release_notes.dart';
Expand Down Expand Up @@ -510,9 +510,8 @@ class _QuickstartHomeViewState extends State<_QuickstartHomeView> {
metadata: metadata,
thumbnail: thumbnail,
onTap: () async {
GoRouter.of(context).pushReplacementNamed('new',
queryParameters: {'path': metadata.directory},
extra: e.createDocument().save());
openNewDocument(
context, e, widget.remote?.identifier);
},
);
},
Expand Down
3 changes: 3 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/79.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Add templates to data settings

View all changes in the blog: https://linwood.dev/butterfly/2.0.0-beta.15

0 comments on commit eecb9ef

Please sign in to comment.