Skip to content

Commit

Permalink
✨ (feat) Auto update screen added
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlphamerc committed Jul 15, 2020
1 parent a7958d4 commit b890649
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 12 deletions.
79 changes: 73 additions & 6 deletions lib/page/common/splash.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import 'dart:convert';
import 'dart:io';

import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_twitter_clone/helper/enum.dart';
import 'package:flutter_twitter_clone/helper/theme.dart';
import 'package:flutter_twitter_clone/helper/utility.dart';
import 'package:flutter_twitter_clone/page/Auth/selectAuthMethod.dart';
import 'package:flutter_twitter_clone/page/common/updateApp.dart';
import 'package:flutter_twitter_clone/page/homePage.dart';
import 'package:flutter_twitter_clone/state/authState.dart';
import 'package:flutter_twitter_clone/widgets/customWidgets.dart';
import 'package:package_info/package_info.dart';
import 'package:provider/provider.dart';

class SplashPage extends StatefulWidget {
Expand All @@ -27,11 +32,73 @@ class _SplashPageState extends State<SplashPage> {
}

void timer() async {
Future.delayed(Duration(seconds: 1)).then((_) {
var state = Provider.of<AuthState>(context, listen: false);
// state.authStatus = AuthStatus.NOT_DETERMINED;
state.getCurrentUser();
});
final isAppUpdated = await _checkAppVersion();
if (isAppUpdated) {
print("App is updated");
Future.delayed(Duration(seconds: 1)).then((_) {
var state = Provider.of<AuthState>(context, listen: false);
// state.authStatus = AuthStatus.NOT_DETERMINED;
state.getCurrentUser();
});
}
}
/// Return installed app version
/// For testing purpose in debug mode update screen will not be open up
/// In an old version of realease app is installed on user's device then
/// User will not be able to see home screen
/// User will redirected to update app screen.
/// Once user update app with latest verson and back to app then user automatically redirected to welcome / Home page
Future<bool> _checkAppVersion() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
final currentAppVersion = "${packageInfo.version}";
final appVersion = await _getAppVersionFromFirebaseConfig();
if (appVersion != currentAppVersion) {
if(kDebugMode){
cprint("Latest version of app is not installed on your system");
cprint("In debug mode we are not restrict devlopers to redirect to update screen");
cprint("Redirect devs to update screen can put other devs in confusion");
return true;
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => UpdateApp(),
),
);
return false;
} else {
return true;
}
}

/// Returns app version from firebase config
/// Fecth Latest app version from firebase Remote config
/// To check current installed app version check [version] in pubspec.yaml
/// you have to add latest app version in firebase remote config
/// To fetch this key go to project setting in firebase
/// Click on `cloud messaging` tab
/// Copy server key from `Project credentials`
/// Now goto `Remote Congig` section in fireabse
/// Add [appVersion] as paramerter key and below json in Default vslue
/// ``` json
/// {
/// "key": "1.0.0"
/// } ```
/// After adding app version key click on Publish Change button
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
Future<String> _getAppVersionFromFirebaseConfig() async {
final RemoteConfig remoteConfig = await RemoteConfig.instance;
await remoteConfig.fetch(expiration: const Duration(minutes: 1));
await remoteConfig.activateFetched();
var data = remoteConfig.getString('appVersion');
if (data != null && data.isNotEmpty) {
return jsonDecode(data)["key"];
} else {
cprint(
"Please add your app's current version into Remote config in firebase",
errorIn: "_getAppVersionFromFirebaseConfig");
return null;
}
}

Widget _body() {
Expand Down
79 changes: 79 additions & 0 deletions lib/page/common/updateApp.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:flutter/material.dart';
import 'package:flutter_twitter_clone/helper/theme.dart';
import 'package:flutter_twitter_clone/helper/utility.dart';
import 'package:flutter_twitter_clone/page/common/splash.dart';
import 'package:flutter_twitter_clone/widgets/customWidgets.dart';
import 'package:flutter_twitter_clone/widgets/newWidget/title_text.dart';

class UpdateApp extends StatefulWidget {
const UpdateApp({Key key}) : super(key: key);

@override
_UpdateAppState createState() => _UpdateAppState();
}

class _UpdateAppState extends State<UpdateApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => SplashPage()));
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: TwitterColor.mystic,
body: Container(
margin: EdgeInsets.symmetric(horizontal: 36),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset("assets/images/icon-480.png"),
TitleText(
"New Update is available",
fontSize: 25,
textAlign: TextAlign.center,
),
SizedBox(height: 20),
TitleText(
"The current version of app is no longer supported. We aploigize for any inconveiience we may have caused you",
fontSize: 14,
color: AppColor.darkGrey,
textAlign: TextAlign.center,
),
SizedBox(height: 30),
Container(
width: fullWidth(context),
margin: EdgeInsets.symmetric(vertical: 35),
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30)),
color: TwitterColor.dodgetBlue,
onPressed: () {
launchURL(
"https://play.google.com/store/apps/details?id=com.thealphamerc.flutter_twitter_clone");
},
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
child: TitleText('Update now', color: Colors.white),
),
)
],
),
),
);
}
}
2 changes: 1 addition & 1 deletion lib/state/chats/chatState.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ChatState extends AppState {
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
void getFCMServerKey() async {
final RemoteConfig remoteConfig = await RemoteConfig.instance;
await remoteConfig.fetch(expiration: const Duration(hours: 5));
await remoteConfig.fetch(expiration: const Duration(days: 5));
await remoteConfig.activateFetched();
var data = remoteConfig.getString('FcmServerKey');
if (data != null && data.isNotEmpty) {
Expand Down
9 changes: 8 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.3"
package_info:
dependency: "direct main"
description:
name: package_info
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1"
package_resolver:
dependency: transitive
description:
Expand Down Expand Up @@ -669,4 +676,4 @@ packages:
version: "2.2.0"
sdks:
dart: ">=2.7.0 <3.0.0"
flutter: ">=1.12.13+hotfix.4 <2.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ dependencies:
firebase_auth:
firebase_database:
firebase_analytics:

cloud_firestore:
firebase_storage:
image_picker: ^0.6.2
uuid: ^2.0.0
intl: ^0.15.8
http:
google_sign_in:
image_picker: ^0.6.2
package_info: ^0.4.1
shared_preferences: ^0.5.1+2
firebase_messaging: ^6.0.13
google_sign_in:
intl: ^0.15.8
url_launcher:
share: ^0.6.3
google_fonts: ^0.3.9
Expand Down

0 comments on commit b890649

Please sign in to comment.