-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create rating system hydrated bloc
- Loading branch information
Showing
15 changed files
with
603 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:hydrated_bloc/hydrated_bloc.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:rtu_mirea_app/rating_system_calculator/models/models.dart'; | ||
|
||
part 'rating_system_bloc.g.dart'; | ||
part 'rating_system_event.dart'; | ||
part 'rating_system_state.dart'; | ||
|
||
typedef Group = String; | ||
|
||
class RatingSystemBloc | ||
extends HydratedBloc<RatingSystemEvent, RatingSystemState> { | ||
RatingSystemBloc() : super(const RatingSystemState()) { | ||
on<UpdateSubjectsByCurrentSchedule>(_onUpdateSubjectsByCurrentSchedule); | ||
} | ||
|
||
void _onUpdateSubjectsByCurrentSchedule( | ||
UpdateSubjectsByCurrentSchedule event, | ||
Emitter<RatingSystemState> emit, | ||
) { | ||
final actualSubjects = event.subjects; | ||
|
||
final newSubjects = <(Group, Subject)>[]; | ||
|
||
for (final actualSubject in actualSubjects) { | ||
final index = state.subjects.indexWhere( | ||
(element) => | ||
element.$2.name == actualSubject.name && element.$1 == event.group, | ||
); | ||
|
||
if (index == -1) { | ||
newSubjects.add( | ||
(event.group, actualSubject), | ||
); | ||
} else { | ||
final subject = state.subjects[index]; | ||
|
||
if (actualSubject.mainScore == 0 && | ||
actualSubject.additionalScore == 0 && | ||
actualSubject.classScore == 0) { | ||
state.subjects.removeAt(index); | ||
} else { | ||
newSubjects.add(subject); | ||
} | ||
} | ||
} | ||
|
||
emit( | ||
state.copyWith( | ||
subjects: newSubjects, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
RatingSystemState? fromJson(Map<String, dynamic> json) => | ||
RatingSystemState.fromJson(json); | ||
|
||
@override | ||
Map<String, dynamic>? toJson(RatingSystemState state) => state.toJson(); | ||
} |
19 changes: 19 additions & 0 deletions
19
lib/rating_system_calculator/bloc/rating_system_bloc.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
lib/rating_system_calculator/bloc/rating_system_event.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
part of 'rating_system_bloc.dart'; | ||
|
||
abstract class RatingSystemEvent extends Equatable { | ||
const RatingSystemEvent(); | ||
} | ||
|
||
class UpdateSubjectsByCurrentSchedule extends RatingSystemEvent { | ||
const UpdateSubjectsByCurrentSchedule({ | ||
required this.group, | ||
required this.subjects, | ||
}); | ||
|
||
final Group group; | ||
final List<Subject> subjects; | ||
|
||
@override | ||
List<Object> get props => [group, subjects]; | ||
} |
55 changes: 55 additions & 0 deletions
55
lib/rating_system_calculator/bloc/rating_system_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
part of 'rating_system_bloc.dart'; | ||
|
||
@JsonSerializable() | ||
class RatingSystemState extends Equatable { | ||
const RatingSystemState({ | ||
this.subjects = const [], | ||
}); | ||
|
||
@SubjectsConverter() | ||
final List<(Group, Subject)> subjects; | ||
|
||
factory RatingSystemState.fromJson(Map<String, dynamic> json) => | ||
_$RatingSystemStateFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$RatingSystemStateToJson(this); | ||
|
||
RatingSystemState copyWith({ | ||
List<(Group, Subject)>? subjects, | ||
}) { | ||
return RatingSystemState( | ||
subjects: subjects ?? this.subjects, | ||
); | ||
} | ||
|
||
@override | ||
List<Object?> get props => [ | ||
subjects, | ||
]; | ||
} | ||
|
||
class SubjectsConverter | ||
implements JsonConverter<List<(Group, Subject)>, List<dynamic>> { | ||
const SubjectsConverter(); | ||
|
||
@override | ||
List<(Group, Subject)> fromJson(List<dynamic> json) { | ||
return json | ||
.map( | ||
(dynamic e) => (e['group'] as String, Subject.fromJson(e)), | ||
) | ||
.toList(); | ||
} | ||
|
||
@override | ||
List<dynamic> toJson(List<(Group, Subject)> object) { | ||
return object | ||
.map( | ||
(e) => { | ||
'group': e.$1, | ||
...e.$2.toJson(), | ||
}, | ||
) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'subject.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
part 'subject.g.dart'; | ||
|
||
@immutable | ||
@JsonSerializable() | ||
class Subject extends Equatable { | ||
const Subject({ | ||
required this.name, | ||
required this.dates, | ||
this.mainScore, | ||
this.additionalScore, | ||
this.classScore, | ||
this.visitedDays, | ||
}); | ||
|
||
/// {@macro from_json} | ||
factory Subject.fromJson(Map<String, dynamic> json) => | ||
_$SubjectFromJson(json); | ||
|
||
/// {@macro to_json} | ||
Map<String, dynamic> toJson() => _$SubjectToJson(this); | ||
|
||
/// Название предмета. | ||
final String name; | ||
|
||
/// Даты занятия. | ||
final List<DateTime> dates; | ||
|
||
/// Основные баллы. | ||
final double? mainScore; | ||
|
||
/// Дополнительные баллы. | ||
final double? additionalScore; | ||
|
||
/// Баллы за работу на занятиях. | ||
final double? classScore; | ||
|
||
/// Посещенные дни. | ||
final List<DateTime>? visitedDays; | ||
|
||
Subject copyWith({ | ||
String? name, | ||
List<DateTime>? dates, | ||
double? mainScore, | ||
double? additionalScore, | ||
double? classScore, | ||
List<DateTime>? visitedDays, | ||
}) { | ||
return Subject( | ||
name: name ?? this.name, | ||
dates: dates ?? this.dates, | ||
mainScore: mainScore ?? this.mainScore, | ||
additionalScore: additionalScore ?? this.additionalScore, | ||
classScore: classScore ?? this.classScore, | ||
visitedDays: visitedDays ?? this.visitedDays, | ||
); | ||
} | ||
|
||
@override | ||
List<Object?> get props => [ | ||
name, | ||
dates, | ||
mainScore, | ||
additionalScore, | ||
classScore, | ||
visitedDays, | ||
]; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
lib/rating_system_calculator/view/about_rating_system_page.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.