Skip to content

Commit

Permalink
Fix small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
BhasherBEL committed Aug 4, 2023
1 parent 0648409 commit ea5f3f7
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 60 deletions.
8 changes: 4 additions & 4 deletions lib/data/local/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LocalItemFields {
static const values = [
localId,
remoteId,
project,
projectId,
title,
emitter,
amount,
Expand All @@ -24,7 +24,7 @@ class LocalItemFields {

static const String localId = 'local_id';
static const String remoteId = 'remote_id';
static const String project = 'project';
static const String projectId = 'project';
static const String title = 'title';
static const String emitter = 'emitter';
static const String amount = 'amount';
Expand Down Expand Up @@ -70,7 +70,7 @@ class LocalItem extends LocalGeneric {
Map<String, Object?> toJson() => {
LocalItemFields.localId: item.localId,
LocalItemFields.remoteId: item.remoteId,
LocalItemFields.project: item.project.localId,
LocalItemFields.projectId: item.project.localId,
LocalItemFields.title: item.title,
LocalItemFields.emitter: item.emitter.localId,
LocalItemFields.amount: item.amount,
Expand All @@ -84,7 +84,7 @@ class LocalItem extends LocalGeneric {
if (project != null) {
p = project;
} else {
p = Project.fromId(json[LocalItemFields.project] as int)!;
p = Project.fromId(json[LocalItemFields.projectId] as int)!;
}

return Item(
Expand Down
62 changes: 41 additions & 21 deletions lib/data/local/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'participant.dart';

const String tableProjects = 'projects';

class ProjectFields {
class LocalProjectFields {
static const values = [
localId,
remoteId,
Expand Down Expand Up @@ -46,7 +46,7 @@ class LocalProject extends LocalGeneric {
static Future<Set<Project>> getAllProjects() async {
final res = await AppData.db.query(
tableProjects,
columns: ProjectFields.values,
columns: LocalProjectFields.values,
);
return res.map((e) => fromJson(e)).toSet();
}
Expand Down Expand Up @@ -76,7 +76,7 @@ class LocalProject extends LocalGeneric {
final rawItems = await AppData.db.query(
tableItems,
where:
'${LocalItemFields.project} = ? AND (${LocalItemFields.deleted} == 0 OR ${LocalItemFields.lastUpdate} > ?)',
'${LocalItemFields.projectId} = ? AND (${LocalItemFields.deleted} == 0 OR ${LocalItemFields.lastUpdate} > ?)',
whereArgs: [project.localId, project.lastSync.millisecondsSinceEpoch],
orderBy: '${LocalItemFields.date} DESC',
);
Expand Down Expand Up @@ -127,33 +127,53 @@ class LocalProject extends LocalGeneric {

Map<String, Object?> toJson() {
return {
ProjectFields.localId: project.localId,
ProjectFields.remoteId: project.remoteId,
ProjectFields.name: project.name,
ProjectFields.code: project.code ?? getRandom(5),
ProjectFields.currentParticipant: project.currentParticipant?.localId,
ProjectFields.instance: project.provider.instance.localId,
ProjectFields.lastSync: project.lastSync.millisecondsSinceEpoch,
ProjectFields.lastUpdate: project.lastUpdate.millisecondsSinceEpoch,
ProjectFields.deleted: project.deleted ? 1 : 0,
LocalProjectFields.localId: project.localId,
LocalProjectFields.remoteId: project.remoteId,
LocalProjectFields.name: project.name,
LocalProjectFields.code: project.code ?? getRandom(5),
LocalProjectFields.currentParticipant:
project.currentParticipant?.localId,
LocalProjectFields.instance: project.provider.instance.localId,
LocalProjectFields.lastSync: project.lastSync.millisecondsSinceEpoch,
LocalProjectFields.lastUpdate: project.lastUpdate.millisecondsSinceEpoch,
LocalProjectFields.deleted: project.deleted ? 1 : 0,
};
}

static Project fromJson(Map<String, Object?> json) {
return Project(
localId: json[ProjectFields.localId] as int?,
remoteId: json[ProjectFields.remoteId] as String?,
name: json[ProjectFields.name] as String,
code: json[ProjectFields.code] as String?,
currentParticipantId: json[ProjectFields.currentParticipant] as int?,
instance: Instance.fromId(json[ProjectFields.instance] as int)!,
localId: json[LocalProjectFields.localId] as int?,
remoteId: json[LocalProjectFields.remoteId] as String?,
name: json[LocalProjectFields.name] as String,
code: json[LocalProjectFields.code] as String?,
currentParticipantId: json[LocalProjectFields.currentParticipant] as int?,
instance: Instance.fromId(json[LocalProjectFields.instance] as int) ??
Instance.fromName('local')!,
lastSync: DateTime.fromMillisecondsSinceEpoch(
json[ProjectFields.lastSync] as int),
json[LocalProjectFields.lastSync] as int),
lastUpdate: DateTime.fromMillisecondsSinceEpoch(
json[ProjectFields.lastUpdate]
json[LocalProjectFields.lastUpdate]
as int //? ?? DateTime.now().millisecondsSinceEpoch
),
deleted: (json[ProjectFields.deleted] as int) == 1,
deleted: (json[LocalProjectFields.deleted] as int) == 1,
);
}

Future<bool> delete() async {
int a = await AppData.db.delete(tableProjects,
where: '${LocalProjectFields.localId} = ?',
whereArgs: [project.localId]);
a += await AppData.db.delete(tableParticipants,
where: '${LocalParticipantFields.projectId} = ?',
whereArgs: [project.localId]);
a += await AppData.db.delete(tableItems,
where: '${LocalItemFields.projectId} = ?',
whereArgs: [project.localId]);
// await AppData.db.query(tableItemParts, where: '${LocalItemPartFields.itemId} = ?', whereArgs: [project.localId]);
a += await AppData.db.delete(tableGroup,
where: '${LocalGroupFields.projectId} = ?',
whereArgs: [project.localId]);
// await AppData.db.query(tableGroupMembership, where: '${LocalGroupMembershipFields.groupId} = ?', whereArgs: [project.localId]);
return a > 0;
}
}
8 changes: 6 additions & 2 deletions lib/models/instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ class Instance {
late LocalInstance conn;

static Instance? fromId(int localId) {
return AppData.instances
.firstWhere((element) => element.localId == localId);
try {
return AppData.instances
.firstWhere((element) => element.localId == localId);
} catch (e) {
return null;
}
}

static Instance? fromName(String s) {
Expand Down
10 changes: 10 additions & 0 deletions lib/models/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,14 @@ class Project extends Data {
return null;
}
}

void clear() {
currentParticipant = null;
participants.clear();
items.clear();
name = '';
groups.clear();
lastSync = DateTime(1970);
notSyncCount = 0;
}
}
8 changes: 7 additions & 1 deletion lib/screens/new_project/new_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ class NewProjectScreen extends StatelessWidget {
}
if (first) {
AppData.firstRun = false;
runApp(const MainScreen());
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => const MainScreen(),
),
(Route<dynamic> route) => false,
);
} else {
if (context.mounted) {
if (isNew) {
Expand Down
63 changes: 43 additions & 20 deletions lib/screens/project/project_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:share_plus/share_plus.dart';
import 'package:splitr/data/local/project.dart';
import 'package:splitr/utils/ext/list.dart';
import 'package:splitr/utils/helper/confirm_box.dart';

import '../../models/project.dart';
import '../new_project/new_project.dart';
Expand All @@ -28,28 +30,37 @@ class _ProjectPageState extends State<ProjectPage> {
appBar: AppBar(
centerTitle: true,
elevation: 4,
title: Column(
children: [
Text(
widget.project.name,
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
title: Padding(
padding: const EdgeInsets.only(left: 40),
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
widget.project.name,
style: const TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
),
),
),
),
),
Text(
widget.project.participants.enabled().length <= 4
? widget.project.participants
.enabled()
.map((e) => e.pseudo)
.join(', ')
: '${widget.project.participants.enabled().length} participants',
style: const TextStyle(
fontSize: 14,
fontStyle: FontStyle.italic,
Text(
widget.project.participants.enabled().length <= 4
? widget.project.participants
.enabled()
.map((e) => e.pseudo)
.join(', ')
: '${widget.project.participants.enabled().length} participants',
style: const TextStyle(
fontSize: 14,
fontStyle: FontStyle.italic,
),
),
),
],
],
),
),
actions: [
actionMenu(),
Expand Down Expand Up @@ -130,6 +141,18 @@ class _ProjectPageState extends State<ProjectPage> {
);
setState(() {});
break;
case 4:
await confirmBox(
context: context,
title: 'Are you sure you want to recreate this project ?',
content: 'You will NOT be able to undi this action!',
onValidate: () async {
await (widget.project.conn as LocalProject).delete();
widget.project.clear();
await widget.project.provider.sync();
if (mounted) setState(() {});
});
break;
case 3:
if (Navigator.canPop(context)) {
Navigator.pop(context);
Expand Down
20 changes: 10 additions & 10 deletions lib/services/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class SplitrDatabase {
Future _createDB(Database db, int version) async {
await db.execute('''
CREATE TABLE $tableProjects (
${ProjectFields.localId} INTEGER PRIMARY KEY AUTOINCREMENT,
${ProjectFields.remoteId} TEXT,
${ProjectFields.name} TEXT NOT NULL,
${ProjectFields.code} TEXT NOT NULL,
${ProjectFields.currentParticipant} INTEGER,
${ProjectFields.instance} INTEGER NOT NULL,
${ProjectFields.lastSync} INTEGER,
${ProjectFields.lastUpdate} INTEGER,
${ProjectFields.deleted} INTEGER NOT NULL
${LocalProjectFields.localId} INTEGER PRIMARY KEY AUTOINCREMENT,
${LocalProjectFields.remoteId} TEXT,
${LocalProjectFields.name} TEXT NOT NULL,
${LocalProjectFields.code} TEXT NOT NULL,
${LocalProjectFields.currentParticipant} INTEGER,
${LocalProjectFields.instance} INTEGER NOT NULL,
${LocalProjectFields.lastSync} INTEGER,
${LocalProjectFields.lastUpdate} INTEGER,
${LocalProjectFields.deleted} INTEGER NOT NULL
)
''');

Expand All @@ -70,7 +70,7 @@ CREATE TABLE $tableParticipants (
CREATE TABLE $tableItems (
${LocalItemFields.localId} INTEGER PRIMARY KEY AUTOINCREMENT,
${LocalItemFields.remoteId} TEXT,
${LocalItemFields.project} INTEGER NOT NULL,
${LocalItemFields.projectId} INTEGER NOT NULL,
${LocalItemFields.title} TEXT NOT NULL,
${LocalItemFields.emitter} INTEGER NOT NULL,
${LocalItemFields.amount} REAL NOT NULL,
Expand Down
7 changes: 6 additions & 1 deletion lib/widgets/new_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ class NewScreen extends StatelessWidget {
width: double.infinity,
child: ElevatedButton(
onPressed: onValidate == null
? null
? () async =>
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Please check your entries'),
),
)
: () async => await onValidate!(context, formKey),
child: Padding(
padding: const EdgeInsets.all(15.0),
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: splitr is a free and open-source app that lets you create multiple

publish_to: 'none'

version: 0.4.3
version: 0.4.4

environment:
sdk: '>=2.18.6 <3.0.0'
Expand Down

0 comments on commit ea5f3f7

Please sign in to comment.