-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/force_update' into 'develop'
Feature/force update See merge request milind.mevada/flutter_scaffold_project!12
- Loading branch information
Showing
7 changed files
with
168 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//TODO Update store URL for update | ||
const androidPlayStoreUrl = | ||
'https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1'; | ||
const iosAppStoreUrl = ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"welcome_message": "Welcome to Scaffold Project" | ||
"welcome_message": "Welcome to Scaffold Project", | ||
"flexible_update_msg": "New App Update Available", | ||
"title_immediate_update": "Update available", | ||
"content_immediate_update": "Please update App to new version for better experience.", | ||
"btn_update": "UPDATE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import 'dart:io' show Platform; | ||
|
||
import 'package:easy_localization/easy_localization.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_base_project/app_constants.dart'; | ||
import 'package:flutter_base_project/src/app_update/app_update_key_provider.dart'; | ||
import 'package:flutter_base_project/src/remote_config/remote_config_repository.dart'; | ||
import 'package:package_info/package_info.dart'; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
||
enum UpdateMode { NoUpdate, FlexibleUpdate, ImmediateUpdate } | ||
|
||
class AppUpdateWidget extends StatefulWidget { | ||
@override | ||
_AppUpdateWidgetState createState() => _AppUpdateWidgetState(); | ||
} | ||
|
||
class _AppUpdateWidgetState extends State<AppUpdateWidget> { | ||
@override | ||
void initState() { | ||
super.initState(); | ||
promptAppUpdate(context); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container(); | ||
} | ||
} | ||
|
||
Future<void> promptAppUpdate(BuildContext context) async { | ||
final update = await checkAppUpdate( | ||
remoteConfigRepository: RemoteConfigRepository(), | ||
); | ||
|
||
if (update == UpdateMode.NoUpdate) { | ||
return null; | ||
} | ||
|
||
if (update == UpdateMode.FlexibleUpdate) { | ||
Scaffold.of(context).showSnackBar( | ||
SnackBar( | ||
content: Text(AppLocalizations.of(context).tr('flexible_update_msg')), | ||
action: SnackBarAction( | ||
label: AppLocalizations.of(context).tr('btn_update'), | ||
onPressed: () => openStore(), | ||
), | ||
), | ||
); | ||
} | ||
|
||
if (update == UpdateMode.ImmediateUpdate) { | ||
return showDialog( | ||
context: context, | ||
barrierDismissible: false, | ||
builder: (context) { | ||
return AlertDialog( | ||
title: | ||
Text(AppLocalizations.of(context).tr('title_immediate_update')), | ||
content: | ||
Text(AppLocalizations.of(context).tr('content_immediate_update')), | ||
actions: <Widget>[ | ||
FlatButton( | ||
child: Text(AppLocalizations.of(context).tr('btn_update')), | ||
onPressed: () => openStore(), | ||
) | ||
], | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
Future<UpdateMode> checkAppUpdate({ | ||
@required RemoteConfigRepository remoteConfigRepository, | ||
}) async { | ||
final keyProvider = platformKeyProvider(); | ||
|
||
final currentVersion = int.parse(await getCurrentAppVersion()); | ||
final latestStableVersion = int.parse( | ||
await remoteConfigRepository.getString( | ||
keyProvider.keyLatestStableVersion, | ||
), | ||
); | ||
final latestVersion = int.parse( | ||
await remoteConfigRepository.getString( | ||
keyProvider.keyLatestVersion, | ||
), | ||
); | ||
|
||
if (currentVersion >= latestVersion) { | ||
return UpdateMode.NoUpdate; | ||
} | ||
|
||
if (currentVersion < latestStableVersion) { | ||
return UpdateMode.ImmediateUpdate; | ||
} | ||
|
||
return UpdateMode.FlexibleUpdate; | ||
} | ||
|
||
Future<String> getCurrentAppVersion() async { | ||
final packageInfo = await PackageInfo.fromPlatform(); | ||
return packageInfo.buildNumber; | ||
} | ||
|
||
Future<void> openStore() async { | ||
String url; | ||
if (Platform.isAndroid) { | ||
url = androidPlayStoreUrl; | ||
} else if (Platform.isIOS) { | ||
url = iosAppStoreUrl; | ||
} | ||
if (await canLaunch(url)) { | ||
await launch(url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'dart:io' show Platform; | ||
|
||
abstract class AppUpdateKeyProvider { | ||
String keyLatestStableVersion; | ||
String keyLatestVersion; | ||
} | ||
|
||
class AndroidAppUpdateKey implements AppUpdateKeyProvider { | ||
@override | ||
String keyLatestStableVersion = 'android_latest_stable_version'; | ||
|
||
@override | ||
String keyLatestVersion = 'android_latest_version'; | ||
} | ||
|
||
class IOSAppUpdateKey implements AppUpdateKeyProvider { | ||
@override | ||
String keyLatestStableVersion = 'iOS_latest_stable_version'; | ||
|
||
@override | ||
String keyLatestVersion = 'iOS_latest_version'; | ||
} | ||
|
||
AppUpdateKeyProvider platformKeyProvider() { | ||
if (Platform.isAndroid) { | ||
return AndroidAppUpdateKey(); | ||
} else if (Platform.isIOS) { | ||
return IOSAppUpdateKey(); | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters