From 701e0d054ac0a5a614b24a5af082b4c4fdbda966 Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Wed, 24 Aug 2016 13:32:27 -0700 Subject: [PATCH] Add update dialog to gallery (#5569) Add update dialog to gallery, open source part of #4626 --- examples/flutter_gallery/lib/gallery/app.dart | 40 ++++++++--- .../flutter_gallery/lib/gallery/updates.dart | 71 +++++++++++++++++++ .../flutter_gallery/test/update_test.dart | 49 +++++++++++++ 3 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 examples/flutter_gallery/lib/gallery/updates.dart create mode 100644 examples/flutter_gallery/test/update_test.dart diff --git a/examples/flutter_gallery/lib/gallery/app.dart b/examples/flutter_gallery/lib/gallery/app.dart index bea6bcf1af..4b3acc12f7 100644 --- a/examples/flutter_gallery/lib/gallery/app.dart +++ b/examples/flutter_gallery/lib/gallery/app.dart @@ -7,11 +7,12 @@ import 'package:flutter/scheduler.dart' show timeDilation; import 'item.dart'; import 'home.dart'; +import 'updates.dart'; final Map _kRoutes = new Map.fromIterable( kAllGalleryItems, key: (GalleryItem item) => item.routeName, - value: (GalleryItem item) => item.buildRoute + value: (GalleryItem item) => item.buildRoute, ); final ThemeData _kGalleryLightTheme = new ThemeData( @@ -21,11 +22,13 @@ final ThemeData _kGalleryLightTheme = new ThemeData( final ThemeData _kGalleryDarkTheme = new ThemeData( brightness: Brightness.dark, - primarySwatch: Colors.lightBlue + primarySwatch: Colors.lightBlue, ); class GalleryApp extends StatefulWidget { - GalleryApp({ Key key }) : super(key: key); + GalleryApp({this.updateUrlFetcher, Key key}) : super(key: key); + + final UpdateUrlFetcher updateUrlFetcher; @override GalleryAppState createState() => new GalleryAppState(); @@ -47,27 +50,44 @@ class GalleryAppState extends State { Widget home = new GalleryHome( useLightTheme: _useLightTheme, - onThemeChanged: (bool value) { setState(() { _useLightTheme = value; }); }, + onThemeChanged: (bool value) { + setState(() { + _useLightTheme = value; + }); + }, showPerformanceOverlay: _showPerformanceOverlay, - onShowPerformanceOverlayChanged: (bool value) { setState(() { _showPerformanceOverlay = value; }); }, + onShowPerformanceOverlayChanged: (bool value) { + setState(() { + _showPerformanceOverlay = value; + }); + }, timeDilation: timeDilation, - onTimeDilationChanged: (double value) { setState(() { timeDilation = value; }); } + onTimeDilationChanged: (double value) { + setState(() { + timeDilation = value; + }); + }, ); - if (showPreviewBanner) { + if (showPreviewBanner) home = new Banner( message: 'PREVIEW', location: BannerLocation.topRight, - child: home + child: home, + ); + + if (config.updateUrlFetcher != null) + home = new Updater( + updateUrlFetcher: config.updateUrlFetcher, + child: home, ); - } return new MaterialApp( title: 'Flutter Gallery', theme: _useLightTheme ? _kGalleryLightTheme : _kGalleryDarkTheme, showPerformanceOverlay: _showPerformanceOverlay, routes: _kRoutes, - home: home + home: home, ); } } diff --git a/examples/flutter_gallery/lib/gallery/updates.dart b/examples/flutter_gallery/lib/gallery/updates.dart new file mode 100644 index 0000000000..36f245274b --- /dev/null +++ b/examples/flutter_gallery/lib/gallery/updates.dart @@ -0,0 +1,71 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +typedef Future UpdateUrlFetcher(); + +class Updater extends StatefulWidget { + Updater({ this.updateUrlFetcher, this.child, Key key }) : super(key: key) { + assert(updateUrlFetcher != null); + } + + final UpdateUrlFetcher updateUrlFetcher; + final Widget child; + + @override + State createState() => new UpdaterState(); +} + +class UpdaterState extends State { + @override + void initState() { + super.initState(); + _checkForUpdates(); + } + + static DateTime _lastUpdateCheck; + Future _checkForUpdates() async { + // Only prompt once a day + if (_lastUpdateCheck != null && + new DateTime.now().difference(_lastUpdateCheck) < new Duration(days: 1)) { + return; // We already checked for updates recently + } + _lastUpdateCheck = new DateTime.now(); + + String updateUrl = await config.updateUrlFetcher(); + if (updateUrl != null) { + bool wantsUpdate = await showDialog(context: context, child: _buildDialog()); + if (wantsUpdate != null && wantsUpdate) + UrlLauncher.launch(updateUrl); + } + } + + Widget _buildDialog() { + final ThemeData theme = Theme.of(context); + final TextStyle dialogTextStyle = + theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color); + return new Dialog( + title: new Text('Update Flutter Gallery?'), + content: new Text('A newer version is available.', style: dialogTextStyle), + actions: [ + new FlatButton( + child: new Text('NO THANKS'), + onPressed: () { + Navigator.pop(context, false); + }), + new FlatButton( + child: new Text('UPDATE'), + onPressed: () { + Navigator.pop(context, true); + }), + ]); + } + + @override + Widget build(BuildContext context) => config.child; +} diff --git a/examples/flutter_gallery/test/update_test.dart b/examples/flutter_gallery/test/update_test.dart new file mode 100644 index 0000000000..5f98c4f74a --- /dev/null +++ b/examples/flutter_gallery/test/update_test.dart @@ -0,0 +1,49 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter_gallery/gallery/app.dart'; + +Future mockUpdateUrlFetcher() { + // A real implementation would connect to the network to retrieve this value + return new Future.value('http://www.example.com/'); +} + +Finder byTooltip(WidgetTester tester, String message) { + return find.byWidgetPredicate((Widget widget) { + return widget is Tooltip && widget.message == message; + }); +} + +Finder findBackButton(WidgetTester tester) => byTooltip(tester, 'Back'); + +void main() { + TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized(); + if (binding is LiveTestWidgetsFlutterBinding) binding.allowAllFrames = true; + + // Regression test for https://github.com/flutter/flutter/pull/5168 + testWidgets('update dialog', (WidgetTester tester) async { + await tester.pumpWidget(new GalleryApp(updateUrlFetcher: mockUpdateUrlFetcher)); + await tester.pump(); // see https://github.com/flutter/flutter/issues/1865 + await tester.pump(); // triggers a frame + + expect(find.text('UPDATE'), findsOneWidget); + + await tester.tap(find.text('NO THANKS')); + await tester.pump(); + + await tester.tap(find.text('Shrine')); + await tester.pump(); // Launch shrine + await tester.pump(const Duration(seconds: 1)); // transition is complete + + Finder backButton = findBackButton(tester); + expect(backButton, findsOneWidget); + await tester.tap(backButton); + await tester.pump(); // Start the pop "back" operation. + await tester.pump(const Duration(seconds: 1)); // transition is complete + + expect(find.text('UPDATE'), findsNothing); + }); +}