57 lines
1.8 KiB
Dart
57 lines
1.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../../domain/usecases/get_theme_usecase.dart';
|
|
import 'theme_state.dart';
|
|
|
|
class ThemeCubit extends Cubit<ThemeState> {
|
|
final GetThemeUseCase getThemeUseCase;
|
|
|
|
/// Base URL for loading theme images (logo, etc.)
|
|
static const String _imageBaseUrl = 'https://hrm.go.iq/Images/';
|
|
|
|
/// Guard against re-entrant / duplicate calls
|
|
bool _isLoading = false;
|
|
|
|
ThemeCubit({required this.getThemeUseCase}) : super(const ThemeInitial());
|
|
|
|
/// Load theme. Set [forceReload] to true after login to refresh.
|
|
Future<void> loadTheme({bool forceReload = false}) async {
|
|
// Prevent duplicate concurrent calls
|
|
if (_isLoading) {
|
|
debugPrint('[ThemeCubit] loadTheme() skipped — already loading');
|
|
return;
|
|
}
|
|
|
|
// If already loaded and not forced, skip
|
|
if (!forceReload && state is ThemeLoaded) {
|
|
debugPrint('[ThemeCubit] loadTheme() skipped — already loaded');
|
|
return;
|
|
}
|
|
|
|
_isLoading = true;
|
|
debugPrint('[ThemeCubit] loadTheme() called (forceReload=$forceReload)');
|
|
emit(const ThemeLoading());
|
|
|
|
final result = await getThemeUseCase();
|
|
|
|
_isLoading = false;
|
|
|
|
result.fold(
|
|
(failure) {
|
|
debugPrint('[ThemeCubit] ❌ FAILED: ${failure.message}');
|
|
emit(ThemeError(failure.message));
|
|
},
|
|
(theme) {
|
|
debugPrint(
|
|
'[ThemeCubit] ✅ Got theme — name: ${theme.name}, logo: ${theme.logo}',
|
|
);
|
|
// Build the full logo URL: https://hrm.go.iq/Images/{filename}
|
|
final encodedLogo = Uri.encodeFull(theme.logo);
|
|
final logoUrl = '$_imageBaseUrl$encodedLogo';
|
|
debugPrint('[ThemeCubit] 🖼️ Logo URL: $logoUrl');
|
|
emit(ThemeLoaded(theme: theme, logoUrl: logoUrl));
|
|
},
|
|
);
|
|
}
|
|
}
|