From 07f490fbef0baa2bf248b8a65fa50de0500dd0fc Mon Sep 17 00:00:00 2001 From: Erfan Rahmati Date: Wed, 14 Sep 2022 08:51:36 +0430 Subject: [PATCH] (add) awareness of other versions --- .gitignore | 2 - lib/constants/app.dart | 5 +++ lib/constants/types.dart | 4 +- lib/modules/api/api_requester.dart | 2 +- lib/modules/api/key_getter.dart | 20 ++++++++-- lib/pages/shared/about_page/about_page.dart | 3 +- lib/pages/splash/splash_screen.dart | 27 +++++++++++++- lib/widgets/error.dart | 41 ++++++++++++--------- 8 files changed, 76 insertions(+), 28 deletions(-) create mode 100644 lib/constants/app.dart diff --git a/.gitignore b/.gitignore index 4e01539..359070e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -lib/modules/api/key_getter.dart - .VSCodeCounter/ # Miscellaneous diff --git a/lib/constants/app.dart b/lib/constants/app.dart new file mode 100644 index 0000000..9718efb --- /dev/null +++ b/lib/constants/app.dart @@ -0,0 +1,5 @@ +const String appVersion = "v3.0.1"; + +String? latestVersion; +List? supportedVersions; +String? secureUrl; diff --git a/lib/constants/types.dart b/lib/constants/types.dart index f1acd12..6c2694b 100644 --- a/lib/constants/types.dart +++ b/lib/constants/types.dart @@ -9,7 +9,9 @@ enum RequestResult { SUCCESS, FAILURE, FAILURE_USER_PROBLEM, - FAILURE_SERVER_PROBLEM + FAILURE_SERVER_PROBLEM, + FAILURE_IMDB_PROBLEM, + FAILURE_VERSION_PROBLEM, } enum ListName { diff --git a/lib/modules/api/api_requester.dart b/lib/modules/api/api_requester.dart index 5a72f5b..fe6e093 100644 --- a/lib/modules/api/api_requester.dart +++ b/lib/modules/api/api_requester.dart @@ -276,7 +276,7 @@ class APIRequester { (apiKeys.length == 1 && apiKeys[0] == "XXXXXXXXXX")) { var response; await key_getter().then((result) async { - if (!result) { + if (result == RequestResult.FAILURE) { if (kDebugMode) { print( "You haven't add any api key to the app, so it won't work!\nFor more information check out the documentation at https://github.com/ErfanRht/MovieLab#getting-started"); diff --git a/lib/modules/api/key_getter.dart b/lib/modules/api/key_getter.dart index cc5fba1..dfef0cd 100644 --- a/lib/modules/api/key_getter.dart +++ b/lib/modules/api/key_getter.dart @@ -3,17 +3,29 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import 'package:movielab/.api.dart'; +import 'package:movielab/constants/app.dart'; +import 'package:movielab/constants/types.dart'; -Future key_getter() async { +Future key_getter() async { const String url = ""; if (url != "") { var response = await http.get(Uri.parse(url)).timeout( const Duration(seconds: 10), ); if (kDebugMode) { - print(jsonDecode(response.body).length); + print( + "${jsonDecode(response.body)['keys'].length} api keys has been added."); } - apiKeys = [for (dynamic key in jsonDecode(response.body)) key.toString()]; + apiKeys = [ + for (dynamic key in jsonDecode(response.body)['keys']) key.toString() + ]; + supportedVersions = [ + for (dynamic version in jsonDecode(response.body)['supported_versions']) + version.toString() + ]; + latestVersion = jsonDecode(response.body)['latest_version'].toString(); + secureUrl = jsonDecode(response.body)['secure_url'].toString(); + return RequestResult.SUCCESS; } - return true; + return RequestResult.FAILURE; } diff --git a/lib/pages/shared/about_page/about_page.dart b/lib/pages/shared/about_page/about_page.dart index 1e6f24c..14776b5 100644 --- a/lib/pages/shared/about_page/about_page.dart +++ b/lib/pages/shared/about_page/about_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:movielab/constants/app.dart'; import 'package:movielab/constants/colors.dart'; import 'package:movielab/pages/shared/app_name.dart'; import 'package:movielab/widgets/buttons/social_media_button.dart'; @@ -37,7 +38,7 @@ class AboutPage extends StatelessWidget { height: 10, ), Text( - "Version 2.0.7", + "Version ${appVersion.replaceAll('v', '')}", style: TextStyle( fontSize: 17.5, color: Colors.white.withOpacity(0.5), diff --git a/lib/pages/splash/splash_screen.dart b/lib/pages/splash/splash_screen.dart index 3ee7103..c288ec0 100644 --- a/lib/pages/splash/splash_screen.dart +++ b/lib/pages/splash/splash_screen.dart @@ -1,9 +1,11 @@ import 'package:flutter/material.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; +import 'package:movielab/constants/app.dart'; import 'package:movielab/modules/Recommender/Recommender.dart'; import 'package:movielab/modules/tools/navigate.dart'; import 'package:movielab/pages/main/main_page.dart'; import 'package:movielab/widgets/error.dart'; +import 'package:url_launcher/url_launcher.dart'; import '../../constants/colors.dart'; import '../../constants/types.dart'; import '../../modules/tools/system_ui_overlay_style.dart'; @@ -27,6 +29,10 @@ class _SplashScreenState extends State { _loadData(); } + void _launchUrl(final String url) async { + if (!await launchUrl(Uri.parse(url))) throw 'Could not launch $url'; + } + @override Widget build(BuildContext context) { return Scaffold( @@ -67,6 +73,15 @@ class _SplashScreenState extends State { borderWidth: 10, ), ); + case RequestResult.FAILURE_VERSION_PROBLEM: + return ConnectionErrorWidget( + isItTryAgain: false, + tryAgain: () { + _launchUrl("https://erfanrht.github.io/MovieLab-Intro"); + }, + errorText: + 'The version of MovieLab that you are using is outdated.\nFor more information check out:\n${secureUrl ?? "erfanrht.github.io/movielab-intro"}', + ); default: return ConnectionErrorWidget( tryAgain: () { @@ -87,7 +102,17 @@ class _SplashScreenState extends State { if (result == RequestResult.SUCCESS) { recommender(); getUserData(); - Navigate.replaceTo(context, const MainPage()); + if (supportedVersions != null && supportedVersions!.isNotEmpty) { + if (supportedVersions!.contains(appVersion)) { + Navigate.replaceTo(context, const MainPage()); + } else { + setState(() { + _loadingStatus = RequestResult.FAILURE_VERSION_PROBLEM; + }); + } + } else { + Navigate.replaceTo(context, const MainPage()); + } } else { setState(() { _loadingStatus = result; diff --git a/lib/widgets/error.dart b/lib/widgets/error.dart index 87ccb39..54f5468 100644 --- a/lib/widgets/error.dart +++ b/lib/widgets/error.dart @@ -1,14 +1,15 @@ import 'package:flutter/material.dart'; -import 'package:google_fonts/google_fonts.dart'; import 'package:movielab/constants/colors.dart'; class ConnectionErrorWidget extends StatelessWidget { final String errorText; - final VoidCallback tryAgain; + final bool isItTryAgain; + final VoidCallback? tryAgain; const ConnectionErrorWidget( {this.errorText = 'An unexpected error occurred while loading data.', required this.tryAgain, - Key? key}) + Key? key, + this.isItTryAgain = true}) : super(key: key); @override @@ -25,6 +26,7 @@ class ConnectionErrorWidget extends StatelessWidget { onTap: tryAgain, borderRadius: BorderRadius.circular(10), child: Container( + // width: MediaQuery.of(context).size.width, padding: const EdgeInsets.all(10.0), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), @@ -34,24 +36,27 @@ class ConnectionErrorWidget extends StatelessWidget { children: [ Text( errorText, - style: TextStyle( + textAlign: TextAlign.center, + softWrap: true, + style: const TextStyle( fontSize: 12.5, fontWeight: FontWeight.w600), ), const SizedBox(height: 5), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'Try again', - style: TextStyle( - fontSize: 12.5, - fontWeight: FontWeight.w600), - ), - const Icon( - Icons.refresh, - size: 15, - ) - ]) + if (isItTryAgain) + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: const [ + Text( + 'Try again', + style: TextStyle( + fontSize: 12.5, + fontWeight: FontWeight.w600), + ), + Icon( + Icons.refresh, + size: 15, + ) + ]) ], ), )),