Skip to content

Commit

Permalink
Fix share & participant removal
Browse files Browse the repository at this point in the history
  • Loading branch information
BhasherBEL committed Jun 6, 2023
1 parent 9c60832 commit 0d4a6b0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 43 deletions.
30 changes: 27 additions & 3 deletions lib/components/pages/project/project_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -98,9 +99,32 @@ class _ProjectPageState extends State<ProjectPage> {
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(
Expand Down
75 changes: 37 additions & 38 deletions lib/model/app_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AppData {
static late Database db;
static Project? _current;
static Set<Instance> instances = {};
static bool hasBeenInit = false;

static bool get firstRun {
return _firstRun;
Expand Down Expand Up @@ -45,6 +46,7 @@ class AppData {
}

static init() async {
hasBeenInit = true;
sharedPreferences = await SharedPreferences.getInstance();
db = await SharedDatabase.instance.database;

Expand All @@ -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,
),
);
});
}
}
10 changes: 10 additions & 0 deletions lib/model/connectors/local/participant.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:shared/model/app_data.dart';
import 'package:shared/model/connectors/local/deleted.dart';

import '../../participant.dart';

Expand Down Expand Up @@ -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(),
);
}
}
}
6 changes: 6 additions & 0 deletions lib/model/connectors/local/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/screens/new_project_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
Expand Down Expand Up @@ -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());
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 0d4a6b0

Please sign in to comment.