Skip to content

Commit

Permalink
Merge pull request #32 from kauemurakami/fix-5.0.0
Browse files Browse the repository at this point in the history
fix bugs with normal versions
  • Loading branch information
kauemurakami committed Jun 18, 2024
2 parents ba20348 + 1e6bee5 commit 6283fa6
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 48 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 5.0.2
* Closing widgets mandatory when back user update app in store
* Add support to x.xx.x+y

## 5.0.1
* Fix bug with normal versions

## 5.0.0
* Upgrade `package_info_plus` to `^8.0.0`
* Set appropriate `sdk` constraints to `>=3.0.0 <4.0.0`
Expand Down
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class _MyHomePageState extends State<MyHomePage> {

void _verifyVersion() async {
await AppVersionUpdate.checkForUpdates(
appleId: '284882215',
playStoreId: 'com.facebook.katana')
.then((result) async {
appleId: '284882215',
playStoreId: 'com.zhiliaoapp.musically',
).then((result) async {
if (result.canUpdate!) {
// await AppVersionUpdate.showBottomSheetUpdate(context: context, appVersionResult: appVersionResult)
// await AppVersionUpdate.showPageUpdate(context: context, appVersionResult: appVersionResult)
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "4.1.0"
version: "5.0.1"
async:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.

# version: 50.0.0
version: 1.0.0

environment:
Expand Down
2 changes: 2 additions & 0 deletions lib/app_version_update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class AppVersionUpdate {
await fetchVersion(playStoreId: playStoreId, appleId: appleId);
return AppVersionResult(
appleId: appleId,
playStoreId: playStoreId,
canUpdate: data.canUpdate,
storeUrl: data.storeUrl,
storeVersion: data.storeVersion,
Expand Down
66 changes: 30 additions & 36 deletions lib/core/functions/fetch_version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import 'convert_version.dart';
/// * ```playStoreId``` unique identifier in Play Store, if null, we will use your package name.
/// * ```country```, region of store, if null, we will use 'us'.
Future<AppVersionData> fetchVersion(
{String? playStoreId, String? appleId, String? country}) async {
{String? playStoreId, String? appleId}) async {
final packageInfo = await PackageInfo.fromPlatform();
AppVersionData data = AppVersionData();
if (Platform.isAndroid) {
data = await fetchAndroid(
packageInfo: packageInfo, playStoreId: playStoreId, country: country);
data =
await fetchAndroid(packageInfo: packageInfo, playStoreId: playStoreId);
} else if (Platform.isIOS) {
data = await fetchIOS(
packageInfo: packageInfo, appleId: appleId, country: country);
data = await fetchIOS(packageInfo: packageInfo, appleId: appleId);
} else {
throw "Unkown platform";
}
Expand All @@ -31,51 +30,46 @@ Future<AppVersionData> fetchVersion(
}
Future<AppVersionData> fetchAndroid(
{PackageInfo? packageInfo, String? playStoreId, String? country}) async {
{PackageInfo? packageInfo, String? playStoreId}) async {
playStoreId = playStoreId ?? packageInfo?.packageName;
final parameters = {"id": playStoreId, "hl": country};
final parameters = {
"id": playStoreId,
};
var uri = Uri.https(playStoreAuthority, playStoreUndecodedPath, parameters);
final response =
await http.get(uri, headers: headers).catchError((e) => throw e);
if (response.statusCode == 200) {
final String htmlString = response.body;
RegExp regex;
Iterable<RegExpMatch> matches;
// Use regex to find all occurrences of version in the format "]]],"<version-number>"
final RegExp regex = RegExp(r'"\]\]\],"(.*?)"');
final Iterable<RegExpMatch> matches = regex.allMatches(htmlString);
if (packageInfo!.version.split('.').length == 3) {
regex = RegExp(r'"\]\]\],null,null,null,\[\[\["(.*?)"\]\]\]');
matches = regex.allMatches(htmlString);
} else {
regex = RegExp(r'"\]\]\],"(.*?)"');
matches = regex.allMatches(htmlString);
}
// Extract the last version found
if (matches.isNotEmpty) {
final lastMatch = matches.last;
final lastVersion = lastMatch.group(1);
print('Última versão encontrada: $lastVersion');
if (matches != null) {
return AppVersionData(
storeVersion: lastVersion,
storeUrl: uri.toString(),
localVersion: packageInfo?.version,
targetPlatform: TargetPlatform.android,
);
} else {
throw " Aplication not found in Play Store, verify your app id. ";
}
String? lastVersion = lastMatch.group(1);
lastVersion = lastVersion!.split('\"').first;
print(
'Versão local ${packageInfo.version} Última versão encontrada: $lastVersion');
return AppVersionData(
// canUpdate: packageInfo.version < lastVersion ? true : false,
storeVersion: lastVersion,
storeUrl: uri.toString(),
localVersion: packageInfo.version,
targetPlatform: TargetPlatform.android,
);
} else {
throw " Aplication not found in Play Store, verify your app id. ";
throw "Aplication not found in Play Store, verify your app id. ";
}
// final versionMatch =
// RegExp(r',\[\[\["([0-9,\.]*)"]],').firstMatch(response.body);
// if (versionMatch != null) {
// return AppVersionData(
// storeVersion: versionMatch.group(1),
// storeUrl: uri.toString(),
// localVersion: packageInfo?.version,
// targetPlatform: TargetPlatform.android,
// );
// } else {
// throw " Aplication not found in Play Store, verify your app id. ";
// }
} else {
throw " Aplication not found in Play Store, verify your app id. ";
}
Expand Down
10 changes: 8 additions & 2 deletions lib/data/models/app_version_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import 'package:flutter/cupertino.dart';
/// * ```platform``` [TargetPlatform] for determine use in android or iOS
class AppVersionResult {
AppVersionResult(
{this.storeVersion, this.storeUrl, this.platform, this.canUpdate});
String? storeVersion, storeUrl;
{this.storeVersion,
this.storeUrl,
this.platform,
this.canUpdate,
this.appleId,
this.playStoreId});
String? storeVersion, storeUrl, appleId, playStoreId;
bool? canUpdate = false;
TargetPlatform? platform;
then(Null Function(dynamic data) param0) {}
Expand Down
30 changes: 25 additions & 5 deletions lib/widgets/alert_dialog_update.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:io';

import 'package:app_version_update/app_version_update.dart';
import 'package:app_version_update/data/models/app_version_result.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
Expand All @@ -7,7 +10,7 @@ import 'package:url_launcher/url_launcher.dart';
class UpdateVersionDialog extends Container {
String? title;
String? content;
bool? mandatory;
bool mandatory, updated;
String? updateButtonText;
String? cancelButtonText;
ButtonStyle? updateButtonStyle;
Expand All @@ -22,7 +25,8 @@ class UpdateVersionDialog extends Container {
UpdateVersionDialog(
{this.title,
this.content,
this.mandatory,
this.mandatory = false,
this.updated = false,
this.updateButtonText,
this.cancelButtonText,
this.updateButtonStyle,
Expand Down Expand Up @@ -56,7 +60,7 @@ class UpdateVersionDialog extends Container {
fontWeight: FontWeight.w400),
),
actions: [
mandatory!
mandatory && !updated
? const SizedBox.shrink()
: TextButton(
style: cancelButtonStyle,
Expand All @@ -67,8 +71,24 @@ class UpdateVersionDialog extends Container {
)),
TextButton(
style: updateButtonStyle,
onPressed: () => launchUrl(Uri.parse(appVersionResult!.storeUrl!),
mode: LaunchMode.externalApplication),
onPressed: () async {
await launchUrl(
Uri.parse(
appVersionResult!.storeUrl!,
),
mode: LaunchMode.externalApplication,
);
if (mandatory && !updated) {
await AppVersionUpdate.checkForUpdates(
appleId: appVersionResult!.appleId,
playStoreId: appVersionResult!.playStoreId,
).then((checkIfUpdated) {
if (!checkIfUpdated.canUpdate!) {
updated = true;
}
});
}
},
child: Text(
updateButtonText!,
style: updateTextStyle,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: app_version_update
description: An easy and quick way to check if the local app is updated with the same version in their respective stores (Play Store / Apple Store ).
# author: Kauê Tomaz Murakami<[email protected]>
version: 5.0.0
version: 5.0.1
homepage: https://github.com/kauemurakami/app_version_update

environment:
Expand Down

0 comments on commit 6283fa6

Please sign in to comment.