subscription_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:awesome_dialog/awesome_dialog.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/widgets.dart';
  7. import 'package:flutter_inappwebview/flutter_inappwebview.dart';
  8. import 'package:in_app_purchase/in_app_purchase.dart';
  9. import 'package:logging/logging.dart';
  10. import 'package:photos/core/event_bus.dart';
  11. import 'package:photos/events/user_authenticated_event.dart';
  12. import 'package:photos/models/billing_plan.dart';
  13. import 'package:photos/services/billing_service.dart';
  14. import 'package:photos/ui/loading_widget.dart';
  15. import 'package:photos/utils/dialog_util.dart';
  16. class SubscriptionPage extends StatefulWidget {
  17. const SubscriptionPage({Key key}) : super(key: key);
  18. @override
  19. _SubscriptionPageState createState() => _SubscriptionPageState();
  20. }
  21. class _SubscriptionPageState extends State<SubscriptionPage> {
  22. final _logger = Logger("SubscriptionPage");
  23. StreamSubscription _purchaseUpdateSubscription;
  24. @override
  25. void initState() {
  26. _purchaseUpdateSubscription = InAppPurchaseConnection
  27. .instance.purchaseUpdatedStream
  28. .listen((event) async {
  29. for (final e in event) {
  30. if (e.status == PurchaseStatus.purchased && e.pendingCompletePurchase) {
  31. final dialog = createProgressDialog(context, "verifying purchase...");
  32. await dialog.show();
  33. try {
  34. await BillingService.instance.verifySubscription(
  35. e.productID, e.verificationData.serverVerificationData);
  36. } catch (e) {
  37. _logger.warning("Could not complete payment ", e);
  38. await dialog.hide();
  39. showErrorDialog(
  40. context,
  41. "payment failed",
  42. "please talk to " +
  43. (Platform.isAndroid ? "PlayStore" : "AppStore") +
  44. " support if you were charged");
  45. return;
  46. }
  47. await InAppPurchaseConnection.instance.completePurchase(e);
  48. Bus.instance.fire(UserAuthenticatedEvent());
  49. await dialog.hide();
  50. Navigator.pop(context);
  51. AwesomeDialog(
  52. context: context,
  53. dialogType: DialogType.SUCCES,
  54. animType: AnimType.RIGHSLIDE,
  55. title: '',
  56. desc:
  57. 'your photos and videos will now be encrypted and backed up in the background',
  58. btnOkOnPress: () {
  59. Navigator.pop(context);
  60. },
  61. btnOkText: 'ok',
  62. headerAnimationLoop: false,
  63. )..show();
  64. } else if (Platform.isIOS) {
  65. await InAppPurchaseConnection.instance.completePurchase(e);
  66. }
  67. }
  68. });
  69. super.initState();
  70. }
  71. @override
  72. void dispose() {
  73. _purchaseUpdateSubscription.cancel();
  74. super.dispose();
  75. }
  76. @override
  77. Widget build(BuildContext context) {
  78. final appBar = AppBar(
  79. title: Text("choose plan"),
  80. );
  81. return Scaffold(
  82. appBar: appBar,
  83. body: _getBody(appBar.preferredSize.height),
  84. );
  85. }
  86. Widget _getBody(final appBarSize) {
  87. return FutureBuilder<List<BillingPlan>>(
  88. future: BillingService.instance.getBillingPlans(),
  89. builder: (BuildContext context, AsyncSnapshot snapshot) {
  90. if (snapshot.hasData) {
  91. return _buildPlans(context, snapshot.data, appBarSize);
  92. } else if (snapshot.hasError) {
  93. return Text("Oops, something went wrong.");
  94. } else {
  95. return loadWidget;
  96. }
  97. },
  98. );
  99. }
  100. Widget _buildPlans(
  101. BuildContext context, List<BillingPlan> plans, final appBarSize) {
  102. final planWidgets = List<Widget>();
  103. for (final plan in plans) {
  104. planWidgets.add(
  105. Material(
  106. child: InkWell(
  107. onTap: () async {
  108. final dialog = createProgressDialog(context, "please wait...");
  109. await dialog.show();
  110. // ignore: sdk_version_set_literal
  111. Set<String> _kIds = {
  112. Platform.isAndroid ? plan.androidID : plan.iosID
  113. };
  114. final ProductDetailsResponse response =
  115. await InAppPurchaseConnection.instance
  116. .queryProductDetails(_kIds);
  117. if (response.notFoundIDs.isNotEmpty) {
  118. await dialog.hide();
  119. showGenericErrorDialog(context);
  120. return;
  121. }
  122. List<ProductDetails> productDetails = response.productDetails;
  123. final PurchaseParam purchaseParam =
  124. PurchaseParam(productDetails: productDetails[0]);
  125. await InAppPurchaseConnection.instance
  126. .buyNonConsumable(purchaseParam: purchaseParam);
  127. await dialog.hide();
  128. },
  129. child: SubscriptionPlanWidget(plan: plan),
  130. ),
  131. ),
  132. );
  133. }
  134. final pageSize = MediaQuery.of(context).size.height;
  135. final notifySize = MediaQuery.of(context).padding.top;
  136. return SingleChildScrollView(
  137. child: Container(
  138. height: pageSize - (appBarSize + notifySize),
  139. child: Column(
  140. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  141. children: <Widget>[
  142. Padding(
  143. padding: const EdgeInsets.fromLTRB(12, 20, 12, 12),
  144. child: Text(
  145. "ente preserves your photos and videos, so they're always available, even if you lose your device",
  146. style: TextStyle(
  147. color: Colors.white54,
  148. height: 1.2,
  149. ),
  150. ),
  151. ),
  152. Padding(padding: EdgeInsets.all(12)),
  153. Column(
  154. mainAxisAlignment: MainAxisAlignment.center,
  155. children: planWidgets,
  156. ),
  157. Padding(padding: EdgeInsets.all(8)),
  158. Align(
  159. alignment: Alignment.topLeft,
  160. child: Padding(
  161. padding: EdgeInsets.all(12),
  162. child: Text(
  163. "we offer a 14 day free trial, you can cancel anytime",
  164. style: TextStyle(
  165. color: Colors.white54,
  166. height: 1.2,
  167. ),
  168. ),
  169. ),
  170. ),
  171. Expanded(child: Container()),
  172. Align(
  173. alignment: Alignment.center,
  174. child: GestureDetector(
  175. onTap: () {
  176. showDialog(
  177. context: context,
  178. builder: (builder) {
  179. return LearnMoreWidget();
  180. },
  181. );
  182. },
  183. child: Container(
  184. padding: EdgeInsets.all(40),
  185. child: RichText(
  186. text: TextSpan(
  187. text: "learn more",
  188. style: TextStyle(
  189. color: Colors.blue,
  190. fontFamily: 'Ubuntu',
  191. ),
  192. ),
  193. ),
  194. ),
  195. ),
  196. ),
  197. ],
  198. ),
  199. ),
  200. );
  201. }
  202. }
  203. class LearnMoreWidget extends StatefulWidget {
  204. const LearnMoreWidget({
  205. Key key,
  206. }) : super(key: key);
  207. @override
  208. _LearnMoreWidgetState createState() => _LearnMoreWidgetState();
  209. }
  210. class _LearnMoreWidgetState extends State<LearnMoreWidget> {
  211. int _progress = 0;
  212. @override
  213. Widget build(BuildContext context) {
  214. return Padding(
  215. padding: const EdgeInsets.fromLTRB(0, 40, 0, 0),
  216. child: ClipRRect(
  217. borderRadius: BorderRadius.only(
  218. topLeft: Radius.circular(20),
  219. topRight: Radius.circular(20),
  220. ),
  221. child: Column(
  222. children: [
  223. Expanded(
  224. child: InAppWebView(
  225. initialUrl: 'https://ente.io/faq',
  226. onProgressChanged: (c, progress) {
  227. setState(() {
  228. _progress = progress;
  229. });
  230. },
  231. ),
  232. ),
  233. Column(
  234. children: [
  235. _progress < 100
  236. ? LinearProgressIndicator(
  237. value: _progress / 100,
  238. minHeight: 2,
  239. )
  240. : Container(),
  241. Padding(
  242. padding: const EdgeInsets.fromLTRB(16, 0, 16, 0),
  243. child: FlatButton(
  244. child: Text("close"),
  245. shape: RoundedRectangleBorder(
  246. borderRadius: BorderRadius.circular(12.0),
  247. ),
  248. color: Colors.grey[850],
  249. minWidth: double.infinity,
  250. onPressed: () {
  251. Navigator.of(context).pop();
  252. },
  253. ),
  254. ),
  255. ],
  256. ),
  257. ],
  258. ),
  259. ),
  260. );
  261. }
  262. }
  263. class SubscriptionPlanWidget extends StatelessWidget {
  264. const SubscriptionPlanWidget({
  265. Key key,
  266. @required this.plan,
  267. }) : super(key: key);
  268. final BillingPlan plan;
  269. @override
  270. Widget build(BuildContext context) {
  271. return Container(
  272. color: Theme.of(context).cardColor,
  273. child: Column(
  274. children: [
  275. Row(
  276. children: [
  277. Padding(
  278. padding: const EdgeInsets.all(10.0),
  279. child: Card(
  280. shape: RoundedRectangleBorder(
  281. borderRadius: BorderRadius.circular(12.0),
  282. ),
  283. color: Color(0xDFFFFFFF),
  284. child: Container(
  285. width: 100,
  286. padding: EdgeInsets.fromLTRB(0, 20, 0, 20),
  287. child: Column(
  288. children: [
  289. Text(
  290. (plan.storageInMBs / 1024).round().toString() + " GB",
  291. style: TextStyle(
  292. fontWeight: FontWeight.bold,
  293. fontSize: 16,
  294. color: Theme.of(context).cardColor,
  295. ),
  296. ),
  297. ],
  298. ),
  299. ),
  300. ),
  301. ),
  302. Text(plan.price + " per " + plan.period),
  303. ],
  304. ),
  305. Divider(
  306. height: 1,
  307. ),
  308. ],
  309. ),
  310. );
  311. }
  312. }
  313. class SubsriptionSuccessfulDialog extends StatelessWidget {
  314. const SubsriptionSuccessfulDialog({Key key}) : super(key: key);
  315. @override
  316. Widget build(BuildContext context) {
  317. return AlertDialog(
  318. title: Text("success!",
  319. style: TextStyle(
  320. fontWeight: FontWeight.bold,
  321. )),
  322. content: SingleChildScrollView(
  323. child: Column(children: [
  324. Text("your photos and videos will now be backed up"),
  325. Padding(padding: EdgeInsets.all(6)),
  326. Text("the first sync might take a while, please bear with us"),
  327. ]),
  328. ),
  329. actions: [
  330. FlatButton(
  331. child: Text("ok"),
  332. onPressed: () {
  333. Navigator.of(context).pop();
  334. },
  335. ),
  336. ],
  337. );
  338. }
  339. }