Skip to content

Commit

Permalink
Join & share projects fix
Browse files Browse the repository at this point in the history
  • Loading branch information
BhasherBEL committed Mar 26, 2023
1 parent 829de63 commit fb8bd6f
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 99 deletions.
12 changes: 12 additions & 0 deletions lib/components/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared/components/projects_list.dart';
import 'package:shared/model/app_data.dart';
import 'package:shared/screens/new_project_screen.dart';
Expand Down Expand Up @@ -70,6 +71,17 @@ class _HomePageState extends State<HomePage> {
),
],
),
actions: [
if (hasProject && project!.code != null)
IconButton(
onPressed: () {
Share.share(
'Join my shared project with this code: ${project!.code!}',
);
},
icon: const Icon(Icons.share),
),
],
),
body: Center(
child: hasProject
Expand Down
7 changes: 3 additions & 4 deletions lib/components/new_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class _NewEntryPageState extends State<NewEntryPage> {
autocorrect: true,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(horizontal: 10),
labelText: "Title",
labelText: "What",
border: OutlineInputBorder(),
),
onChanged: (value) => bill.title = value,
Expand All @@ -131,7 +131,7 @@ class _NewEntryPageState extends State<NewEntryPage> {
decoration: const InputDecoration(
suffixText: ' €',
contentPadding: EdgeInsets.symmetric(horizontal: 10),
labelText: "Title",
labelText: "How much",
border: OutlineInputBorder(),
),
controller: amountController,
Expand Down Expand Up @@ -190,7 +190,7 @@ class _NewEntryPageState extends State<NewEntryPage> {
decoration: const InputDecoration(
contentPadding:
EdgeInsets.symmetric(horizontal: 10),
labelText: "When?",
labelText: "When",
border: OutlineInputBorder(),
hintText: 'Pick your Date',
),
Expand Down Expand Up @@ -342,7 +342,6 @@ class _NewEntryPageState extends State<NewEntryPage> {
}
},
child: Text(widget.item == null ? 'Create' : 'Update'),

),
),
),
Expand Down
66 changes: 32 additions & 34 deletions lib/model/connectors/pocketbase/item.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:pocketbase/pocketbase.dart';
import 'package:shared/model/connectors/external_connector.dart';
import 'package:shared/model/connectors/pocketbase/deleted.dart';
import 'package:shared/model/connectors/pocketbase/item_part.dart';
import 'package:shared/model/project.dart';

import '../../item.dart';
Expand Down Expand Up @@ -56,8 +57,8 @@ class PocketBaseItem implements ExternalConnector {
PocketBaseItemsFields.title: item.title,
PocketBaseItemsFields.emitterId: item.emitter.remoteId,
PocketBaseItemsFields.projectId: item.project.remoteId,
PocketBaseItemsFields.date: item.date.millisecondsSinceEpoch,
PocketBaseItemsFields.amount: item.amount,
PocketBaseItemsFields.date: item.date.toUtc().toString(),
},
);
item.remoteId = recordModel.id;
Expand All @@ -73,46 +74,43 @@ class PocketBaseItem implements ExternalConnector {
PocketBaseItemsFields.title: item.title,
PocketBaseItemsFields.emitterId: item.emitter.remoteId,
PocketBaseItemsFields.projectId: item.project.remoteId,
PocketBaseItemsFields.date: item.date.millisecondsSinceEpoch,
PocketBaseItemsFields.amount: item.amount,
PocketBaseItemsFields.date: item.date.toUtc().toString(),
},
);
return true;
}

