Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 47 additions & 39 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:async/async.dart';
import 'package:connectivity/connectivity.dart';
import 'package:dropdown_banner/dropdown_banner.dart';
import 'package:flutter/cupertino.dart';
Expand All @@ -10,6 +11,7 @@ import 'package:get_it/get_it.dart';
import 'package:selftrackingapp/app_localizations.dart';
import 'package:selftrackingapp/networking/data_repository.dart';
import 'package:selftrackingapp/networking/db.dart';
import 'package:selftrackingapp/page/screen/dashboard_screen.dart';
import 'package:selftrackingapp/page/screen/root_screen.dart';
import 'package:selftrackingapp/page/screen/welcome_screen.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand All @@ -26,6 +28,8 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
final navigatorKey = GlobalKey<NavigatorState>();

@override
void initState() {
super.initState();
Expand All @@ -35,8 +39,6 @@ class _MyAppState extends State<MyApp> {

@override
Widget build(BuildContext context) {
final navigatorKey = GlobalKey<NavigatorState>();

return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'COVID-19 Tracker',
Expand Down Expand Up @@ -71,54 +73,52 @@ class HomeScreen extends StatefulWidget {
}

class _HomeScreenState extends State<HomeScreen> {
static const Duration SPLASH_DURATION = Duration(seconds: 3);
Widget _nextScreen;
bool _isTimeoutCompleted;
static const int SPLASH_DURATION = 3;
AsyncMemoizer<bool> _asyncMemoizer;
bool _hasSplashFinished = false;
bool _requiresLanguage = false;

@override
void initState() {
super.initState();

_isTimeoutCompleted = false;
_asyncMemoizer = AsyncMemoizer();
print("Showing Splash");
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
Timer(SPLASH_DURATION, () {
if (_nextScreen != null) {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (_) => _nextScreen));
} else {
_isTimeoutCompleted = true;
}
_asyncMemoizer.runOnce(() {
loadLang();
});
checkReachability();
}

loadLang();
@override
void dispose() {
super.dispose();
}

Future<void> loadLang() async {
await Future.delayed(Duration(seconds: SPLASH_DURATION), () {});
final SharedPreferences pref = await SharedPreferences.getInstance();

String language = pref.getString("language");
if (language != null) {
if (language == "en") {
AppLocalizations.of(context).load(Locale("en", "US"));
await AppLocalizations.of(context).load(Locale("en", "US"));
} else if (language == "ta") {
AppLocalizations.of(context).load(Locale("ta", "TA"));
await AppLocalizations.of(context).load(Locale("ta", "TA"));
} else {
AppLocalizations.of(context).load(Locale("si", "LK"));
}
_nextScreen = RootScreen();
if (_isTimeoutCompleted) {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (_) => _nextScreen));
await AppLocalizations.of(context).load(Locale("si", "LK"));
}
setState(() {
_hasSplashFinished = true;
_requiresLanguage = false;
});
} else {
_nextScreen = WelcomeScreen();
if (_isTimeoutCompleted) {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (_) => _nextScreen));
}
setState(() {
_hasSplashFinished = true;
_requiresLanguage = true;
});
}
}

Expand All @@ -136,22 +136,30 @@ class _HomeScreenState extends State<HomeScreen> {
text: 'Please check your WiFi or mobile data connection',
color: Colors.redAccent,
textStyle: TextStyle(color: Colors.white),
duration: Duration(seconds: 3));
duration: Duration(seconds: 10));
}

void checkReachability() async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
} else {
reachabilityFailedFail();
}
Connectivity connectivity = Connectivity();
Stream<ConnectivityResult> connectivityResult =
connectivity.onConnectivityChanged;
connectivityResult.listen((ConnectivityResult data) {
if (data == ConnectivityResult.mobile) {
} else if (data == ConnectivityResult.wifi) {
print("Connected");
} else {
print("Disconnected");
reachabilityFailedFail();
}
});
}

@override
Widget build(BuildContext context) {
checkReachability();
return _createSplashScreen();
return Scaffold(
body: _hasSplashFinished
? _requiresLanguage ? WelcomeScreen() : RootScreen()
: _createSplashScreen(),
);
}
}
78 changes: 39 additions & 39 deletions lib/page/screen/root_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,46 +290,46 @@ class _RootScreenState extends State<RootScreen> {

isIOS
? showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
CupertinoDialogAction(
isDefaultAction: true,
child: Text(affirmativeLabel),
onPressed: () => launchAppStore(IOS_APP_URL),
),
CupertinoDialogAction(
child: Text(negativeLabel),
onPressed: () => Navigator.pop(context),
),
],
);
})
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
CupertinoDialogAction(
isDefaultAction: true,
child: Text(affirmativeLabel),
onPressed: () => launchAppStore(IOS_APP_URL),
),
CupertinoDialogAction(
child: Text(negativeLabel),
onPressed: () => Navigator.pop(context),
),
],
);
})
: showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
FlatButton(
child: Text(
affirmativeLabel.toUpperCase(),
style: TextStyle(color: Colors.blue),
),
onPressed: () => launchAppStore(ANDROID_APP_URL),
),
FlatButton(
child: Text(negativeLabel.toUpperCase()),
onPressed: () => Navigator.pop(context),
),
],
);
},
);
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
FlatButton(
child: Text(
affirmativeLabel.toUpperCase(),
style: TextStyle(color: Colors.blue),
),
onPressed: () => launchAppStore(ANDROID_APP_URL),
),
FlatButton(
child: Text(negativeLabel.toUpperCase()),
onPressed: () => Navigator.pop(context),
),
],
);
},
);
}

void launchAppStore(String url) async {
Expand Down