浏览代码

Add serializers for UserDetails

vishnukvmd 2 年之前
父节点
当前提交
006cf071c3
共有 2 个文件被更改,包括 89 次插入5 次删除
  1. 35 3
      lib/models/subscription.dart
  2. 54 2
      lib/models/user_details.dart

+ 35 - 3
lib/models/subscription.dart

@@ -1,3 +1,5 @@
+import 'dart:convert';
+
 const freeProductID = "free";
 const stripe = "stripe";
 const appStore = "appstore";
@@ -47,6 +49,23 @@ class Subscription {
           : null,
     );
   }
+
+  Map<String, dynamic> toMap() {
+    return {
+      'productID': productID,
+      'storage': storage,
+      'originalTransactionID': originalTransactionID,
+      'paymentProvider': paymentProvider,
+      'expiryTime': expiryTime,
+      'price': price,
+      'period': period,
+      'attributes': attributes?.toMap(),
+    };
+  }
+
+  String toJson() => json.encode(toMap());
+
+  factory Subscription.fromJson(String source) => Subscription.fromMap(json.decode(source));
 }
 
 class Attributes {
@@ -58,8 +77,21 @@ class Attributes {
     this.customerID,
   });
 
-  Attributes.fromJson(dynamic json) {
-    isCancelled = json["isCancelled"];
-    customerID = json["customerID"];
+  Map<String, dynamic> toMap() {
+    return {
+      'isCancelled': isCancelled,
+      'customerID': customerID,
+    };
   }
+
+  factory Attributes.fromMap(Map<String, dynamic> map) {
+    return Attributes(
+      isCancelled: map['isCancelled'],
+      customerID: map['customerID'],
+    );
+  }
+
+  String toJson() => json.encode(toMap());
+
+  factory Attributes.fromJson(String source) => Attributes.fromMap(json.decode(source));
 }

+ 54 - 2
lib/models/user_details.dart

@@ -1,3 +1,4 @@
+import 'dart:convert';
 import 'dart:math';
 
 import 'package:collection/collection.dart';
@@ -62,6 +63,22 @@ class UserDetails {
       FamilyData.fromMap(map['familyData']),
     );
   }
+
+  Map<String, dynamic> toMap() {
+    return {
+      'email': email,
+      'usage': usage,
+      'fileCount': fileCount,
+      'sharedCollectionsCount': sharedCollectionsCount,
+      'subscription': subscription.toMap(),
+      'familyData': familyData?.toMap(),
+    };
+  }
+
+  String toJson() => json.encode(toMap());
+
+  factory UserDetails.fromJson(String source) =>
+      UserDetails.fromMap(json.decode(source));
 }
 
 class FamilyMember {
@@ -70,7 +87,12 @@ class FamilyMember {
   final String id;
   final bool isAdmin;
 
-  FamilyMember(this.email, this.usage, this.id, this.isAdmin);
+  FamilyMember(
+    this.email,
+    this.usage,
+    this.id,
+    this.isAdmin,
+  );
 
   factory FamilyMember.fromMap(Map<String, dynamic> map) {
     return FamilyMember(
@@ -80,6 +102,20 @@ class FamilyMember {
       map['isAdmin'] as bool,
     );
   }
+
+
+  Map<String, dynamic> toMap() {
+    return {
+      'email': email,
+      'usage': usage,
+      'id': id,
+      'isAdmin': isAdmin,
+    };
+  }
+
+  String toJson() => json.encode(toMap());
+
+  factory FamilyMember.fromJson(String source) => FamilyMember.fromMap(json.decode(source));
 }
 
 class FamilyData {
@@ -89,7 +125,11 @@ class FamilyData {
   final int storage;
   final int expiryTime;
 
-  FamilyData(this.members, this.storage, this.expiryTime);
+  FamilyData(
+    this.members,
+    this.storage,
+    this.expiryTime,
+  );
 
   int getTotalUsage() {
     return members!.map((e) => e.usage).toList().sum;
@@ -107,6 +147,18 @@ class FamilyData {
       map['expiryTime'] as int,
     );
   }
+
+  Map<String, dynamic> toMap() {
+    return {
+      'members': members?.map((x) => x.toMap()).toList(),
+      'storage': storage,
+      'expiryTime': expiryTime,
+    };
+  }
+
+  String toJson() => json.encode(toMap());
+
+  factory FamilyData.fromJson(String source) => FamilyData.fromMap(json.decode(source));
 }
 
 class FilesCount {