Skip to content

Commit ce44bfc

Browse files
committed
Implemented the bloc for sorting by features.
1 parent f4520df commit ce44bfc

File tree

3 files changed

+369
-0
lines changed

3 files changed

+369
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import 'package:bloc/bloc.dart';
2+
import 'package:equatable/equatable.dart';
3+
4+
part 'sort_by_torrent_event.dart';
5+
part 'sort_by_torrent_state.dart';
6+
7+
class SortByTorrentBloc extends Bloc<SortByTorrentEvent, SortByTorrentState> {
8+
SortByTorrentBloc() : super(SortByTorrentState()) {
9+
on<SetNameDirectionEvent>((event, emit) {
10+
_setNameDirectionEvent(event, emit);
11+
});
12+
13+
on<SetPercentageDirectionEvent>((event, emit) {
14+
_setPercentageDirectionEvent(event, emit);
15+
});
16+
17+
on<SetDownloadedDirectionEvent>((event, emit) {
18+
_setDownloadedDirectionEvent(event, emit);
19+
});
20+
21+
on<SetDownSpeedDirectionEvent>((event, emit) {
22+
_setDownSpeedDirectionEvent(event, emit);
23+
});
24+
25+
on<SetUploadedDirectionEvent>((event, emit) {
26+
_setUploadedDirectionEvent(event, emit);
27+
});
28+
29+
on<SetUpSpeedDirectionEvent>((event, emit) {
30+
_setUpSpeedDirectionEvent(event, emit);
31+
});
32+
33+
on<SetRatioDirectionEvent>((event, emit) {
34+
_setRatioDirectionEvent(event, emit);
35+
});
36+
37+
on<SetFileSizeDirectionEvent>((event, emit) {
38+
_setFileSizeDirectionEvent(event, emit);
39+
});
40+
41+
on<SetPeersDirectionEvent>((event, emit) {
42+
_setPeersDirectionEvent(event, emit);
43+
});
44+
45+
on<SetSeedsDirectionEvent>((event, emit) {
46+
_setSeedsDirectionEvent(event, emit);
47+
});
48+
49+
on<SetDateAddedDirectionEvent>((event, emit) {
50+
_setDateAddedDirectionEvent(event, emit);
51+
});
52+
53+
on<SetDateCreatedDirectionEvent>((event, emit) {
54+
_setDateCreatedDirectionEvent(event, emit);
55+
});
56+
}
57+
58+
void _setNameDirectionEvent(
59+
SetNameDirectionEvent event, Emitter<SortByTorrentState> emit) {
60+
emit(state.copyWith(
61+
nameDirection: event.nameDirection,
62+
sortByStatus: SortByValue.name,
63+
));
64+
}
65+
66+
void _setPercentageDirectionEvent(
67+
SetPercentageDirectionEvent event, Emitter<SortByTorrentState> emit) {
68+
emit(state.copyWith(
69+
percentageDirection: event.percentageDirection,
70+
sortByStatus: SortByValue.percentage,
71+
));
72+
}
73+
74+
void _setDownloadedDirectionEvent(
75+
SetDownloadedDirectionEvent event, Emitter<SortByTorrentState> emit) {
76+
emit(state.copyWith(
77+
downloadedDirection: event.downloadedDirection,
78+
sortByStatus: SortByValue.downloaded,
79+
));
80+
}
81+
82+
void _setDownSpeedDirectionEvent(
83+
SetDownSpeedDirectionEvent event, Emitter<SortByTorrentState> emit) {
84+
emit(state.copyWith(
85+
downSpeedDirection: event.downSpeedDirection,
86+
sortByStatus: SortByValue.downSpeed,
87+
));
88+
}
89+
90+
void _setUploadedDirectionEvent(
91+
SetUploadedDirectionEvent event, Emitter<SortByTorrentState> emit) {
92+
emit(state.copyWith(
93+
uploadedDirection: event.uploadedDirection,
94+
sortByStatus: SortByValue.uploaded,
95+
));
96+
}
97+
98+
void _setUpSpeedDirectionEvent(
99+
SetUpSpeedDirectionEvent event, Emitter<SortByTorrentState> emit) {
100+
emit(state.copyWith(
101+
upSpeedDirection: event.upSpeedDirection,
102+
sortByStatus: SortByValue.upSpeed,
103+
));
104+
}
105+
106+
void _setRatioDirectionEvent(
107+
SetRatioDirectionEvent event, Emitter<SortByTorrentState> emit) {
108+
emit(state.copyWith(
109+
ratioDirection: event.ratioDirection,
110+
sortByStatus: SortByValue.ratio,
111+
));
112+
}
113+
114+
void _setFileSizeDirectionEvent(
115+
SetFileSizeDirectionEvent event, Emitter<SortByTorrentState> emit) {
116+
emit(state.copyWith(
117+
fileSizeDirection: event.fileSizeDirection,
118+
sortByStatus: SortByValue.fileSize,
119+
));
120+
}
121+
122+
void _setPeersDirectionEvent(
123+
SetPeersDirectionEvent event, Emitter<SortByTorrentState> emit) {
124+
emit(state.copyWith(
125+
peersDirection: event.peersDirection, sortByStatus: SortByValue.peers));
126+
}
127+
128+
void _setSeedsDirectionEvent(
129+
SetSeedsDirectionEvent event, Emitter<SortByTorrentState> emit) {
130+
emit(state.copyWith(
131+
seedsDirection: event.seedsDirection,
132+
sortByStatus: SortByValue.seeds,
133+
));
134+
}
135+
136+
void _setDateAddedDirectionEvent(
137+
SetDateAddedDirectionEvent event, Emitter<SortByTorrentState> emit) {
138+
emit(state.copyWith(
139+
dateAddedDirection: event.dateAddedDirection,
140+
sortByStatus: SortByValue.dateAdded,
141+
));
142+
}
143+
144+
void _setDateCreatedDirectionEvent(
145+
SetDateCreatedDirectionEvent event, Emitter<SortByTorrentState> emit) {
146+
emit(state.copyWith(
147+
dateCreatedDirection: event.dateCreatedDirection,
148+
sortByStatus: SortByValue.dateCreated,
149+
));
150+
}
151+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
part of 'sort_by_torrent_bloc.dart';
2+
3+
abstract class SortByTorrentEvent extends Equatable {
4+
const SortByTorrentEvent();
5+
6+
@override
7+
List<Object?> get props => [];
8+
}
9+
10+
class SetNameDirectionEvent extends SortByTorrentEvent {
11+
final SortByDirection nameDirection;
12+
13+
SetNameDirectionEvent({required this.nameDirection});
14+
15+
@override
16+
List<Object?> get props => [nameDirection];
17+
}
18+
19+
class SetPercentageDirectionEvent extends SortByTorrentEvent {
20+
final SortByDirection percentageDirection;
21+
22+
SetPercentageDirectionEvent({required this.percentageDirection});
23+
24+
@override
25+
List<Object?> get props => [percentageDirection];
26+
}
27+
28+
class SetDownloadedDirectionEvent extends SortByTorrentEvent {
29+
final SortByDirection downloadedDirection;
30+
31+
SetDownloadedDirectionEvent({required this.downloadedDirection});
32+
33+
@override
34+
List<Object?> get props => [downloadedDirection];
35+
}
36+
37+
class SetDownSpeedDirectionEvent extends SortByTorrentEvent {
38+
final SortByDirection downSpeedDirection;
39+
40+
SetDownSpeedDirectionEvent({required this.downSpeedDirection});
41+
42+
@override
43+
List<Object?> get props => [downSpeedDirection];
44+
}
45+
46+
class SetUploadedDirectionEvent extends SortByTorrentEvent {
47+
final SortByDirection uploadedDirection;
48+
49+
SetUploadedDirectionEvent({required this.uploadedDirection});
50+
51+
@override
52+
List<Object?> get props => [uploadedDirection];
53+
}
54+
55+
class SetUpSpeedDirectionEvent extends SortByTorrentEvent {
56+
final SortByDirection upSpeedDirection;
57+
58+
SetUpSpeedDirectionEvent({required this.upSpeedDirection});
59+
60+
@override
61+
List<Object?> get props => [upSpeedDirection];
62+
}
63+
64+
class SetRatioDirectionEvent extends SortByTorrentEvent {
65+
final SortByDirection ratioDirection;
66+
67+
SetRatioDirectionEvent({required this.ratioDirection});
68+
69+
@override
70+
List<Object?> get props => [ratioDirection];
71+
}
72+
73+
class SetFileSizeDirectionEvent extends SortByTorrentEvent {
74+
final SortByDirection fileSizeDirection;
75+
76+
SetFileSizeDirectionEvent({required this.fileSizeDirection});
77+
78+
@override
79+
List<Object?> get props => [fileSizeDirection];
80+
}
81+
82+
class SetPeersDirectionEvent extends SortByTorrentEvent {
83+
final SortByDirection peersDirection;
84+
85+
SetPeersDirectionEvent({required this.peersDirection});
86+
87+
@override
88+
List<Object?> get props => [peersDirection];
89+
}
90+
91+
class SetSeedsDirectionEvent extends SortByTorrentEvent {
92+
final SortByDirection seedsDirection;
93+
94+
SetSeedsDirectionEvent({required this.seedsDirection});
95+
96+
@override
97+
List<Object?> get props => [seedsDirection];
98+
}
99+
100+
class SetDateAddedDirectionEvent extends SortByTorrentEvent {
101+
final SortByDirection dateAddedDirection;
102+
103+
SetDateAddedDirectionEvent({required this.dateAddedDirection});
104+
105+
@override
106+
List<Object?> get props => [dateAddedDirection];
107+
}
108+
109+
class SetDateCreatedDirectionEvent extends SortByTorrentEvent {
110+
final SortByDirection dateCreatedDirection;
111+
112+
SetDateCreatedDirectionEvent({required this.dateCreatedDirection});
113+
114+
@override
115+
List<Object?> get props => [dateCreatedDirection];
116+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
part of 'sort_by_torrent_bloc.dart';
2+
3+
enum SortByValue {
4+
name,
5+
percentage,
6+
downloaded,
7+
downSpeed,
8+
uploaded,
9+
upSpeed,
10+
ratio,
11+
fileSize,
12+
peers,
13+
seeds,
14+
dateAdded,
15+
dateCreated,
16+
}
17+
18+
enum SortByDirection {
19+
ascending,
20+
descending,
21+
}
22+
23+
class SortByTorrentState extends Equatable {
24+
final SortByValue sortByStatus;
25+
final SortByDirection nameDirection;
26+
final SortByDirection percentageDirection;
27+
final SortByDirection downloadedDirection;
28+
final SortByDirection downSpeedDirection;
29+
final SortByDirection uploadedDirection;
30+
final SortByDirection upSpeedDirection;
31+
final SortByDirection ratioDirection;
32+
final SortByDirection fileSizeDirection;
33+
final SortByDirection peersDirection;
34+
final SortByDirection seedsDirection;
35+
final SortByDirection dateAddedDirection;
36+
final SortByDirection dateCreatedDirection;
37+
38+
const SortByTorrentState({
39+
this.sortByStatus = SortByValue.name,
40+
this.nameDirection = SortByDirection.ascending,
41+
this.percentageDirection = SortByDirection.ascending,
42+
this.downloadedDirection = SortByDirection.ascending,
43+
this.downSpeedDirection = SortByDirection.ascending,
44+
this.uploadedDirection = SortByDirection.ascending,
45+
this.upSpeedDirection = SortByDirection.ascending,
46+
this.ratioDirection = SortByDirection.ascending,
47+
this.fileSizeDirection = SortByDirection.ascending,
48+
this.peersDirection = SortByDirection.ascending,
49+
this.seedsDirection = SortByDirection.ascending,
50+
this.dateAddedDirection = SortByDirection.ascending,
51+
this.dateCreatedDirection = SortByDirection.ascending,
52+
});
53+
54+
SortByTorrentState copyWith({
55+
SortByValue? sortByStatus,
56+
SortByDirection? nameDirection,
57+
SortByDirection? percentageDirection,
58+
SortByDirection? downloadedDirection,
59+
SortByDirection? downSpeedDirection,
60+
SortByDirection? uploadedDirection,
61+
SortByDirection? upSpeedDirection,
62+
SortByDirection? ratioDirection,
63+
SortByDirection? fileSizeDirection,
64+
SortByDirection? peersDirection,
65+
SortByDirection? seedsDirection,
66+
SortByDirection? dateAddedDirection,
67+
SortByDirection? dateCreatedDirection,
68+
}) {
69+
return SortByTorrentState(
70+
sortByStatus: sortByStatus ?? this.sortByStatus,
71+
nameDirection: nameDirection ?? this.nameDirection,
72+
percentageDirection: percentageDirection ?? this.percentageDirection,
73+
downloadedDirection: downloadedDirection ?? this.downloadedDirection,
74+
downSpeedDirection: downSpeedDirection ?? this.downSpeedDirection,
75+
uploadedDirection: uploadedDirection ?? this.uploadedDirection,
76+
upSpeedDirection: upSpeedDirection ?? this.upSpeedDirection,
77+
ratioDirection: ratioDirection ?? this.ratioDirection,
78+
fileSizeDirection: fileSizeDirection ?? this.fileSizeDirection,
79+
peersDirection: peersDirection ?? this.peersDirection,
80+
seedsDirection: seedsDirection ?? this.seedsDirection,
81+
dateAddedDirection: dateAddedDirection ?? this.dateAddedDirection,
82+
dateCreatedDirection: dateCreatedDirection ?? this.dateCreatedDirection,
83+
);
84+
}
85+
86+
@override
87+
List<Object> get props => [
88+
sortByStatus,
89+
nameDirection,
90+
percentageDirection,
91+
downloadedDirection,
92+
downSpeedDirection,
93+
uploadedDirection,
94+
upSpeedDirection,
95+
ratioDirection,
96+
fileSizeDirection,
97+
peersDirection,
98+
seedsDirection,
99+
dateAddedDirection,
100+
dateCreatedDirection
101+
];
102+
}

0 commit comments

Comments
 (0)