ente/lib/models/subscription.dart

119 lines
2.9 KiB
Dart
Raw Normal View History

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";
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;
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,
});
bool isValid() {
return expiryTime > DateTime.now().microsecondsSinceEpoch;
}
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() {
return {
'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,
};
}
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'],
);
}
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)';
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
}
}