74 lines
2.3 KiB
Dart
74 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:upgrader/upgrader.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../utils/translations.dart';
|
|
import '../widgets/custom_upgrade_alert.dart';
|
|
|
|
// GitHub Configuration
|
|
const String _githubRepoOwner = 'Bl3kiiie';
|
|
const String _githubRepoName = 'shooting_app';
|
|
const String _githubToken =
|
|
'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'; // Replace with your GitHub Personal Access Token
|
|
|
|
class UpdateChecker {
|
|
static final UpdateChecker _instance = UpdateChecker._internal();
|
|
|
|
factory UpdateChecker() {
|
|
return _instance;
|
|
}
|
|
|
|
UpdateChecker._internal();
|
|
|
|
Future<void> checkForUpdate(BuildContext context, String language) async {
|
|
// Check if we've already checked today
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final lastCheckTime = prefs.getInt('last_update_check_time') ?? 0;
|
|
final now = DateTime.now().millisecondsSinceEpoch;
|
|
final oneDayInMilliseconds = 24 * 60 * 60 * 1000;
|
|
|
|
// Only check once per day
|
|
if (now - lastCheckTime < oneDayInMilliseconds) {
|
|
return;
|
|
}
|
|
|
|
// Update the last check time
|
|
await prefs.setInt('last_update_check_time', now);
|
|
|
|
final upgrader = Upgrader();
|
|
|
|
try {
|
|
// Check if update is available
|
|
final isUpdateAvailable = await upgrader.isUpdateAvailable();
|
|
|
|
if (isUpdateAvailable && context.mounted) {
|
|
_showCustomUpdateDialog(context, language);
|
|
}
|
|
} catch (e) {
|
|
// Silently fail if update check fails
|
|
print('Update check failed: $e');
|
|
}
|
|
}
|
|
|
|
void _showCustomUpdateDialog(BuildContext context, String language) {
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return CustomUpdateDialog(
|
|
title: AppTranslations.translate('Update Available', language),
|
|
message: AppTranslations.translate(
|
|
'A new version of the app is available', language),
|
|
updateButtonText: AppTranslations.translate('Update', language),
|
|
laterButtonText: AppTranslations.translate('Later', language),
|
|
onUpdate: () {
|
|
Upgrader().sendUserToAppStore();
|
|
},
|
|
onLater: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|