static Future<bool> checkNews(PocketBase pb, Project project) async {
(await pb.collection("items").getFullList(
filter:
'updated > "${project.lastSync.toUtc()}" && ${PocketBaseItemsFields.projectId} = "${project.remoteId}"',
))
.map(
(e) async {
Item? i = project.itemByRemoteId(e.id);
if (i == null) {
i = Item(
project: project,
amount: e.getDoubleValue(PocketBaseItemsFields.amount),
date: DateTime.fromMillisecondsSinceEpoch(
e.getIntValue(PocketBaseItemsFields.date)),
emitter: project.participantByRemoteId(
e.getStringValue(PocketBaseItemsFields.emitterId))!,
title: e.getStringValue(PocketBaseItemsFields.title),
lastUpdate: DateTime.parse(e.updated),
remoteId: e.id,
);
project.items.add(i);
} else {
i.amount = e.getDoubleValue(PocketBaseItemsFields.amount);
i.date = DateTime.fromMillisecondsSinceEpoch(
e.getIntValue(PocketBaseItemsFields.date));
i.emitter = project.participantByRemoteId(
e.getStringValue(PocketBaseItemsFields.emitterId))!;
i.title = e.getStringValue(PocketBaseItemsFields.title);
i.lastUpdate = DateTime.parse(e.updated);
}
await i.conn.save();
},
);
List<RecordModel> records = await pb.collection("items").getFullList(
filter:
'updated > "${project.lastSync.toUtc()}" && ${PocketBaseItemsFields.projectId} = "${project.remoteId}"',
);
for (RecordModel e in records) {
Item? i = project.itemByRemoteId(e.id);
if (i == null) {
i = Item(
project: project,
amount: e.getDoubleValue(PocketBaseItemsFields.amount),
date: DateTime.parse(e.getStringValue(PocketBaseItemsFields.date)),
emitter: project.participantByRemoteId(
e.getStringValue(PocketBaseItemsFields.emitterId))!,
title: e.getStringValue(PocketBaseItemsFields.title),
lastUpdate: DateTime.parse(e.updated),
remoteId: e.id,
);
project.items.add(i);
} else {
i.amount = e.getDoubleValue(PocketBaseItemsFields.amount);
i.date = DateTime.parse(e.getStringValue(PocketBaseItemsFields.date));
i.emitter = project.participantByRemoteId(
e.getStringValue(PocketBaseItemsFields.emitterId))!;
i.title = e.getStringValue(PocketBaseItemsFields.title);
i.lastUpdate = DateTime.parse(e.updated);
}
await i.conn.save();
await PocketBaseItemPart.checkNews(pb, project, i);
}
return true;
}
}
58 changes: 30 additions & 28 deletions lib/model/connectors/pocketbase/item_part.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,36 @@ class PocketBaseItemPart implements ExternalConnector {
Project project,
Item item,
) async {
(await pb.collection("itemParts").getFullList(
filter:
'updated > "${project.lastSync.toUtc()}" && ${PocketBaseItemPartsFields.itemId} = "${item.remoteId}"',
))
.map(
(e) async {
ItemPart? i = item.partByRemoteId(e.id);
if (i == null) {
i = ItemPart(
item: item,
participant: project.participantByRemoteId(
e.getStringValue(PocketBaseItemPartsFields.participantId))!,
amount: e.getDoubleValue(PocketBaseItemPartsFields.amount),
rate: e.getDoubleValue(PocketBaseItemPartsFields.rate),
lastUpdate: DateTime.parse(e.updated),
remoteId: e.id,
);
item.itemParts.add(i);
} else {
i.participant = project.participantByRemoteId(
e.getStringValue(PocketBaseItemPartsFields.participantId))!;
i.amount = e.getDoubleValue(PocketBaseItemPartsFields.amount);
i.rate = e.getDoubleValue(PocketBaseItemPartsFields.rate);
i.lastUpdate = DateTime.parse(e.updated);
}
await i.conn.save();
},
);
List<RecordModel> records = await pb.collection("itemParts").getFullList(
filter:
'updated > "${project.lastSync.toUtc()}" && ${PocketBaseItemPartsFields.itemId} = "${item.remoteId}"',
);
for (RecordModel e in records) {
ItemPart? i = item.partByRemoteId(e.id);
if (i == null) {
i = ItemPart(
item: item,
participant: project.participantByRemoteId(
e.getStringValue(PocketBaseItemPartsFields.participantId))!,
amount: e.getStringValue(PocketBaseItemPartsFields.amount).isEmpty
? null
: e.getDoubleValue(PocketBaseItemPartsFields.amount),
rate: e.getDoubleValue(PocketBaseItemPartsFields.rate),
lastUpdate: DateTime.parse(e.updated),
remoteId: e.id,
);
item.itemParts.add(i);
} else {
i.participant = project.participantByRemoteId(
e.getStringValue(PocketBaseItemPartsFields.participantId))!;
i.amount = e.getStringValue(PocketBaseItemPartsFields.amount).isEmpty
? null
: e.getDoubleValue(PocketBaseItemPartsFields.amount);
i.rate = e.getDoubleValue(PocketBaseItemPartsFields.rate);
i.lastUpdate = DateTime.parse(e.updated);
}
await i.conn.save();
}
return true;
}
}
51 changes: 25 additions & 26 deletions lib/model/connectors/pocketbase/participant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,31 @@ class PocketBaseParticipant implements ExternalConnector {
}

