Skip to content

Commit

Permalink
next thingy magic
Browse files Browse the repository at this point in the history
  • Loading branch information
Bonfra04 committed Jan 28, 2024
1 parent 2758b9a commit 77e165e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 66 deletions.
4 changes: 4 additions & 0 deletions lib/backend/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class Backend {
PeerManager.stopWatching();
}

static void watchNext(List<int> seasons) {
Storage.watchNext(seasons);
}

static void updateWatching(Watchable watchable, int time) {
Storage.updateWatching(watchable, time);
}
Expand Down
17 changes: 17 additions & 0 deletions lib/backend/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ final class Storage {
return Storage.keepWatching[Storage._lastId]!.startAt;
}

static void watchNext(List<int> seasons) {
SerialInfo serialInfo = Storage.keepWatching[Storage._lastId]!;
serialInfo.startAt = 0;
List<String> seasonXepisode = serialInfo.episode.split("x");
int season = int.parse(seasonXepisode[0]) - 1;
int episode = int.parse(seasonXepisode[1]) - 1;

if (episode + 1 < seasons[season])
serialInfo.episode = "${season + 1}x${episode + 2}";
else if (season + 1 < seasons.length)
serialInfo.episode = "${season + 2}x1";
else
throw Exception("No more episodes");

Storage.keepWatching[Storage._lastId!] = serialInfo;
}

static void removeWatching(String site, String siteUrl) {
String id = Storage._calcID(site, siteUrl);
Storage._keepWatchingList.remove(id);
Expand Down
133 changes: 67 additions & 66 deletions lib/components/player_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,68 +41,16 @@ class _PlayerControlsState extends State<PlayerControls> {
Watchable? _nextMedia;
late FocusScopeNode _focusNode;

KeyEventResult _handleKeyControls(RawKeyEvent event) {
if(FocusManager.instance.primaryFocus != this._focusNode)
return KeyEventResult.ignored;

if(event is! RawKeyDownEvent)
return KeyEventResult.ignored;

if(event.logicalKey == LogicalKeyboardKey.arrowUp) {
this._cancelAndRestartTimer();
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.arrowDown) {
super.setState(() => this._hidden = true);
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.select || event.logicalKey == LogicalKeyboardKey.space) {
this._playPause();
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.arrowLeft) {
this._cancelAndRestartTimer();
final position = this._controller.value.position;
final seekTo = position - const Duration(seconds: 10);
this._controller.seekTo(seekTo > Duration.zero ? seekTo : Duration.zero);
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.arrowRight) {
this._cancelAndRestartTimer();
final position = this._controller.value.position;
final seekTo = position + const Duration(seconds: 10);
this._controller.seekTo(seekTo < this._controller.value.duration ? seekTo : this._controller.value.duration);
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.keyM) {
this._cancelAndRestartTimer();

if (this._controller.value.volume == 0)
this._controller.setVolume(1.0);
else
this._controller.setVolume(0.0);

return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.keyF) {
this._onExpandCollapse();
return KeyEventResult.handled;
}

return KeyEventResult.ignored;
}

@override
void initState() {
super.initState();
this._initialize();
this._focusNode = FocusScopeNode();
}

@override
void dispose() {
this._dispose();
super.dispose();
}

@override
Expand Down Expand Up @@ -138,12 +86,6 @@ class _PlayerControlsState extends State<PlayerControls> {
);
}

@override
void dispose() {
this._dispose();
super.dispose();
}

Widget _buildChatDrawer(BuildContext context) {
return ChatDrawer(
shown: this._chatOpened,
Expand Down Expand Up @@ -199,7 +141,7 @@ class _PlayerControlsState extends State<PlayerControls> {
),
this._buildTitle(context),
const Spacer(),
// if (PeerManager.connected)
if (PeerManager.connected)
IconButton(
icon: const Icon(Icons.chat),
onPressed: () => super.setState(() =>
Expand Down Expand Up @@ -338,7 +280,8 @@ class _PlayerControlsState extends State<PlayerControls> {
}

void _onNextMedia() {
// TODO: backend shits
Backend.watchNext((this._currentMedia as Episode).series.seasons.map((e) => e.length).toList());

Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => MediaPage(playable: this._nextMedia!),
));
Expand All @@ -356,6 +299,7 @@ class _PlayerControlsState extends State<PlayerControls> {
}

Future<void> _initialize() async {
this._focusNode = FocusScopeNode();
this._controller = super.widget.controller;
this._controller.addListener(this._updateState);
this._updateState();
Expand Down Expand Up @@ -451,4 +395,61 @@ class _PlayerControlsState extends State<PlayerControls> {
void _updateState() {
super.setState(() => this._buffering = this._controller.value.isBuffering);
}

KeyEventResult _handleKeyControls(RawKeyEvent event) {
if(FocusManager.instance.primaryFocus != this._focusNode)
return KeyEventResult.ignored;

if(event is! RawKeyDownEvent)
return KeyEventResult.ignored;

if(event.logicalKey == LogicalKeyboardKey.arrowUp) {
this._cancelAndRestartTimer();
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.arrowDown) {
super.setState(() => this._hidden = true);
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.select || event.logicalKey == LogicalKeyboardKey.space) {
this._playPause();
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.arrowLeft) {
this._cancelAndRestartTimer();
final position = this._controller.value.position;
final seekTo = position - const Duration(seconds: 10);
this._controller.seekTo(seekTo > Duration.zero ? seekTo : Duration.zero);
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.arrowRight) {
this._cancelAndRestartTimer();
final position = this._controller.value.position;
final seekTo = position + const Duration(seconds: 10);
this._controller.seekTo(seekTo < this._controller.value.duration ? seekTo : this._controller.value.duration);
return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.keyM) {
this._cancelAndRestartTimer();

if (this._controller.value.volume == 0)
this._controller.setVolume(1.0);
else
this._controller.setVolume(0.0);

return KeyEventResult.handled;
}

if(event.logicalKey == LogicalKeyboardKey.keyF) {
this._onExpandCollapse();
return KeyEventResult.handled;
}

return KeyEventResult.ignored;
}
}

0 comments on commit 77e165e

Please sign in to comment.