Skip to content

Commit

Permalink
Groups models & move export settings from model to localData
Browse files Browse the repository at this point in the history
  • Loading branch information
BhasherBEL committed Jun 17, 2023
1 parent ddf81ee commit fa8ce5b
Show file tree
Hide file tree
Showing 20 changed files with 819 additions and 333 deletions.
74 changes: 74 additions & 0 deletions lib/data/local/group.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'package:splitr/data/local/group_membership.dart';
import 'package:sqflite/sqflite.dart';

import '../../models/app_data.dart';
import '../../models/group.dart';
import '../../models/project.dart';
import 'generic.dart';

String tableGroup = 'groups';

class LocalGroupFields {
static const values = [
localId,
remoteId,
projectId,
lastUpdate,
deleted,
];

static const String localId = 'local_id';
static const String remoteId = 'remote_id';
static const String projectId = 'project_id';
static const String name = 'name';
static const String lastUpdate = 'last_update';
static const String deleted = 'deleted';
}

class LocalGroup extends LocalGeneric {
LocalGroup(this.group);

final Group group;

@override
Future<bool> save() async {
group.localId = await AppData.db.insert(
tableGroup,
toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
return true;
}

Map<String, Object?> toJson() => {
LocalGroupFields.localId: group.localId,
LocalGroupFields.remoteId: group.remoteId,
LocalGroupFields.projectId: group.project.localId,
LocalGroupFields.name: group.name,
LocalGroupFields.lastUpdate: group.lastUpdate,
LocalGroupFields.deleted: group.deleted,
};

static Group fromJson(Project p, Map<String, Object?> json) {
return Group(
localId: json[LocalGroupFields.localId] as int?,
remoteId: json[LocalGroupFields.remoteId] as String?,
project: p,
name: json[LocalGroupFields.name] as String,
lastUpdate: DateTime.fromMillisecondsSinceEpoch(
json[LocalGroupFields.lastUpdate] as int),
deleted: (json[LocalGroupFields.deleted] as int) == 1,
);
}

Future loadMembers() async {
group.members = (await AppData.db.query(
tableGroupMembership,
columns: LocalGroupMembershipFields.values,
where: '${LocalGroupMembershipFields.groupId} = ?',
whereArgs: [group.localId],
))
.map((e) => LocalGroupMembership.fromJson(group, e))
.toSet();
}
}
63 changes: 63 additions & 0 deletions lib/data/local/group_membership.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:splitr/models/group.dart';
import 'package:sqflite/sqflite.dart';

import '../../models/app_data.dart';
import '../../models/group_membership.dart';
import 'generic.dart';

String tableGroupMembership = 'groupMemberships';

class LocalGroupMembershipFields {
static const values = [
localId,
remoteId,
groupId,
participantId,
lastUpdate,
deleted,
];

static const String localId = 'local_id';
static const String remoteId = 'remote_id';
static const String groupId = 'group_id';
static const String participantId = 'participant_id';
static const String lastUpdate = 'last_update';
static const String deleted = 'deleted';
}

class LocalGroupMembership extends LocalGeneric {
LocalGroupMembership(this.groupMembership);

final GroupMembership groupMembership;

@override
Future<bool> save() async {
groupMembership.localId = await AppData.db.insert(
tableGroupMembership,
toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
return true;
}

Map<String, Object?> toJson() => {
LocalGroupMembershipFields.localId: groupMembership.localId,
LocalGroupMembershipFields.remoteId: groupMembership.remoteId,
LocalGroupMembershipFields.groupId: groupMembership.group.localId,
LocalGroupMembershipFields.lastUpdate: groupMembership.lastUpdate,
LocalGroupMembershipFields.deleted: groupMembership.deleted,
};

static GroupMembership fromJson(Group group, Map<String, Object?> json) {
return GroupMembership(
localId: json[LocalGroupMembershipFields.localId] as int?,
remoteId: json[LocalGroupMembershipFields.remoteId] as String?,
group: group,
participant: group.project.participants.firstWhere((e) =>
e.localId == json[LocalGroupMembershipFields.participantId] as int),
lastUpdate: DateTime.fromMillisecondsSinceEpoch(
json[LocalGroupMembershipFields.lastUpdate] as int),
deleted: (json[LocalGroupMembershipFields.deleted] as int) == 1,
);
}
}
47 changes: 45 additions & 2 deletions lib/data/local/instance.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import 'dart:convert';

import 'package:sqflite/sqflite.dart';

import '../../models/app_data.dart';
import '../../models/instance.dart';

const String tableInstances = 'instances';

class LocalInstanceFields {
static const values = [
localId,
type,
name,
data,
];

static const String localId = 'local_id';
static const String type = 'type';
static const String name = 'name';
static const String data = 'data';
}

class LocalInstance {
LocalInstance(this.instance);

Expand All @@ -13,17 +29,44 @@ class LocalInstance {
Future save() async {
instance.localId = await AppData.db.insert(
tableInstances,
instance.toJson(),
toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

Future delete() async {
return await AppData.db.delete(
tableInstances,
where: '${InstanceFields.localId} = ?',
where: '${LocalInstanceFields.localId} = ?',
whereArgs: [instance.localId],
) >
0;
}

Map<String, Object?> toJson() {
return {
LocalInstanceFields.localId: instance.localId,
LocalInstanceFields.type: instance.type,
LocalInstanceFields.name: instance.name,
LocalInstanceFields.data: json.encode(instance.data),
};
}

static Instance fromJson(Map<String, Object?> jsonMap) {
return Instance(
localId: jsonMap[LocalInstanceFields.localId] as int?,
type: jsonMap[LocalInstanceFields.type] as String,
name: jsonMap[LocalInstanceFields.name] as String,
data: json.decode(jsonMap[LocalInstanceFields.data] as String)
as Map<String, dynamic>,
);
}

static Future<Set<Instance>> getAllInstances() async {
final res = await AppData.db.query(
tableInstances,
columns: LocalInstanceFields.values,
);
return res.map((e) => fromJson(e)).toSet();
}
}
69 changes: 65 additions & 4 deletions lib/data/local/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,35 @@ import 'package:sqflite/sqlite_api.dart';
import '../../models/app_data.dart';
import '../../models/item.dart';
import '../../models/item_part.dart';
import '../../models/project.dart';
import 'item_part.dart';

const String tableItems = 'items';

class LocalItemFields {
static const values = [
localId,
remoteId,
project,
title,
emitter,
amount,
date,
lastUpdate,
deleted,
];

static const String localId = 'local_id';
static const String remoteId = 'remote_id';
static const String project = 'project';
static const String title = 'title';
static const String emitter = 'emitter';
static const String amount = 'amount';
static const String date = 'date';
static const String lastUpdate = 'last_update';
static const String deleted = 'deleted';
}

class LocalItem extends LocalGeneric {
LocalItem(this.item);

Expand All @@ -16,19 +41,19 @@ class LocalItem extends LocalGeneric {
Future loadParts() async {
item.itemParts = (await AppData.db.query(
tableItemParts,
columns: ItemPartFields.values,
where: '${ItemPartFields.itemId} = ?',
columns: LocalItemPartFields.values,
where: '${LocalItemPartFields.itemId} = ?',
whereArgs: [item.localId],
))
.map((e) => ItemPart.fromJson(e, item))
.map((e) => LocalItemPart.fromJson(e, item))
.toList();
}

@override
Future<bool> save() async {
item.localId = await AppData.db.insert(
tableItems,
item.toJson(),
toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
item.project.notSyncCount++;
Expand All @@ -41,4 +66,40 @@ class LocalItem extends LocalGeneric {
await ip.conn.save();
}
}

Map<String, Object?> toJson() => {
LocalItemFields.localId: item.localId,
LocalItemFields.remoteId: item.remoteId,
LocalItemFields.project: item.project.localId,
LocalItemFields.title: item.title,
LocalItemFields.emitter: item.emitter.localId,
LocalItemFields.amount: item.amount,
LocalItemFields.date: item.date.millisecondsSinceEpoch,
LocalItemFields.lastUpdate: DateTime.now().millisecondsSinceEpoch,
LocalItemFields.deleted: item.deleted ? 1 : 0,
};

static Item fromJson(Map<String, Object?> json, {Project? project}) {
Project p;
if (project != null) {
p = project;
} else {
p = Project.fromId(json[LocalItemFields.project] as int)!;
}

return Item(
localId: json[LocalItemFields.localId] as int?,
remoteId: json[LocalItemFields.remoteId] as String?,
project: p,
title: json[LocalItemFields.title] as String,
emitter: p.participants.firstWhere((participant) =>
participant.localId == json[LocalItemFields.emitter] as int),
amount: json[LocalItemFields.amount] as double,
date: DateTime.fromMillisecondsSinceEpoch(
json[LocalItemFields.date] as int),
lastUpdate: DateTime.fromMillisecondsSinceEpoch(
json[LocalItemFields.lastUpdate] as int),
deleted: (json[LocalItemFields.deleted] as int) == 1,
);
}
}
51 changes: 50 additions & 1 deletion lib/data/local/item_part.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@ import 'package:splitr/data/local/generic.dart';
import 'package:sqflite/sqflite.dart';

import '../../models/app_data.dart';
import '../../models/item.dart';
import '../../models/item_part.dart';

const String tableItemParts = 'itemParts';

class LocalItemPartFields {
static const values = [
localId,
remoteId,
itemId,
participantId,
rate,
amount,
lastUpdate,
deleted,
];

static const String localId = 'local_id';
static const String remoteId = 'remote_id';
static const String itemId = 'item';
static const String participantId = 'participant';
static const String rate = 'rate';
static const String amount = 'amount';
static const String lastUpdate = 'last_update';
static const String deleted = 'deleted';
}

class LocalItemPart extends LocalGeneric {
LocalItemPart(this.itemPart);

Expand All @@ -15,11 +38,37 @@ class LocalItemPart extends LocalGeneric {
Future<bool> save() async {
itemPart.localId = await AppData.db.insert(
tableItemParts,
itemPart.toJson(),
toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);

itemPart.item.project.notSyncCount++;
return true;
}

Map<String, Object?> toJson() => {
LocalItemPartFields.localId: itemPart.localId,
LocalItemPartFields.remoteId: itemPart.remoteId,
LocalItemPartFields.itemId: itemPart.item.localId,
LocalItemPartFields.participantId: itemPart.participant.localId,
LocalItemPartFields.rate: itemPart.rate,
LocalItemPartFields.amount: itemPart.amount,
LocalItemPartFields.lastUpdate: DateTime.now().millisecondsSinceEpoch,
LocalItemPartFields.deleted: itemPart.deleted ? 1 : 0,
};

static ItemPart fromJson(Map<String, Object?> json, Item item) {
return ItemPart(
localId: json[LocalItemPartFields.localId] as int?,
remoteId: json[LocalItemPartFields.remoteId] as String?,
item: item,
participant: item.project.participants.firstWhere(
(e) => e.localId == json[LocalItemPartFields.participantId] as int),
rate: json[LocalItemPartFields.rate] as double?,
amount: json[LocalItemPartFields.amount] as double?,
lastUpdate: DateTime.fromMillisecondsSinceEpoch(
json[LocalItemPartFields.lastUpdate] as int),
deleted: (json[LocalItemPartFields.deleted] as int) == 1,
);
}
}
Loading

0 comments on commit fa8ce5b

Please sign in to comment.