Vishnu Mohandas 4 лет назад
Родитель
Сommit
669eed55e4
2 измененных файлов с 105 добавлено и 0 удалено
  1. 75 0
      lib/models/billing_plan.dart
  2. 30 0
      lib/services/billing_service.dart

+ 75 - 0
lib/models/billing_plan.dart

@@ -0,0 +1,75 @@
+import 'dart:convert';
+
+class BillingPlan {
+  final String id;
+  final String storage;
+  final String price;
+  final String duration;
+
+  BillingPlan({
+    this.id,
+    this.storage,
+    this.price,
+    this.duration,
+  });
+
+  BillingPlan copyWith({
+    String id,
+    String storage,
+    String price,
+    String duration,
+  }) {
+    return BillingPlan(
+      id: id ?? this.id,
+      storage: storage ?? this.storage,
+      price: price ?? this.price,
+      duration: duration ?? this.duration,
+    );
+  }
+
+  Map<String, dynamic> toMap() {
+    return {
+      'id': id,
+      'storage': storage,
+      'price': price,
+      'duration': duration,
+    };
+  }
+
+  factory BillingPlan.fromMap(Map<String, dynamic> map) {
+    if (map == null) return null;
+
+    return BillingPlan(
+      id: map['id'],
+      storage: map['storage'],
+      price: map['price'],
+      duration: map['duration'],
+    );
+  }
+
+  String toJson() => json.encode(toMap());
+
+  factory BillingPlan.fromJson(String source) =>
+      BillingPlan.fromMap(json.decode(source));
+
+  @override
+  String toString() {
+    return 'BillingPlan(id: $id, storage: $storage, price: $price, duration: $duration)';
+  }
+
+  @override
+  bool operator ==(Object o) {
+    if (identical(this, o)) return true;
+
+    return o is BillingPlan &&
+        o.id == id &&
+        o.storage == storage &&
+        o.price == price &&
+        o.duration == duration;
+  }
+
+  @override
+  int get hashCode {
+    return id.hashCode ^ storage.hashCode ^ price.hashCode ^ duration.hashCode;
+  }
+}

+ 30 - 0
lib/services/billing_service.dart

@@ -0,0 +1,30 @@
+import 'package:logging/logging.dart';
+import 'package:photos/core/configuration.dart';
+import 'package:photos/core/network.dart';
+import 'package:photos/models/billing_plan.dart';
+
+class BillingService {
+  BillingService._privateConstructor() {}
+
+  static final BillingService instance = BillingService._privateConstructor();
+
+  final _logger = Logger("BillingService");
+  final _dio = Network.instance.getDio();
+
+  Future<List<BillingPlan>> _future;
+
+  Future<List<BillingPlan>> getBillingPlans() {
+    if (_future == null) {
+      _future = _dio
+          .get(Configuration.instance.getHttpEndpoint() + "/billing/plans")
+          .then((response) {
+        final plans = List<BillingPlan>();
+        for (final plan in response.data["plans"]) {
+          plans.add(BillingPlan.fromMap(plan));
+        }
+        return plans;
+      });
+    }
+    return _future;
+  }
+}