passo_mobile_app/lib/screens/unit2/login/components/update_required.dart

254 lines
9.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:io';
2023-01-23 08:43:02 +00:00
2023-09-25 03:38:00 +00:00
import 'package:better_open_file/better_open_file.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:dio/dio.dart';
import 'package:easy_app_installer/easy_app_installer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_svg/svg.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
2023-09-25 03:38:00 +00:00
import 'package:unit2/model/login_data/version_info.dart';
import 'package:unit2/screens/unit2/login/components/showAlert.dart';
import 'package:unit2/theme-data.dart/btn-style.dart';
import '../../../../bloc/user/user_bloc.dart';
import '../../../../theme-data.dart/colors.dart';
import '../../../../utils/cpu_architecture.dart';
import '../../../../utils/text_container.dart';
import '../../../../widgets/wave.dart';
class Update extends StatefulWidget {
final String apkVersion;
2023-09-25 03:38:00 +00:00
final VersionInfo versionInfo;
const Update(
2023-09-25 03:38:00 +00:00
{super.key, required this.apkVersion, required this.versionInfo});
@override
State<Update> createState() => _UpdateState();
}
class _UpdateState extends State<Update> {
String progressRating = '0';
bool downloading = false;
bool isDownloaded = false;
bool asyncCall = false;
Dio dio = Dio();
@override
Widget build(BuildContext context) {
return SafeArea(
child: Stack(children: [
BlocBuilder<UserBloc, UserState>(
builder: (context, state) {
if (state is VersionLoaded) {
return ProgressHUD(
child: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 40, vertical: 25),
height: MediaQuery.of(context).size.height,
alignment: Alignment.center,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SvgPicture.asset(
'assets/svgs/download.svg',
height: 200.0,
width: 200.0,
allowDrawingOutsideViewBox: true,
),
const SizedBox(
height: 5,
),
Text("UPDATE REQUIRED!",
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.displaySmall!
.copyWith(color: primary, fontSize: 28)),
],
),
const SizedBox(
2023-09-25 03:38:00 +00:00
height: 25,
),
RichText(
textAlign: TextAlign.justify,
text: TextSpan(
text: 'Your app version ',
style: const TextStyle(
2023-09-25 03:38:00 +00:00
fontSize: 14,
color: Colors.black,
),
children: <TextSpan>[
TextSpan(
text: widget.apkVersion,
style: const TextStyle(
color: primary,
fontWeight: FontWeight.bold)),
const TextSpan(
text:
" did not match with the latest version ",
style: TextStyle(color: Colors.black)),
TextSpan(
2023-09-25 03:38:00 +00:00
text: widget.versionInfo.versionInfo,
style: const TextStyle(
color: primary,
fontWeight: FontWeight.bold)),
const TextSpan(
text:
". Download the app latest version to procceed using the uniT-App.",
style: TextStyle(color: Colors.black)),
])),
const SizedBox(
2023-09-25 03:38:00 +00:00
height: 25,
),
Container(
child: downloading
? FittedBox(
child: Text(
'Downloading application $progressRating%',
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headlineSmall!
.copyWith(fontWeight: FontWeight.bold),
),
)
: Container(),
),
SizedBox(
height: 60,
width: MediaQuery.of(context).size.width,
child: downloading
? Container()
: ElevatedButton.icon(
icon: const Icon(Icons.download),
style: mainBtnStyle(
primary, Colors.transparent, second),
2023-09-25 03:38:00 +00:00
onPressed: () async {
setState(() {
downloading = true;
progressRating = '0';
});
try {
await openFile();
} catch (e) {
showAlert(context, () {
setState(() {
downloading = false;
progressRating = '0';
});
});
}
},
label: const Text(
"Download Latest App Version.")),
),
],
),
),
),
);
}
if (state is UserError) {
showAlert(context, () {
setState(() {
downloading = false;
});
});
}
return Container();
},
),
const Positioned(bottom: 0, child: WaveReverse(height: 80))
]),
);
}
Future<void> openFile() async {
try {
final filePath = await downloadFile();
2023-09-25 01:43:03 +00:00
await OpenFile.open(filePath);
} catch (e) {
2023-09-25 01:43:03 +00:00
throw e.toString();
}
}
Future<void> openAPK(String path) async {
PermissionStatus result = await Permission.storage.request();
if (result.isGranted) {
await EasyAppInstaller.instance.installApk(path);
}
}
2023-09-25 03:38:00 +00:00
Future<String> getCPUArchitecture() async {
String downloadURL = "";
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
AndroidDeviceInfo androidDeviceInfo = await deviceInfoPlugin.androidInfo;
try{
List<String> cpuArchitecture = androidDeviceInfo.supportedAbis;
if (cpuArchitecture.first == 'arm64-v8a') {
downloadURL = widget.versionInfo.arm64v8aDownloadUrl!;
}
else if (cpuArchitecture.first == 'armeabi-v7a') {
downloadURL = widget.versionInfo.armeabiv7aDownloadUrl!;
} else {
downloadURL = widget.versionInfo.x8664DownloadUrl!;
}
print(downloadURL);
}catch(e){
throw e.toString();
}
return downloadURL;
}
Future<String?> downloadFile() async {
final appStorage = await getApplicationDocumentsDirectory();
2023-09-25 03:38:00 +00:00
String? appDir;
try {
String url = await getCPUArchitecture();
final response = await dio.download(url, '${appStorage.path}/uniT.apk',
deleteOnError: true,
options: Options(
receiveDataWhenStatusError: true,
responseType: ResponseType.bytes,
followRedirects: false,
validateStatus: (status) {
return status! < 500;
}), onReceiveProgress: (recv, total) {
setState(() {
progressRating = ((recv / total) * 100).toStringAsFixed(0);
downloading = true;
});
if (progressRating == '100') {
setState(() {
downloading = false;
isDownloaded = true;
});
}
2023-09-25 03:38:00 +00:00
appDir = '${appStorage.path}/uniT.apk';
});
2023-09-25 03:38:00 +00:00
} on TimeoutException catch (e) {
throw e.toString();
} on SocketException catch (e) {
throw e.toString();
} on Error catch (e) {
throw e.toString();
} catch (e) {
throw e.toString();
}
2023-09-25 03:38:00 +00:00
return appDir!;
}
2023-09-25 03:38:00 +00:00
}