static Future<bool> checkNews(PocketBase pb, Project project) async {
(await pb.collection("participants").getFullList(
filter:
'updated > "${project.lastSync.toUtc()}" && ${PocketBaseParticipantFields.projectId} = "${project.remoteId}"',
))
.map(
(e) async {
Participant? p = project.participantByRemoteId(e.id);
if (p == null) {
p = Participant(
project: project,
pseudo: e.getStringValue(PocketBaseParticipantFields.pseudo),
firstname: e.getStringValue(PocketBaseParticipantFields.firstname),
lastname: e.getStringValue(PocketBaseParticipantFields.lastname),
lastUpdate: DateTime.parse(e.updated),
remoteId: e.id,
);
project.participants.add(p);
} else {
p.pseudo = e.getStringValue(PocketBaseParticipantFields.pseudo);
p.firstname = e.getStringValue(PocketBaseParticipantFields.firstname);
p.lastname = e.getStringValue(PocketBaseParticipantFields.lastname);
p.lastUpdate = DateTime.parse(e.updated);
}
await p.conn.save();
},
);
List<RecordModel> records = await pb.collection("participants").getFullList(
filter:
'updated > "${project.lastSync.toUtc()}" && ${PocketBaseParticipantFields.projectId} = "${project.remoteId}"',
);
for (RecordModel e in records) {
Participant? p = project.participantByRemoteId(e.id);
if (p == null) {
p = Participant(
project: project,
pseudo: e.getStringValue(PocketBaseParticipantFields.pseudo),
firstname: e.getStringValue(PocketBaseParticipantFields.firstname),
lastname: e.getStringValue(PocketBaseParticipantFields.lastname),
lastUpdate: DateTime.parse(e.updated),
remoteId: e.id,
);
project.participants.add(p);
} else {
p.pseudo = e.getStringValue(PocketBaseParticipantFields.pseudo);
p.firstname = e.getStringValue(PocketBaseParticipantFields.firstname);
p.lastname = e.getStringValue(PocketBaseParticipantFields.lastname);
p.lastUpdate = DateTime.parse(e.updated);
}
await p.conn.save();
}
;
return true;
}
}
2 changes: 1 addition & 1 deletion lib/model/connectors/pocketbase/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PocketBaseProvider extends Provider {
}
}
}
await PocketBaseParticipant.checkNews(pb, project);
await PocketBaseItem.checkNews(pb, project);

project.lastSync = DateTime.now();
await project.conn.save();
Expand Down
6 changes: 1 addition & 5 deletions lib/model/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ class Project {
provider = Provider.initFromId(providerId, this, providerData);
conn = LocalProject(this);
AppData.projects.add(this);
if (lastSync == null) {
this.lastSync = DateTime(1970);
} else {
this.lastSync = lastSync;
}
this.lastSync = lastSync ?? DateTime(1970);
this.lastUpdate = lastUpdate ?? DateTime.now();
try {
currentParticipant = participants
Expand Down
14 changes: 13 additions & 1 deletion lib/screens/new_project_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class NewProjectScreen extends StatelessWidget {
if (project == null) {
project = Project(
name: setupData.projectName!,
code: setupData.join ? null : getRandom(5),
providerId: setupData.providerId!,
providerData: setupData.getProviderData(),
);
Expand Down Expand Up @@ -78,7 +79,18 @@ class NewProjectScreen extends StatelessWidget {
if (setupData.join) {
await project!.provider.joinWithTitle();
}
await project!.sync();
try {
await project!.sync();
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.toString()),
),
);
}
return;
}
if (first) {
AppData.firstRun = false;
runApp(const MainScreen());
Expand Down
Loading

0 comments on commit fb8bd6f

Please sign in to comment.