-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Groups models & move export settings from model to localData
- Loading branch information
1 parent
ddf81ee
commit fa8ce5b
Showing
20 changed files
with
819 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.