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.
63 lines
2.5 KiB
63 lines
2.5 KiB
import 'package:hadi_hoda_flutter/core/constants/my_constants.dart';
|
|
import 'package:hadi_hoda_flutter/core/error_handler/my_exception.dart';
|
|
import 'package:hadi_hoda_flutter/core/params/question_params.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/node_entity.dart';
|
|
import 'package:hadi_hoda_flutter/features/level/domain/entity/total_data_entity.dart';
|
|
import 'package:hive/hive.dart';
|
|
|
|
abstract class IQuestionDatasource {
|
|
Future<LevelEntity> getLevel({required QuestionParams params});
|
|
Future<LevelEntity> getNextLevel({required QuestionParams params});
|
|
}
|
|
|
|
/// Local
|
|
class QuestionDatasourceImpl implements IQuestionDatasource {
|
|
const QuestionDatasourceImpl();
|
|
|
|
@override
|
|
Future<LevelEntity> getLevel({required QuestionParams params}) async {
|
|
try {
|
|
final String selectedLanguage = LocalStorage.readData(
|
|
key: MyConstants.selectLanguage) ?? MyConstants.defaultLanguage;
|
|
final Box<TotalDataEntity> levelBox = Hive.box(MyConstants.levelBox);
|
|
final TotalDataEntity findData = levelBox.values.singleWhere(
|
|
(e) => e.code == selectedLanguage,
|
|
orElse: () => TotalDataEntity(),
|
|
);
|
|
final NodeEntity? findLevel = findData.nodes?.singleWhere(
|
|
(e) => e.level?.id == params.id,
|
|
orElse: () => NodeEntity(),
|
|
);
|
|
return findLevel?.level ?? LevelEntity();
|
|
} catch (e) {
|
|
throw MyException(errorMessage: '$e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<LevelEntity> getNextLevel({required QuestionParams params}) async {
|
|
try {
|
|
final String selectedLanguage = LocalStorage.readData(
|
|
key: MyConstants.selectLanguage) ?? MyConstants.defaultLanguage;
|
|
final int index = int.parse(
|
|
LocalStorage.readData(key: MyConstants.currentLevel) ?? '1');
|
|
final Box<TotalDataEntity> levelBox = Hive.box(MyConstants.levelBox);
|
|
final TotalDataEntity findData = levelBox.values.singleWhere(
|
|
(e) => e.code == selectedLanguage,
|
|
orElse: () => TotalDataEntity(),
|
|
);
|
|
if(index > (findData.nodes?.length ?? 0)){
|
|
throw MyException();
|
|
}
|
|
final NodeEntity? findLevel = findData.nodes?.singleWhere(
|
|
(e) => e.level?.order == index,
|
|
orElse: () => NodeEntity(),
|
|
);
|
|
return findLevel?.level ?? LevelEntity();
|
|
} catch (e) {
|
|
throw MyException(errorMessage: '$e');
|
|
}
|
|
}
|
|
}
|