121 lines
2.9 KiB
Dart
121 lines
2.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:novyronst/common/api/video_request.dart';
|
|
import 'package:novyronst/common/model/ny_drama_model.dart';
|
|
import 'package:novyronst/pages/play/widgets/player_view.dart';
|
|
import 'package:novyronst/pages/widgets/ny_loading_widget.dart';
|
|
|
|
class PlayPage extends StatefulWidget {
|
|
const PlayPage({super.key, this.isActive = true});
|
|
|
|
final bool isActive;
|
|
|
|
@override
|
|
State<StatefulWidget> createState() {
|
|
return _RootPageState();
|
|
}
|
|
}
|
|
|
|
class _RootPageState extends State<PlayPage> {
|
|
final List<NyDramaModel> _videoList = [];
|
|
int _page = 1;
|
|
bool _isLoading = false;
|
|
bool _isLoadingMore = false;
|
|
bool _hasMore = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_getVideoList(refresh: true);
|
|
}
|
|
|
|
Future<void> _getVideoList({bool refresh = false}) async {
|
|
if (_isLoading || _isLoadingMore) return;
|
|
if (!refresh && !_hasMore) return;
|
|
|
|
final nextPage = refresh ? 1 : _page + 1;
|
|
setState(() {
|
|
if (refresh) {
|
|
_isLoading = true;
|
|
} else {
|
|
_isLoadingMore = true;
|
|
}
|
|
});
|
|
|
|
final value = await VideoRequest.getRecommandsList(page: nextPage);
|
|
if (!mounted) return;
|
|
|
|
setState(() {
|
|
if (refresh) {
|
|
_videoList
|
|
..clear()
|
|
..addAll(value);
|
|
} else {
|
|
_videoList.addAll(value);
|
|
}
|
|
|
|
_page = nextPage;
|
|
_hasMore = value.isNotEmpty;
|
|
_isLoading = false;
|
|
_isLoadingMore = false;
|
|
});
|
|
}
|
|
|
|
void _handlePageChanged(int index) {
|
|
if (index >= _videoList.length - 1) {
|
|
_getVideoList();
|
|
}
|
|
}
|
|
|
|
Future<void> _toggleCollect(int index) async {
|
|
if (index < 0 || index >= _videoList.length) return;
|
|
|
|
final item = _videoList[index];
|
|
final _info = item.videoInfo ?? {};
|
|
final videoId = _info['short_play_video_id'];
|
|
if (videoId == null) {
|
|
return;
|
|
}
|
|
final shortPlayId = _info['short_play_id'];
|
|
if (shortPlayId == null) {
|
|
return;
|
|
}
|
|
|
|
final success = item.isCollect == false
|
|
? await VideoRequest.collect(shortPlayId: shortPlayId ?? 0, videoId: videoId)
|
|
: await VideoRequest.cancelCollect(shortPlayId: shortPlayId ?? 0);
|
|
|
|
if (!mounted) return;
|
|
|
|
setState(() {
|
|
|
|
_videoList[index] = item.copyWith(isCollect: !item.isCollect!);
|
|
// item.isCollect = !item.isCollect!;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(backgroundColor: Colors.black, body: _buildBody());
|
|
}
|
|
|
|
Widget _buildBody() {
|
|
if (_isLoading && _videoList.isEmpty) {
|
|
return const NyLoadingWidget();
|
|
}
|
|
|
|
if (_videoList.isEmpty) {
|
|
return const Center(
|
|
child: Text('No videos', style: TextStyle(color: Colors.white70)),
|
|
);
|
|
}
|
|
|
|
return PlayerView(
|
|
videos: _videoList,
|
|
isActive: widget.isActive,
|
|
isLoadingMore: _isLoadingMore,
|
|
onPageChanged: _handlePageChanged,
|
|
onCollectTap: _toggleCollect,
|
|
);
|
|
}
|
|
}
|