-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
160 additions
and
161 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
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
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
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
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
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 |
---|---|---|
@@ -1,85 +1,57 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:application/controllers/sort_controller.dart'; | ||
import 'package:application/controllers/streamable_scroll.dart'; | ||
import 'package:application/dtos/anime_dto.dart'; | ||
import 'package:application/dtos/episode_mapping_dto.dart'; | ||
import 'package:application/dtos/season_dto.dart'; | ||
import 'package:application/utils/http_request.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class AnimeDetailsController { | ||
class AnimeDetailsController extends StreamableScroll<EpisodeMappingDto> { | ||
static AnimeDetailsController instance = AnimeDetailsController(); | ||
final episodes = <EpisodeMappingDto>[]; | ||
final scrollController = ScrollController(); | ||
final streamController = | ||
StreamController<List<EpisodeMappingDto>>.broadcast(); | ||
int page = 1; | ||
bool isLoading = false; | ||
bool canLoadMore = true; | ||
|
||
AnimeDto? anime; | ||
SeasonDto? season; | ||
|
||
Future<void> init() async { | ||
episodes.clear(); | ||
streamController.add(episodes); | ||
|
||
page = 1; | ||
isLoading = false; | ||
canLoadMore = true; | ||
await nextPage(); | ||
|
||
scrollController.addListener(() { | ||
// If the user is going to the end of the list | ||
final position = scrollController.position; | ||
|
||
if (position.pixels >= position.maxScrollExtent - 300 && | ||
!isLoading && | ||
canLoadMore) { | ||
nextPage(); | ||
} | ||
}); | ||
} | ||
|
||
void dispose() { | ||
anime = null; | ||
season = null; | ||
episodes.clear(); | ||
streamController.add(episodes); | ||
} | ||
|
||
Future<void> refresh() async { | ||
episodes.clear(); | ||
page = 1; | ||
isLoading = false; | ||
canLoadMore = true; | ||
await nextPage(); | ||
} | ||
|
||
Future<void> nextPage() async { | ||
@override | ||
Future<void> loadItems() async { | ||
if (isLoading) { | ||
return; | ||
} | ||
|
||
isLoading = true; | ||
|
||
try { | ||
final pageableDto = await HttpRequest.instance.getPage( | ||
'/v1/episode-mappings?anime=${anime?.uuid}${season != null ? '&season=${season!.number}' : ''}&${SortController.instance.sortType.value}&page=$page&limit=4', | ||
final pageableDto = await HttpRequest.instance.getPageWithParams( | ||
'/v1/episode-mappings', | ||
queryParameters: { | ||
'anime': anime?.uuid, | ||
'season': season?.number, | ||
'sort': SortController.instance.sortType.value, | ||
'page': page, | ||
'limit': 4, | ||
}, | ||
); | ||
|
||
episodes.addAll( | ||
addAll( | ||
pageableDto.data | ||
.map((e) => EpisodeMappingDto.fromJson(e as Map<String, dynamic>)), | ||
); | ||
|
||
streamController.add(episodes); | ||
canLoadMore = episodes.length < pageableDto.total; | ||
canLoadMore = items.length < pageableDto.total; | ||
} catch (e) { | ||
debugPrint(e.toString()); | ||
} finally { | ||
isLoading = false; | ||
page++; | ||
} | ||
} | ||
|
||
@override | ||
void dispose() { | ||
anime = null; | ||
season = null; | ||
super.dispose(); | ||
} | ||
} |
Oops, something went wrong.