Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion in_app_purchases/.gitignore

This file was deleted.

118 changes: 23 additions & 95 deletions in_app_purchases/codelab_rebuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ steps:
patch-u: |
--- b/in_app_purchases/step_00/app/.gitignore
+++ a/in_app_purchases/step_00/app/.gitignore
@@ -43,3 +43,9 @@ app.*.map.json
@@ -43,3 +43,8 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
Expand All @@ -23,10 +23,9 @@ steps:
+GoogleService-Info.plist
+google-services.json
+ios/firebase_app_id_file.json
+lib/firebase_options.dart
- name: Add app dependencies
path: steps/app
flutter: pub add cloud_firestore cupertino_icons firebase_auth firebase_core google_sign_in http in_app_purchase in_app_purchase_platform_interface intl provider
flutter: pub add cloud_firestore cupertino_icons firebase_auth firebase_core google_sign_in http intl provider
- name: Patch app/pubspec.yaml
path: steps/app/pubspec.yaml
patch-u: |
Expand All @@ -39,7 +38,7 @@ steps:
publish_to: 'none'
version: 0.1.0

@@ -27,3 +27,6 @@ dev_dependencies:
@@ -25,3 +25,6 @@ dev_dependencies:

flutter:
uses-material-design: true
Expand Down Expand Up @@ -16734,7 +16733,6 @@ steps:
assets/*.p8
assets/appstore.token
assets/service-account*.json
lib/constants.dart
- name: Replace analysis_options.yaml
path: steps/dart-backend/analysis_options.yaml
replace-contents: |
Expand Down Expand Up @@ -17179,6 +17177,18 @@ steps:
from: steps
to: step_00

- name: step_03
steps:
- name: Remove step_03
rmdir: step_03
- name: Add dependencies
path: steps/app
flutter: pub add in_app_purchase dev:in_app_purchase_platform_interface
- name: Copy to step_03
copydir:
from: steps
to: step_03

- name: step_07
steps:
- name: Remove step_07
Expand Down Expand Up @@ -18543,98 +18553,16 @@ steps:
from: steps
to: step_10

- name: complete
- name: step_11
steps:
- name: Remove complete
rmdir: complete
- name: Remove step_11
rmdir: step_11
- name: Patch app/lib/logic/dash_purchases.dart
path: steps/app/lib/logic/dash_purchases.dart
patch-u: |
--- b/in_app_purchases/complete/app/lib/logic/dash_purchases.dart
+++ a/in_app_purchases/complete/app/lib/logic/dash_purchases.dart
@@ -1,3 +1,5 @@
+// ignore_for_file: avoid_print
+
import 'dart:async';
import 'dart:convert';

@@ -17,10 +19,10 @@ import 'firebase_notifier.dart';
class DashPurchases extends ChangeNotifier {
DashCounter counter;
FirebaseNotifier firebaseNotifier;
+ IAPRepo iapRepo;
StoreState storeState = StoreState.loading;
late StreamSubscription<List<PurchaseDetails>> _subscription;
List<PurchasableProduct> products = [];
- IAPRepo iapRepo;

bool get beautifiedDash => _beautifiedDashUpgrade;
bool _beautifiedDashUpgrade = false;
@@ -44,6 +46,7 @@ class DashPurchases extends ChangeNotifier {
notifyListeners();
return;
}
+
const ids = <String>{
storeKeyConsumable,
storeKeySubscription,
@@ -58,8 +61,8 @@ class DashPurchases extends ChangeNotifier {

@override
void dispose() {
- _subscription.cancel();
iapRepo.removeListener(purchasesUpdate);
+ _subscription.cancel();
super.dispose();
}

@@ -126,8 +129,10 @@ class DashPurchases extends ChangeNotifier {
headers: headers,
);
if (response.statusCode == 200) {
+ print('Successfully verified purchase');
return true;
} else {
+ print('failed request: ${response.statusCode} - ${response.body}');
return false;
}
}
@@ -137,7 +142,7 @@ class DashPurchases extends ChangeNotifier {
}

void _updateStreamOnError(dynamic error) {
- //Handle error here
+ print(error);
}

void purchasesUpdate() {
@@ -158,21 +163,21 @@ class DashPurchases extends ChangeNotifier {
// purchases page.
if (iapRepo.hasActiveSubscription) {
counter.applyPaidMultiplier();
- for (var element in subscriptions) {
+ for (final element in subscriptions) {
_updateStatus(element, ProductStatus.purchased);
}
} else {
counter.removePaidMultiplier();
- for (var element in subscriptions) {
+ for (final element in subscriptions) {
_updateStatus(element, ProductStatus.purchasable);
}
}

- // Set the Dash beautifier and show/hide purchased on
+ // Set the dash beautifier and show/hide purchased on
// the purchases page.
if (iapRepo.hasUpgrade != _beautifiedDashUpgrade) {
_beautifiedDashUpgrade = iapRepo.hasUpgrade;
- for (var element in upgrades) {
+ for (final element in upgrades) {
_updateStatus(
element,
_beautifiedDashUpgrade
@@ -184,8 +189,8 @@ class DashPurchases extends ChangeNotifier {
--- b/in_app_purchases/step_11/app/lib/logic/dash_purchases.dart
+++ a/in_app_purchases/step_11/app/lib/logic/dash_purchases.dart
@@ -184,8 +184,8 @@ class DashPurchases extends ChangeNotifier {
}

void _updateStatus(PurchasableProduct product, ProductStatus status) {
Expand Down Expand Up @@ -18690,10 +18618,10 @@ steps:
expect(token, 'TOKEN');
});
}
- name: Copy to complete
- name: Copy to step_11
copydir:
from: steps
to: complete
to: step_11

- name: Cleanup
steps:
Expand Down
1 change: 0 additions & 1 deletion in_app_purchases/step_00/app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ app.*.map.json
GoogleService-Info.plist
google-services.json
ios/firebase_app_id_file.json
lib/firebase_options.dart
2 changes: 0 additions & 2 deletions in_app_purchases/step_00/app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ dependencies:
firebase_core: ^3.8.1
google_sign_in: ^6.2.2
http: ^1.2.2
in_app_purchase: ^3.2.0
in_app_purchase_platform_interface: ^1.4.0
intl: ^0.20.1
provider: ^6.1.2

Expand Down
1 change: 0 additions & 1 deletion in_app_purchases/step_00/dart-backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ build/
assets/*.p8
assets/appstore.token
assets/service-account*.json
lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ app.*.map.json
GoogleService-Info.plist
google-services.json
ios/firebase_app_id_file.json
lib/firebase_options.dart
40 changes: 40 additions & 0 deletions in_app_purchases/step_03/app/lib/logic/dash_purchases.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../model/purchasable_product.dart';
import '../model/store_state.dart';
import 'dash_counter.dart';

class DashPurchases extends ChangeNotifier {
DashCounter counter;
StoreState storeState = StoreState.available;
List<PurchasableProduct> products = [
PurchasableProduct(
'Spring is in the air',
'Many dashes flying out from their nests',
'\$0.99',
),
PurchasableProduct(
'Jet engine',
'Doubles you clicks per second for a day',
'\$1.99',
),
];

bool get beautifiedDash => false;

DashPurchases(this.counter);

Future<void> buy(PurchasableProduct product) async {
product.status = ProductStatus.pending;
notifyListeners();
await Future<void>.delayed(const Duration(seconds: 5));
product.status = ProductStatus.purchased;
notifyListeners();
await Future<void>.delayed(const Duration(seconds: 5));
product.status = ProductStatus.purchasable;
notifyListeners();
}
}
81 changes: 81 additions & 0 deletions in_app_purchases/step_03/app/lib/logic/firebase_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:google_sign_in/google_sign_in.dart';

import '../firebase_options.dart';
import '../model/firebase_state.dart';

class FirebaseNotifier extends ChangeNotifier {
bool loggedIn = false;
FirebaseState state = FirebaseState.loading;
bool isLoggingIn = false;

FirebaseNotifier() {
load();
}

late final Completer<bool> _isInitialized = Completer();

Future<FirebaseFirestore> get firestore async {
var isInitialized = await _isInitialized.future;
if (!isInitialized) {
throw Exception('Firebase is not initialized');
}
return FirebaseFirestore.instance;
}

Future<void> load() async {
try {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
loggedIn = FirebaseAuth.instance.currentUser != null;
state = FirebaseState.available;
_isInitialized.complete(true);
notifyListeners();
} catch (e) {
state = FirebaseState.notAvailable;
_isInitialized.complete(false);
notifyListeners();
}
}

Future<void> login() async {
isLoggingIn = true;
notifyListeners();
// Trigger the authentication flow
try {
final googleUser = await GoogleSignIn().signIn();
if (googleUser == null) {
isLoggingIn = false;
notifyListeners();
return;
}

// Obtain the auth details from the request
final googleAuth = await googleUser.authentication;

// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);

// Once signed in, return the UserCredential
await FirebaseAuth.instance.signInWithCredential(credential);

loggedIn = true;
isLoggingIn = false;
notifyListeners();
} catch (e) {
isLoggingIn = false;
notifyListeners();
return;
}
}
}
Loading
Loading