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.
157 lines
5.4 KiB
157 lines
5.4 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/core/constants/my_constants.dart';
|
|
import 'package:hadi_hoda_flutter/core/params/level_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/features/level/domain/entity/level_entity.dart';
|
|
import 'package:hadi_hoda_flutter/features/level/domain/entity/level_location.dart';
|
|
import 'package:hadi_hoda_flutter/features/level/domain/usecases/get_levels_usecase.dart';
|
|
import 'package:hadi_hoda_flutter/features/level/presentation/bloc/level_event.dart';
|
|
import 'package:hadi_hoda_flutter/features/level/presentation/bloc/level_state.dart';
|
|
import 'package:hadi_hoda_flutter/features/level/presentation/ui/widgets/level_widget.dart';
|
|
|
|
class LevelBloc extends Bloc<LevelEvent, LevelState> {
|
|
/// ------------constructor------------
|
|
LevelBloc(
|
|
this._getLeveslUseCase,
|
|
) : super(const LevelState()) {
|
|
on<GetLevelListEvent>(_getLevelListEvent);
|
|
on<SetCurrentLevelEvent>(_setCurrentLevelEvent);
|
|
on<StartScrollEvent>(_startScrollEvent);
|
|
}
|
|
|
|
@override
|
|
Future<void> close() {
|
|
scrollController.dispose();
|
|
return super.close();
|
|
}
|
|
|
|
/// ------------UseCases------------
|
|
final GetLevelsUseCase _getLeveslUseCase;
|
|
|
|
/// ------------Variables------------
|
|
final List<LevelLocation> bottomLocationList = [
|
|
LevelLocation(bottom: -30, left: 30, index: 1),
|
|
LevelLocation(bottom: 50, left: 100, index: 2),
|
|
LevelLocation(bottom: 150, left: 60, index: 3),
|
|
LevelLocation(bottom: 210, left: 120, index: 4),
|
|
LevelLocation(bottom: 250, right: 60, index: 5),
|
|
LevelLocation(top: 150, right: 40, index: 6),
|
|
LevelLocation(top: 70, right: 70, index: 7),
|
|
LevelLocation(top: -20, right: 70, index: 8),
|
|
];
|
|
|
|
final List<LevelLocation> topLocationList = [
|
|
LevelLocation(bottom: 30, right: 80, index: 9),
|
|
LevelLocation(bottom: 70, left: 20, index: 10),
|
|
LevelLocation(bottom: 150, left: 50, index: 11),
|
|
LevelLocation(bottom: 180, left: 140, index: 12),
|
|
LevelLocation(bottom: 260, right: 20, index: 13),
|
|
LevelLocation(bottom: 370, right: 30, index: 14),
|
|
LevelLocation(bottom: 420, left: 40, index: 15),
|
|
LevelLocation(top: 410, left: 0, index: 16),
|
|
LevelLocation(top: 320, left: 60, index: 17),
|
|
LevelLocation(top: 220, left: 80, index: 18),
|
|
LevelLocation(top: 130, left: 20, index: 19),
|
|
LevelLocation(top: 50, left: 70, index: 20),
|
|
];
|
|
|
|
final List<LevelEntity> bottom8LevelList = [];
|
|
final List<LevelEntity> top12LevelList = [];
|
|
|
|
|
|
/// ------------Controllers------------
|
|
final ScrollController scrollController = ScrollController();
|
|
|
|
/// ------------Functions------------
|
|
void goToQuestionPage(BuildContext context, LevelEntity level){
|
|
context.pushNamed(
|
|
Routes.questionPage,
|
|
pathParameters: {
|
|
'id': '${level.id}',
|
|
},
|
|
);
|
|
}
|
|
|
|
void goToHomePage(BuildContext context){
|
|
context.pop();
|
|
}
|
|
|
|
LevelType getLevelType(int index) {
|
|
final int currentLevel = int.parse(
|
|
LocalStorage.readData(key: MyConstants.currentLevel),
|
|
);
|
|
|
|
if (index < currentLevel) {
|
|
return LevelType.finished;
|
|
} else if (index == currentLevel) {
|
|
return LevelType.current;
|
|
} else {
|
|
return LevelType.unFinished;
|
|
}
|
|
}
|
|
|
|
/// ------------Api Calls------------
|
|
FutureOr<void> _getLevelListEvent(GetLevelListEvent event,
|
|
Emitter<LevelState> emit) async {
|
|
final int currentLevel = int.parse(
|
|
LocalStorage.readData(key: MyConstants.currentLevel),
|
|
);
|
|
await _getLeveslUseCase(LevelParams()).then((value) {
|
|
value.fold(
|
|
(data) async {
|
|
bottom8LevelList.addAll(data.take(8));
|
|
if(data.length > 8){
|
|
top12LevelList.addAll(data.sublist(8, data.length));
|
|
}
|
|
emit(state.copyWith(
|
|
getLevelStatus: const BaseComplete(''),
|
|
chooseLevel: data.singleWhere((e) => e.order == currentLevel),
|
|
));
|
|
add(StartScrollEvent());
|
|
},
|
|
(error) {},
|
|
);
|
|
});
|
|
}
|
|
|
|
FutureOr<void> _startScrollEvent(
|
|
StartScrollEvent event,
|
|
Emitter<LevelState> emit,
|
|
) async {
|
|
final int currentLevel = int.parse(
|
|
LocalStorage.readData(key: MyConstants.currentLevel),
|
|
);
|
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
if (scrollController.hasClients) {
|
|
if(currentLevel < 9){
|
|
scrollController.animateTo(
|
|
scrollController.position.maxScrollExtent,
|
|
duration: const Duration(milliseconds: 500), // Note: 500 seconds is very long.
|
|
curve: Curves.easeInOut,
|
|
);
|
|
} else if( currentLevel > 8 && currentLevel < 14){
|
|
scrollController.animateTo(
|
|
scrollController.position.maxScrollExtent / 2,
|
|
duration: const Duration(milliseconds: 500), // Note: 500 seconds is very long.
|
|
curve: Curves.easeInOut,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
FutureOr<void> _setCurrentLevelEvent(SetCurrentLevelEvent event,
|
|
Emitter<LevelState> emit) async {
|
|
final String currentLevel = LocalStorage.readData(
|
|
key: MyConstants.currentLevel);
|
|
if (currentLevel.isEmpty) {
|
|
await LocalStorage.saveData(key: MyConstants.currentLevel, value: '1');
|
|
}
|
|
add(GetLevelListEvent());
|
|
}
|
|
}
|