Skip to content

Commit b890649

Browse files
committed
✨ (feat) Auto update screen added
1 parent a7958d4 commit b890649

File tree

5 files changed

+165
-12
lines changed

5 files changed

+165
-12
lines changed

lib/page/common/splash.dart

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
import 'dart:convert';
12
import 'dart:io';
2-
3+
import 'package:firebase_remote_config/firebase_remote_config.dart';
34
import 'package:flutter/cupertino.dart';
5+
import 'package:flutter/foundation.dart';
46
import 'package:flutter/material.dart';
57
import 'package:flutter_twitter_clone/helper/enum.dart';
68
import 'package:flutter_twitter_clone/helper/theme.dart';
9+
import 'package:flutter_twitter_clone/helper/utility.dart';
710
import 'package:flutter_twitter_clone/page/Auth/selectAuthMethod.dart';
11+
import 'package:flutter_twitter_clone/page/common/updateApp.dart';
812
import 'package:flutter_twitter_clone/page/homePage.dart';
913
import 'package:flutter_twitter_clone/state/authState.dart';
1014
import 'package:flutter_twitter_clone/widgets/customWidgets.dart';
15+
import 'package:package_info/package_info.dart';
1116
import 'package:provider/provider.dart';
1217

1318
class SplashPage extends StatefulWidget {
@@ -27,11 +32,73 @@ class _SplashPageState extends State<SplashPage> {
2732
}
2833

2934
void timer() async {
30-
Future.delayed(Duration(seconds: 1)).then((_) {
31-
var state = Provider.of<AuthState>(context, listen: false);
32-
// state.authStatus = AuthStatus.NOT_DETERMINED;
33-
state.getCurrentUser();
34-
});
35+
final isAppUpdated = await _checkAppVersion();
36+
if (isAppUpdated) {
37+
print("App is updated");
38+
Future.delayed(Duration(seconds: 1)).then((_) {
39+
var state = Provider.of<AuthState>(context, listen: false);
40+
// state.authStatus = AuthStatus.NOT_DETERMINED;
41+
state.getCurrentUser();
42+
});
43+
}
44+
}
45+
/// Return installed app version
46+
/// For testing purpose in debug mode update screen will not be open up
47+
/// In an old version of realease app is installed on user's device then
48+
/// User will not be able to see home screen
49+
/// User will redirected to update app screen.
50+
/// Once user update app with latest verson and back to app then user automatically redirected to welcome / Home page
51+
Future<bool> _checkAppVersion() async {
52+
PackageInfo packageInfo = await PackageInfo.fromPlatform();
53+
final currentAppVersion = "${packageInfo.version}";
54+
final appVersion = await _getAppVersionFromFirebaseConfig();
55+
if (appVersion != currentAppVersion) {
56+
if(kDebugMode){
57+
cprint("Latest version of app is not installed on your system");
58+
cprint("In debug mode we are not restrict devlopers to redirect to update screen");
59+
cprint("Redirect devs to update screen can put other devs in confusion");
60+
return true;
61+
}
62+
Navigator.pushReplacement(
63+
context,
64+
MaterialPageRoute(
65+
builder: (_) => UpdateApp(),
66+
),
67+
);
68+
return false;
69+
} else {
70+
return true;
71+
}
72+
}
73+
74+
/// Returns app version from firebase config
75+
/// Fecth Latest app version from firebase Remote config
76+
/// To check current installed app version check [version] in pubspec.yaml
77+
/// you have to add latest app version in firebase remote config
78+
/// To fetch this key go to project setting in firebase
79+
/// Click on `cloud messaging` tab
80+
/// Copy server key from `Project credentials`
81+
/// Now goto `Remote Congig` section in fireabse
82+
/// Add [appVersion] as paramerter key and below json in Default vslue
83+
/// ``` json
84+
/// {
85+
/// "key": "1.0.0"
86+
/// } ```
87+
/// After adding app version key click on Publish Change button
88+
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
89+
Future<String> _getAppVersionFromFirebaseConfig() async {
90+
final RemoteConfig remoteConfig = await RemoteConfig.instance;
91+
await remoteConfig.fetch(expiration: const Duration(minutes: 1));
92+
await remoteConfig.activateFetched();
93+
var data = remoteConfig.getString('appVersion');
94+
if (data != null && data.isNotEmpty) {
95+
return jsonDecode(data)["key"];
96+
} else {
97+
cprint(
98+
"Please add your app's current version into Remote config in firebase",
99+
errorIn: "_getAppVersionFromFirebaseConfig");
100+
return null;
101+
}
35102
}
36103

37104
Widget _body() {

lib/page/common/updateApp.dart

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_twitter_clone/helper/theme.dart';
3+
import 'package:flutter_twitter_clone/helper/utility.dart';
4+
import 'package:flutter_twitter_clone/page/common/splash.dart';
5+
import 'package:flutter_twitter_clone/widgets/customWidgets.dart';
6+
import 'package:flutter_twitter_clone/widgets/newWidget/title_text.dart';
7+
8+
class UpdateApp extends StatefulWidget {
9+
const UpdateApp({Key key}) : super(key: key);
10+
11+
@override
12+
_UpdateAppState createState() => _UpdateAppState();
13+
}
14+
15+
class _UpdateAppState extends State<UpdateApp> with WidgetsBindingObserver {
16+
@override
17+
void initState() {
18+
super.initState();
19+
WidgetsBinding.instance.addObserver(this);
20+
}
21+
22+
@override
23+
void dispose() {
24+
WidgetsBinding.instance.removeObserver(this);
25+
super.dispose();
26+
}
27+
28+
@override
29+
void didChangeAppLifecycleState(AppLifecycleState state) {
30+
if (state == AppLifecycleState.resumed) {
31+
Navigator.pushReplacement(
32+
context, MaterialPageRoute(builder: (context) => SplashPage()));
33+
}
34+
}
35+
36+
@override
37+
Widget build(BuildContext context) {
38+
return Scaffold(
39+
backgroundColor: TwitterColor.mystic,
40+
body: Container(
41+
margin: EdgeInsets.symmetric(horizontal: 36),
42+
child: Column(
43+
mainAxisAlignment: MainAxisAlignment.center,
44+
children: <Widget>[
45+
Image.asset("assets/images/icon-480.png"),
46+
TitleText(
47+
"New Update is available",
48+
fontSize: 25,
49+
textAlign: TextAlign.center,
50+
),
51+
SizedBox(height: 20),
52+
TitleText(
53+
"The current version of app is no longer supported. We aploigize for any inconveiience we may have caused you",
54+
fontSize: 14,
55+
color: AppColor.darkGrey,
56+
textAlign: TextAlign.center,
57+
),
58+
SizedBox(height: 30),
59+
Container(
60+
width: fullWidth(context),
61+
margin: EdgeInsets.symmetric(vertical: 35),
62+
child: FlatButton(
63+
shape: RoundedRectangleBorder(
64+
borderRadius: BorderRadius.circular(30)),
65+
color: TwitterColor.dodgetBlue,
66+
onPressed: () {
67+
launchURL(
68+
"https://play.google.com/store/apps/details?id=com.thealphamerc.flutter_twitter_clone");
69+
},
70+
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
71+
child: TitleText('Update now', color: Colors.white),
72+
),
73+
)
74+
],
75+
),
76+
),
77+
);
78+
}
79+
}

lib/state/chats/chatState.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ChatState extends AppState {
8181
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
8282
void getFCMServerKey() async {
8383
final RemoteConfig remoteConfig = await RemoteConfig.instance;
84-
await remoteConfig.fetch(expiration: const Duration(hours: 5));
84+
await remoteConfig.fetch(expiration: const Duration(days: 5));
8585
await remoteConfig.activateFetched();
8686
var data = remoteConfig.getString('FcmServerKey');
8787
if (data != null && data.isNotEmpty) {

pubspec.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ packages:
361361
url: "https://pub.dartlang.org"
362362
source: hosted
363363
version: "1.9.3"
364+
package_info:
365+
dependency: "direct main"
366+
description:
367+
name: package_info
368+
url: "https://pub.dartlang.org"
369+
source: hosted
370+
version: "0.4.1"
364371
package_resolver:
365372
dependency: transitive
366373
description:
@@ -669,4 +676,4 @@ packages:
669676
version: "2.2.0"
670677
sdks:
671678
dart: ">=2.7.0 <3.0.0"
672-
flutter: ">=1.12.13+hotfix.4 <2.0.0"
679+
flutter: ">=1.12.13+hotfix.5 <2.0.0"

pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ dependencies:
3131
firebase_auth:
3232
firebase_database:
3333
firebase_analytics:
34-
3534
cloud_firestore:
3635
firebase_storage:
37-
image_picker: ^0.6.2
3836
uuid: ^2.0.0
37+
intl: ^0.15.8
3938
http:
39+
google_sign_in:
40+
image_picker: ^0.6.2
41+
package_info: ^0.4.1
4042
shared_preferences: ^0.5.1+2
4143
firebase_messaging: ^6.0.13
42-
google_sign_in:
43-
intl: ^0.15.8
4444
url_launcher:
4545
share: ^0.6.3
4646
google_fonts: ^0.3.9

0 commit comments

Comments
 (0)