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

117 lines
3 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
2021-12-09 23:05:44 -05:00
late Timer overlayTimer;
late Timer updateTimer;
2021-11-11 21:32:05 -05:00
2021-12-09 23:05:44 -05:00
@readonly
var _overlayVisible = true;
2021-11-11 21:32:05 -05:00
@observable
var paused = true;
2021-11-11 21:32:05 -05:00
2021-12-09 23:05:44 -05:00
@readonly
StreamTwitch? _streamInfo;
final String userLogin;
final AuthStore authStore;
final SettingsStore settingsStore;
_VideoStoreBase({
required this.twitchApi,
required this.userLogin,
required this.authStore,
required this.settingsStore,
}) {
overlayTimer = Timer(const Duration(seconds: 3), () => _overlayVisible = false);
updateStreamInfo();
}
@action
2021-11-11 21:32:05 -05:00
void handlePausePlay() {
try {
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
paused = !paused;
} catch (e) {
debugPrint(e.toString());
}
2021-11-11 21:32:05 -05:00
}
@action
void handleVideoTap() {
if (_overlayVisible) {
2021-12-09 23:05:44 -05:00
overlayTimer.cancel();
_overlayVisible = false;
} else {
2021-12-09 23:05:44 -05:00
overlayTimer.cancel();
overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
2021-12-09 23:05:44 -05:00
_overlayVisible = true;
updateStreamInfo();
}
}
@action
Future<void> updateStreamInfo() async {
2022-01-15 13:37:43 -05:00
try {
final updatedStreamInfo = await twitchApi.getStream(userLogin: userLogin, headers: authStore.headersTwitch);
2021-12-09 23:05:44 -05:00
_streamInfo = updatedStreamInfo;
2022-01-15 13:37:43 -05:00
} catch (e) {
debugPrint(e.toString());
}
2021-11-11 21:32:05 -05:00
}
@action
void handleExpand() {
overlayTimer.cancel();
settingsStore.expandInfo = !settingsStore.expandInfo;
overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
}
2022-01-07 16:14:59 -05:00
void handleRefresh() async {
HapticFeedback.lightImpact();
controller?.reload();
}
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
}
}