Hibok
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

118 righe
3.9 KiB

  1. import 'dart:io';
  2. import 'package:app_installer/app_installer.dart';
  3. import 'package:chat/data/constants.dart';
  4. import 'package:chat/generated/i18n.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:oktoast/oktoast.dart';
  7. import 'package:path_provider/path_provider.dart';
  8. import 'package:url_launcher/url_launcher.dart';
  9. import 'CustomUI.dart';
  10. import 'HttpUtil.dart';
  11. class VersionUtils {
  12. static bool hasShowUpdate=false;
  13. static void versionUpdate(BuildContext context,{bool fromProfileNeedShow=false}) async {
  14. if(hasShowUpdate && !fromProfileNeedShow){
  15. return;
  16. }
  17. hasShowUpdate=true;
  18. Directory tempDir ;
  19. if(Platform.isAndroid){
  20. tempDir = await getExternalStorageDirectory();
  21. }
  22. final ValueNotifier<String> updateProgress = ValueNotifier<String>('-1');
  23. HttpUtil().appVersionUpdate((bool needNotice, bool forceUpdate,
  24. String versionName, String describe, String downloadUrl,String appId) {
  25. List<String> online = versionName.split('.');
  26. List<String> current = Constants.versionName.split('.');
  27. bool isSandbox =false;
  28. bool needUpdate = false;
  29. if(int.parse(current[0]) > int.parse(online[0])){
  30. isSandbox=true;
  31. }
  32. if(int.parse(current[0]) == int.parse(online[0]) && int.parse(current[1]) > int.parse(online[1])){
  33. isSandbox=true;
  34. }
  35. if(int.parse(current[0]) == int.parse(online[0]) && int.parse(current[1]) == int.parse(online[1]) && int.parse(current[2]) > int.parse(online[2])){
  36. isSandbox=true;
  37. }
  38. if(int.parse(online[0]) > int.parse(current[0])){
  39. needUpdate=true;
  40. }
  41. if(int.parse(current[0]) == int.parse(online[0]) && int.parse(online[1]) > int.parse(current[1])){
  42. needUpdate=true;
  43. }
  44. if(int.parse(current[0]) == int.parse(online[0]) && int.parse(current[1]) == int.parse(online[1]) && int.parse(online[2]) > int.parse(current[2])){
  45. needUpdate=true;
  46. }
  47. print('isSandbox $isSandbox');
  48. Constants.isSandbox = isSandbox;
  49. // needUpdate=true;///test
  50. // needNotice=true;///test
  51. String filePath ;
  52. if(Platform.isAndroid){
  53. filePath = '${tempDir.path}/$versionName.apk';
  54. }
  55. print(
  56. '当前版本号:${Constants.versionName} --新包版本号 $versionName needNotice:$needNotice forceUpdate"$forceUpdate describe:$describe');
  57. if (needUpdate) {
  58. if (needNotice) {
  59. ///需要弹窗提示更新
  60. CustomUI.buildVersionUpdate(
  61. context, versionName, forceUpdate, describe, updateProgress, () {
  62. if (Constants.isStoreVersion) {
  63. String androidAppId = Constants.packageName;
  64. String iOSAppId = appId;
  65. AppInstaller.goStore(androidAppId, iOSAppId);
  66. } else {
  67. if (Platform.isAndroid) {
  68. HttpUtil().downloadApk(context, filePath, downloadUrl,
  69. (int count, int total) {
  70. ///下载进度更新
  71. double percent = (count / total) * 100;
  72. print('count:$count total:$total percent:$percent');
  73. updateProgress.value = percent.toStringAsFixed(0);
  74. if (count == total) {
  75. ///下载完成-安装
  76. AppInstaller.installApk(filePath);
  77. Future.delayed(Duration(seconds: 3), () {
  78. updateProgress.value = '-1';
  79. ///重置状态
  80. });
  81. }
  82. });
  83. } else {
  84. launch(downloadUrl);
  85. }
  86. }
  87. });
  88. }
  89. } else {
  90. if(fromProfileNeedShow){
  91. showToast(I18n.of(context).already_new);
  92. }
  93. }
  94. });
  95. }
  96. }