locally cache subscription info
This commit is contained in:
parent
1a341cc72f
commit
0add6b19eb
5 changed files with 89 additions and 28 deletions
|
|
@ -7,4 +7,5 @@ class PLocalKey {
|
|||
static const String messagesSinceUpdate = 'messagesSinceLastUpdate';
|
||||
static const String completedActivities = 'completedActivities';
|
||||
static const String justInputtedCode = 'justInputtedCode';
|
||||
static const String availableSubscriptionInfo = 'availableSubscriptionInfo';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/pangea/config/environment.dart';
|
||||
import 'package:fluffychat/pangea/constants/local.key.dart';
|
||||
|
|
@ -409,4 +410,24 @@ class SubscriptionDetails {
|
|||
? AppConfig.appleMangementUrl
|
||||
: Environment.stripeManagementUrl;
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
data['price'] = price;
|
||||
data['id'] = id;
|
||||
data['duration'] = duration?.value;
|
||||
data['appId'] = appId;
|
||||
return data;
|
||||
}
|
||||
|
||||
factory SubscriptionDetails.fromJson(Map<String, dynamic> json) {
|
||||
return SubscriptionDetails(
|
||||
price: json['price'],
|
||||
duration: SubscriptionDuration.values.firstWhereOrNull(
|
||||
(duration) => duration.value == json['duration'],
|
||||
),
|
||||
id: json['id'],
|
||||
appId: json['appId'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
import 'package:collection/collection.dart';
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/pangea/constants/local.key.dart';
|
||||
import 'package:fluffychat/pangea/controllers/subscription_controller.dart';
|
||||
import 'package:fluffychat/pangea/repo/subscription_repo.dart';
|
||||
import 'package:fluffychat/pangea/utils/error_handler.dart';
|
||||
import 'package:fluffychat/pangea/utils/subscription_app_id.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
|
||||
/// Contains information about the users's current subscription
|
||||
class CurrentSubscriptionInfo {
|
||||
|
|
@ -87,28 +90,65 @@ class AvailableSubscriptionsInfo {
|
|||
SubscriptionAppIds? appIds;
|
||||
List<SubscriptionDetails>? allProducts;
|
||||
|
||||
AvailableSubscriptionsInfo({
|
||||
this.appIds,
|
||||
this.allProducts,
|
||||
});
|
||||
|
||||
Future<void> setAvailableSubscriptions() async {
|
||||
appIds ??= await SubscriptionRepo.getAppIds();
|
||||
allProducts ??= await SubscriptionRepo.getAllProducts();
|
||||
final cachedInfo = _getCachedSubscriptionInfo();
|
||||
appIds ??= cachedInfo?.appIds ?? await SubscriptionRepo.getAppIds();
|
||||
allProducts ??=
|
||||
cachedInfo?.allProducts ?? await SubscriptionRepo.getAllProducts();
|
||||
|
||||
if (cachedInfo == null) await _cacheSubscriptionInfo();
|
||||
|
||||
availableSubscriptions = (allProducts ?? [])
|
||||
.where((product) => product.appId == appIds!.currentAppId)
|
||||
.sorted((a, b) => a.price.compareTo(b.price))
|
||||
.toList();
|
||||
// //@Gabby - temporary solution to add trial to list
|
||||
// if (currentSubscriptionId == null && !hasSubscribed) {
|
||||
// final id = availableSubscriptions[0].id;
|
||||
// final package = availableSubscriptions[0].package;
|
||||
// final duration = availableSubscriptions[0].duration;
|
||||
// availableSubscriptions.insert(
|
||||
// 0,
|
||||
// SubscriptionDetails(
|
||||
// price: 0,
|
||||
// id: id,
|
||||
// duration: duration,
|
||||
// package: package,
|
||||
// periodType: SubscriptionPeriodType.trial,
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
}
|
||||
|
||||
Future<void> _cacheSubscriptionInfo() async {
|
||||
await MatrixState.pangeaController.pStoreService.save(
|
||||
PLocalKey.availableSubscriptionInfo,
|
||||
toJson(),
|
||||
);
|
||||
}
|
||||
|
||||
static AvailableSubscriptionsInfo? _getCachedSubscriptionInfo() {
|
||||
final json = MatrixState.pangeaController.pStoreService.read(
|
||||
PLocalKey.availableSubscriptionInfo,
|
||||
);
|
||||
if (json is! Map<String, dynamic>) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return AvailableSubscriptionsInfo.fromJson(json);
|
||||
} catch (e, s) {
|
||||
ErrorHandler.logError(e: e, s: s);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
factory AvailableSubscriptionsInfo.fromJson(Map<String, dynamic> json) {
|
||||
final appIds = SubscriptionAppIds.fromJson(json['app_ids']);
|
||||
final allProducts = (json['all_products'] as List<dynamic>)
|
||||
.map((product) => SubscriptionDetails.fromJson(product))
|
||||
.toList()
|
||||
.cast<SubscriptionDetails>();
|
||||
return AvailableSubscriptionsInfo(
|
||||
appIds: appIds,
|
||||
allProducts: allProducts,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
data['app_ids'] = appIds?.toJson();
|
||||
data['all_products'] =
|
||||
allProducts?.map((product) => product.toJson()).toList();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,16 +91,7 @@ class RCProductsResponseModel {
|
|||
) {
|
||||
final List<dynamic> offerings = json["items"] as List<dynamic>;
|
||||
final res = offerings
|
||||
.map(
|
||||
(offering) => SubscriptionDetails(
|
||||
price: offering['price'],
|
||||
duration: SubscriptionDuration.values.firstWhereOrNull(
|
||||
(duration) => duration.value == offering['duration'],
|
||||
),
|
||||
id: offering['id'],
|
||||
appId: offering['appId'],
|
||||
),
|
||||
)
|
||||
.map((offering) => SubscriptionDetails.fromJson(offering))
|
||||
.toList()
|
||||
.cast<SubscriptionDetails>();
|
||||
return RCProductsResponseModel(allProducts: res);
|
||||
|
|
|
|||
|
|
@ -28,6 +28,14 @@ class SubscriptionAppIds {
|
|||
..androidId = json['android_id']
|
||||
..appleId = json['apple_id'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
data['stripe_id'] = stripeId;
|
||||
data['android_id'] = androidId;
|
||||
data['apple_id'] = appleId;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
enum RCPlatform {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue