Skip to content

Commit

Permalink
switch playstore and appStore checking (bug) + update exemple
Browse files Browse the repository at this point in the history
  • Loading branch information
X-SLAYER committed Dec 2, 2021
1 parent 2787a80 commit 3458c15
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
15 changes: 13 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:developer';

import 'package:app_version_checker/app_version_checker.dart';
import 'package:flutter/material.dart';

void main() {
Expand All @@ -12,18 +15,26 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
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
Widget build(BuildContext context) {
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'),
Expand Down
49 changes: 31 additions & 18 deletions lib/app_version_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppCheckerResult> 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<AppCheckerResult> _checkPlayStore(
Future<AppCheckerResult> _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'];
Expand All @@ -59,27 +61,30 @@ class AppVersionChecker {
return AppCheckerResult(
currentVersion,
newVersion,
url ?? "",
url,
errorMsg,
);
}

Future<AppCheckerResult> _checkAppleStore(
Future<AppCheckerResult> _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 class="htlgb"><div class="IQ1z0d"><span class="htlgb">(.*?)<\/span>')
.firstMatch(response.body)!
.group(1);
url = uri.toString();
}
} catch (e) {
log("$e");
Expand All @@ -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(
Expand All @@ -107,6 +119,7 @@ class AppCheckerResult {
this.errorMessage,
);

/// return `true` if update is available
bool get canUpdate => currentVersion != (newVersion ?? currentVersion);

@override
Expand Down

0 comments on commit 3458c15

Please sign in to comment.