From 3458c15a9a1a7ceb84ed07f689dd2053c8d08891 Mon Sep 17 00:00:00 2001 From: X-SLAYER Date: Thu, 2 Dec 2021 14:28:12 +0100 Subject: [PATCH] switch playstore and appStore checking (bug) + update exemple --- example/lib/main.dart | 15 +++++++++-- lib/app_version_checker.dart | 49 +++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index b7fcd3c..973239a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,3 +1,6 @@ +import 'dart:developer'; + +import 'package:app_version_checker/app_version_checker.dart'; import 'package:flutter/material.dart'; void main() { @@ -12,10 +15,18 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { + final _checker = AppVersionChecker(appId: "com.localiragps.app"); + @override void initState() { super.initState(); - // initPlatformState(); + checkVersion(); + } + + void checkVersion() async { + _checker.checkUpdate().then((value) { + log(value.toString()); + }); } @override @@ -23,7 +34,7 @@ class _MyAppState extends State { return MaterialApp( home: Scaffold( appBar: AppBar( - title: const Text('Plugin example app'), + title: const Text('APP Version Checker'), ), body: const Center( child: Text('Current Version'), diff --git a/lib/app_version_checker.dart b/lib/app_version_checker.dart index 83d8947..db68dfc 100644 --- a/lib/app_version_checker.dart +++ b/lib/app_version_checker.dart @@ -14,39 +14,41 @@ class AppVersionChecker { /// if [appId] is null the [appId] will take the Flutter package identifier final String? appId; - AppVersionChecker._({this.currentVersion, this.appId}); + AppVersionChecker({this.currentVersion, this.appId}); - void check() async { + Future checkUpdate() async { PackageInfo packageInfo = await PackageInfo.fromPlatform(); final _currentVersion = currentVersion ?? packageInfo.version; final _packageName = appId ?? packageInfo.packageName; if (Platform.isAndroid) { - _checkPlayStore(_currentVersion, _packageName); + return await _checkPlayStore(_currentVersion, _packageName); } else if (Platform.isIOS) { - _checkAppleStore(_currentVersion, _packageName); + return await _checkAppleStore(_currentVersion, _packageName); } else { log('The target platform "${Platform.operatingSystem}" is not yet supported by this package.'); + return AppCheckerResult(_currentVersion, null, "", + 'The target platform "${Platform.operatingSystem}" is not yet supported by this package.'); } } - Future _checkPlayStore( + Future _checkAppleStore( String currentVersion, String packageName) async { String? errorMsg; String? newVersion; String? url; - final uri = Uri.https( - "play.google.com", "/store/apps/details", {"id": packageName}); + var uri = + Uri.https("itunes.apple.com", "/lookup", {"bundleId": packageName}); try { final response = await http.get(uri); - if (response.statusCode >= 200 && response.statusCode <= 300) { + if (response.statusCode != 200) { errorMsg = - "Can't find an app in the Play Store with the id: $packageName"; + "Can't find an app in the Apple Store with the id: $packageName"; } else { final jsonObj = jsonDecode(response.body); final List results = jsonObj['results']; if (results.isEmpty) { errorMsg = - "Can't find an app in the Play Store with the id: $packageName"; + "Can't find an app in the Apple Store with the id: $packageName"; } else { newVersion = jsonObj['results'][0]['version']; url = jsonObj['results'][0]['trackViewUrl']; @@ -59,27 +61,30 @@ class AppVersionChecker { return AppCheckerResult( currentVersion, newVersion, - url ?? "", + url, errorMsg, ); } - Future _checkAppleStore( + Future _checkPlayStore( String currentVersion, String packageName) async { String? errorMsg; String? newVersion; - var uri = - Uri.https("itunes.apple.com", "/lookup", {"bundleId": packageName}); + String? url; + final uri = Uri.https( + "play.google.com", "/store/apps/details", {"id": packageName}); + try { final response = await http.get(uri); - if (response.statusCode >= 200 && response.statusCode <= 300) { + if (response.statusCode != 200) { errorMsg = - "Can't find an app in the Apple Store with the id: $packageName"; + "Can't find an app in the Google Play Store with the id: $packageName"; } else { newVersion = RegExp( r'Current Version<\/div>
(.*?)<\/span>') .firstMatch(response.body)! .group(1); + url = uri.toString(); } } catch (e) { log("$e"); @@ -88,16 +93,23 @@ class AppVersionChecker { return AppCheckerResult( currentVersion, newVersion, - uri.toString(), + url, errorMsg, ); } } class AppCheckerResult { + /// return current app version final String currentVersion; + + /// return the new app version final String? newVersion; - final String appURL; + + /// return the app url + final String? appURL; + + /// return error message if found else it will return `null` final String? errorMessage; AppCheckerResult( @@ -107,6 +119,7 @@ class AppCheckerResult { this.errorMessage, ); + /// return `true` if update is available bool get canUpdate => currentVersion != (newVersion ?? currentVersion); @override