ObjectBox
This commit is contained in:
1 parent
f8317be627
commit
809bffc7a2
9 files changed
+125
-42
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import 'package:objectbox/objectbox.dart';
|
||||
import 'package:stadtbote/main.dart';
|
||||
import 'package:stadtbote/objectbox.g.dart';
|
||||
import 'package:stadtbote/tools/util.dart';
|
||||
|
||||
@Entity()
|
||||
@@ -7,6 +7,10 @@ class ChatMessage {
|
||||
@Id()
|
||||
int id = 0;
|
||||
|
||||
// External ID from server
|
||||
@Unique()
|
||||
late int externalId;
|
||||
|
||||
late int time;
|
||||
|
||||
late String from;
|
||||
@@ -21,7 +25,8 @@ class ChatMessage {
|
||||
|
||||
factory ChatMessage.fromJson(Map<String, dynamic> json) {
|
||||
final message = ChatMessage();
|
||||
message.id = json['id'] as int? ?? 0;
|
||||
message.id = 0; // Let ObjectBox assign the ID
|
||||
message.externalId = json['id'] as int; // Store external ID separately
|
||||
message.time = json['time'] as int;
|
||||
message.from = json['from'] as String;
|
||||
message.subj = json['subj'] as String;
|
||||
@@ -32,7 +37,7 @@ class ChatMessage {
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'id': externalId, // Use external ID for JSON
|
||||
'time': time,
|
||||
'from': from,
|
||||
'subj': subj,
|
||||
@@ -42,10 +47,16 @@ class ChatMessage {
|
||||
}
|
||||
|
||||
static Future<ChatMessage> mergeFromJson(Map<String, dynamic> json) async {
|
||||
var availableMessage = MyApp.objectBoxStore.box<ChatMessage>().get(json["id"] as int);
|
||||
final box = MyApp.objectBoxStore.box<ChatMessage>();
|
||||
final externalId = json["id"] as int;
|
||||
|
||||
// Find message by external ID
|
||||
final query = box.query(ChatMessage_.externalId.equals(externalId)).build();
|
||||
var availableMessage = query.findFirst();
|
||||
query.close();
|
||||
|
||||
if (availableMessage != null) {
|
||||
availableMessage.id = json["id"];
|
||||
availableMessage.externalId = json["id"];
|
||||
availableMessage.time = json["time"];
|
||||
availableMessage.from = json["from"];
|
||||
availableMessage.subj = json["subj"];
|
||||
|
||||
+8
-4
@@ -1,13 +1,16 @@
|
||||
import 'dart:convert';
|
||||
import 'package:dart_mq/dart_mq.dart';
|
||||
import 'package:objectbox/objectbox.dart';
|
||||
import 'package:stadtbote/model/job_tour.dart';
|
||||
|
||||
@Entity()
|
||||
class Job with ProducerMixin {
|
||||
class Job {
|
||||
@Id()
|
||||
int id = 0;
|
||||
|
||||
// External ID from server
|
||||
@Unique()
|
||||
late int externalId;
|
||||
|
||||
late String ver;
|
||||
late int state;
|
||||
String? crFilter;
|
||||
@@ -35,7 +38,8 @@ class Job with ProducerMixin {
|
||||
|
||||
factory Job.fromJson(Map<String, dynamic> json) {
|
||||
final job = Job();
|
||||
job.id = json['id'] as int? ?? 0;
|
||||
job.id = 0; // Let ObjectBox assign the ID
|
||||
job.externalId = json['id'] as int; // Store external ID separately
|
||||
job.ver = json['ver'] as String;
|
||||
job.state = json['state'] as int;
|
||||
job.crFilter = json['cr_filter'] as String?;
|
||||
@@ -53,7 +57,7 @@ class Job with ProducerMixin {
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'id': externalId, // Use external ID for JSON
|
||||
'ver': ver,
|
||||
'state': state,
|
||||
'cr_filter': crFilter,
|
||||
|
||||
Reference in new issue
Block a user