Skip to content

Commit ea5f3f7

Browse files
committed
Fix small issues
1 parent 0648409 commit ea5f3f7

File tree

9 files changed

+128
-60
lines changed

9 files changed

+128
-60
lines changed

lib/data/local/item.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LocalItemFields {
1313
static const values = [
1414
localId,
1515
remoteId,
16-
project,
16+
projectId,
1717
title,
1818
emitter,
1919
amount,
@@ -24,7 +24,7 @@ class LocalItemFields {
2424

2525
static const String localId = 'local_id';
2626
static const String remoteId = 'remote_id';
27-
static const String project = 'project';
27+
static const String projectId = 'project';
2828
static const String title = 'title';
2929
static const String emitter = 'emitter';
3030
static const String amount = 'amount';
@@ -70,7 +70,7 @@ class LocalItem extends LocalGeneric {
7070
Map<String, Object?> toJson() => {
7171
LocalItemFields.localId: item.localId,
7272
LocalItemFields.remoteId: item.remoteId,
73-
LocalItemFields.project: item.project.localId,
73+
LocalItemFields.projectId: item.project.localId,
7474
LocalItemFields.title: item.title,
7575
LocalItemFields.emitter: item.emitter.localId,
7676
LocalItemFields.amount: item.amount,
@@ -84,7 +84,7 @@ class LocalItem extends LocalGeneric {
8484
if (project != null) {
8585
p = project;
8686
} else {
87-
p = Project.fromId(json[LocalItemFields.project] as int)!;
87+
p = Project.fromId(json[LocalItemFields.projectId] as int)!;
8888
}
8989

9090
return Item(

lib/data/local/project.dart

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'participant.dart';
1414

1515
const String tableProjects = 'projects';
1616

17-
class ProjectFields {
17+
class LocalProjectFields {
1818
static const values = [
1919
localId,
2020
remoteId,
@@ -46,7 +46,7 @@ class LocalProject extends LocalGeneric {
4646
static Future<Set<Project>> getAllProjects() async {
4747
final res = await AppData.db.query(
4848
tableProjects,
49-
columns: ProjectFields.values,
49+
columns: LocalProjectFields.values,
5050
);
5151
return res.map((e) => fromJson(e)).toSet();
5252
}
@@ -76,7 +76,7 @@ class LocalProject extends LocalGeneric {
7676
final rawItems = await AppData.db.query(
7777
tableItems,
7878
where:
79-
'${LocalItemFields.project} = ? AND (${LocalItemFields.deleted} == 0 OR ${LocalItemFields.lastUpdate} > ?)',
79+
'${LocalItemFields.projectId} = ? AND (${LocalItemFields.deleted} == 0 OR ${LocalItemFields.lastUpdate} > ?)',
8080
whereArgs: [project.localId, project.lastSync.millisecondsSinceEpoch],
8181
orderBy: '${LocalItemFields.date} DESC',
8282
);
@@ -127,33 +127,53 @@ class LocalProject extends LocalGeneric {
127127

128128
Map<String, Object?> toJson() {
129129
return {
130-
ProjectFields.localId: project.localId,
131-
ProjectFields.remoteId: project.remoteId,
132-
ProjectFields.name: project.name,
133-
ProjectFields.code: project.code ?? getRandom(5),
134-
ProjectFields.currentParticipant: project.currentParticipant?.localId,
135-
ProjectFields.instance: project.provider.instance.localId,
136-
ProjectFields.lastSync: project.lastSync.millisecondsSinceEpoch,
137-
ProjectFields.lastUpdate: project.lastUpdate.millisecondsSinceEpoch,
138-
ProjectFields.deleted: project.deleted ? 1 : 0,
130+
LocalProjectFields.localId: project.localId,
131+
LocalProjectFields.remoteId: project.remoteId,
132+
LocalProjectFields.name: project.name,
133+
LocalProjectFields.code: project.code ?? getRandom(5),
134+
LocalProjectFields.currentParticipant:
135+
project.currentParticipant?.localId,
136+
LocalProjectFields.instance: project.provider.instance.localId,
137+
LocalProjectFields.lastSync: project.lastSync.millisecondsSinceEpoch,
138+
LocalProjectFields.lastUpdate: project.lastUpdate.millisecondsSinceEpoch,
139+
LocalProjectFields.deleted: project.deleted ? 1 : 0,
139140
};
140141
}
141142

142143
static Project fromJson(Map<String, Object?> json) {
143144
return Project(
144-
localId: json[ProjectFields.localId] as int?,
145-
remoteId: json[ProjectFields.remoteId] as String?,
146-
name: json[ProjectFields.name] as String,
147-
code: json[ProjectFields.code] as String?,
148-
currentParticipantId: json[ProjectFields.currentParticipant] as int?,
149-
instance: Instance.fromId(json[ProjectFields.instance] as int)!,
145+
localId: json[LocalProjectFields.localId] as int?,
146+
remoteId: json[LocalProjectFields.remoteId] as String?,
147+
name: json[LocalProjectFields.name] as String,
148+
code: json[LocalProjectFields.code] as String?,
149+
currentParticipantId: json[LocalProjectFields.currentParticipant] as int?,
150+
instance: Instance.fromId(json[LocalProjectFields.instance] as int) ??
151+
Instance.fromName('local')!,
150152
lastSync: DateTime.fromMillisecondsSinceEpoch(
151-
json[ProjectFields.lastSync] as int),
153+
json[LocalProjectFields.lastSync] as int),
152154
lastUpdate: DateTime.fromMillisecondsSinceEpoch(
153-
json[ProjectFields.lastUpdate]
155+
json[LocalProjectFields.lastUpdate]
154156
as int //? ?? DateTime.now().millisecondsSinceEpoch
155157
),
156-
deleted: (json[ProjectFields.deleted] as int) == 1,
158+
deleted: (json[LocalProjectFields.deleted] as int) == 1,
157159
);
158160
}
161+
162+
Future<bool> delete() async {
163+
int a = await AppData.db.delete(tableProjects,
164+
where: '${LocalProjectFields.localId} = ?',
165+
whereArgs: [project.localId]);
166+
a += await AppData.db.delete(tableParticipants,
167+
where: '${LocalParticipantFields.projectId} = ?',
168+
whereArgs: [project.localId]);
169+
a += await AppData.db.delete(tableItems,
170+
where: '${LocalItemFields.projectId} = ?',
171+
whereArgs: [project.localId]);
172+
// await AppData.db.query(tableItemParts, where: '${LocalItemPartFields.itemId} = ?', whereArgs: [project.localId]);
173+
a += await AppData.db.delete(tableGroup,
174+
where: '${LocalGroupFields.projectId} = ?',
175+
whereArgs: [project.localId]);
176+
// await AppData.db.query(tableGroupMembership, where: '${LocalGroupMembershipFields.groupId} = ?', whereArgs: [project.localId]);
177+
return a > 0;
178+
}
159179
}

lib/models/instance.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ class Instance {
1818
late LocalInstance conn;
1919

2020
static Instance? fromId(int localId) {
21-
return AppData.instances
22-
.firstWhere((element) => element.localId == localId);
21+
try {
22+
return AppData.instances
23+
.firstWhere((element) => element.localId == localId);
24+
} catch (e) {
25+
return null;
26+
}
2327
}
2428

2529
static Instance? fromName(String s) {

lib/models/project.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,14 @@ class Project extends Data {
170170
return null;
171171
}
172172
}
173+
174+
void clear() {
175+
currentParticipant = null;
176+
participants.clear();
177+
items.clear();
178+
name = '';
179+
groups.clear();
180+
lastSync = DateTime(1970);
181+
notSyncCount = 0;
182+
}
173183
}

lib/screens/new_project/new_project.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ class NewProjectScreen extends StatelessWidget {
6969
}
7070
if (first) {
7171
AppData.firstRun = false;
72-
runApp(const MainScreen());
72+
Navigator.pushAndRemoveUntil(
73+
context,
74+
MaterialPageRoute(
75+
builder: (context) => const MainScreen(),
76+
),
77+
(Route<dynamic> route) => false,
78+
);
7379
} else {
7480
if (context.mounted) {
7581
if (isNew) {

lib/screens/project/project_page.dart

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
33
import 'package:share_plus/share_plus.dart';
4+
import 'package:splitr/data/local/project.dart';
45
import 'package:splitr/utils/ext/list.dart';
6+
import 'package:splitr/utils/helper/confirm_box.dart';
57

68
import '../../models/project.dart';
79
import '../new_project/new_project.dart';
@@ -28,28 +30,37 @@ class _ProjectPageState extends State<ProjectPage> {
2830
appBar: AppBar(
2931
centerTitle: true,
3032
elevation: 4,
31-
title: Column(
32-
children: [
33-
Text(
34-
widget.project.name,
35-
style: const TextStyle(
36-
fontSize: 24,
37-
fontWeight: FontWeight.bold,
33+
title: Padding(
34+
padding: const EdgeInsets.only(left: 40),
35+
child: Column(
36+
children: [
37+
Padding(
38+
padding: const EdgeInsets.only(left: 10, right: 10),
39+
child: FittedBox(
40+
fit: BoxFit.scaleDown,
41+
child: Text(
42+
widget.project.name,
43+
style: const TextStyle(
44+
fontSize: 26,
45+
fontWeight: FontWeight.bold,
46+
),
47+
),
48+
),
3849
),
39-
),
40-
Text(
41-
widget.project.participants.enabled().length <= 4
42-
? widget.project.participants
43-
.enabled()
44-
.map((e) => e.pseudo)
45-
.join(', ')
46-
: '${widget.project.participants.enabled().length} participants',
47-
style: const TextStyle(
48-
fontSize: 14,
49-
fontStyle: FontStyle.italic,
50+
Text(
51+
widget.project.participants.enabled().length <= 4
52+
? widget.project.participants
53+
.enabled()
54+
.map((e) => e.pseudo)
55+
.join(', ')
56+
: '${widget.project.participants.enabled().length} participants',
57+
style: const TextStyle(
58+
fontSize: 14,
59+
fontStyle: FontStyle.italic,
60+
),
5061
),
51-
),
52-
],
62+
],
63+
),
5364
),
5465
actions: [
5566
actionMenu(),
@@ -130,6 +141,18 @@ class _ProjectPageState extends State<ProjectPage> {
130141
);
131142
setState(() {});
132143
break;
144+
case 4:
145+
await confirmBox(
146+
context: context,
147+
title: 'Are you sure you want to recreate this project ?',
148+
content: 'You will NOT be able to undi this action!',
149+
onValidate: () async {
150+
await (widget.project.conn as LocalProject).delete();
151+
widget.project.clear();
152+
await widget.project.provider.sync();
153+
if (mounted) setState(() {});
154+
});
155+
break;
133156
case 3:
134157
if (Navigator.canPop(context)) {
135158
Navigator.pop(context);

lib/services/database.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class SplitrDatabase {
4141
Future _createDB(Database db, int version) async {
4242
await db.execute('''
4343
CREATE TABLE $tableProjects (
44-
${ProjectFields.localId} INTEGER PRIMARY KEY AUTOINCREMENT,
45-
${ProjectFields.remoteId} TEXT,
46-
${ProjectFields.name} TEXT NOT NULL,
47-
${ProjectFields.code} TEXT NOT NULL,
48-
${ProjectFields.currentParticipant} INTEGER,
49-
${ProjectFields.instance} INTEGER NOT NULL,
50-
${ProjectFields.lastSync} INTEGER,
51-
${ProjectFields.lastUpdate} INTEGER,
52-
${ProjectFields.deleted} INTEGER NOT NULL
44+
${LocalProjectFields.localId} INTEGER PRIMARY KEY AUTOINCREMENT,
45+
${LocalProjectFields.remoteId} TEXT,
46+
${LocalProjectFields.name} TEXT NOT NULL,
47+
${LocalProjectFields.code} TEXT NOT NULL,
48+
${LocalProjectFields.currentParticipant} INTEGER,
49+
${LocalProjectFields.instance} INTEGER NOT NULL,
50+
${LocalProjectFields.lastSync} INTEGER,
51+
${LocalProjectFields.lastUpdate} INTEGER,
52+
${LocalProjectFields.deleted} INTEGER NOT NULL
5353
)
5454
''');
5555

@@ -70,7 +70,7 @@ CREATE TABLE $tableParticipants (
7070
CREATE TABLE $tableItems (
7171
${LocalItemFields.localId} INTEGER PRIMARY KEY AUTOINCREMENT,
7272
${LocalItemFields.remoteId} TEXT,
73-
${LocalItemFields.project} INTEGER NOT NULL,
73+
${LocalItemFields.projectId} INTEGER NOT NULL,
7474
${LocalItemFields.title} TEXT NOT NULL,
7575
${LocalItemFields.emitter} INTEGER NOT NULL,
7676
${LocalItemFields.amount} REAL NOT NULL,

lib/widgets/new_screen.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ class NewScreen extends StatelessWidget {
4848
width: double.infinity,
4949
child: ElevatedButton(
5050
onPressed: onValidate == null
51-
? null
51+
? () async =>
52+
ScaffoldMessenger.of(context).showSnackBar(
53+
const SnackBar(
54+
content: Text('Please check your entries'),
55+
),
56+
)
5257
: () async => await onValidate!(context, formKey),
5358
child: Padding(
5459
padding: const EdgeInsets.all(15.0),

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: splitr is a free and open-source app that lets you create multiple
33

44
publish_to: 'none'
55

6-
version: 0.4.3
6+
version: 0.4.4
77

88
environment:
99
sdk: '>=2.18.6 <3.0.0'

0 commit comments

Comments
 (0)