155 lines
4.5 KiB
Dart
155 lines
4.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/models/overview_data.dart';
|
|
import 'package:didvan/models/requests/studio.dart';
|
|
import 'package:didvan/models/studio_details_data.dart';
|
|
import 'package:didvan/providers/core_provider.dart';
|
|
import 'package:didvan/services/media/media.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
|
|
class StudioDetailsState extends CoreProvier {
|
|
final List<StudioDetailsData?> studios = [];
|
|
late int initialIndex;
|
|
late StudioRequestArgs args;
|
|
int _selectedDetailsIndex = 0;
|
|
bool isFetchingNewItem = false;
|
|
final List<int> relatedQueue = [];
|
|
bool currentTypeIsVideo = true;
|
|
|
|
int _currentIndex = 0;
|
|
int get currentIndex => _currentIndex;
|
|
|
|
int get selectedDetailsIndex => _selectedDetailsIndex;
|
|
set selectedDetailsIndex(int value) {
|
|
_selectedDetailsIndex = value;
|
|
if (value == 2) {
|
|
getRelatedContents();
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
StudioDetailsData get currentStudio {
|
|
try {
|
|
return studios[_currentIndex]!;
|
|
} catch (e) {
|
|
return studios[_currentIndex + 1]!;
|
|
}
|
|
}
|
|
|
|
Future<void> getStudioDetails(
|
|
int id, {
|
|
bool? isForward,
|
|
StudioRequestArgs? args,
|
|
}) async {
|
|
if (args != null) {
|
|
this.args = args;
|
|
}
|
|
if (isForward == null) {
|
|
_selectedDetailsIndex = 0;
|
|
appState = AppState.busy;
|
|
} else {
|
|
isFetchingNewItem = true;
|
|
notifyListeners();
|
|
}
|
|
final service = RequestService(RequestHelper.studioDetails(id, this.args));
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
studios.clear();
|
|
final result = service.result;
|
|
final studio = StudioDetailsData.fromJson(result['studio']);
|
|
await _handlePodcastPlayback(studio);
|
|
if (this.args.page == 0) {
|
|
studios.add(studio);
|
|
initialIndex = 0;
|
|
appState = AppState.idle;
|
|
return;
|
|
}
|
|
|
|
StudioDetailsData? prevStudio;
|
|
if (result['prevStudio'].isNotEmpty) {
|
|
prevStudio = StudioDetailsData.fromJson(result['prevStudio']);
|
|
}
|
|
|
|
StudioDetailsData? nextStudio;
|
|
if (result['nextStudio'].isNotEmpty) {
|
|
nextStudio = StudioDetailsData.fromJson(result['nextStudio']);
|
|
}
|
|
|
|
if (isForward == null) {
|
|
studios
|
|
.addAll(List.generate(max(studio.order - 2, 0), (index) => null));
|
|
if (prevStudio != null) {
|
|
studios.add(prevStudio);
|
|
}
|
|
studios.add(studio);
|
|
if (nextStudio != null) {
|
|
studios.add(nextStudio);
|
|
}
|
|
_currentIndex = initialIndex = studio.order - 1;
|
|
} else if (isForward) {
|
|
if (!exists(nextStudio) && nextStudio != null) {
|
|
studios.add(nextStudio);
|
|
}
|
|
_currentIndex++;
|
|
} else if (!isForward) {
|
|
if (!exists(prevStudio) && prevStudio != null) {
|
|
studios[_currentIndex - 2] = prevStudio;
|
|
}
|
|
_currentIndex--;
|
|
}
|
|
isFetchingNewItem = false;
|
|
appState = AppState.idle;
|
|
return;
|
|
}
|
|
//why? total page state shouldn't die!
|
|
if (isForward == null) {
|
|
appState = AppState.failed;
|
|
}
|
|
}
|
|
|
|
Future<void> _handlePodcastPlayback(StudioDetailsData studio) async {
|
|
if (args.type == 'podcast') {
|
|
MediaService.currentPodcast = studio;
|
|
MediaService.podcastPlaylistArgs = args;
|
|
await MediaService.handleAudioPlayback(
|
|
audioSource: studio.media,
|
|
isVoiceMessage: false,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> getRelatedContents() async {
|
|
if (currentStudio.relatedContents.isNotEmpty) return;
|
|
relatedQueue.add(currentStudio.id);
|
|
final service = RequestService(RequestHelper.tag(
|
|
ids: currentStudio.tags.map((tag) => tag.id).toList(),
|
|
itemId: currentStudio.id,
|
|
type: currentStudio.media.contains('iframe') ? 'video' : 'podcast',
|
|
));
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
final relateds = service.result['contents'];
|
|
for (var i = 0; i < relateds.length; i++) {
|
|
studios
|
|
.where((element) => element != null)
|
|
.firstWhere((element) => element!.id == currentStudio.id)!
|
|
.relatedContents
|
|
.add(OverviewData.fromJson(relateds[i]));
|
|
}
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
bool exists(StudioDetailsData? studio) =>
|
|
studios.any((r) => studio != null && r != null && r.id == studio.id);
|
|
|
|
void onCommentsChanged(int count) {
|
|
studios.firstWhere((studio) => studio?.id == currentStudio.id)!.comments =
|
|
count;
|
|
notifyListeners();
|
|
}
|
|
}
|