89 lines
2.8 KiB
Dart
89 lines
2.8 KiB
Dart
import 'package:battery_plus/battery_plus.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:in_app_update/in_app_update.dart';
|
|
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
|
|
import 'package:storage_info/storage_info.dart';
|
|
import 'package:yimaru_app/services/secure_storage_service.dart';
|
|
|
|
import '../app/app.locator.dart';
|
|
|
|
class StatusCheckerService {
|
|
final storage = locator<SecureStorageService>();
|
|
|
|
bool _previousConnection = true;
|
|
|
|
bool get previousConnection => _previousConnection;
|
|
|
|
Future<int> getBatteryLevel() async {
|
|
final battery = Battery();
|
|
final batteryLevel = await battery.batteryLevel;
|
|
return batteryLevel;
|
|
}
|
|
|
|
Future<bool> userAuthenticated() async {
|
|
await checkAndUpdate();
|
|
|
|
if (await storage.getString('authenticated') != null) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Future<bool> checkConnection() async {
|
|
if (await InternetConnection().hasInternetAccess) {
|
|
_previousConnection = true;
|
|
return true;
|
|
} else {
|
|
if (_previousConnection) {
|
|
// showErrorToast('Check your internet connection');
|
|
_previousConnection = false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<int> getAvailableStorage() async {
|
|
try {
|
|
final availableStorage =
|
|
await StorageInfo().getStorageFreeSpace(SpaceUnit.Bytes);
|
|
return availableStorage.toInt(); // Convert GB to bytes
|
|
} catch (e) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
Future<void> checkAndUpdate() async {
|
|
const requiredStorage = 500 * 1024 * 1024;
|
|
|
|
final batteryLevel =
|
|
await getBatteryLevel(); // Implement getBatteryLevel function
|
|
final int storageAvailable =
|
|
await getAvailableStorage(); // Implement getAvailableStorage
|
|
if (batteryLevel < 20 || storageAvailable < requiredStorage) {
|
|
if (batteryLevel < 20 || storageAvailable < requiredStorage) {
|
|
// KewedeConst().showErrorToast(
|
|
// 'Unable to update app, please charge your phone & free up space.');
|
|
} else if (batteryLevel < 20) {
|
|
// KewedeConst()
|
|
// .showErrorToast('Unable to update app, please charge your phone.');
|
|
} else if (storageAvailable < requiredStorage) {
|
|
// KewedeConst()
|
|
// .showErrorToast('Unable to update app, please free up space.');
|
|
}
|
|
// Show user-friendly message explaining why update failed and suggesting solutions (e.g., charge device, free up space)
|
|
return; // Prevent update from starting
|
|
}
|
|
try {
|
|
if (await checkConnection()) {
|
|
await InAppUpdate
|
|
.checkForUpdate(); // Continue update only if sufficient resources available
|
|
}
|
|
|
|
// ... rest of your update logic ...
|
|
} on PlatformException {
|
|
// Handle specific error code for better user experience and potentially different error messages for each issue
|
|
}
|
|
}
|
|
}
|