2021-11-11 21:32:05 -05:00
|
|
|
import 'dart:async';
|
2021-12-30 04:07:49 -05:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-01-07 16:14:59 -05:00
|
|
|
import 'package:flutter/services.dart';
|
2021-11-24 04:39:05 -05:00
|
|
|
import 'package:frosty/api/twitch_api.dart';
|
|
|
|
|
import 'package:frosty/core/auth/auth_store.dart';
|
|
|
|
|
import 'package:frosty/models/stream.dart';
|
2022-01-04 02:58:02 -05:00
|
|
|
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 {
|
2022-01-14 14:56:54 -05:00
|
|
|
final TwitchApi twitchApi;
|
|
|
|
|
|
2021-12-25 22:35:10 -05:00
|
|
|
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
|
2022-01-09 19:51:38 -05:00
|
|
|
var _overlayVisible = true;
|
2021-11-11 21:32:05 -05:00
|
|
|
|
2022-01-09 19:51:38 -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;
|
2021-11-24 04:39:05 -05:00
|
|
|
|
|
|
|
|
final String userLogin;
|
|
|
|
|
final AuthStore authStore;
|
2021-12-28 03:02:58 -05:00
|
|
|
final SettingsStore settingsStore;
|
2021-11-24 04:39:05 -05:00
|
|
|
|
2021-12-28 03:02:58 -05:00
|
|
|
_VideoStoreBase({
|
2022-01-14 14:56:54 -05:00
|
|
|
required this.twitchApi,
|
2021-12-28 03:02:58 -05:00
|
|
|
required this.userLogin,
|
|
|
|
|
required this.authStore,
|
|
|
|
|
required this.settingsStore,
|
|
|
|
|
}) {
|
2022-01-09 19:51:38 -05:00
|
|
|
overlayTimer = Timer(const Duration(seconds: 3), () => _overlayVisible = false);
|
2021-11-24 04:39:05 -05:00
|
|
|
updateStreamInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@action
|
2021-11-11 21:32:05 -05:00
|
|
|
void handlePausePlay() {
|
2022-01-18 15:43:29 -05:00
|
|
|
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
|
|
|
|
2022-01-18 15:43:29 -05:00
|
|
|
paused = !paused;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
}
|
2021-11-11 21:32:05 -05:00
|
|
|
}
|
|
|
|
|
|
2021-11-24 04:39:05 -05:00
|
|
|
@action
|
|
|
|
|
void handleVideoTap() {
|
2022-01-09 19:51:38 -05:00
|
|
|
if (_overlayVisible) {
|
2021-12-09 23:05:44 -05:00
|
|
|
overlayTimer.cancel();
|
2021-11-12 03:45:27 -05:00
|
|
|
|
2022-01-09 19:51:38 -05:00
|
|
|
_overlayVisible = false;
|
2021-11-12 03:45:27 -05:00
|
|
|
} else {
|
2021-12-09 23:05:44 -05:00
|
|
|
overlayTimer.cancel();
|
2022-01-09 19:51:38 -05:00
|
|
|
overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
|
2021-12-09 23:05:44 -05:00
|
|
|
|
2022-01-09 19:51:38 -05:00
|
|
|
_overlayVisible = true;
|
2021-11-12 03:45:27 -05:00
|
|
|
|
2021-11-24 04:39:05 -05:00
|
|
|
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-12 03:45:27 -05:00
|
|
|
}
|
2021-11-11 21:32:05 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-26 03:19:08 -05:00
|
|
|
@action
|
|
|
|
|
void handleExpand() {
|
|
|
|
|
overlayTimer.cancel();
|
2021-12-28 03:02:58 -05:00
|
|
|
settingsStore.expandInfo = !settingsStore.expandInfo;
|
2022-01-09 19:51:38 -05:00
|
|
|
overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
|
2021-12-26 03:19:08 -05:00
|
|
|
}
|
|
|
|
|
|
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() {
|
2021-12-30 04:07:49 -05:00
|
|
|
try {
|
2022-01-09 19:51:38 -05:00
|
|
|
controller?.runJavascript('document.getElementsByTagName("video")[0].addEventListener("pause", () => Pause.postMessage("video paused"));');
|
|
|
|
|
controller?.runJavascript('document.getElementsByTagName("video")[0].addEventListener("play", () => Play.postMessage("video playing"));');
|
2022-01-18 15:43:29 -05:00
|
|
|
controller?.runJavascript('document.getElementsByTagName("video")[0].muted = false;');
|
2021-12-30 04:07:49 -05:00
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
}
|
2021-11-11 21:32:05 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-09 23:05:44 -05:00
|
|
|
void requestPictureInPicture() {
|
2021-12-30 04:07:49 -05:00
|
|
|
try {
|
|
|
|
|
controller?.runJavascript('document.getElementsByTagName("video")[0].requestPictureInPicture();');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
}
|
2021-11-11 21:32:05 -05:00
|
|
|
}
|
|
|
|
|
}
|