ente/lib/models/subscription.dart

160 lines
3.9 KiB
Dart
Raw Normal View History

// @dart=2.9
import 'dart:convert';
2021-02-25 14:59:54 +00:00
const kFreeProductID = "free";
2021-05-07 22:39:28 +00:00
const kStripe = "stripe";
const kAppStore = "appstore";
const kPlayStore = "playstore";
2021-02-25 14:59:54 +00:00
class Subscription {
2021-01-18 17:02:44 +00:00
final int id;
2021-01-29 11:07:58 +00:00
final String productID;
2021-02-01 10:38:07 +00:00
final int storage;
2021-01-29 11:07:58 +00:00
final String originalTransactionID;
final String paymentProvider;
2021-01-18 17:02:44 +00:00
final int expiryTime;
final String price;
final String period;
final Attributes attributes;
Subscription({
this.id,
2021-01-29 11:07:58 +00:00
this.productID,
2021-02-01 10:38:07 +00:00
this.storage,
2021-01-29 11:07:58 +00:00
this.originalTransactionID,
this.paymentProvider,
2021-01-18 17:02:44 +00:00
this.expiryTime,
this.price,
this.period,
this.attributes,
});
bool isValid() {
return expiryTime > DateTime.now().microsecondsSinceEpoch;
}
bool isYearlyPlan() {
return 'year' == period;
}
Subscription copyWith({
2021-01-18 17:02:44 +00:00
int id,
2021-05-07 22:39:28 +00:00
String productID,
2021-02-01 10:38:07 +00:00
int storage,
2021-05-07 22:39:28 +00:00
String originalTransactionID,
String paymentProvider,
2021-01-18 17:02:44 +00:00
int expiryTime,
String price,
String period,
}) {
return Subscription(
id: id ?? this.id,
2021-01-29 11:07:58 +00:00
productID: productID ?? this.productID,
2021-02-01 10:38:07 +00:00
storage: storage ?? this.storage,
2021-01-29 11:07:58 +00:00
originalTransactionID:
originalTransactionID ?? this.originalTransactionID,
paymentProvider: paymentProvider ?? this.paymentProvider,
2021-01-18 17:02:44 +00:00
expiryTime: expiryTime ?? this.expiryTime,
price: price ?? this.price,
period: period ?? this.period,
);
}
Map<String, dynamic> toMap() {
2021-08-23 10:15:45 +00:00
final map = <String, dynamic>{
'id': id,
2021-01-29 11:07:58 +00:00
'productID': productID,
2021-02-01 10:38:07 +00:00
'storage': storage,
2021-01-29 11:07:58 +00:00
'originalTransactionID': originalTransactionID,
'paymentProvider': paymentProvider,
2021-01-18 17:02:44 +00:00
'expiryTime': expiryTime,
'price': price,
'period': period,
2021-08-23 10:15:45 +00:00
'attributes': attributes?.toJson()
};
return map;
}
factory Subscription.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return Subscription(
id: map['id'],
2021-01-29 11:07:58 +00:00
productID: map['productID'],
2021-02-01 10:38:07 +00:00
storage: map['storage'],
2021-01-29 11:07:58 +00:00
originalTransactionID: map['originalTransactionID'],
paymentProvider: map['paymentProvider'],
2021-01-18 17:02:44 +00:00
expiryTime: map['expiryTime'],
price: map['price'],
period: map['period'],
attributes: map["attributes"] != null
? Attributes.fromJson(map["attributes"])
: null,
);
}
String toJson() => json.encode(toMap());
factory Subscription.fromJson(String source) =>
Subscription.fromMap(json.decode(source));
@override
2021-01-29 11:07:58 +00:00
String toString() {
return 'Subscription{id: $id, productID: $productID, storage: $storage, originalTransactionID: $originalTransactionID, paymentProvider: $paymentProvider, expiryTime: $expiryTime, price: $price, period: $period, attributes: $attributes}';
2021-01-29 11:07:58 +00:00
}
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
return o is Subscription &&
o.id == id &&
2021-01-29 11:07:58 +00:00
o.productID == productID &&
2021-02-01 10:38:07 +00:00
o.storage == storage &&
2021-01-29 11:07:58 +00:00
o.originalTransactionID == originalTransactionID &&
o.paymentProvider == paymentProvider &&
o.expiryTime == expiryTime &&
o.price == price &&
o.period == period;
}
@override
2021-01-29 11:07:58 +00:00
int get hashCode {
return id.hashCode ^
productID.hashCode ^
2021-02-01 10:38:07 +00:00
storage.hashCode ^
2021-01-29 11:07:58 +00:00
originalTransactionID.hashCode ^
paymentProvider.hashCode ^
expiryTime.hashCode ^
price.hashCode ^
period.hashCode;
2021-01-29 11:07:58 +00:00
}
}
class Attributes {
bool isCancelled;
String customerID;
Attributes({
this.isCancelled,
2022-06-11 08:23:52 +00:00
this.customerID,
});
Attributes.fromJson(dynamic json) {
isCancelled = json["isCancelled"];
customerID = json["customerID"];
}
Map<String, dynamic> toJson() {
2022-08-29 14:43:31 +00:00
final map = <String, dynamic>{};
map["isCancelled"] = isCancelled;
map["customerID"] = customerID;
return map;
}
@override
String toString() {
return 'Attributes{isCancelled: $isCancelled, customerID: $customerID}';
}
2022-06-11 08:23:52 +00:00
}