Files
1356/lifetimer/lib/features/achievements/application/achievements_controller.dart
T
Tomas Dvorak 37ffb93923 feat: Complete Phase 1 - Full Flutter app implementation with comprehensive features
Version: 1.1.0

Major changes:
- Implemented complete Flutter app structure with all core features
- Added comprehensive UI screens for auth, countdown, goals, profile, settings, and social features
- Integrated Supabase backend with authentication and data repositories
- Added offline support with Hive caching and local storage
- Implemented comprehensive routing with go_router
- Added location services with Google Maps integration
- Implemented notifications and home widget support
- Added voice recording capabilities and AI chat features
- Created comprehensive test suite and documentation
- Added Android and iOS platform configurations
- Implemented achievements system and social features
- Added calendar integration and bucket list functionality

This represents a complete Phase 1 milestone with 3,775 additions across 31 files.
2026-01-04 14:33:54 +01:00

141 lines
3.7 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../data/models/achievement_model.dart';
import '../../../data/repositories/achievements_repository.dart';
import '../../../bootstrap/supabase_client.dart';
import '../../auth/application/auth_controller.dart';
class AchievementsState {
final bool isLoading;
final String? error;
final List<Achievement> availableAchievements;
final List<Achievement> unlockedAchievements;
final Achievement? newlyUnlocked;
const AchievementsState({
this.isLoading = false,
this.error,
this.availableAchievements = const [],
this.unlockedAchievements = const [],
this.newlyUnlocked,
});
int get unlockedCount => unlockedAchievements.length;
int get totalCount => availableAchievements.length;
double get completionPercentage =>
totalCount > 0 ? (unlockedCount / totalCount) * 100 : 0;
int get level {
if (unlockedCount <= 0) {
return 1;
}
return 1 + (unlockedCount ~/ 3);
}
AchievementsState copyWith({
bool? isLoading,
String? error,
List<Achievement>? availableAchievements,
List<Achievement>? unlockedAchievements,
Achievement? newlyUnlocked,
}) {
return AchievementsState(
isLoading: isLoading ?? this.isLoading,
error: error,
availableAchievements: availableAchievements ?? this.availableAchievements,
unlockedAchievements: unlockedAchievements ?? this.unlockedAchievements,
newlyUnlocked: newlyUnlocked,
);
}
}
class AchievementsController extends StateNotifier<AchievementsState> {
final AchievementsRepository _repository;
final AuthController _authController;
AchievementsController(
this._repository,
this._authController,
) : super(const AchievementsState()) {
_loadAchievements();
}
Future<void> _loadAchievements() async {
final userId = _authController.currentUserId;
if (userId == null) return;
state = state.copyWith(isLoading: true);
try {
final available = await _repository.getAvailableAchievements();
final unlocked = await _repository.getUserAchievements(userId);
state = state.copyWith(
isLoading: false,
availableAchievements: available,
unlockedAchievements: unlocked,
);
} catch (e) {
state = state.copyWith(
isLoading: false,
error: e.toString(),
);
}
}
Future<Achievement?> checkAndUnlockAchievement(
AchievementType type,
int currentValue,
) async {
final userId = _authController.currentUserId;
if (userId == null) return null;
try {
final newlyUnlocked = await _repository.checkAndUnlockAchievement(
userId,
type,
currentValue,
);
if (newlyUnlocked != null) {
final updatedUnlocked = [...state.unlockedAchievements, newlyUnlocked];
state = state.copyWith(
unlockedAchievements: updatedUnlocked,
newlyUnlocked: newlyUnlocked,
);
return newlyUnlocked;
}
} catch (e) {
state = state.copyWith(error: e.toString());
}
return null;
}
void clearNewlyUnlocked() {
state = state.copyWith(newlyUnlocked: null);
}
void clearError() {
state = state.copyWith(error: null);
}
Future<void> refresh() async {
await _loadAchievements();
}
}
final achievementsControllerProvider =
StateNotifierProvider<AchievementsController, AchievementsState>((ref) {
final achievementsRepository = ref.watch(achievementsRepositoryProvider);
final authController = ref.watch(authControllerProvider.notifier);
return AchievementsController(
achievementsRepository,
authController,
);
});
final achievementsRepositoryProvider = Provider<AchievementsRepository>((ref) {
return AchievementsRepository(supabaseClient);
});