Skip to content

Commit

Permalink
Merge branch 'feature/force_update' into 'develop'
Browse files Browse the repository at this point in the history
Feature/force update

See merge request milind.mevada/flutter_scaffold_project!12
  • Loading branch information
Milind Mevada committed Jan 3, 2020
2 parents ecb175a + 01f2e7d commit f11032f
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 18 deletions.
14 changes: 0 additions & 14 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ android {
resValue "string", "app_name", "Flutter-Base"
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def project = "Flutter-Base"
def SEP = "_"
def flavor = variant.productFlavors[0].name
def buildType = variant.variantData.variantConfiguration.buildType.name
def version = variant.versionName
def versionCode = variant.versionCode

def newApkName = project + SEP + flavor + SEP + buildType + SEP + version + SEP + "(" + versionCode + ")" + ".apk"

outputFileName = new File(newApkName)
}
}
}

flutter {
Expand Down
4 changes: 4 additions & 0 deletions lib/app_constants.dart
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 = '';
6 changes: 5 additions & 1 deletion lib/assets/strings/en-US.json
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"
}
8 changes: 6 additions & 2 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:firebase_performance/firebase_performance.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_project/src/analytics/analytics.dart';
import 'package:flutter_base_project/config.dart';
import 'package:flutter_base_project/src/app_update/app_update.dart';
import 'package:flutter_base_project/src/error_logger/error_logger.dart';
import 'package:flutter_base_project/src/remote_config/remote_config_repository.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
Expand All @@ -28,7 +29,9 @@ Future<void> initApp() async {
try {
await RemoteConfigRepository().initConfig();
await RemoteConfigRepository().syncConfig();
} catch (_) {}
} catch (e) {
debugPrint(e);
}

await runZoned<Future<Null>>(
() async {
Expand Down Expand Up @@ -105,7 +108,8 @@ class _AppState extends State<App> {
await dio.get(url);
debugPrint("DIO RESPONSE");
},
)
),
AppUpdateWidget(),
],
),
),
Expand Down
119 changes: 119 additions & 0 deletions lib/src/app_update/app_update.dart
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);
}
}
31 changes: 31 additions & 0 deletions lib/src/app_update/app_update_key_provider.dart
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;
}
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_base_project
description: Scaffold project with all basic setup.

version: 1.0.0+1
version: 1.0.0+2

environment:
sdk: ">=2.6.0 <3.0.0"
Expand All @@ -22,6 +22,8 @@ dependencies:

sentry: ^3.0.0+1

url_launcher: ^5.4.1

#Firebase
firebase_crashlytics: ^0.1.2+4
firebase_analytics: ^5.0.9
Expand Down

0 comments on commit f11032f

Please sign in to comment.