Skip to content

Commit 6c882f7

Browse files
committed
fix: отказ от реактивного спобоса обновления статуса пользователя в пользу более простой реализации логики взаимодействия компонентов + вынесения апи ключа в .env файл
1 parent 26e1f19 commit 6c882f7

9 files changed

Lines changed: 23 additions & 42 deletions

File tree

frontend/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
REVENUE_CAT_API_KEY=

frontend/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.env
2+
13
# Miscellaneous
24
*.class
35
*.log
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import 'package:flutter/material.dart';
2-
31
abstract interface class IInAppPurchaseDataSource {
42
Future<bool> buyProduct(String productId);
53
Future<bool> checkStatus(String productId);
6-
void init(ValueNotifier<bool> notifier);
74
}
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import 'package:flutter/material.dart';
21
import 'package:frontend/data_sources/in_app_purchase_data_source.dart';
32
import 'package:purchases_flutter/purchases_flutter.dart';
43

54
class RevenueCatDataSource implements IInAppPurchaseDataSource {
6-
//The key of a product should be the same as an entitlement name attached to it
7-
static const _proVersionKey = 'chessknock_pro_version';
85
@override
96
Future<bool> buyProduct(String productId) async {
107
final product = (await Purchases.getProducts([productId])).first;
@@ -18,17 +15,4 @@ class RevenueCatDataSource implements IInAppPurchaseDataSource {
1815
final customerInfo = await Purchases.getCustomerInfo();
1916
return customerInfo.entitlements.active.containsKey(productId);
2017
}
21-
22-
@override
23-
void init(ValueNotifier<bool> notifier) {
24-
try {
25-
Purchases.addCustomerInfoUpdateListener((info) {
26-
info.entitlements.active.containsKey(_proVersionKey)
27-
? notifier.value = true
28-
: notifier.value = false;
29-
});
30-
} catch (e) {
31-
throw Exception('function type should be void Function(bool)');
32-
}
33-
}
3418
}

frontend/lib/main.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "dart:io";
22

3+
import "package:flutter_dotenv/flutter_dotenv.dart";
34
import "package:frontend/data_sources/revenuecat_data_source.dart";
45
import "package:frontend/exports.dart";
56
import "package:flutter/material.dart";
@@ -10,18 +11,18 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
1011
import "package:purchases_flutter/purchases_flutter.dart";
1112

1213
void main() async {
14+
await dotenv.load(fileName: ".env");
1315
WidgetsFlutterBinding.ensureInitialized();
1416
if (Platform.isIOS) {
1517
await configureRevenueCat();
1618
}
1719
runApp(const MyApp());
1820
}
1921

20-
const _revenueCatApiKey = 'appl_XXuSbFegqvobEdAwmWZnhIlglOX';
21-
2222
Future<void> configureRevenueCat() async {
2323
Purchases.setLogLevel(LogLevel.debug);
24-
Purchases.configure(PurchasesConfiguration(_revenueCatApiKey));
24+
final apiKey = dotenv.env['REVENUE_CAT_API_KEY'];
25+
Purchases.configure(PurchasesConfiguration(apiKey!));
2526
}
2627

2728
class MyApp extends StatelessWidget {
@@ -43,7 +44,6 @@ class MyApp extends StatelessWidget {
4344
repository: InAppPurchaseRepository(
4445
//TODO: choose data source based off of a platform
4546
dataSource: RevenueCatDataSource(),
46-
proStatusNotifier: ValueNotifier<bool>(false),
4747
),
4848
),
4949
)

frontend/lib/providers/pro_version_provider.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class ProVersionProvider extends ChangeNotifier {
1616
}
1717

1818
void _init() async {
19-
_setProStatus(_repository.getStatus());
19+
final isPro = await _repository.checkStatus(_proVersionKey);
20+
_setProStatus(isPro);
2021
}
2122

2223
void _setProStatus(bool status) {
@@ -32,7 +33,7 @@ class ProVersionProvider extends ChangeNotifier {
3233
Future<void> upgradeToPro() async {
3334
try {
3435
await _repository.buyProduct(_proVersionKey);
35-
final isPro = _repository.getStatus();
36+
final isPro = await _repository.checkStatus(_proVersionKey);
3637
_setProStatus(isPro);
3738
_setPurchaseError(false);
3839
} catch (_) {
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import 'package:flutter/material.dart';
21
import 'package:frontend/data_sources/in_app_purchase_data_source.dart';
32

43
class InAppPurchaseRepository {
54
final IInAppPurchaseDataSource _dataSource;
6-
final ValueNotifier<bool> _proStatusNotifier;
75

8-
InAppPurchaseRepository(
9-
{required IInAppPurchaseDataSource dataSource,
10-
required ValueNotifier<bool> proStatusNotifier})
11-
: _dataSource = dataSource,
12-
_proStatusNotifier = proStatusNotifier {
13-
_init();
14-
}
6+
InAppPurchaseRepository({
7+
required IInAppPurchaseDataSource dataSource,
8+
}) : _dataSource = dataSource;
159

1610
Future<bool> buyProduct(String productId) {
1711
return _dataSource.buyProduct(productId);
@@ -20,12 +14,4 @@ class InAppPurchaseRepository {
2014
Future<bool> checkStatus(String productId) {
2115
return _dataSource.checkStatus(productId);
2216
}
23-
24-
void _init() {
25-
_dataSource.init(_proStatusNotifier);
26-
}
27-
28-
bool getStatus() {
29-
return _proStatusNotifier.value;
30-
}
3117
}

frontend/pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ packages:
126126
description: flutter
127127
source: sdk
128128
version: "0.0.0"
129+
flutter_dotenv:
130+
dependency: "direct main"
131+
description:
132+
name: flutter_dotenv
133+
sha256: b7c7be5cd9f6ef7a78429cabd2774d3c4af50e79cb2b7593e3d5d763ef95c61b
134+
url: "https://pub.dev"
135+
source: hosted
136+
version: "5.2.1"
129137
flutter_launcher_icons:
130138
dependency: "direct dev"
131139
description:

frontend/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies:
2828
flutter_localizations:
2929
sdk: flutter
3030
purchases_flutter: ^5.8.0
31+
flutter_dotenv: ^5.2.1
3132

3233
dev_dependencies:
3334
flutter_test:
@@ -52,6 +53,7 @@ flutter:
5253
- assets/images/small_pieces/
5354
- assets/images/icons/
5455
- assets/images/guide_boards/
56+
- .env
5557

5658
fonts:
5759
- family: Roboto

0 commit comments

Comments
 (0)