You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							83 lines
						
					
					
						
							2.8 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							83 lines
						
					
					
						
							2.8 KiB
						
					
					
				
								import 'dart:async';
							 | 
						|
								
							 | 
						|
								import 'package:bloc/bloc.dart';
							 | 
						|
								import 'package:flutter/cupertino.dart';
							 | 
						|
								import 'package:go_router/go_router.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/common_ui/resources/my_audios.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/core/constants/my_constants.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/core/routers/my_routes.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/core/services/audio_service.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/core/utils/context_provider.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/core/utils/local_storage.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/core/widgets/dialog/about_us_dialog.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/core/widgets/dialog/exit_dialog.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/features/home/presentation/bloc/home_event.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/features/home/presentation/bloc/home_state.dart';
							 | 
						|
								import 'package:hadi_hoda_flutter/features/level/domain/entity/total_data_entity.dart';
							 | 
						|
								import 'package:hive/hive.dart';
							 | 
						|
								
							 | 
						|
								class HomeBloc extends Bloc<HomeEvent, HomeState> {
							 | 
						|
								  /// ------------constructor------------
							 | 
						|
								  HomeBloc(
							 | 
						|
								      this._mainAudioService,
							 | 
						|
								      this._effectAudioService,
							 | 
						|
								      ) : super(const HomeState()) {
							 | 
						|
								    volumeStream = _mainAudioService.volumeStream();
							 | 
						|
								    playMusic();
							 | 
						|
								    on<GetHomeEvent>(_getHomeEvent);
							 | 
						|
								  }
							 | 
						|
								
							 | 
						|
								  /// ------------UseCases------------
							 | 
						|
								
							 | 
						|
								  /// ------------Variables------------
							 | 
						|
								  late final Stream<double> volumeStream;
							 | 
						|
								
							 | 
						|
								  /// ------------Controllers------------
							 | 
						|
								  final AudioService _mainAudioService;
							 | 
						|
								  final AudioService _effectAudioService;
							 | 
						|
								
							 | 
						|
								  /// ------------Functions------------
							 | 
						|
								  void goToLevelPage(BuildContext context){
							 | 
						|
								    final String? selectedLanguage = LocalStorage.readData(key: MyConstants.selectLanguage);
							 | 
						|
								    final Box<TotalDataEntity> dataBox = Hive.box(MyConstants.levelBox);
							 | 
						|
								    final TotalDataEntity findData = dataBox.values.singleWhere(
							 | 
						|
								      (e) => e.code == selectedLanguage,
							 | 
						|
								      orElse: () => TotalDataEntity(),
							 | 
						|
								    );
							 | 
						|
								    if (findData.levels?.isNotEmpty ?? false) {
							 | 
						|
								      context.pushNamed(Routes.introPage);
							 | 
						|
								    } else {
							 | 
						|
								      context.goNamed(Routes.downloadPage);
							 | 
						|
								    }
							 | 
						|
								  }
							 | 
						|
								
							 | 
						|
								  void goToLanguagePage(BuildContext context){
							 | 
						|
								    context.pushNamed(Routes.languagePage);
							 | 
						|
								  }
							 | 
						|
								
							 | 
						|
								  void showAboutUs(BuildContext context){
							 | 
						|
								    showAboutUsDialog(context: context);
							 | 
						|
								  }
							 | 
						|
								
							 | 
						|
								  Future<void> changeMute() async {
							 | 
						|
								    await Future.wait([
							 | 
						|
								      _mainAudioService.changeMute(),
							 | 
						|
								      _effectAudioService.changeMute(),
							 | 
						|
								    ]);
							 | 
						|
								  }
							 | 
						|
								
							 | 
						|
								  Future<void> playMusic() async {
							 | 
						|
								    Future.wait([
							 | 
						|
								      _mainAudioService.setAudio(assetPath: MyAudios.homeMusic),
							 | 
						|
								      _mainAudioService.setLoopMode(isLoop: true),
							 | 
						|
								    ]);
							 | 
						|
								    await _mainAudioService.play();
							 | 
						|
								  }
							 | 
						|
								
							 | 
						|
								  void onPopInvokedWithResult(bool didPop, dynamic result) {
							 | 
						|
								    showExitDialog(context: ContextProvider.context);
							 | 
						|
								  }
							 | 
						|
								
							 | 
						|
								  /// ------------Api Calls------------
							 | 
						|
								  FutureOr<void> _getHomeEvent(event, emit) async {}
							 | 
						|
								}
							 |