Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:get/get.dart';
- import 'package:package_info_plus/package_info_plus.dart';
- import 'package:url_launcher/url_launcher.dart';
- import '../core/constants/constants.dart';
- import '../core/extensions/extensions.dart';
- import '../data/local/preference.dart';
- import '../data/models/versioning_model.dart';
- import '../domain/repositories/versioning_repository.dart';
- import '../routes/app_pages.dart';
- class VersionController extends GetxController
- with StateMixin<VersioningModel> {
- final _repository = Get.find<VersioningRepository>();
- final _prefs = Get.find<Preference>();
- VersioningModel versionModel = VersioningModel();
- @override
- void onInit() async {
- super.onInit();
- final hasNewVersion = await checkNewVersion();
- if (!hasNewVersion) {
- change(versionModel, status: RxStatus.success());
- }
- }
- Future<PackageInfo> _initPackageInfo() async {
- final info = await PackageInfo.fromPlatform();
- return info;
- }
- Future<VersioningModel> getVersion() async {
- VersioningModel _versionModel;
- _versionModel = await _repository.getVersion();
- final json = versionModelToJSon(_versionModel);
- await _prefs.setVersion(json);
- return _versionModel;
- }
- Future<bool> checkNewVersion() async {
- versionModel = await getVersion();
- final _packageInfo = await _initPackageInfo();
- if (_packageInfo.version != versionModel.versiAsli) {
- if (!(Get.isDialogOpen ?? true)) {
- newVersionDialog(versionModel.linkDownload!);
- }
- return true;
- }
- return false;
- }
- void newVersionDialog(String linkDownload) {
- final link = Uri.parse(linkDownload);
- Get.dialog(
- SimpleDialog(
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.r)),
- insetPadding: EdgeInsets.all(10.w),
- contentPadding: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0).w,
- children: [
- Text("Pembaruan aplikasi diperlukan").text12(isBold: true),
- 10.verticalSpace,
- Text("Apk tracking perlu di-update ke versi terbaru").text12(),
- Align(
- alignment: Alignment.bottomRight,
- child: TextButton(
- onPressed: () => link.host == "play.google.com"
- ? launchToPlayStore(linkDownload)
- : Get.toNamed(Routes.UPDATE_APK, arguments: linkDownload),
- child: Text("Update".toUpperCase())
- .text12(color: TextColors.blue)),
- )
- ],
- ),
- barrierDismissible: false,
- );
- }
- Future<void> launchToPlayStore(String url) async {
- Uri toLaunch = Uri.parse(url);
- if (await canLaunchUrl(toLaunch)) {
- await launchUrl(toLaunch, mode: LaunchMode.externalNonBrowserApplication);
- } else {
- debugPrint("Can't launch url");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement