mirror of
https://github.com/R0m1k3/xtremobile.git
synced 2026-10-11 17:29:26 +02:00
307 lines
9.4 KiB
Dart
307 lines
9.4 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import '../../../core/models/playlist_config.dart';
|
|
import '../../../core/theme/app_colors.dart';
|
|
import '../../../core/theme/app_theme.dart';
|
|
import '../../../core/utils/responsive_layout.dart';
|
|
import '../../../core/widgets/components/hero_carousel.dart';
|
|
import '../../../core/widgets/components/ui_components.dart';
|
|
import '../providers/watch_history_provider.dart';
|
|
import '../models/xtream_models.dart';
|
|
import '../providers/xtream_provider.dart';
|
|
import '../providers/settings_provider.dart';
|
|
import '../screens/player_screen.dart';
|
|
|
|
class MoviesTab extends ConsumerStatefulWidget {
|
|
final PlaylistConfig playlist;
|
|
|
|
const MoviesTab({super.key, required this.playlist});
|
|
|
|
@override
|
|
ConsumerState<MoviesTab> createState() => _MoviesTabState();
|
|
}
|
|
|
|
class _MoviesTabState extends ConsumerState<MoviesTab> {
|
|
final ScrollController _scrollController = ScrollController();
|
|
final TextEditingController _searchController = TextEditingController();
|
|
final List<Movie> _movies = [];
|
|
List<Movie>? _searchResults;
|
|
String _searchQuery = '';
|
|
bool _isLoading = false;
|
|
bool _isSearching = false;
|
|
bool _hasMore = true;
|
|
int _currentOffset = 0;
|
|
static const int _pageSize = 100;
|
|
Timer? _searchDebounce;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadMoreMovies();
|
|
_scrollController.addListener(_onScroll);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_scrollController.dispose();
|
|
_searchController.dispose();
|
|
_searchDebounce?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onScroll() {
|
|
if (_scrollController.position.pixels >=
|
|
_scrollController.position.maxScrollExtent * 0.8) {
|
|
_loadMoreMovies();
|
|
}
|
|
}
|
|
|
|
void _onSearchChanged(String query) {
|
|
_searchDebounce?.cancel();
|
|
|
|
setState(() {
|
|
_searchQuery = query;
|
|
if (query.isEmpty) {
|
|
_searchResults = null;
|
|
_isSearching = false;
|
|
}
|
|
});
|
|
|
|
if (query.length >= 2) {
|
|
_searchDebounce = Timer(const Duration(milliseconds: 500), () {
|
|
_performSearch(query);
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _performSearch(String query) async {
|
|
if (query.isEmpty) return;
|
|
|
|
setState(() => _isSearching = true);
|
|
|
|
try {
|
|
final service = ref.read(xtreamServiceProvider(widget.playlist));
|
|
final results = await service.searchMovies(query);
|
|
|
|
if (mounted && _searchQuery == query) {
|
|
setState(() {
|
|
_searchResults = results;
|
|
_isSearching = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
setState(() => _isSearching = false);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _loadMoreMovies() async {
|
|
if (_isLoading || !_hasMore) return;
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
try {
|
|
final service = ref.read(xtreamServiceProvider(widget.playlist));
|
|
final newMovies = await service.getMoviesPaginated(
|
|
offset: _currentOffset,
|
|
limit: _pageSize,
|
|
);
|
|
|
|
setState(() {
|
|
_movies.addAll(newMovies);
|
|
_currentOffset += _pageSize;
|
|
_hasMore = newMovies.length == _pageSize;
|
|
_isLoading = false;
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Failed to load movies: $e')),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
void _playMovie(Movie movie) {
|
|
ref.read(watchHistoryProvider.notifier).markMovieWatched(movie.streamId);
|
|
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => PlayerScreen(
|
|
streamId: movie.streamId,
|
|
title: movie.name,
|
|
playlist: widget.playlist,
|
|
streamType: StreamType.vod,
|
|
containerExtension: movie.containerExtension ?? 'mp4',
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final settings = ref.watch(iptvSettingsProvider);
|
|
final watchHistory = ref.watch(watchHistoryProvider);
|
|
|
|
List<Movie> displayMovies;
|
|
if (_searchQuery.isNotEmpty && _searchResults != null) {
|
|
displayMovies = _searchResults!;
|
|
} else {
|
|
displayMovies = settings.moviesKeywords.isEmpty
|
|
? _movies
|
|
: _movies.where((m) => settings.matchesMoviesFilter(m.categoryName)).toList();
|
|
}
|
|
|
|
// Hero Items (Take 5 random or first 5)
|
|
final heroItems = _movies.take(5).map((m) => HeroItem(
|
|
id: m.streamId,
|
|
title: m.name,
|
|
imageUrl: m.streamIcon ?? '',
|
|
subtitle: m.rating != null ? '${m.rating} ★' : null,
|
|
onMoreInfo: () {
|
|
_playMovie(m);
|
|
},
|
|
)).toList();
|
|
|
|
if (_movies.isEmpty && !_isLoading) {
|
|
return const Center(child: Text('No movies available'));
|
|
}
|
|
|
|
final double gridItemRatio = 0.65;
|
|
final int crossAxisCount = ResponsiveLayout.value(
|
|
context,
|
|
mobile: 3,
|
|
tablet: 5,
|
|
desktop: 7,
|
|
);
|
|
|
|
return CustomScrollView(
|
|
controller: _scrollController,
|
|
slivers: [
|
|
// Header
|
|
SliverToBoxAdapter(
|
|
child: Container(
|
|
padding: const EdgeInsets.fromLTRB(24, 16, 24, 8),
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
'Movies',
|
|
style: Theme.of(context).textTheme.headlineMedium,
|
|
),
|
|
const Spacer(),
|
|
Container(
|
|
width: 300,
|
|
height: 40,
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surface,
|
|
borderRadius: BorderRadius.circular(AppTheme.radiusFull),
|
|
border: Border.all(color: AppColors.border),
|
|
),
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: Row(
|
|
children: [
|
|
const Icon(Icons.search, size: 20, color: AppColors.textSecondary),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _searchController,
|
|
style: const TextStyle(fontSize: 14, color: AppColors.textPrimary),
|
|
decoration: const InputDecoration(
|
|
hintText: 'Search',
|
|
border: InputBorder.none,
|
|
enabledBorder: InputBorder.none,
|
|
focusedBorder: InputBorder.none,
|
|
contentPadding: EdgeInsets.only(bottom: 12),
|
|
isDense: true,
|
|
),
|
|
onChanged: _onSearchChanged,
|
|
),
|
|
),
|
|
if (_isSearching)
|
|
const SizedBox(width: 12, height: 12, child: CircularProgressIndicator(strokeWidth: 2)),
|
|
if (_searchQuery.isNotEmpty)
|
|
GestureDetector(
|
|
onTap: () {
|
|
_searchController.clear();
|
|
_onSearchChanged('');
|
|
},
|
|
child: const Icon(Icons.close, size: 16, color: AppColors.textSecondary),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
// Hero Carousel
|
|
if (_searchQuery.isEmpty && heroItems.isNotEmpty)
|
|
SliverToBoxAdapter(
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(bottom: 24),
|
|
child: HeroCarousel(
|
|
items: heroItems,
|
|
onTap: (item) {
|
|
final movie = _movies.firstWhere((element) => element.streamId == item.id);
|
|
_playMovie(movie);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
|
|
// Grid
|
|
SliverPadding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
|
|
sliver: SliverGrid(
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: crossAxisCount,
|
|
crossAxisSpacing: 24,
|
|
mainAxisSpacing: 32,
|
|
childAspectRatio: gridItemRatio,
|
|
),
|
|
delegate: SliverChildBuilderDelegate(
|
|
(context, index) {
|
|
if (index >= displayMovies.length) return null;
|
|
final movie = displayMovies[index];
|
|
final isWatched = watchHistory.isMovieWatched(movie.streamId);
|
|
|
|
return MediaCard(
|
|
title: movie.name,
|
|
imageUrl: movie.streamIcon,
|
|
subtitle: movie.rating != null ? '${movie.rating} ★' : null,
|
|
rating: movie.rating,
|
|
isWatched: isWatched,
|
|
onTap: () => _playMovie(movie),
|
|
onLongPress: () {
|
|
ref.read(watchHistoryProvider.notifier).toggleMovieWatched(movie.streamId);
|
|
},
|
|
);
|
|
},
|
|
childCount: displayMovies.length,
|
|
),
|
|
),
|
|
),
|
|
|
|
// Loader
|
|
if (_isLoading)
|
|
const SliverToBoxAdapter(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(24),
|
|
child: Center(child: CircularProgressIndicator()),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|