Skip to content

Commit

Permalink
Bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
venkatbalajim committed Mar 7, 2024
1 parent 64b90b3 commit 339fe9a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
14 changes: 6 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ class MyApp extends StatelessWidget {
}

Future<void> _requestPermissions() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.camera,
Permission.storage,
].request();

if (statuses[Permission.camera] != PermissionStatus.granted ||
statuses[Permission.storage] != PermissionStatus.granted) {

final mobileAndroidInfo = await DeviceInfoPlugin().androidInfo;
int? androidVersion = int.tryParse(mobileAndroidInfo.version.release);
if (androidVersion != null && androidVersion <= 12) {
await [Permission.camera,Permission.storage,].request();
} else {
await [Permission.camera,Permission.manageExternalStorage,].request();
}
}
}
36 changes: 26 additions & 10 deletions lib/utils/download.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ Future<void> downloadQRCode(
ScreenshotController screenshotController,
BuildContext context
) async {

final mobileAndroidInfo = await DeviceInfoPlugin().androidInfo;
int? androidVersion = int.tryParse(mobileAndroidInfo.version.release);
Permission storagePermission = Permission.storage;
if (androidVersion != null && androidVersion > 12) {
storagePermission = Permission.manageExternalStorage;
}

try {
await Future.delayed(const Duration(seconds: 1));
Uint8List? image = await screenshotController.capture();

if (image != null) {
if (await Permission.storage.isGranted) {
if (await storagePermission.isGranted) {
String downloadDir = (await DownloadsPath.downloadsDirectory())!.path;
String appFolderName = "QR Hub";
String downloadFolder = "QR Codes";
Expand All @@ -23,12 +31,12 @@ Future<void> downloadQRCode(
String filePath = "${directory.path}/$fileName.jpg";
await File(filePath).writeAsBytes(image)
.then((value) => SnackBarWidget(context, 'File downloaded successfully.\nFile directory: Download/QR Hub/QR Codes'));
} else if (await Permission.storage.isDenied) {
await Permission.storage.request();
if (await Permission.storage.isGranted) {
} else if (await storagePermission.isDenied) {
await storagePermission.request();
if (await storagePermission.isGranted) {
SnackBarWidget(context, 'Permission granted. Click the download button again.');
}
} else if (await Permission.storage.isPermanentlyDenied) {
} else if (await storagePermission.isPermanentlyDenied) {
SnackBarWidget(context, 'Storage Permission Denied. So, unable to download the history.');
}
} else {
Expand All @@ -41,7 +49,15 @@ Future<void> downloadQRCode(
}

Future<void> downloadHistory(BuildContext context, List<QRCodeData> historyList) async {
if (await Permission.storage.isGranted) {

final mobileAndroidInfo = await DeviceInfoPlugin().androidInfo;
int? androidVersion = int.tryParse(mobileAndroidInfo.version.release);
Permission storagePermission = Permission.storage;
if (androidVersion != null && androidVersion > 12) {
storagePermission = Permission.manageExternalStorage;
}

if (await storagePermission.isGranted) {
String downloadDir = (await DownloadsPath.downloadsDirectory())!.path;
print(downloadDir);
try {
Expand All @@ -66,12 +82,12 @@ Future<void> downloadHistory(BuildContext context, List<QRCodeData> historyList)
print(e.toString());
SnackBarWidget(context, 'Sorry, unable to download history.');
}
} else if (await Permission.storage.isDenied) {
await Permission.storage.request();
if (await Permission.storage.isGranted) {
} else if (await storagePermission.isDenied) {
await storagePermission.request();
if (await storagePermission.isGranted) {
SnackBarWidget(context, 'Permission granted. Click the download button again.');
}
} else if (await Permission.storage.isPermanentlyDenied) {
} else if (await storagePermission.isPermanentlyDenied) {
SnackBarWidget(context, 'Storage Permission Denied. So, unable to download the history.');
}
}
4 changes: 4 additions & 0 deletions lib/utils/imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,7 @@ export 'package:shared_preferences/shared_preferences.dart';
// Lecle Downloads Path Provider Package
import 'package:lecle_downloads_path_provider/lecle_downloads_path_provider.dart';
export 'package:lecle_downloads_path_provider/lecle_downloads_path_provider.dart';

// Device Info Plus Package
import 'package:device_info_plus/device_info_plus.dart';
export 'package:device_info_plus/device_info_plus.dart';
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:
provider: ^6.1.2
shared_preferences: ^2.2.2
lecle_downloads_path_provider: ^0.0.2+8
device_info_plus: ^9.1.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 339fe9a

Please sign in to comment.