Skip to content

Commit 7f88c25

Browse files
committed
refactor: watch history screen
1 parent 43bfe44 commit 7f88c25

8 files changed

+593
-513
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:another_iptv_player/database/database.dart';
3+
import 'package:another_iptv_player/models/content_type.dart';
4+
import 'package:another_iptv_player/models/playlist_content_model.dart';
5+
import 'package:another_iptv_player/models/watch_history.dart';
6+
import 'package:another_iptv_player/repositories/iptv_repository.dart';
7+
import 'package:another_iptv_player/services/app_state.dart';
8+
import 'package:another_iptv_player/services/watch_history_service.dart';
9+
import 'package:another_iptv_player/utils/navigate_by_content_type.dart';
10+
import '../services/service_locator.dart';
11+
import '../screens/series/episode_screen.dart';
12+
13+
class WatchHistoryController extends ChangeNotifier {
14+
late WatchHistoryService _historyService;
15+
late IptvRepository _repository;
16+
final _database = getIt<AppDatabase>();
17+
18+
List<WatchHistory> _continueWatching = [];
19+
List<WatchHistory> _recentlyWatched = [];
20+
List<WatchHistory> _liveHistory = [];
21+
List<WatchHistory> _movieHistory = [];
22+
List<WatchHistory> _seriesHistory = [];
23+
bool _isLoading = true;
24+
String? _errorMessage;
25+
26+
// Getters
27+
List<WatchHistory> get continueWatching => _continueWatching;
28+
List<WatchHistory> get recentlyWatched => _recentlyWatched;
29+
List<WatchHistory> get liveHistory => _liveHistory;
30+
List<WatchHistory> get movieHistory => _movieHistory;
31+
List<WatchHistory> get seriesHistory => _seriesHistory;
32+
bool get isLoading => _isLoading;
33+
String? get errorMessage => _errorMessage;
34+
35+
WatchHistoryController() {
36+
_historyService = WatchHistoryService();
37+
_repository = AppState.repository!;
38+
}
39+
40+
bool get isAllEmpty =>
41+
_continueWatching.isEmpty &&
42+
_recentlyWatched.isEmpty &&
43+
_liveHistory.isEmpty &&
44+
_movieHistory.isEmpty &&
45+
_seriesHistory.isEmpty;
46+
47+
Future<void> loadWatchHistory() async {
48+
_setLoading(true);
49+
_clearError();
50+
51+
final playlistId = AppState.currentPlaylist!.id;
52+
53+
try {
54+
final futures = await Future.wait([
55+
_historyService.getContinueWatching(playlistId),
56+
_historyService.getRecentlyWatched(limit: 20, playlistId),
57+
_historyService.getWatchHistoryByContentType(ContentType.liveStream, playlistId),
58+
_historyService.getWatchHistoryByContentType(ContentType.vod, playlistId),
59+
_historyService.getWatchHistoryByContentType(ContentType.series, playlistId),
60+
]);
61+
62+
_continueWatching = futures[0];
63+
_recentlyWatched = futures[1];
64+
_liveHistory = futures[2];
65+
_movieHistory = futures[3];
66+
_seriesHistory = futures[4];
67+
68+
_setLoading(false);
69+
} catch (e) {
70+
_setError('İzleme geçmişi yüklenirken hata oluştu: $e');
71+
_setLoading(false);
72+
}
73+
}
74+
75+
Future<void> playContent(BuildContext context, WatchHistory history) async {
76+
try {
77+
switch (history.contentType) {
78+
case ContentType.liveStream:
79+
await _playLiveStream(context, history);
80+
break;
81+
case ContentType.vod:
82+
await _playMovie(context, history);
83+
break;
84+
case ContentType.series:
85+
await _playSeries(context, history);
86+
break;
87+
}
88+
} catch (e) {
89+
_setError('Video oynatılırken hata oluştu: $e');
90+
}
91+
}
92+
93+
Future<void> removeHistory(WatchHistory history) async {
94+
try {
95+
await _historyService.deleteWatchHistory(history.playlistId, history.streamId);
96+
await loadWatchHistory();
97+
} catch (e) {
98+
_setError('Hata oluştu: $e');
99+
}
100+
}
101+
102+
Future<void> clearAllHistory() async {
103+
try {
104+
await _historyService.clearAllHistory();
105+
await loadWatchHistory();
106+
} catch (e) {
107+
_setError('Hata oluştu: $e');
108+
}
109+
}
110+
111+
// Private methods
112+
void _setLoading(bool loading) {
113+
_isLoading = loading;
114+
notifyListeners();
115+
}
116+
117+
void _setError(String error) {
118+
_errorMessage = error;
119+
notifyListeners();
120+
}
121+
122+
void _clearError() {
123+
_errorMessage = null;
124+
notifyListeners();
125+
}
126+
127+
Future<void> _playLiveStream(BuildContext context, WatchHistory history) async {
128+
final liveStream = await _database.findLiveStreamById(
129+
history.streamId,
130+
AppState.currentPlaylist!.id,
131+
);
132+
133+
navigateByContentType(
134+
context,
135+
ContentItem(
136+
history.streamId,
137+
history.title,
138+
history.imagePath ?? '',
139+
history.contentType,
140+
liveStream: liveStream,
141+
),
142+
);
143+
}
144+
145+
Future<void> _playMovie(BuildContext context, WatchHistory history) async {
146+
final movie = await _database.findMovieById(
147+
history.streamId,
148+
AppState.currentPlaylist!.id,
149+
);
150+
151+
navigateByContentType(
152+
context,
153+
ContentItem(
154+
history.streamId,
155+
history.title,
156+
history.imagePath ?? '',
157+
history.contentType,
158+
containerExtension: movie!.containerExtension,
159+
vodStream: movie,
160+
),
161+
);
162+
}
163+
164+
Future<void> _playSeries(BuildContext context, WatchHistory history) async {
165+
final episode = await _database.findEpisodesById(
166+
history.streamId,
167+
AppState.currentPlaylist!.id,
168+
);
169+
170+
final seriesResponse = await _repository.getSeriesInfo(episode!.seriesId);
171+
172+
Navigator.push(
173+
context,
174+
MaterialPageRoute(
175+
builder: (context) => EpisodeScreen(
176+
seriesInfo: seriesResponse!.seriesInfo,
177+
seasons: seriesResponse.seasons,
178+
episodes: seriesResponse.episodes,
179+
contentItem: ContentItem(
180+
episode.episodeId.toString(),
181+
history.title,
182+
history.imagePath ?? "",
183+
ContentType.series,
184+
containerExtension: episode.containerExtension,
185+
season: episode.season,
186+
),
187+
watchHistory: history,
188+
),
189+
),
190+
);
191+
}
192+
}

0 commit comments

Comments
 (0)