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.
130 lines
4.1 KiB
130 lines
4.1 KiB
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.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/local_storage.dart';
|
|
import 'package:hadi_hoda_flutter/core/utils/my_context.dart';
|
|
import 'package:hadi_hoda_flutter/core/utils/storage_path.dart';
|
|
import 'package:hadi_hoda_flutter/core/widgets/dialog/about_us_dialog.dart';
|
|
import 'package:hadi_hoda_flutter/features/download/domain/entities/download_entity.dart';
|
|
import 'package:hadi_hoda_flutter/features/download/domain/usecases/get_last_downloaded_level.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 '../../../../core/params/no_params.dart';
|
|
|
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|
/// ------------constructor------------
|
|
HomeBloc(this._mainAudioService, this._effectAudioService, this._getLastDownloadedLevel)
|
|
: super(const HomeState()) {
|
|
volumeStream = _mainAudioService.volumeStream();
|
|
playMusic();
|
|
preCacheQuestionGifs();
|
|
preCacheAnswerGifs();
|
|
on<GetHomeEvent>(_getHomeEvent);
|
|
}
|
|
|
|
/// ------------UseCases------------
|
|
final GetLastDownloadedLevel _getLastDownloadedLevel;
|
|
/// ------------Variables------------
|
|
late final Stream<double> volumeStream;
|
|
|
|
/// ------------Controllers------------
|
|
final AudioService _mainAudioService;
|
|
final AudioService _effectAudioService;
|
|
|
|
/// ------------Functions------------
|
|
void goToLevelPage(BuildContext context) async {
|
|
final lastDownloadedLevel =
|
|
(await _getLastDownloadedLevel(NoParams())).data ?? 0;
|
|
final hasFirstDownload = MyConstants.firstDownloadBatchCount <= lastDownloadedLevel;
|
|
|
|
if(!context.mounted) return;
|
|
|
|
if (hasFirstDownload) {
|
|
context.goNamed(Routes.levelPage);
|
|
} else {
|
|
context.goNamed(
|
|
Routes.downloadPage,
|
|
extra: const DownloadPageConfig(
|
|
downloadToLevel: MyConstants.firstDownloadBatchCount,
|
|
redirectTo: Routes.homePage
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<int> getLastDownloadedLevel() async {
|
|
final result = await _getLastDownloadedLevel(NoParams());
|
|
return result.data ?? 0;
|
|
}
|
|
|
|
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 {
|
|
await Future.wait([
|
|
_mainAudioService.setAudio(assetPath: MyAudios.home),
|
|
_mainAudioService.setLoopMode(isLoop: true),
|
|
]);
|
|
await _mainAudioService.play();
|
|
}
|
|
|
|
Future<void> preCacheQuestionGifs() async {
|
|
try {
|
|
final Directory directory = Directory(
|
|
'${StoragePath.documentDir}/question_image',
|
|
);
|
|
final List<FileSystemEntity> files = directory.listSync();
|
|
await Future.wait(
|
|
files.map(
|
|
(file) => precacheImage(FileImage(File(file.path)), MyContext.get),
|
|
),
|
|
);
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('$e');
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> preCacheAnswerGifs() async {
|
|
try {
|
|
final Directory directory = Directory(
|
|
'${StoragePath.documentDir}/answer_image',
|
|
);
|
|
final List<FileSystemEntity> files = directory.listSync();
|
|
await Future.wait(
|
|
files.map(
|
|
(file) => precacheImage(FileImage(File(file.path)), MyContext.get),
|
|
),
|
|
);
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print('$e');
|
|
}
|
|
}
|
|
}
|
|
|
|
/// ------------Api Calls------------
|
|
FutureOr<void> _getHomeEvent(event, emit) async {}
|
|
}
|