Advertisement
antasofa

GetX update controller

Feb 6th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:get/get.dart';
  4. import 'package:package_info_plus/package_info_plus.dart';
  5. import 'package:url_launcher/url_launcher.dart';
  6.  
  7. import '../core/constants/constants.dart';
  8. import '../core/extensions/extensions.dart';
  9. import '../data/local/preference.dart';
  10. import '../data/models/versioning_model.dart';
  11. import '../domain/repositories/versioning_repository.dart';
  12. import '../routes/app_pages.dart';
  13.  
  14. class VersionController extends GetxController
  15. with StateMixin<VersioningModel> {
  16. final _repository = Get.find<VersioningRepository>();
  17. final _prefs = Get.find<Preference>();
  18.  
  19. VersioningModel versionModel = VersioningModel();
  20.  
  21. @override
  22. void onInit() async {
  23. super.onInit();
  24. final hasNewVersion = await checkNewVersion();
  25. if (!hasNewVersion) {
  26. change(versionModel, status: RxStatus.success());
  27. }
  28. }
  29.  
  30. Future<PackageInfo> _initPackageInfo() async {
  31. final info = await PackageInfo.fromPlatform();
  32. return info;
  33. }
  34.  
  35. Future<VersioningModel> getVersion() async {
  36. VersioningModel _versionModel;
  37. _versionModel = await _repository.getVersion();
  38. final json = versionModelToJSon(_versionModel);
  39. await _prefs.setVersion(json);
  40.  
  41. return _versionModel;
  42. }
  43.  
  44. Future<bool> checkNewVersion() async {
  45. versionModel = await getVersion();
  46. final _packageInfo = await _initPackageInfo();
  47.  
  48. if (_packageInfo.version != versionModel.versiAsli) {
  49. if (!(Get.isDialogOpen ?? true)) {
  50. newVersionDialog(versionModel.linkDownload!);
  51. }
  52. return true;
  53. }
  54. return false;
  55. }
  56.  
  57. void newVersionDialog(String linkDownload) {
  58. final link = Uri.parse(linkDownload);
  59.  
  60. Get.dialog(
  61. SimpleDialog(
  62. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.r)),
  63. insetPadding: EdgeInsets.all(10.w),
  64. contentPadding: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0).w,
  65. children: [
  66. Text("Pembaruan aplikasi diperlukan").text12(isBold: true),
  67. 10.verticalSpace,
  68. Text("Apk tracking perlu di-update ke versi terbaru").text12(),
  69. Align(
  70. alignment: Alignment.bottomRight,
  71. child: TextButton(
  72. onPressed: () => link.host == "play.google.com"
  73. ? launchToPlayStore(linkDownload)
  74. : Get.toNamed(Routes.UPDATE_APK, arguments: linkDownload),
  75. child: Text("Update".toUpperCase())
  76. .text12(color: TextColors.blue)),
  77. )
  78. ],
  79. ),
  80. barrierDismissible: false,
  81. );
  82. }
  83.  
  84. Future<void> launchToPlayStore(String url) async {
  85. Uri toLaunch = Uri.parse(url);
  86. if (await canLaunchUrl(toLaunch)) {
  87. await launchUrl(toLaunch, mode: LaunchMode.externalNonBrowserApplication);
  88. } else {
  89. debugPrint("Can't launch url");
  90. }
  91. }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement