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
@@ -1,6 +1,6 @@
import 'package:hive/hive.dart';
part 'cached_goal.g.dart';
part 'cached_goal_model.g.dart';
@HiveType(typeId: 0)
class CachedGoal extends HiveObject {
@@ -1,5 +1,11 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'cached_goal_model.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class CachedGoalAdapter extends TypeAdapter<CachedGoal> {
@override
final int typeId = 0;
@@ -29,33 +35,34 @@ class CachedGoalAdapter extends TypeAdapter<CachedGoal> {
@override
void write(BinaryWriter writer, CachedGoal obj) {
writer.writeByte(13);
writer.writeByte(0);
writer.write(obj.id);
writer.writeByte(1);
writer.write(obj.ownerId);
writer.writeByte(2);
writer.write(obj.title);
writer.writeByte(3);
writer.write(obj.description);
writer.writeByte(4);
writer.write(obj.progress);
writer.writeByte(5);
writer.write(obj.locationLat);
writer.writeByte(6);
writer.write(obj.locationLng);
writer.writeByte(7);
writer.write(obj.locationName);
writer.writeByte(8);
writer.write(obj.imageUrl);
writer.writeByte(9);
writer.write(obj.completed);
writer.writeByte(10);
writer.write(obj.createdAt);
writer.writeByte(11);
writer.write(obj.updatedAt);
writer.writeByte(12);
writer.write(obj.isDirty);
writer
..writeByte(13)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.ownerId)
..writeByte(2)
..write(obj.title)
..writeByte(3)
..write(obj.description)
..writeByte(4)
..write(obj.progress)
..writeByte(5)
..write(obj.locationLat)
..writeByte(6)
..write(obj.locationLng)
..writeByte(7)
..write(obj.locationName)
..writeByte(8)
..write(obj.imageUrl)
..writeByte(9)
..write(obj.completed)
..writeByte(10)
..write(obj.createdAt)
..writeByte(11)
..write(obj.updatedAt)
..writeByte(12)
..write(obj.isDirty);
}
@override
@@ -64,5 +71,7 @@ class CachedGoalAdapter extends TypeAdapter<CachedGoal> {
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is CachedGoalAdapter && runtimeType == other.runtimeType && typeId == other.typeId;
other is CachedGoalAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
+79
View File
@@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart';
import '../../core/utils/unit_conversion_utils.dart';
class User extends Equatable {
final String id;
@@ -13,6 +14,13 @@ class User extends Equatable {
final String? websiteUrl;
final DateTime? countdownStartDate;
final DateTime? countdownEndDate;
final Gender? gender;
final DateTime? birthDate;
final int? storedAge;
final double? heightCm;
final double? weightKg;
final HeightUnit heightUnit;
final WeightUnit weightUnit;
final DateTime createdAt;
final DateTime updatedAt;
@@ -29,6 +37,13 @@ class User extends Equatable {
this.websiteUrl,
this.countdownStartDate,
this.countdownEndDate,
this.gender,
this.birthDate,
this.storedAge,
this.heightCm,
this.weightKg,
this.heightUnit = HeightUnit.metric,
this.weightUnit = WeightUnit.metric,
required this.createdAt,
required this.updatedAt,
});
@@ -45,6 +60,33 @@ class User extends Equatable {
return countdownEndDate!.difference(DateTime.now()).inDays;
}
int? get age {
if (storedAge != null) return storedAge;
if (birthDate == null) return null;
return UnitConversionUtils.calculateAge(birthDate!);
}
String get formattedHeight {
if (heightCm == null) return '';
return UnitConversionUtils.formatHeight(heightCm!, heightUnit);
}
String get formattedWeight {
if (weightKg == null) return '';
return UnitConversionUtils.formatWeight(weightKg!, weightUnit);
}
double? get bmi {
if (heightCm == null || weightKg == null) return null;
return UnitConversionUtils.calculateBmi(weightKg!, heightCm!);
}
String get bmiCategory {
final bmiValue = bmi;
if (bmiValue == null) return '';
return UnitConversionUtils.getBmiCategory(bmiValue);
}
User copyWith({
String? id,
String? username,
@@ -58,6 +100,13 @@ class User extends Equatable {
String? websiteUrl,
DateTime? countdownStartDate,
DateTime? countdownEndDate,
Gender? gender,
DateTime? birthDate,
int? storedAge,
double? heightCm,
double? weightKg,
HeightUnit? heightUnit,
WeightUnit? weightUnit,
DateTime? createdAt,
DateTime? updatedAt,
}) {
@@ -74,6 +123,13 @@ class User extends Equatable {
websiteUrl: websiteUrl ?? this.websiteUrl,
countdownStartDate: countdownStartDate ?? this.countdownStartDate,
countdownEndDate: countdownEndDate ?? this.countdownEndDate,
gender: gender ?? this.gender,
birthDate: birthDate ?? this.birthDate,
storedAge: storedAge ?? this.storedAge,
heightCm: heightCm ?? this.heightCm,
weightKg: weightKg ?? this.weightKg,
heightUnit: heightUnit ?? this.heightUnit,
weightUnit: weightUnit ?? this.weightUnit,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
);
@@ -93,6 +149,13 @@ class User extends Equatable {
websiteUrl,
countdownStartDate,
countdownEndDate,
gender,
birthDate,
storedAge,
heightCm,
weightKg,
heightUnit,
weightUnit,
createdAt,
updatedAt,
];
@@ -111,6 +174,13 @@ class User extends Equatable {
'website_url': websiteUrl,
'countdown_start_date': countdownStartDate?.toIso8601String(),
'countdown_end_date': countdownEndDate?.toIso8601String(),
'gender': gender?.toDatabaseString(),
'birth_date': birthDate?.toIso8601String().split('T').first,
'age': storedAge,
'height_cm': heightCm,
'weight_kg': weightKg,
'height_unit': heightUnit.code,
'weight_unit': weightUnit.code,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
};
@@ -134,6 +204,15 @@ class User extends Equatable {
countdownEndDate: json['countdown_end_date'] != null
? DateTime.parse(json['countdown_end_date'] as String)
: null,
gender: json['gender'] != null ? Gender.fromString(json['gender'] as String) : null,
birthDate: json['birth_date'] != null ? DateTime.parse(json['birth_date'] as String) : null,
storedAge: json['age'] as int?,
heightCm: json['height_cm'] as double?,
weightKg: json['weight_kg'] as double?,
heightUnit: json['height_unit'] != null ?
HeightUnit.values.firstWhere((unit) => unit.code == json['height_unit']) : HeightUnit.metric,
weightUnit: json['weight_unit'] != null ?
WeightUnit.values.firstWhere((unit) => unit.code == json['weight_unit']) : WeightUnit.metric,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
);
@@ -1,17 +1,23 @@
import 'dart:async';
import '../models/user_model.dart';
import '../../bootstrap/supabase_client.dart';
import '../../core/utils/unit_conversion_utils.dart';
import 'package:supabase_flutter/supabase_flutter.dart' as supabase;
import 'package:google_sign_in/google_sign_in.dart';
class AuthRepository {
final supabase.SupabaseClient _client;
final supabase.SupabaseClient? _client;
StreamSubscription<supabase.AuthState>? _authStateSubscription;
AuthRepository([supabase.SupabaseClient? client]) : _client = client ?? supabaseClient;
AuthRepository([supabase.SupabaseClient? client]) : _client = client;
Stream<User?> get authStateChanges {
return _client.auth.onAuthStateChange.map((data) {
final client = supabaseClient;
if (client == null) {
// Return a stream that never emits if Supabase is not initialized
return Stream.empty();
}
return client.auth.onAuthStateChange.map((data) {
final session = data.session;
if (session?.user != null) {
return _mapSupabaseUserToAppUser(session!.user);
@@ -21,39 +27,53 @@ class AuthRepository {
}
User? get currentUser {
final user = _client.auth.currentUser;
final client = supabaseClient;
if (client == null) return null;
final user = client.auth.currentUser;
return user != null ? _mapSupabaseUserToAppUser(user) : null;
}
bool get isAuthenticated => _client.auth.currentUser != null;
bool get isAuthenticated {
final client = supabaseClient;
if (client == null) return false;
return client.auth.currentUser != null;
}
String? get currentUserId => _client.auth.currentUser?.id;
String? get currentUserId {
final client = supabaseClient;
if (client == null) return null;
return client.auth.currentUser?.id;
}
Future<bool> isSessionValid() async {
final session = _client.auth.currentSession;
assert(_client != null, 'Client must not be null');
final session = _client!.auth.currentSession;
if (session == null) return false;
final now = DateTime.now();
final expiresAt = session.expiresAt;
if (expiresAt == null) return true;
return now.isBefore(DateTime.fromMillisecondsSinceEpoch(expiresAt * 1000));
}
Future<void> refreshSession() async {
assert(_client != null, 'Client must not be null');
try {
await _client.auth.refreshSession();
await _client!.auth.refreshSession();
} catch (e) {
throw Exception('Failed to refresh session: $e');
}
}
Future<supabase.Session?> getCurrentSession() async {
return _client.auth.currentSession;
assert(_client != null, 'Client must not be null');
return _client!.auth.currentSession;
}
void listenToAuthStateChanges(Function(User?) callback) {
_authStateSubscription = _client.auth.onAuthStateChange.listen((data) {
assert(_client != null, 'Client must not be null');
_authStateSubscription = _client!.auth.onAuthStateChange.listen((data) {
final session = data.session;
if (session?.user != null) {
callback(_mapSupabaseUserToAppUser(session!.user));
@@ -68,22 +88,25 @@ class AuthRepository {
}
Future<void> signInWithEmail(String email, String password) async {
await _client.auth.signInWithPassword(email: email, password: password);
assert(_client != null, 'Client must not be null');
await _client!.auth.signInWithPassword(email: email, password: password);
}
Future<void> signUpWithEmail(String email, String password, String username) async {
final response = await _client.auth.signUp(
Future<void> signUpWithEmail(String email, String password, String username, {double? heightCm, double? weightKg, int? age, Gender? gender, HeightUnit? heightUnit, WeightUnit? weightUnit}) async {
assert(_client != null, 'Client must not be null');
final response = await _client!.auth.signUp(
email: email,
password: password,
data: {'username': username},
);
if (response.user != null) {
await _createUserProfile(response.user!.id, username, email);
await _createUserProfile(response.user!.id, username, email, heightCm: heightCm, weightKg: weightKg, age: age, gender: gender, heightUnit: heightUnit, weightUnit: weightUnit);
}
}
Future<void> signInWithGoogle() async {
assert(_client != null, 'Client must not be null');
try {
final GoogleSignIn googleSignIn = GoogleSignIn(
scopes: ['email', 'profile'],
@@ -109,6 +132,7 @@ class AuthRepository {
}
Future<void> _handleGoogleUser(dynamic googleUser) async {
assert(_client != null, 'Client must not be null');
try {
final googleAuth = await googleUser.authentication;
final idToken = googleAuth.idToken;
@@ -118,7 +142,7 @@ class AuthRepository {
throw Exception('No ID token or access token from Google sign-in');
}
final response = await _client.auth.signInWithIdToken(
final response = await _client!.auth.signInWithIdToken(
provider: supabase.OAuthProvider.google,
idToken: idToken,
accessToken: accessToken,
@@ -133,17 +157,20 @@ class AuthRepository {
}
Future<void> signInWithGithub() async {
await _client.auth.signInWithOAuth(
assert(_client != null, 'Client must not be null');
await _client!.auth.signInWithOAuth(
supabase.OAuthProvider.github,
);
}
Future<void> signOut() async {
await _client.auth.signOut();
assert(_client != null, 'Client must not be null');
await _client!.auth.signOut();
}
Future<void> resetPassword(String email) async {
await _client.auth.resetPasswordForEmail(email);
assert(_client != null, 'Client must not be null');
await _client!.auth.resetPasswordForEmail(email);
}
Future<void> updateProfile({
@@ -151,8 +178,15 @@ class AuthRepository {
String? bio,
String? avatarUrl,
bool? isPublicProfile,
double? heightCm,
double? weightKg,
int? age,
Gender? gender,
HeightUnit? heightUnit,
WeightUnit? weightUnit,
}) async {
final userId = _client.auth.currentUser?.id;
assert(_client != null, 'Client must not be null');
final userId = _client!.auth.currentUser?.id;
if (userId == null) throw Exception('User not authenticated');
final updates = <String, dynamic>{};
@@ -160,23 +194,36 @@ class AuthRepository {
if (bio != null) updates['bio'] = bio;
if (avatarUrl != null) updates['avatar_url'] = avatarUrl;
if (isPublicProfile != null) updates['is_public_profile'] = isPublicProfile;
if (heightCm != null) updates['height_cm'] = heightCm;
if (weightKg != null) updates['weight_kg'] = weightKg;
if (age != null) updates['age'] = age;
if (gender != null) updates['gender'] = gender.toDatabaseString();
if (heightUnit != null) updates['height_unit'] = heightUnit.code;
if (weightUnit != null) updates['weight_unit'] = weightUnit.code;
updates['updated_at'] = DateTime.now().toIso8601String();
await _client
await _client!
.from('users')
.update(updates)
.eq('id', userId);
}
Future<User> _createUserProfile(String userId, String username, String email) async {
Future<User> _createUserProfile(String userId, String username, String email, {double? heightCm, double? weightKg, int? age, Gender? gender, HeightUnit? heightUnit, WeightUnit? weightUnit}) async {
assert(_client != null, 'Client must not be null');
final now = DateTime.now().toIso8601String();
try {
// First try with the regular client (might fail due to RLS)
final response = await _client.from('users').insert({
final response = await _client!.from('users').insert({
'id': userId,
'username': username,
'email': email,
'height_cm': heightCm,
'weight_kg': weightKg,
'age': age,
'gender': gender?.toDatabaseString(),
'height_unit': heightUnit?.code ?? HeightUnit.metric.code,
'weight_unit': weightUnit?.code ?? WeightUnit.metric.code,
'created_at': now,
'updated_at': now,
}).select();
@@ -192,6 +239,12 @@ class AuthRepository {
'id': userId,
'username': username,
'email': email,
'height_cm': heightCm,
'weight_kg': weightKg,
'age': age,
'gender': gender?.toDatabaseString(),
'height_unit': heightUnit?.code ?? HeightUnit.metric.code,
'weight_unit': weightUnit?.code ?? WeightUnit.metric.code,
'created_at': now,
'updated_at': now,
}).select();
@@ -206,6 +259,12 @@ class AuthRepository {
id: userId,
username: username,
email: email,
storedAge: age,
heightCm: heightCm,
weightKg: weightKg,
gender: gender,
heightUnit: heightUnit ?? HeightUnit.metric,
weightUnit: weightUnit ?? WeightUnit.metric,
createdAt: DateTime.parse(now),
updatedAt: DateTime.parse(now),
);
@@ -217,14 +276,21 @@ class AuthRepository {
id: userId,
username: username,
email: email,
storedAge: age,
heightCm: heightCm,
weightKg: weightKg,
gender: gender,
heightUnit: heightUnit ?? HeightUnit.metric,
weightUnit: weightUnit ?? WeightUnit.metric,
createdAt: DateTime.parse(now),
updatedAt: DateTime.parse(now),
);
}
Future<void> _ensureUserProfileExists(String userId, dynamic supabaseUser) async {
assert(_client != null, 'Client must not be null');
try {
final existingProfile = await _client
final existingProfile = await _client!
.from('users')
.select('id')
.eq('id', userId)
@@ -270,6 +336,10 @@ class AuthRepository {
countdownEndDate: data['countdown_end_date'] != null
? DateTime.parse(data['countdown_end_date'])
: null,
gender: data['gender'] != null ? Gender.fromString(data['gender']) : null,
storedAge: data['age'] as int?,
heightCm: (data['height_cm'] as num?)?.toDouble(),
weightKg: (data['weight_kg'] as num?)?.toDouble(),
createdAt: DateTime.parse(data['created_at']),
updatedAt: DateTime.parse(data['updated_at']),
);
@@ -1,6 +1,7 @@
import 'package:supabase_flutter/supabase_flutter.dart' as supabase;
import '../models/user_model.dart' as app;
import '../../core/errors/failure.dart';
import '../../core/utils/unit_conversion_utils.dart';
class UserRepository {
final supabase.SupabaseClient _client;
@@ -35,6 +36,12 @@ class UserRepository {
String? instagramHandle,
String? tiktokHandle,
String? websiteUrl,
Gender? gender,
DateTime? birthDate,
double? heightCm,
double? weightKg,
HeightUnit heightUnit = HeightUnit.metric,
WeightUnit weightUnit = WeightUnit.metric,
}) async {
try {
final updates = <String, dynamic>{};
@@ -46,6 +53,12 @@ class UserRepository {
if (instagramHandle != null) updates['instagram_handle'] = instagramHandle;
if (tiktokHandle != null) updates['tiktok_handle'] = tiktokHandle;
if (websiteUrl != null) updates['website_url'] = websiteUrl;
if (gender != null) updates['gender'] = gender.toDatabaseString();
if (birthDate != null) updates['birth_date'] = birthDate.toIso8601String().split('T').first;
if (heightCm != null) updates['height_cm'] = heightCm;
if (weightKg != null) updates['weight_kg'] = weightKg;
updates['height_unit'] = heightUnit.code;
updates['weight_unit'] = weightUnit.code;
updates['updated_at'] = DateTime.now().toIso8601String();
final response = await _client
@@ -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();
}
}