Skip to content

Commit 88d01b8

Browse files
committed
Update in_app_purchases rebuild script
1 parent 73942e4 commit 88d01b8

File tree

1 file changed

+81
-91
lines changed

1 file changed

+81
-91
lines changed

in_app_purchases/codelab_rebuild.yaml

Lines changed: 81 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -15881,11 +15881,10 @@ steps:
1588115881
),
1588215882
ChangeNotifierProvider<DashCounter>(create: (_) => DashCounter()),
1588315883
ChangeNotifierProvider<DashUpgrades>(
15884-
create:
15885-
(context) => DashUpgrades(
15886-
context.read<DashCounter>(),
15887-
context.read<FirebaseNotifier>(),
15888-
),
15884+
create: (context) => DashUpgrades(
15885+
context.read<DashCounter>(),
15886+
context.read<FirebaseNotifier>(),
15887+
),
1588915888
),
1589015889
ChangeNotifierProvider<IAPRepo>(
1589115890
create: (context) => IAPRepo(context.read<FirebaseNotifier>()),
@@ -16401,10 +16400,9 @@ steps:
1640116400
leading: Center(widthFactor: 1, child: Text(upgrade.count.toString())),
1640216401
title: Text(
1640316402
title,
16404-
style:
16405-
!upgrade.purchasable
16406-
? const TextStyle(color: Colors.redAccent)
16407-
: null,
16403+
style: !upgrade.purchasable
16404+
? const TextStyle(color: Colors.redAccent)
16405+
: null,
1640816406
),
1640916407
subtitle: Text('Produces ${upgrade.work} dashes per second'),
1641016408
trailing: Text('${NumberFormat.compact().format(upgrade.cost)} dashes'),
@@ -16504,17 +16502,16 @@ steps:
1650416502
var purchases = context.watch<DashPurchases>();
1650516503
var products = purchases.products;
1650616504
return Column(
16507-
children:
16508-
products
16509-
.map(
16510-
(product) => _PurchaseWidget(
16511-
product: product,
16512-
onPressed: () {
16513-
purchases.buy(product);
16514-
},
16515-
),
16516-
)
16517-
.toList(),
16505+
children: products
16506+
.map(
16507+
(product) => _PurchaseWidget(
16508+
product: product,
16509+
onPressed: () {
16510+
purchases.buy(product);
16511+
},
16512+
),
16513+
)
16514+
.toList(),
1651816515
);
1651916516
}
1652016517
}
@@ -16559,11 +16556,10 @@ steps:
1655916556
return ListView.separated(
1656016557
shrinkWrap: true,
1656116558
itemCount: purchases.length,
16562-
itemBuilder:
16563-
(context, index) => ListTile(
16564-
title: Text(purchases[index].title),
16565-
subtitle: Text(purchases[index].status.toString()),
16566-
),
16559+
itemBuilder: (context, index) => ListTile(
16560+
title: Text(purchases[index].title),
16561+
subtitle: Text(purchases[index].status.toString()),
16562+
),
1656716563
separatorBuilder: (context, index) => const Divider(),
1656816564
);
1656916565
}
@@ -16622,17 +16618,15 @@ steps:
1662216618
hasUpgrade = false;
1662316619
return;
1662416620
}
16625-
var purchaseStream =
16626-
_firestore
16627-
.collection('purchases')
16628-
.where('userId', isEqualTo: user.uid)
16629-
.snapshots();
16621+
var purchaseStream = _firestore
16622+
.collection('purchases')
16623+
.where('userId', isEqualTo: user.uid)
16624+
.snapshots();
1663016625
_purchaseSubscription = purchaseStream.listen((snapshot) {
16631-
purchases =
16632-
snapshot.docs.map((document) {
16633-
var data = document.data();
16634-
return PastPurchase.fromJson(data);
16635-
}).toList();
16626+
purchases = snapshot.docs.map((document) {
16627+
var data = document.data();
16628+
return PastPurchase.fromJson(data);
16629+
}).toList();
1663616630

1663716631
hasActiveSubscription = purchases.any(
1663816632
(element) =>
@@ -16916,10 +16910,9 @@ steps:
1691616910
switch (type) {
1691716911
case ProductType.subscription:
1691816912
return SubscriptionPurchase(
16919-
iapSource:
16920-
e.fields!['iapSource']!.stringValue == 'googleplay'
16921-
? IAPSource.googleplay
16922-
: IAPSource.appstore,
16913+
iapSource: e.fields!['iapSource']!.stringValue == 'googleplay'
16914+
? IAPSource.googleplay
16915+
: IAPSource.appstore,
1692316916
orderId: e.fields!['orderId']!.stringValue!,
1692416917
productId: e.fields!['productId']!.stringValue!,
1692516918
userId: e.fields!['userId']!.stringValue,
@@ -16937,10 +16930,9 @@ steps:
1693716930
);
1693816931
case ProductType.nonSubscription:
1693916932
return NonSubscriptionPurchase(
16940-
iapSource:
16941-
e.fields!['iapSource']!.stringValue == 'googleplay'
16942-
? IAPSource.googleplay
16943-
: IAPSource.appstore,
16933+
iapSource: e.fields!['iapSource']!.stringValue == 'googleplay'
16934+
? IAPSource.googleplay
16935+
: IAPSource.appstore,
1694416936
orderId: e.fields!['orderId']!.stringValue!,
1694516937
productId: e.fields!['productId']!.stringValue!,
1694616938
userId: e.fields!['userId']!.stringValue,
@@ -17180,7 +17172,7 @@ steps:
1718017172
void main() {
1718117173
runApp(const MyApp());
1718217174
}
17183-
@@ -66,6 +80,7 @@ class _MyHomePageState extends State<MyHomePage> {
17175+
@@ -65,6 +79,7 @@ class _MyHomePageState extends State<MyHomePage> {
1718417176
),
1718517177
ChangeNotifierProvider<DashPurchases>(
1718617178
create: (context) => DashPurchases(context.read<DashCounter>()),
@@ -17374,7 +17366,7 @@ steps:
1737417366

1737517367
final iapConnection = IAPConnection.instance;
1737617368

17377-
@@ -37,6 +28,26 @@ class DashPurchases extends ChangeNotifier {
17369+
@@ -37,6 +28,27 @@ class DashPurchases extends ChangeNotifier {
1737817370
onDone: _updateStreamOnDone,
1737917371
onError: _updateStreamOnError,
1738017372
);
@@ -17394,14 +17386,15 @@ steps:
1739417386
+ storeKeyUpgrade,
1739517387
+ };
1739617388
+ final response = await iapConnection.queryProductDetails(ids);
17397-
+ products =
17398-
+ response.productDetails.map((e) => PurchasableProduct(e)).toList();
17389+
+ products = response.productDetails
17390+
+ .map((e) => PurchasableProduct(e))
17391+
+ .toList();
1739917392
+ storeState = StoreState.available;
1740017393
+ notifyListeners();
1740117394
}
1740217395

1740317396
@override
17404-
@@ -46,18 +57,45 @@ class DashPurchases extends ChangeNotifier {
17397+
@@ -46,18 +58,45 @@ class DashPurchases extends ChangeNotifier {
1740517398
}
1740617399

1740717400
Future<void> buy(PurchasableProduct product) async {
@@ -17495,16 +17488,15 @@ steps:
1749517488
patch-u: |
1749617489
--- b/in_app_purchases/step_10/app/lib/main.dart
1749717490
+++ a/in_app_purchases/step_10/app/lib/main.dart
17498-
@@ -79,7 +79,11 @@ class _MyHomePageState extends State<MyHomePage> {
17491+
@@ -78,7 +78,10 @@ class _MyHomePageState extends State<MyHomePage> {
1749917492
create: (context) => IAPRepo(context.read<FirebaseNotifier>()),
1750017493
),
1750117494
ChangeNotifierProvider<DashPurchases>(
1750217495
- create: (context) => DashPurchases(context.read<DashCounter>()),
17503-
+ create:
17504-
+ (context) => DashPurchases(
17505-
+ context.read<DashCounter>(),
17506-
+ context.read<FirebaseNotifier>(),
17507-
+ ),
17496+
+ create: (context) => DashPurchases(
17497+
+ context.read<DashCounter>(),
17498+
+ context.read<FirebaseNotifier>(),
17499+
+ ),
1750817500
lazy: false,
1750917501
),
1751017502
],
@@ -17544,7 +17536,7 @@ steps:
1754417536
final purchaseUpdated = iapConnection.purchaseStream;
1754517537
_subscription = purchaseUpdated.listen(
1754617538
_onPurchaseUpdate,
17547-
@@ -83,13 +87,19 @@ class DashPurchases extends ChangeNotifier {
17539+
@@ -84,13 +88,19 @@ class DashPurchases extends ChangeNotifier {
1754817540

1754917541
Future<void> _handlePurchase(PurchaseDetails purchaseDetails) async {
1755017542
if (purchaseDetails.status == PurchaseStatus.purchased) {
@@ -17571,7 +17563,7 @@ steps:
1757117563
}
1757217564
}
1757317565

17574-
@@ -98,6 +108,30 @@ class DashPurchases extends ChangeNotifier {
17566+
@@ -99,6 +109,30 @@ class DashPurchases extends ChangeNotifier {
1757517567
}
1757617568
}
1757717569

@@ -17656,7 +17648,7 @@ steps:
1765617648
patch-u: |
1765717649
--- b/in_app_purchases/step_10/dart-backend/bin/server.dart
1765817650
+++ a/in_app_purchases/step_10/dart-backend/bin/server.dart
17659-
@@ -2,12 +2,111 @@
17651+
@@ -2,12 +2,113 @@
1766017652
// for details. All rights reserved. Use of this source code is governed by a
1766117653
// BSD-style license that can be found in the LICENSE file.
1766217654

@@ -17679,8 +17671,9 @@ steps:
1767917671
+/// and their dependencies
1768017672
+Future<Map<String, PurchaseHandler>> _createPurchaseHandlers() async {
1768117673
+ // Configure Android Publisher API access
17682-
+ final serviceAccountGooglePlay =
17683-
+ File('assets/service-account-google-play.json').readAsStringSync();
17674+
+ final serviceAccountGooglePlay = File(
17675+
+ 'assets/service-account-google-play.json',
17676+
+ ).readAsStringSync();
1768417677
+ final clientCredentialsGooglePlay = auth.ServiceAccountCredentials.fromJson(
1768517678
+ serviceAccountGooglePlay,
1768617679
+ );
@@ -17691,8 +17684,9 @@ steps:
1769117684
+ final androidPublisher = ap.AndroidPublisherApi(clientGooglePlay);
1769217685
+
1769317686
+ // Configure Firestore API access
17694-
+ final serviceAccountFirebase =
17695-
+ File('assets/service-account-firebase.json').readAsStringSync();
17687+
+ final serviceAccountFirebase = File(
17688+
+ 'assets/service-account-firebase.json',
17689+
+ ).readAsStringSync();
1769617690
+ final clientCredentialsFirebase = auth.ServiceAccountCredentials.fromJson(
1769717691
+ serviceAccountFirebase,
1769817692
+ );
@@ -18113,12 +18107,12 @@ steps:
1811318107
patch-u: |
1811418108
--- b/in_app_purchases/step_11/app/lib/main.dart
1811518109
+++ a/in_app_purchases/step_11/app/lib/main.dart
18116-
@@ -83,6 +83,7 @@ class _MyHomePageState extends State<MyHomePage> {
18117-
(context) => DashPurchases(
18118-
context.read<DashCounter>(),
18119-
context.read<FirebaseNotifier>(),
18120-
+ context.read<IAPRepo>(),
18121-
),
18110+
@@ -81,6 +81,7 @@ class _MyHomePageState extends State<MyHomePage> {
18111+
create: (context) => DashPurchases(
18112+
context.read<DashCounter>(),
18113+
context.read<FirebaseNotifier>(),
18114+
+ context.read<IAPRepo>(),
18115+
),
1812218116
lazy: false,
1812318117
),
1812418118
- name: Patch app/lib/logic/dash_purchases.dart
@@ -18157,15 +18151,15 @@ steps:
1815718151
loadPurchases();
1815818152
}
1815918153

18160-
@@ -57,6 +59,7 @@ class DashPurchases extends ChangeNotifier {
18154+
@@ -58,6 +60,7 @@ class DashPurchases extends ChangeNotifier {
1816118155
@override
1816218156
void dispose() {
1816318157
_subscription.cancel();
1816418158
+ iapRepo.removeListener(purchasesUpdate);
1816518159
super.dispose();
1816618160
}
1816718161

18168-
@@ -139,4 +142,59 @@ class DashPurchases extends ChangeNotifier {
18162+
@@ -140,4 +143,55 @@ class DashPurchases extends ChangeNotifier {
1816918163
void _updateStreamOnError(dynamic error) {
1817018164
//Handle error here
1817118165
}
@@ -18176,16 +18170,12 @@ steps:
1817618170
+ // Get a list of purchasable products for the subscription and upgrade.
1817718171
+ // This should be 1 per type.
1817818172
+ if (products.isNotEmpty) {
18179-
+ subscriptions =
18180-
+ products
18181-
+ .where(
18182-
+ (element) => element.productDetails.id == storeKeySubscription,
18183-
+ )
18184-
+ .toList();
18185-
+ upgrades =
18186-
+ products
18187-
+ .where((element) => element.productDetails.id == storeKeyUpgrade)
18188-
+ .toList();
18173+
+ subscriptions = products
18174+
+ .where((element) => element.productDetails.id == storeKeySubscription)
18175+
+ .toList();
18176+
+ upgrades = products
18177+
+ .where((element) => element.productDetails.id == storeKeyUpgrade)
18178+
+ .toList();
1818918179
+ }
1819018180
+
1819118181
+ // Set the subscription in the counter logic and show/hide purchased on the
@@ -18248,7 +18238,7 @@ steps:
1824818238
import 'package:googleapis_auth/auth_io.dart' as auth;
1824918239
import 'package:shelf/shelf.dart';
1825018240
import 'package:shelf_router/shelf_router.dart';
18251-
@@ -26,12 +29,16 @@ Future<Map<String, PurchaseHandler>> _createPurchaseHandlers() async {
18241+
@@ -27,12 +30,16 @@ Future<Map<String, PurchaseHandler>> _createPurchaseHandlers() async {
1825218242
final clientCredentialsGooglePlay = auth.ServiceAccountCredentials.fromJson(
1825318243
serviceAccountGooglePlay,
1825418244
);
@@ -18267,14 +18257,15 @@ steps:
1826718257
+ final pubsubApi = pubsub.PubsubApi(clientGooglePlay);
1826818258
+
1826918259
// Configure Firestore API access
18270-
final serviceAccountFirebase =
18271-
File('assets/service-account-firebase.json').readAsStringSync();
18272-
@@ -47,9 +54,41 @@ Future<Map<String, PurchaseHandler>> _createPurchaseHandlers() async {
18260+
final serviceAccountFirebase = File(
18261+
'assets/service-account-firebase.json',
18262+
@@ -49,9 +56,42 @@ Future<Map<String, PurchaseHandler>> _createPurchaseHandlers() async {
1827318263
final projectId = json['project_id'] as String;
1827418264
final iapRepository = IapRepository(firestoreApi, projectId);
1827518265

18276-
+ final subscriptionKeyAppStore =
18277-
+ File('assets/SubscriptionKey.p8').readAsStringSync();
18266+
+ final subscriptionKeyAppStore = File(
18267+
+ 'assets/SubscriptionKey.p8',
18268+
+ ).readAsStringSync();
1827818269
+
1827918270
+ // Configure Apple Store API access
1828018271
+ var appStoreEnvironment = AppStoreEnvironment.sandbox(
@@ -18338,7 +18329,7 @@ steps:
1833818329

1833918330
final _iTunesAPI = ITunesApi(
1834018331
ITunesHttpClient(ITunesEnvironment.sandbox(), loggingEnabled: true),
18341-
@@ -96,4 +105,53 @@ class AppStorePurchaseHandler extends PurchaseHandler {
18332+
@@ -96,4 +105,52 @@ class AppStorePurchaseHandler extends PurchaseHandler {
1834218333
return false;
1834318334
}
1834418335
}
@@ -18381,10 +18372,9 @@ steps:
1838118372
+ ),
1838218373
+ type: ProductType.subscription,
1838318374
+ expiryDate: expirationDate,
18384-
+ status:
18385-
+ isExpired
18386-
+ ? SubscriptionStatus.expired
18387-
+ : SubscriptionStatus.active,
18375+
+ status: isExpired
18376+
+ ? SubscriptionStatus.expired
18377+
+ : SubscriptionStatus.active,
1838818378
+ ),
1838918379
+ );
1839018380
+ }
@@ -18526,7 +18516,7 @@ steps:
1852618516
patch-u: |
1852718517
--- b/in_app_purchases/complete/app/lib/logic/dash_purchases.dart
1852818518
+++ a/in_app_purchases/complete/app/lib/logic/dash_purchases.dart
18529-
@@ -192,8 +192,8 @@ class DashPurchases extends ChangeNotifier {
18519+
@@ -189,8 +189,8 @@ class DashPurchases extends ChangeNotifier {
1853018520
}
1853118521

1853218522
void _updateStatus(PurchasableProduct product, ProductStatus status) {

0 commit comments

Comments
 (0)