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.
92 lines
3.0 KiB
92 lines
3.0 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){
|
|
playEffect();
|
|
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.levelPage);
|
|
} else {
|
|
context.goNamed(Routes.downloadPage);
|
|
}
|
|
}
|
|
|
|
void goToLanguagePage(BuildContext context){
|
|
playEffect();
|
|
context.pushNamed(Routes.languagePage);
|
|
}
|
|
|
|
void showAboutUs(BuildContext context){
|
|
playEffect();
|
|
showAboutUsDialog(context: context);
|
|
}
|
|
|
|
Future<void> changeMute() async {
|
|
playEffect();
|
|
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();
|
|
}
|
|
|
|
Future<void> playEffect() async{
|
|
await _effectAudioService.setAudio(assetPath: MyAudios.clickButton);
|
|
await _effectAudioService.play();
|
|
}
|
|
|
|
void onPopInvokedWithResult(bool didPop, dynamic result) {
|
|
showExitDialog(context: ContextProvider.context);
|
|
}
|
|
|
|
/// ------------Api Calls------------
|
|
FutureOr<void> _getHomeEvent(event, emit) async {}
|
|
}
|