small fix, don't worry about it

This commit is contained in:
Tomas Dvorak
2026-04-10 12:05:40 +02:00
parent 7b7ed0083f
commit 5ab2773f98
55 changed files with 3240 additions and 483 deletions
@@ -0,0 +1,275 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';
import 'package:shared_preferences/shared_preferences.dart';
enum BiometricAvailability {
available,
notAvailable,
notEnrolled,
lockedOut,
permanentlyUnavailable;
String get message {
switch (this) {
case BiometricAvailability.available:
return 'Biometric authentication is available';
case BiometricAvailability.notAvailable:
return 'Biometric authentication is not available on this device';
case BiometricAvailability.notEnrolled:
return 'No biometric credentials enrolled on this device';
case BiometricAvailability.lockedOut:
return 'Biometric authentication is temporarily locked out';
case BiometricAvailability.permanentlyUnavailable:
return 'Biometric authentication is permanently unavailable';
}
}
}
class BiometricService {
static const String _biometricEnabledKey = 'biometric_enabled';
static const String _biometricUserIdKey = 'biometric_user_id';
final LocalAuthentication _localAuth = LocalAuthentication();
/// Get display name for biometric type
String getBiometricDisplayName(BiometricType type) {
switch (type) {
case BiometricType.fingerprint:
return 'Fingerprint';
case BiometricType.face:
return 'Face ID';
case BiometricType.iris:
return 'Iris Scanner';
default:
return 'Biometric';
}
}
/// Get emoji for biometric type
String getBiometricEmoji(BiometricType type) {
switch (type) {
case BiometricType.fingerprint:
return '👆';
case BiometricType.face:
return '👤';
case BiometricType.iris:
return '👁️';
default:
return '🔒';
}
}
/// Check if biometric authentication is available
Future<BiometricAvailability> checkAvailability() async {
try {
// Check if device supports biometric authentication
final canCheckBiometrics = await _localAuth.canCheckBiometrics;
if (!canCheckBiometrics) {
return BiometricAvailability.notAvailable;
}
// Check if biometric credentials are enrolled
final isDeviceSupported = await _localAuth.isDeviceSupported();
if (!isDeviceSupported) {
return BiometricAvailability.notAvailable;
}
// Try to get available biometric types
final availableBiometrics = await _localAuth.getAvailableBiometrics();
if (availableBiometrics.isEmpty) {
return BiometricAvailability.notEnrolled;
}
return BiometricAvailability.available;
} on PlatformException catch (e) {
if (e.code == 'LockedOut') {
return BiometricAvailability.lockedOut;
} else if (e.code == 'PermanentlyEnrolled') {
return BiometricAvailability.permanentlyUnavailable;
} else if (e.code == 'NotAvailable') {
return BiometricAvailability.notAvailable;
} else if (e.code == 'NotEnrolled') {
return BiometricAvailability.notEnrolled;
}
return BiometricAvailability.notAvailable;
} catch (e) {
return BiometricAvailability.notAvailable;
}
}
/// Get available biometric types
Future<List<BiometricType>> getAvailableBiometrics() async {
try {
final availableBiometrics = await _localAuth.getAvailableBiometrics();
return availableBiometrics.toList();
} catch (e) {
return [];
}
}
/// Check if biometric login is enabled for a user
Future<bool> isBiometricEnabled() async {
try {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_biometricEnabledKey) ?? false;
} catch (e) {
return false;
}
}
/// Enable biometric login for a user
Future<bool> enableBiometric(String userId) async {
try {
// First verify biometric is available
final availability = await checkAvailability();
if (availability != BiometricAvailability.available) {
return false;
}
// Test biometric authentication
final authenticated = await authenticate(
reason: 'Enable biometric login for faster access',
localizedReason: 'Enable biometric login for faster access to your 1356 day challenge',
);
if (authenticated) {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_biometricEnabledKey, true);
await prefs.setString(_biometricUserIdKey, userId);
return true;
}
return false;
} catch (e) {
return false;
}
}
/// Disable biometric login
Future<bool> disableBiometric() async {
try {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_biometricEnabledKey);
await prefs.remove(_biometricUserIdKey);
return true;
} catch (e) {
return false;
}
}
/// Get the user ID associated with biometric login
Future<String?> getBiometricUserId() async {
try {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_biometricUserIdKey);
} catch (e) {
return null;
}
}
/// Authenticate with biometrics
Future<bool> authenticate({
String reason = 'Authenticate to access your account',
String? localizedReason,
bool useErrorDialogs = true,
bool stickyAuth = false,
bool biometricOnly = true,
}) async {
try {
final authenticated = await _localAuth.authenticate(
localizedReason: localizedReason ?? reason,
options: AuthenticationOptions(
useErrorDialogs: useErrorDialogs,
stickyAuth: stickyAuth,
biometricOnly: biometricOnly,
),
);
return authenticated;
} on PlatformException catch (e) {
// Handle common biometric errors
if (e.code == 'LockedOut') {
// User tried too many times
return false;
} else if (e.code == 'NotAvailable') {
// Biometric not available
return false;
} else if (e.code == 'NotEnrolled') {
// No biometric enrolled
return false;
} else if (e.code == 'OtherOperatingSystem') {
// Not supported on this platform
return false;
} else if (e.code == 'UserFallback') {
// User chose to use password instead
return false;
}
return false;
} catch (e) {
return false;
}
}
/// Get the primary biometric type for display
Future<BiometricType?> getPrimaryBiometricType() async {
try {
final availableBiometrics = await getAvailableBiometrics();
if (availableBiometrics.contains(BiometricType.face)) {
return BiometricType.face;
} else if (availableBiometrics.contains(BiometricType.fingerprint)) {
return BiometricType.fingerprint;
} else if (availableBiometrics.contains(BiometricType.iris)) {
return BiometricType.iris;
}
return null;
} catch (e) {
return null;
}
}
/// Get user-friendly biometric status message
Future<String> getBiometricStatusMessage() async {
final availability = await checkAvailability();
final isEnabled = await isBiometricEnabled();
switch (availability) {
case BiometricAvailability.available:
if (isEnabled) {
final type = await getPrimaryBiometricType();
if (type != null) {
return '${getBiometricDisplayName(type)} is enabled for quick login';
}
return 'Biometric authentication is enabled for quick login';
} else {
final type = await getPrimaryBiometricType();
if (type != null) {
return '${getBiometricDisplayName(type)} is available but not enabled';
}
return 'Biometric authentication is available but not enabled';
}
case BiometricAvailability.notAvailable:
return 'Biometric authentication is not available on this device';
case BiometricAvailability.notEnrolled:
return 'No biometric credentials enrolled on this device';
case BiometricAvailability.lockedOut:
return 'Biometric authentication is temporarily locked. Try again later.';
case BiometricAvailability.permanentlyUnavailable:
return 'Biometric authentication is permanently unavailable';
}
}
/// Check if this is a mobile platform that supports biometrics
bool get isMobilePlatform {
return !kIsWeb && (Platform.isIOS || Platform.isAndroid);
}
/// Get platform-specific biometric name
String getPlatformBiometricName() {
if (Platform.isIOS) {
return 'Face ID / Touch ID';
} else if (Platform.isAndroid) {
return 'Fingerprint / Face Unlock';
}
return 'Biometric Authentication';
}
}
@@ -117,10 +117,23 @@ If user context is provided, use it to personalise your responses while respecti
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
final choices = data['choices'] as List;
final firstChoice = choices.first as Map<String, dynamic>;
final message = firstChoice['message'] as Map<String, dynamic>;
return message['content'] as String;
final choices = data['choices'] as List?;
if (choices == null || choices.isEmpty) {
throw MistralAIException('No choices returned in response');
}
final firstChoice = choices.first as Map<String, dynamic>?;
if (firstChoice == null) {
throw MistralAIException('Invalid choice format in response');
}
final message = firstChoice['message'] as Map<String, dynamic>?;
if (message == null) {
throw MistralAIException('No message in choice');
}
final content = message['content'] as String?;
if (content == null) {
throw MistralAIException('No content in message');
}
return content;
} else {
throw MistralAIException(
'Failed to get chat response',
@@ -6,88 +6,116 @@ class OfflineCacheService {
static const String _userBoxName = 'cached_user';
static const String _countdownBoxName = 'cached_countdown';
late Box<CachedGoal> _goalsBox;
late Box _userBox;
late Box _countdownBox;
Box<CachedGoal>? _goalsBox;
Box? _userBox;
Box? _countdownBox;
Future<void> init() async {
await Hive.initFlutter();
if (!Hive.isAdapterRegistered(0)) {
Hive.registerAdapter(CachedGoalAdapter());
try {
await Hive.initFlutter();
if (!Hive.isAdapterRegistered(0)) {
Hive.registerAdapter(CachedGoalAdapter());
}
_goalsBox = await Hive.openBox<CachedGoal>(_goalsBoxName);
_userBox = await Hive.openBox(_userBoxName);
_countdownBox = await Hive.openBox(_countdownBoxName);
} catch (e) {
print('Error initializing offline cache: $e');
}
_goalsBox = await Hive.openBox<CachedGoal>(_goalsBoxName);
_userBox = await Hive.openBox(_userBoxName);
_countdownBox = await Hive.openBox(_countdownBoxName);
}
Future<void> cacheGoals(List<CachedGoal> goals) async {
await _goalsBox.clear();
if (_goalsBox == null) return;
await _goalsBox!.clear();
for (var goal in goals) {
await _goalsBox.put(goal.id, goal);
await _goalsBox!.put(goal.id, goal);
}
}
Future<List<CachedGoal>> getCachedGoals() async {
return _goalsBox.values.toList();
if (_goalsBox == null) return [];
return _goalsBox!.values.toList();
}
Future<CachedGoal?> getCachedGoal(String goalId) async {
return _goalsBox.get(goalId);
if (_goalsBox == null) return null;
return _goalsBox!.get(goalId);
}
Future<void> cacheGoal(CachedGoal goal) async {
await _goalsBox.put(goal.id, goal);
if (_goalsBox == null) return;
await _goalsBox!.put(goal.id, goal);
}
Future<void> deleteCachedGoal(String goalId) async {
await _goalsBox.delete(goalId);
if (_goalsBox == null) return;
await _goalsBox!.delete(goalId);
}
Future<void> markGoalAsDirty(String goalId) async {
final goal = _goalsBox.get(goalId);
if (_goalsBox == null) return;
final goal = _goalsBox!.get(goalId);
if (goal != null) {
await _goalsBox.put(goalId, goal.copyWith(isDirty: true));
await _goalsBox!.put(goalId, goal.copyWith(isDirty: true));
}
}
Future<List<CachedGoal>> getDirtyGoals() async {
return _goalsBox.values.where((goal) => goal.isDirty).toList();
if (_goalsBox == null) return [];
return _goalsBox!.values.where((goal) => goal.isDirty).toList();
}
Future<void> clearDirtyFlag(String goalId) async {
final goal = _goalsBox.get(goalId);
if (_goalsBox == null) return;
final goal = _goalsBox!.get(goalId);
if (goal != null) {
await _goalsBox.put(goalId, goal.copyWith(isDirty: false));
await _goalsBox!.put(goalId, goal.copyWith(isDirty: false));
}
}
Future<void> cacheUserData(Map<String, dynamic> userData) async {
await _userBox.putAll(userData);
if (_userBox == null) return;
await _userBox!.putAll(userData);
}
Future<Map<String, dynamic>> getCachedUserData() async {
return Map<String, dynamic>.from(_userBox.toMap());
if (_userBox == null) return {};
return Map<String, dynamic>.from(_userBox!.toMap());
}
Future<void> cacheCountdownData(Map<String, dynamic> countdownData) async {
await _countdownBox.putAll(countdownData);
if (_countdownBox == null) return;
await _countdownBox!.putAll(countdownData);
}
Future<Map<String, dynamic>> getCachedCountdownData() async {
return Map<String, dynamic>.from(_countdownBox.toMap());
if (_countdownBox == null) return {};
return Map<String, dynamic>.from(_countdownBox!.toMap());
}
Future<void> clearAllCache() async {
await _goalsBox.clear();
await _userBox.clear();
await _countdownBox.clear();
if (_goalsBox != null) await _goalsBox!.clear();
if (_userBox != null) await _userBox!.clear();
if (_countdownBox != null) await _countdownBox!.clear();
}
Future<void> close() async {
await _goalsBox.close();
await _userBox.close();
await _countdownBox.close();
if (_goalsBox != null) await _goalsBox!.close();
if (_userBox != null) await _userBox!.close();
if (_countdownBox != null) await _countdownBox!.close();
}
}