frosty/lib/screens/channel/stores/video_store.dart

153 lines
4 KiB
Dart
Raw Normal View History

2021-11-11 21:32:05 -05:00
import 'dart:async';
import 'package:flutter/foundation.dart';
2022-01-07 16:14:59 -05:00
import 'package:flutter/services.dart';
import 'package:frosty/api/twitch_api.dart';
import 'package:frosty/core/auth/auth_store.dart';
import 'package:frosty/models/stream.dart';
import 'package:frosty/screens/settings/stores/settings_store.dart';
2021-11-11 21:32:05 -05:00
import 'package:mobx/mobx.dart';
import 'package:webview_flutter/webview_flutter.dart';
part 'video_store.g.dart';
class VideoStore = _VideoStoreBase with _$VideoStore;
abstract class _VideoStoreBase with Store {
final TwitchApi twitchApi;
WebViewController? controller;
2021-11-11 21:32:05 -05:00
2022-01-25 20:02:16 -05:00
late Timer _overlayTimer;
2021-11-11 21:32:05 -05:00
2021-12-09 23:05:44 -05:00
@readonly
2022-01-23 20:13:48 -05:00
var _paused = true;
2021-11-11 21:32:05 -05:00
2022-01-23 20:13:48 -05:00
@readonly
var _overlayVisible = true;
2021-11-11 21:32:05 -05:00
2021-12-09 23:05:44 -05:00
@readonly
StreamTwitch? _streamInfo;
2022-01-23 20:13:48 -05:00
@computed
String get videoUrl => settingsStore.showOverlay
? 'https://player.twitch.tv/?channel=$userLogin&controls=false&muted=false&parent=frosty'
: 'https://player.twitch.tv/?channel=$userLogin&muted=false&parent=frosty';
2022-01-25 20:02:16 -05:00
late final javascriptChannels = {
JavascriptChannel(
name: 'Pause',
onMessageReceived: (message) => _paused = true,
),
JavascriptChannel(
name: 'Play',
onMessageReceived: (message) => _paused = false,
),
};
2022-01-23 20:13:48 -05:00
final String userLogin;
final AuthStore authStore;
final SettingsStore settingsStore;
_VideoStoreBase({
required this.userLogin,
2022-01-25 20:02:16 -05:00
required this.twitchApi,
required this.authStore,
required this.settingsStore,
}) {
2022-01-25 20:02:16 -05:00
_overlayTimer = Timer(const Duration(seconds: 3), () => _overlayVisible = false);
updateStreamInfo();
}
@action
2021-11-11 21:32:05 -05:00
void handlePausePlay() {
try {
2022-01-23 20:13:48 -05:00
if (_paused) {
controller?.runJavascript('document.getElementsByTagName("video")[0].play();');
} else {
controller?.runJavascript('document.getElementsByTagName("video")[0].pause();');
}
2021-11-13 03:44:32 -05:00
2022-01-23 20:13:48 -05:00
_paused = !_paused;
} catch (e) {
debugPrint(e.toString());
}
2021-11-11 21:32:05 -05:00
}
@action
void handleVideoTap() {
2022-01-25 20:02:16 -05:00
_overlayTimer.cancel();
2022-01-25 20:02:16 -05:00
if (_overlayVisible) {
_overlayVisible = false;
} else {
2022-01-25 20:02:16 -05:00
updateStreamInfo();
2021-12-09 23:05:44 -05:00
_overlayVisible = true;
2022-01-25 20:02:16 -05:00
_overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
}
}
@action
Future<void> updateStreamInfo() async {
2022-01-15 13:37:43 -05:00
try {
2022-01-25 20:02:16 -05:00
_streamInfo = await twitchApi.getStream(userLogin: userLogin, headers: authStore.headersTwitch);
2022-01-15 13:37:43 -05:00
} catch (e) {
debugPrint(e.toString());
}
2021-11-11 21:32:05 -05:00
}
@action
void handleExpand() {
settingsStore.expandInfo = !settingsStore.expandInfo;
2022-01-25 20:02:16 -05:00
_overlayTimer.cancel();
_overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
}
2022-01-23 20:13:48 -05:00
@action
2022-01-25 20:02:16 -05:00
Future<void> handleToggleOverlay() async {
2022-01-23 20:13:48 -05:00
if (settingsStore.toggleableOverlay) {
settingsStore.showOverlay = !settingsStore.showOverlay;
2022-01-25 20:02:16 -05:00
await controller?.loadUrl(videoUrl);
if (settingsStore.showOverlay) {
_overlayVisible = true;
_overlayTimer.cancel();
_overlayTimer = Timer(const Duration(seconds: 3), () => _overlayVisible = false);
}
2022-01-23 20:13:48 -05:00
}
}
2022-01-25 20:02:16 -05:00
void handleRefresh() {
2022-01-07 16:14:59 -05:00
HapticFeedback.lightImpact();
controller?.reload();
}
2022-01-23 20:13:48 -05:00
FutureOr<NavigationDecision> handleNavigation(NavigationRequest navigation) {
if (navigation.url.startsWith('https://player.twitch.tv')) {
return NavigationDecision.navigate;
}
return NavigationDecision.prevent;
}
2021-11-14 04:25:33 -05:00
void initVideo() {
try {
controller?.runJavascript('document.getElementsByTagName("video")[0].addEventListener("pause", () => Pause.postMessage("video paused"));');
controller?.runJavascript('document.getElementsByTagName("video")[0].addEventListener("play", () => Play.postMessage("video playing"));');
controller?.runJavascript('document.getElementsByTagName("video")[0].muted = false;');
} catch (e) {
debugPrint(e.toString());
}
2021-11-11 21:32:05 -05:00
}
2021-12-09 23:05:44 -05:00
void requestPictureInPicture() {
try {
controller?.runJavascript('document.getElementsByTagName("video")[0].requestPictureInPicture();');
} catch (e) {
debugPrint(e.toString());
}
2021-11-11 21:32:05 -05:00
}
}