From 0d4a6b0e3826442464ef58e6ba3f22245c1d19db Mon Sep 17 00:00:00 2001 From: Bhasher Date: Tue, 6 Jun 2023 20:19:37 +0200 Subject: [PATCH] Fix share & participant removal --- .../pages/project/project_page.dart | 30 +++++++- lib/model/app_data.dart | 75 +++++++++---------- lib/model/connectors/local/participant.dart | 10 +++ lib/model/connectors/local/project.dart | 6 ++ lib/screens/new_project_screen.dart | 11 ++- pubspec.yaml | 2 +- 6 files changed, 91 insertions(+), 43 deletions(-) diff --git a/lib/components/pages/project/project_page.dart b/lib/components/pages/project/project_page.dart index 93da729..976d1ae 100644 --- a/lib/components/pages/project/project_page.dart +++ b/lib/components/pages/project/project_page.dart @@ -4,6 +4,7 @@ import 'package:share_plus/share_plus.dart'; import 'package:shared/components/pages/project/balances/balancing_page_part.dart'; import 'package:shared/components/pages/projects_list/projects_list_page.dart'; import 'package:shared/screens/new_project_screen.dart'; +import 'package:shared/utils/dialogs/confirm_box.dart'; import '../../../model/project.dart'; import 'expenses/new_entry.dart'; @@ -98,9 +99,32 @@ class _ProjectPageState extends State { onSelected: (value) async { switch (value) { case 0: - // Share.share( - // 'Join my shared project with this link:\nhttps://shared.bhasher.com/join?type=${widget.project.provider.name}&instance=${Uri.encodeComponent(widget.project.provider.getInstance())}&code=${widget.project.code}', - // ); + showDialog( + context: context, + builder: (context) => AlertDialog( + title: const Text("Share project"), + content: const Text( + "You're about to share this project. Please note that in order to join, you must already be connected to the same instance."), + actions: [ + TextButton( + onPressed: () => Share.share( + 'Join my shared project!\n\nInstance: ${Uri.encodeComponent(widget.project.provider.instance.name)}\nCode: ${widget.project.code}', + ), + child: const Text("Share code"), + ), + TextButton( + onPressed: () => Share.share( + 'Join my shared project!\nhttps://shared.bhasher.com/join?instance=${Uri.encodeComponent(widget.project.provider.instance.name)}&code=${widget.project.code}', + ), + child: const Text("Share link"), + ), + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: const Text("Cancel"), + ), + ], + ), + ); break; case 1: await Navigator.push( diff --git a/lib/model/app_data.dart b/lib/model/app_data.dart index ce1beae..7cb8977 100644 --- a/lib/model/app_data.dart +++ b/lib/model/app_data.dart @@ -18,6 +18,7 @@ class AppData { static late Database db; static Project? _current; static Set instances = {}; + static bool hasBeenInit = false; static bool get firstRun { return _firstRun; @@ -45,6 +46,7 @@ class AppData { } static init() async { + hasBeenInit = true; sharedPreferences = await SharedPreferences.getInstance(); db = await SharedDatabase.instance.database; @@ -67,44 +69,41 @@ class AppData { } catch (e) {} } - // final _appLinks = AppLinks(); - - // _appLinks.allUriLinkStream.listen((uri) { - // if (uri.queryParameters.containsKey('type')) { - // runApp( - // _NewProjectFromLink( - // type: uri.queryParameters['type'], - // code: uri.queryParameters['code'], - // instance: - // Uri.decodeComponent(uri.queryParameters['instance'] ?? ""), - // ), - // ); - // } - // }); + final appLinks = AppLinks(); + + appLinks.allUriLinkStream.listen((uri) { + if (uri.queryParameters.containsKey('code') && + uri.queryParameters.containsKey('instance')) { + runApp( + _NewProjectFromLink( + code: uri.queryParameters['code']!, + instanceName: uri.queryParameters['instance']!, + ), + ); + } + }); } } -// class _NewProjectFromLink extends StatelessWidget { -// const _NewProjectFromLink({this.type, this.code, this.instance}); - -// final String? type; -// final String? code; -// final String? instance; - -// @override -// Widget build(BuildContext context) { -// return DynamicColorBuilder(builder: (lightColorScheme, darkColorScheme) { -// return MaterialApp( -// title: 'Shared', -// theme: defaultTheme, -// darkTheme: defaultDarkTheme, -// themeMode: ThemeMode.system, -// home: NewProjectScreen( -// type: type, -// code: code, -// instance: instance, -// ), -// ); -// }); -// } -// } +class _NewProjectFromLink extends StatelessWidget { + const _NewProjectFromLink({required this.code, required this.instanceName}); + + final String code; + final String instanceName; + + @override + Widget build(BuildContext context) { + return DynamicColorBuilder(builder: (lightColorScheme, darkColorScheme) { + return MaterialApp( + title: 'Shared', + theme: defaultTheme, + darkTheme: defaultDarkTheme, + themeMode: ThemeMode.system, + home: NewProjectScreen( + instance: Instance.fromName(instanceName), + code: code, + ), + ); + }); + } +} diff --git a/lib/model/connectors/local/participant.dart b/lib/model/connectors/local/participant.dart index cb352e5..b9a9b55 100644 --- a/lib/model/connectors/local/participant.dart +++ b/lib/model/connectors/local/participant.dart @@ -1,4 +1,5 @@ import 'package:shared/model/app_data.dart'; +import 'package:shared/model/connectors/local/deleted.dart'; import '../../participant.dart'; @@ -39,5 +40,14 @@ class LocalParticipant { where: '${ParticipantFields.localId} = ?', whereArgs: [participant.localId], ); + + if (participant.remoteId != null) { + await LocalDeleted.add( + 'participants', + participant.remoteId!, + participant.project, + DateTime.now(), + ); + } } } diff --git a/lib/model/connectors/local/project.dart b/lib/model/connectors/local/project.dart index 89a5a9d..680a9eb 100644 --- a/lib/model/connectors/local/project.dart +++ b/lib/model/connectors/local/project.dart @@ -85,6 +85,12 @@ class LocalProject { for (Item item in project.items) { await item.conn.delete(); } + + await AppData.db.delete( + tableDeleted, + where: '${DeletedFields.projectId} = ?', + whereArgs: [project.remoteId], + ); } return res; } diff --git a/lib/screens/new_project_screen.dart b/lib/screens/new_project_screen.dart index 9859b37..86f3b89 100644 --- a/lib/screens/new_project_screen.dart +++ b/lib/screens/new_project_screen.dart @@ -34,6 +34,11 @@ class NewProjectScreen extends StatelessWidget { setupData.instance = instance; bool newProject = project == null; + if (newProject && code != null) { + setupData.projectName = code; + setupData.join = true; + } + return NewScreen( title: first ? 'Create your first project!' @@ -100,7 +105,11 @@ class NewProjectScreen extends StatelessWidget { ), ); } else { - Navigator.pop(context, true); + if (Navigator.canPop(context)) { + Navigator.pop(context, true); + } else { + runApp(const MainScreen()); + } } } } diff --git a/pubspec.yaml b/pubspec.yaml index 5747ae8..a31e64b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: Shared is a free and open-source app that lets you create multiple publish_to: 'none' -version: 0.2.0 +version: 0.2.1 environment: sdk: '>=2.18.6 <3.0.0'