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.
131 lines
4.0 KiB
131 lines
4.0 KiB
import 'dart:async';
|
|
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:hadi_hoda_flutter/core/constants/my_constants.dart';
|
|
import 'package:hadi_hoda_flutter/core/params/no_params.dart';
|
|
import 'package:hadi_hoda_flutter/core/routers/my_routes.dart';
|
|
import 'package:hadi_hoda_flutter/core/status/base_status.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/features/guider/domain/usecases/get_first_level_usecase.dart';
|
|
import 'package:hadi_hoda_flutter/features/guider/presentation/bloc/guider_event.dart';
|
|
import 'package:hadi_hoda_flutter/features/guider/presentation/bloc/guider_state.dart';
|
|
import 'package:hadi_hoda_flutter/features/level/domain/entity/level_entity.dart';
|
|
import 'package:hadi_hoda_flutter/features/question/domain/entity/question_entity.dart';
|
|
import 'package:showcaseview/showcaseview.dart';
|
|
|
|
class GuiderBloc extends Bloc<GuiderEvent, GuiderState> {
|
|
/// ------------constructor------------
|
|
GuiderBloc(this._getFirstLevelUseCase) : super(const GuiderState()) {
|
|
registerShowCase();
|
|
on<GetFirstLevelEvent>(_getFirstLevelEvent);
|
|
}
|
|
|
|
@override
|
|
Future<void> close() {
|
|
unRegisterShowCase();
|
|
animationController.dispose();
|
|
return super.close();
|
|
}
|
|
|
|
/// ------------UseCases------------
|
|
final GetFirstLevelUseCase _getFirstLevelUseCase;
|
|
|
|
/// ------------Variables------------
|
|
final Map<String, GlobalKey> showCaseKey = {
|
|
'answer_key_0': GlobalKey(),
|
|
'answer_key_1': GlobalKey(),
|
|
'answer_key_2': GlobalKey(),
|
|
'answer_key_3': GlobalKey(),
|
|
'notif_key_0': GlobalKey(),
|
|
'notif_key_1': GlobalKey(),
|
|
'notif_key_2': GlobalKey(),
|
|
'notif_key_3': GlobalKey(),
|
|
'stepper_key': GlobalKey(),
|
|
'hadith_key': GlobalKey(),
|
|
'guide_key': GlobalKey(),
|
|
};
|
|
String? id;
|
|
|
|
/// ------------Controllers------------
|
|
late final AnimationController animationController;
|
|
|
|
/// ------------Functions------------
|
|
void registerShowCase() {
|
|
try {
|
|
ShowcaseView.register(
|
|
onStart: (showcaseIndex, key) {
|
|
LocalStorage.saveData(key: MyConstants.firstShowcase, value: 'true');
|
|
},
|
|
onDismiss: (onDismiss) {
|
|
MyContext.get.goNamed(
|
|
Routes.questionPage,
|
|
pathParameters: {'id': id ?? ''},
|
|
);
|
|
},
|
|
onFinish: () {
|
|
MyContext.get.goNamed(
|
|
Routes.questionPage,
|
|
pathParameters: {'id': id ?? ''},
|
|
);
|
|
},
|
|
);
|
|
} catch (_) {}
|
|
}
|
|
|
|
void unRegisterShowCase() {
|
|
try {
|
|
ShowcaseView.get().unregister();
|
|
} catch (_) {}
|
|
}
|
|
|
|
void startShowcase() {
|
|
if (LocalStorage.readData(key: MyConstants.firstShowcase) != 'true') {
|
|
try {
|
|
ShowcaseView.get().startShowCase([
|
|
showCaseKey['answer_key_1']!,
|
|
showCaseKey['notif_key_0']!,
|
|
showCaseKey['stepper_key']!,
|
|
showCaseKey['hadith_key']!,
|
|
showCaseKey['guide_key']!,
|
|
]);
|
|
} catch (_) {}
|
|
}
|
|
}
|
|
|
|
/// ------------Event Calls------------
|
|
|
|
FutureOr<void> _getFirstLevelEvent(
|
|
GetFirstLevelEvent event,
|
|
Emitter<GuiderState> emit,
|
|
) async {
|
|
id = event.id;
|
|
await _getFirstLevelUseCase(NoParams()).then(
|
|
(value) => value.fold((data) async {
|
|
final LevelEntity level = LevelEntity(
|
|
id: data.id,
|
|
order: data.order,
|
|
title: data.title,
|
|
questions: [
|
|
...?data.questions,
|
|
QuestionEntity(order: (data.questions?.length ?? 0) + 1),
|
|
],
|
|
);
|
|
emit(
|
|
state.copyWith(
|
|
getQuestionStatus: BaseComplete(''),
|
|
levelEntity: level,
|
|
currentQuestion: data.questions?.first,
|
|
),
|
|
);
|
|
await Future.delayed(Duration(milliseconds: 300), () {
|
|
animationController.forward().then((value) {
|
|
startShowcase();
|
|
});
|
|
});
|
|
}, (error) {}),
|
|
);
|
|
}
|
|
}
|