|
| 1 | +// |
| 2 | +// ignore_for_file: deprecated_member_use |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 6 | +import 'package:go_router/go_router.dart'; |
| 7 | +import 'package:ht_auth_repository/ht_auth_repository.dart'; |
| 8 | +import 'package:ht_data_repository/ht_data_repository.dart'; |
| 9 | +import 'package:ht_kv_storage_service/ht_kv_storage_service.dart'; |
| 10 | +import 'package:ht_dashboard/app/bloc/app_bloc.dart'; |
| 11 | +import 'package:ht_dashboard/app/config/app_environment.dart'; |
| 12 | +import 'package:ht_dashboard/authentication/bloc/authentication_bloc.dart'; |
| 13 | +import 'package:ht_dashboard/l10n/app_localizations.dart'; |
| 14 | +import 'package:ht_dashboard/l10n/l10n.dart'; |
| 15 | +import 'package:ht_dashboard/router/router.dart'; |
| 16 | +import 'package:ht_shared/ht_shared.dart'; |
| 17 | + |
| 18 | +class App extends StatelessWidget { |
| 19 | + const App({ |
| 20 | + required HtAuthRepository htAuthenticationRepository, |
| 21 | + required HtDataRepository<Headline> htHeadlinesRepository, |
| 22 | + required HtDataRepository<Category> htCategoriesRepository, |
| 23 | + required HtDataRepository<Country> htCountriesRepository, |
| 24 | + required HtDataRepository<Source> htSourcesRepository, |
| 25 | + required HtDataRepository<UserAppSettings> htUserAppSettingsRepository, |
| 26 | + required HtDataRepository<UserContentPreferences> |
| 27 | + htUserContentPreferencesRepository, |
| 28 | + required HtDataRepository<AppConfig> htAppConfigRepository, |
| 29 | + required HtKVStorageService kvStorageService, |
| 30 | + required AppEnvironment environment, |
| 31 | + super.key, |
| 32 | + }) : _htAuthenticationRepository = htAuthenticationRepository, |
| 33 | + _htHeadlinesRepository = htHeadlinesRepository, |
| 34 | + _htCategoriesRepository = htCategoriesRepository, |
| 35 | + _htCountriesRepository = htCountriesRepository, |
| 36 | + _htSourcesRepository = htSourcesRepository, |
| 37 | + _htUserAppSettingsRepository = htUserAppSettingsRepository, |
| 38 | + _htUserContentPreferencesRepository = htUserContentPreferencesRepository, |
| 39 | + _htAppConfigRepository = htAppConfigRepository, |
| 40 | + _kvStorageService = kvStorageService, |
| 41 | + _environment = environment; |
| 42 | + |
| 43 | + final HtAuthRepository _htAuthenticationRepository; |
| 44 | + final HtDataRepository<Headline> _htHeadlinesRepository; |
| 45 | + final HtDataRepository<Category> _htCategoriesRepository; |
| 46 | + final HtDataRepository<Country> _htCountriesRepository; |
| 47 | + final HtDataRepository<Source> _htSourcesRepository; |
| 48 | + final HtDataRepository<UserAppSettings> _htUserAppSettingsRepository; |
| 49 | + final HtDataRepository<UserContentPreferences> |
| 50 | + _htUserContentPreferencesRepository; |
| 51 | + final HtDataRepository<AppConfig> _htAppConfigRepository; |
| 52 | + final HtKVStorageService _kvStorageService; |
| 53 | + final AppEnvironment _environment; |
| 54 | + |
| 55 | + @override |
| 56 | + Widget build(BuildContext context) { |
| 57 | + return MultiRepositoryProvider( |
| 58 | + providers: [ |
| 59 | + RepositoryProvider.value(value: _htAuthenticationRepository), |
| 60 | + RepositoryProvider.value(value: _htHeadlinesRepository), |
| 61 | + RepositoryProvider.value(value: _htCategoriesRepository), |
| 62 | + RepositoryProvider.value(value: _htCountriesRepository), |
| 63 | + RepositoryProvider.value(value: _htSourcesRepository), |
| 64 | + RepositoryProvider.value(value: _htUserAppSettingsRepository), |
| 65 | + RepositoryProvider.value(value: _htUserContentPreferencesRepository), |
| 66 | + RepositoryProvider.value(value: _htAppConfigRepository), |
| 67 | + RepositoryProvider.value(value: _kvStorageService), |
| 68 | + ], |
| 69 | + child: MultiBlocProvider( |
| 70 | + providers: [ |
| 71 | + BlocProvider( |
| 72 | + create: (context) => AppBloc( |
| 73 | + authenticationRepository: context.read<HtAuthRepository>(), |
| 74 | + userAppSettingsRepository: |
| 75 | + context.read<HtDataRepository<UserAppSettings>>(), |
| 76 | + appConfigRepository: context.read<HtDataRepository<AppConfig>>(), |
| 77 | + environment: _environment, |
| 78 | + ), |
| 79 | + ), |
| 80 | + BlocProvider( |
| 81 | + create: (context) => AuthenticationBloc( |
| 82 | + authenticationRepository: context.read<HtAuthRepository>(), |
| 83 | + ), |
| 84 | + ), |
| 85 | + ], |
| 86 | + child: _AppView( |
| 87 | + htAuthenticationRepository: _htAuthenticationRepository, |
| 88 | + environment: _environment, |
| 89 | + ), |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +class _AppView extends StatefulWidget { |
| 96 | + const _AppView({ |
| 97 | + required this.htAuthenticationRepository, |
| 98 | + required this.environment, |
| 99 | + }); |
| 100 | + |
| 101 | + final HtAuthRepository htAuthenticationRepository; |
| 102 | + final AppEnvironment environment; |
| 103 | + |
| 104 | + @override |
| 105 | + State<_AppView> createState() => _AppViewState(); |
| 106 | +} |
| 107 | + |
| 108 | +class _AppViewState extends State<_AppView> { |
| 109 | + late final GoRouter _router; |
| 110 | + late final ValueNotifier<AppStatus> _statusNotifier; |
| 111 | + |
| 112 | + @override |
| 113 | + void initState() { |
| 114 | + super.initState(); |
| 115 | + final appBloc = context.read<AppBloc>(); |
| 116 | + _statusNotifier = ValueNotifier<AppStatus>(appBloc.state.status); |
| 117 | + _router = createRouter( |
| 118 | + authStatusNotifier: _statusNotifier, |
| 119 | + htAuthenticationRepository: widget.htAuthenticationRepository, |
| 120 | + environment: widget.environment, |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + @override |
| 125 | + void dispose() { |
| 126 | + _statusNotifier.dispose(); |
| 127 | + super.dispose(); |
| 128 | + } |
| 129 | + |
| 130 | + @override |
| 131 | + Widget build(BuildContext context) { |
| 132 | + return BlocListener<AppBloc, AppState>( |
| 133 | + listenWhen: (previous, current) => previous.status != current.status, |
| 134 | + listener: (context, state) { |
| 135 | + _statusNotifier.value = state.status; |
| 136 | + }, |
| 137 | + child: BlocBuilder<AppBloc, AppState>( |
| 138 | + builder: (context, state) { |
| 139 | + return MaterialApp.router( |
| 140 | + debugShowCheckedModeBanner: false, |
| 141 | + routerConfig: _router, |
| 142 | + localizationsDelegates: AppLocalizations.localizationsDelegates, |
| 143 | + supportedLocales: AppLocalizations.supportedLocales, |
| 144 | + ); |
| 145 | + }, |
| 146 | + ), |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
0 commit comments