local_auth_entity.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. @immutable
  4. class LocalAuthEntity {
  5. final int generatedID;
  6. // id can be null if a code has been scanned locally but it's yet to be
  7. // synced with the remote server.
  8. final String? id;
  9. final String encryptedData;
  10. final String header;
  11. // createdAt and updateAt will be equal to local time of creation or updation
  12. // till remote sync is completed.
  13. final int createdAt;
  14. final int updatedAt;
  15. // shouldSync indicates that the entry was locally created or updated. The
  16. // app should try to sync it to the server during next sync
  17. final bool shouldSync;
  18. LocalAuthEntity(
  19. this.generatedID,
  20. this.id,
  21. this.encryptedData,
  22. this.header,
  23. this.createdAt,
  24. this.updatedAt,
  25. this.shouldSync,
  26. );
  27. LocalAuthEntity copyWith({
  28. int? generatedID,
  29. String? id,
  30. String? encryptedData,
  31. String? header,
  32. int? createdAt,
  33. int? updatedAt,
  34. bool? shouldSync,
  35. }) {
  36. return LocalAuthEntity(
  37. generatedID ?? this.generatedID,
  38. id ?? this.id,
  39. encryptedData ?? this.encryptedData,
  40. header ?? this.header,
  41. createdAt ?? this.createdAt,
  42. updatedAt ?? this.updatedAt,
  43. shouldSync ?? this.shouldSync,
  44. );
  45. }
  46. Map<String, dynamic> toMap() {
  47. return {
  48. '_generatedID': generatedID,
  49. 'id': id,
  50. 'encryptedData': encryptedData,
  51. 'header': header,
  52. 'createdAt': createdAt,
  53. 'updatedAt': updatedAt,
  54. // sqlite doesn't support bool type. map true to 1 and false to 0
  55. 'shouldSync': shouldSync ? 1 : 0,
  56. };
  57. }
  58. factory LocalAuthEntity.fromMap(Map<String, dynamic> map) {
  59. return LocalAuthEntity(
  60. map['_generatedID']!,
  61. map['id'],
  62. map['encryptedData']!,
  63. map['header']!,
  64. map['createdAt']!,
  65. map['updatedAt']!,
  66. (map['shouldSync']! == 0) ? false : true,
  67. );
  68. }
  69. String toJson() => json.encode(toMap());
  70. factory LocalAuthEntity.fromJson(String source) =>
  71. LocalAuthEntity.fromMap(json.decode(source));
  72. }