|
1 | 1 | import 'package:flood_mobile/Blocs/filter_torrent_bloc/filter_torrent_bloc.dart';
|
2 | 2 | import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart';
|
| 3 | +import 'package:flood_mobile/Blocs/sort_by_torrent_bloc/sort_by_torrent_bloc.dart'; |
3 | 4 | import 'package:flood_mobile/Model/torrent_model.dart';
|
4 | 5 |
|
5 | 6 | bool isFilteredTorrent(
|
@@ -27,28 +28,148 @@ bool isFilteredTorrent(
|
27 | 28 | return true;
|
28 | 29 | }
|
29 | 30 | }
|
30 |
| - |
31 | 31 | return false;
|
32 | 32 | }
|
33 | 33 |
|
34 | 34 | int countDisplayTorrent(HomeScreenState model, FilterTorrentState filterModel) {
|
35 | 35 | int showTorrentCount = 0;
|
36 |
| - model.torrentList.forEach((torrent) { |
37 |
| - if (torrent.name |
38 |
| - .toLowerCase() |
39 |
| - .contains(filterModel.searchKeyword.toLowerCase()) && |
40 |
| - torrent.status |
41 |
| - .contains(filterModel.filterStatus.toString().split(".").last)) { |
42 |
| - showTorrentCount++; |
43 |
| - } else if (torrent.trackerURIs.contains(filterModel.trackerURISelected)) { |
44 |
| - showTorrentCount++; |
45 |
| - } else if (torrent.tags.contains(filterModel.tagSelected)) { |
46 |
| - showTorrentCount++; |
47 |
| - } else if (filterModel.filterStatus.toString().split(".").last == "all") { |
48 |
| - showTorrentCount++; |
49 |
| - } else if (torrent.tags.isEmpty && filterModel.tagSelected == "Untagged") { |
50 |
| - showTorrentCount++; |
51 |
| - } |
52 |
| - }); |
| 36 | + model.torrentList.forEach( |
| 37 | + (torrent) { |
| 38 | + if (torrent.name |
| 39 | + .toLowerCase() |
| 40 | + .contains(filterModel.searchKeyword.toLowerCase()) && |
| 41 | + torrent.status |
| 42 | + .contains(filterModel.filterStatus.toString().split(".").last)) { |
| 43 | + showTorrentCount++; |
| 44 | + } else if (torrent.trackerURIs.contains(filterModel.trackerURISelected)) { |
| 45 | + showTorrentCount++; |
| 46 | + } else if (torrent.tags.contains(filterModel.tagSelected)) { |
| 47 | + showTorrentCount++; |
| 48 | + } else if (filterModel.filterStatus.toString().split(".").last == "all") { |
| 49 | + showTorrentCount++; |
| 50 | + } else if (torrent.tags.isEmpty && |
| 51 | + filterModel.tagSelected == "Untagged") { |
| 52 | + showTorrentCount++; |
| 53 | + } |
| 54 | + }, |
| 55 | + ); |
53 | 56 | return showTorrentCount;
|
54 | 57 | }
|
| 58 | + |
| 59 | +List<TorrentModel> sortTorrents( |
| 60 | + {required List<TorrentModel> torrents, |
| 61 | + required SortByTorrentState sortState}) { |
| 62 | + switch (sortState.sortByStatus) { |
| 63 | + case SortByValue.name: |
| 64 | + torrents.sort((a, b) { |
| 65 | + if (sortState.nameDirection == SortByDirection.ascending) { |
| 66 | + return a.name.toLowerCase().compareTo(b.name.toLowerCase()); |
| 67 | + } else { |
| 68 | + return b.name.toLowerCase().compareTo(a.name.toLowerCase()); |
| 69 | + } |
| 70 | + }); |
| 71 | + break; |
| 72 | + case SortByValue.percentage: |
| 73 | + torrents.sort((a, b) { |
| 74 | + if (sortState.percentageDirection == SortByDirection.ascending) { |
| 75 | + return a.percentComplete.compareTo(b.percentComplete); |
| 76 | + } else { |
| 77 | + return b.percentComplete.compareTo(a.percentComplete); |
| 78 | + } |
| 79 | + }); |
| 80 | + break; |
| 81 | + case SortByValue.downloaded: |
| 82 | + torrents.sort((a, b) { |
| 83 | + if (sortState.downloadedDirection == SortByDirection.ascending) { |
| 84 | + return a.downTotal.compareTo(b.downTotal); |
| 85 | + } else { |
| 86 | + return b.downTotal.compareTo(a.downTotal); |
| 87 | + } |
| 88 | + }); |
| 89 | + break; |
| 90 | + case SortByValue.downSpeed: |
| 91 | + torrents.sort((a, b) { |
| 92 | + if (sortState.downSpeedDirection == SortByDirection.ascending) { |
| 93 | + return a.downRate.compareTo(b.downRate); |
| 94 | + } else { |
| 95 | + return b.downRate.compareTo(a.downRate); |
| 96 | + } |
| 97 | + }); |
| 98 | + break; |
| 99 | + case SortByValue.uploaded: |
| 100 | + torrents.sort((a, b) { |
| 101 | + if (sortState.uploadedDirection == SortByDirection.ascending) { |
| 102 | + return a.upTotal.compareTo(b.upTotal); |
| 103 | + } else { |
| 104 | + return b.upTotal.compareTo(a.upTotal); |
| 105 | + } |
| 106 | + }); |
| 107 | + break; |
| 108 | + case SortByValue.upSpeed: |
| 109 | + torrents.sort((a, b) { |
| 110 | + if (sortState.upSpeedDirection == SortByDirection.ascending) { |
| 111 | + return a.upRate.compareTo(b.upRate); |
| 112 | + } else { |
| 113 | + return b.upRate.compareTo(a.upRate); |
| 114 | + } |
| 115 | + }); |
| 116 | + break; |
| 117 | + case SortByValue.ratio: |
| 118 | + torrents.sort((a, b) { |
| 119 | + if (sortState.ratioDirection == SortByDirection.ascending) { |
| 120 | + return a.ratio.compareTo(b.ratio); |
| 121 | + } else { |
| 122 | + return b.ratio.compareTo(a.ratio); |
| 123 | + } |
| 124 | + }); |
| 125 | + break; |
| 126 | + case SortByValue.fileSize: |
| 127 | + torrents.sort((a, b) { |
| 128 | + if (sortState.fileSizeDirection == SortByDirection.ascending) { |
| 129 | + return a.sizeBytes.compareTo(b.sizeBytes); |
| 130 | + } else { |
| 131 | + return b.sizeBytes.compareTo(a.sizeBytes); |
| 132 | + } |
| 133 | + }); |
| 134 | + break; |
| 135 | + case SortByValue.peers: |
| 136 | + torrents.sort((a, b) { |
| 137 | + if (sortState.peersDirection == SortByDirection.ascending) { |
| 138 | + return a.peersTotal.compareTo(b.peersTotal); |
| 139 | + } else { |
| 140 | + return b.peersTotal.compareTo(a.peersTotal); |
| 141 | + } |
| 142 | + }); |
| 143 | + break; |
| 144 | + case SortByValue.seeds: |
| 145 | + torrents.sort((a, b) { |
| 146 | + if (sortState.seedsDirection == SortByDirection.ascending) { |
| 147 | + return a.seedsTotal.compareTo(b.seedsTotal); |
| 148 | + } else { |
| 149 | + return b.seedsTotal.compareTo(a.seedsTotal); |
| 150 | + } |
| 151 | + }); |
| 152 | + break; |
| 153 | + case SortByValue.dateAdded: |
| 154 | + torrents.sort((a, b) { |
| 155 | + if (sortState.dateAddedDirection == SortByDirection.ascending) { |
| 156 | + return a.dateAdded.compareTo(b.dateAdded); |
| 157 | + } else { |
| 158 | + return b.dateAdded.compareTo(a.dateAdded); |
| 159 | + } |
| 160 | + }); |
| 161 | + break; |
| 162 | + case SortByValue.dateCreated: |
| 163 | + torrents.sort((a, b) { |
| 164 | + if (sortState.dateCreatedDirection == SortByDirection.ascending) { |
| 165 | + return a.dateCreated.compareTo(b.dateCreated); |
| 166 | + } else { |
| 167 | + return b.dateCreated.compareTo(a.dateCreated); |
| 168 | + } |
| 169 | + }); |
| 170 | + break; |
| 171 | + default: |
| 172 | + throw Exception('Unsupported sort criteria'); |
| 173 | + } |
| 174 | + return torrents; |
| 175 | +} |
0 commit comments