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

211 lines
5.6 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;
2021-11-11 21:32:05 -05:00
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
Timer? sleepTimer;
@observable
var sleepHours = 0;
@observable
var sleepMinutes = 0;
@observable
var timeRemaining = const Duration();
2021-12-09 23:05:44 -05:00
@readonly
var _paused = false;
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;
controller?.runJavascript('document.getElementsByTagName("video")[0].muted = false;');
},
2022-01-25 20:02:16 -05:00
),
};
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();');
controller?.runJavascript('document.getElementsByTagName("video")[0].muted = false;');
} 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());
_overlayTimer.cancel();
_streamInfo = null;
_paused = true;
}
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"));');
} 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
}
/// Updates the sleep timer with [sleepHours] and [sleepMinutes].
/// Calls [onTimerFinished] when the sleep timer completes.
void updateSleepTimer({required void Function() onTimerFinished}) {
// If hours and minutes are 0, do nothing.
if (sleepHours == 0 && sleepMinutes == 0) return;
// If there is an ongoing timer, cancel it since it'll be replaced.
if (sleepTimer != null) cancelSleepTimer();
// Update the new time remaining
timeRemaining = Duration(hours: sleepHours, minutes: sleepMinutes);
// Reset the hours and minutes in the dropdown buttons.
sleepHours = 0;
sleepMinutes = 0;
// Set a periodic timer that will update the time remaining every second.
sleepTimer = Timer.periodic(
const Duration(seconds: 1),
(timer) {
// If the timer is up, cancel the timer and exit the app.
if (timeRemaining.inSeconds == 0) {
timer.cancel();
onTimerFinished();
return;
}
// Decrement the time remaining.
timeRemaining = Duration(seconds: timeRemaining.inSeconds - 1);
},
);
}
/// Cancels the sleep timer and resets the time remaining.
void cancelSleepTimer() {
sleepTimer?.cancel();
timeRemaining = const Duration();
}
2021-11-11 21:32:05 -05:00
}