1
+ import 'dart:convert' ;
1
2
import 'dart:io' ;
2
-
3
+ import 'package:firebase_remote_config/firebase_remote_config.dart' ;
3
4
import 'package:flutter/cupertino.dart' ;
5
+ import 'package:flutter/foundation.dart' ;
4
6
import 'package:flutter/material.dart' ;
5
7
import 'package:flutter_twitter_clone/helper/enum.dart' ;
6
8
import 'package:flutter_twitter_clone/helper/theme.dart' ;
9
+ import 'package:flutter_twitter_clone/helper/utility.dart' ;
7
10
import 'package:flutter_twitter_clone/page/Auth/selectAuthMethod.dart' ;
11
+ import 'package:flutter_twitter_clone/page/common/updateApp.dart' ;
8
12
import 'package:flutter_twitter_clone/page/homePage.dart' ;
9
13
import 'package:flutter_twitter_clone/state/authState.dart' ;
10
14
import 'package:flutter_twitter_clone/widgets/customWidgets.dart' ;
15
+ import 'package:package_info/package_info.dart' ;
11
16
import 'package:provider/provider.dart' ;
12
17
13
18
class SplashPage extends StatefulWidget {
@@ -27,11 +32,73 @@ class _SplashPageState extends State<SplashPage> {
27
32
}
28
33
29
34
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
+ }
35
102
}
36
103
37
104
Widget _body () {
0 commit comments