ente/lib/models/subscription.dart

103 lines
2.5 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;
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,
});
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,
}) {
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,
);
}
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,
};
}
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'],
);
}
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() {
2021-02-01 10:38:07 +00:00
return 'Subscription(id: $id, productID: $productID, storage: $storage, originalTransactionID: $originalTransactionID, paymentProvider: $paymentProvider, expiryTime: $expiryTime)';
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 &&
2021-01-18 17:02:44 +00:00
o.expiryTime == expiryTime;
}
@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;
}
}