import 'dart:developer'; import 'package:hydrated_bloc/hydrated_bloc.dart'; import '../../../../../api/marianumconnect/queries/get_capabilities/get_capabilities.dart'; import 'capabilities_state.dart'; /// Holds the current user's mobile capability flags. Hydrated so the last /// known state is available immediately on cold start (no feature-flicker) /// and offline. [load] refreshes it from the server after login. class CapabilitiesCubit extends HydratedCubit { CapabilitiesCubit() : super(const CapabilitiesState()); bool get canViewForeignTimetables => state.viewForeignTimetables; /// Refreshes capabilities from the server. On any failure (endpoint not yet /// live, network error, 4xx) the previously hydrated flags are kept but the /// state is marked `loaded` — a failed fetch never silently grants a /// capability, and an offline launch keeps whatever was cached. Future load() async { try { final response = await GetCapabilities().run(); emit( CapabilitiesState( viewForeignTimetables: response.viewForeignTimetables, loaded: true, ), ); } catch (e) { log('Failed to load capabilities: $e'); emit(state.copyWith(loaded: true)); } } Future reset() async => emit(const CapabilitiesState()); @override CapabilitiesState fromJson(Map json) { try { return CapabilitiesState.fromJson(json); } catch (_) { return const CapabilitiesState(); } } @override Map? toJson(CapabilitiesState state) => state.toJson(); }