feat: add alot
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
import '../utils/app_config.dart';
|
||||
|
||||
enum CommandType {
|
||||
closeTab('close_tab');
|
||||
|
||||
final String value;
|
||||
|
||||
const CommandType(this.value);
|
||||
|
||||
static CommandType? fromValue(String value) {
|
||||
for (final type in CommandType.values) {
|
||||
if (type.value == value) return type;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
enum CommandResult { completed, conflict, failed, expired }
|
||||
|
||||
class RemoteCommand {
|
||||
final String id;
|
||||
final String type;
|
||||
final String rawType;
|
||||
final String tabId;
|
||||
final int expectedVersion;
|
||||
final String paymentMethod;
|
||||
@@ -9,7 +28,7 @@ class RemoteCommand {
|
||||
|
||||
const RemoteCommand({
|
||||
required this.id,
|
||||
required this.type,
|
||||
required this.rawType,
|
||||
required this.tabId,
|
||||
required this.expectedVersion,
|
||||
required this.paymentMethod,
|
||||
@@ -20,7 +39,7 @@ class RemoteCommand {
|
||||
factory RemoteCommand.fromJson(Map<String, dynamic> json) {
|
||||
return RemoteCommand(
|
||||
id: json['id'] as String,
|
||||
type: json['type'] as String,
|
||||
rawType: json['type'] as String,
|
||||
tabId: json['tabId'] as String,
|
||||
expectedVersion: json['expectedVersion'] as int,
|
||||
paymentMethod: json['paymentMethod'] as String? ?? 'cash',
|
||||
@@ -29,8 +48,13 @@ class RemoteCommand {
|
||||
);
|
||||
}
|
||||
|
||||
/// Null when the server sends a command this app version doesn't know.
|
||||
CommandType? get type => CommandType.fromValue(rawType);
|
||||
|
||||
bool get isExpired {
|
||||
final cutoff = DateTime.now().subtract(const Duration(hours: 24));
|
||||
final cutoff = DateTime.now().subtract(
|
||||
const Duration(hours: kCommandExpiryHours),
|
||||
);
|
||||
return createdAt.isBefore(cutoff);
|
||||
}
|
||||
}
|
||||
@@ -40,24 +64,23 @@ class SyncResponse {
|
||||
final String? cursor;
|
||||
final List<String> plannedTabNames;
|
||||
|
||||
SyncResponse({
|
||||
const SyncResponse({
|
||||
required this.commands,
|
||||
this.cursor,
|
||||
this.plannedTabNames = const [],
|
||||
});
|
||||
|
||||
factory SyncResponse.fromJson(Map<String, dynamic> json) {
|
||||
final commandsList = (json['commands'] as List<dynamic>?)
|
||||
?.map((e) => RemoteCommand.fromJson(e as Map<String, dynamic>))
|
||||
.toList() ??
|
||||
[];
|
||||
return SyncResponse(
|
||||
commands: commandsList,
|
||||
commands: [
|
||||
for (final command in json['commands'] as List<dynamic>? ?? [])
|
||||
RemoteCommand.fromJson(command as Map<String, dynamic>),
|
||||
],
|
||||
cursor: json['cursor'] as String?,
|
||||
plannedTabNames: (json['plannedTabNames'] as List<dynamic>?)
|
||||
?.map((e) => e as String)
|
||||
.toList() ??
|
||||
[],
|
||||
plannedTabNames: [
|
||||
for (final name in json['plannedTabNames'] as List<dynamic>? ?? [])
|
||||
name as String,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -78,12 +101,12 @@ class SnapshotTab {
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'customerName': customerName,
|
||||
'version': version,
|
||||
'totalInCents': totalInCents,
|
||||
'itemCount': itemCount,
|
||||
};
|
||||
'id': id,
|
||||
'customerName': customerName,
|
||||
'version': version,
|
||||
'totalInCents': totalInCents,
|
||||
'itemCount': itemCount,
|
||||
};
|
||||
}
|
||||
|
||||
class SyncRequest {
|
||||
@@ -98,15 +121,15 @@ class SyncRequest {
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'deviceId': deviceId,
|
||||
if (cursor != null) 'cursor': cursor,
|
||||
'openTabs': openTabs.map((t) => t.toJson()).toList(),
|
||||
};
|
||||
'deviceId': deviceId,
|
||||
if (cursor != null) 'cursor': cursor,
|
||||
'openTabs': openTabs.map((tab) => tab.toJson()).toList(),
|
||||
};
|
||||
}
|
||||
|
||||
class CommandAck {
|
||||
final String commandId;
|
||||
final String status;
|
||||
final CommandResult status;
|
||||
final String? result;
|
||||
|
||||
const CommandAck({
|
||||
@@ -116,10 +139,10 @@ class CommandAck {
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'commandId': commandId,
|
||||
'status': status,
|
||||
if (result != null) 'result': result,
|
||||
};
|
||||
'commandId': commandId,
|
||||
'status': status.name,
|
||||
if (result != null) 'result': result,
|
||||
};
|
||||
}
|
||||
|
||||
enum RemoteSyncEventType {
|
||||
@@ -127,6 +150,7 @@ enum RemoteSyncEventType {
|
||||
syncCompleted,
|
||||
syncFailed,
|
||||
conflict,
|
||||
unauthorized,
|
||||
}
|
||||
|
||||
class RemoteSyncEvent {
|
||||
@@ -141,4 +165,4 @@ class RemoteSyncEvent {
|
||||
this.requestedBy,
|
||||
this.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user