2021-11-11 21:32:05 -05:00
|
|
|
import 'dart:async';
|
2022-06-05 18:29:13 -04:00
|
|
|
import 'dart:io';
|
2022-03-22 17:07:49 -04:00
|
|
|
|
2022-06-05 17:24:51 -04:00
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
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';
|
|
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
class VideoStore = VideoStoreBase with _$VideoStore;
|
2021-11-11 21:32:05 -05:00
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
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
|
|
|
|
2022-01-25 20:02:16 -05:00
|
|
|
late Timer _overlayTimer;
|
2021-11-11 21:32:05 -05:00
|
|
|
|
2022-03-22 17:07:49 -04: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
|
2022-05-30 17:23:38 -04: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
|
|
|
|
2022-06-05 17:24:51 -04:00
|
|
|
@readonly
|
|
|
|
|
var _isIPad = false;
|
|
|
|
|
|
2021-12-09 23:05:44 -05:00
|
|
|
@readonly
|
|
|
|
|
StreamTwitch? _streamInfo;
|
2021-11-24 04:39:05 -05:00
|
|
|
|
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',
|
2022-05-25 14:19:33 -04:00
|
|
|
onMessageReceived: (message) {
|
|
|
|
|
_paused = false;
|
|
|
|
|
controller?.runJavascript('document.getElementsByTagName("video")[0].muted = false;');
|
2022-06-24 08:48:55 -04:00
|
|
|
controller?.runJavascript('document.getElementsByTagName("video")[0].volume = 1.0;');
|
2022-05-25 14:19:33 -04:00
|
|
|
},
|
2022-01-25 20:02:16 -05:00
|
|
|
),
|
|
|
|
|
};
|
2022-01-23 20:13:48 -05:00
|
|
|
|
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
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
VideoStoreBase({
|
2021-12-28 03:02:58 -05:00
|
|
|
required this.userLogin,
|
2022-01-25 20:02:16 -05:00
|
|
|
required this.twitchApi,
|
2021-12-28 03:02:58 -05:00
|
|
|
required this.authStore,
|
|
|
|
|
required this.settingsStore,
|
|
|
|
|
}) {
|
2022-01-25 20:02:16 -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 {
|
2022-01-23 20:13:48 -05:00
|
|
|
if (_paused) {
|
2022-01-18 15:43:29 -05:00
|
|
|
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;
|
2022-01-18 15:43:29 -05:00
|
|
|
} 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-25 20:02:16 -05:00
|
|
|
_overlayTimer.cancel();
|
2021-11-12 03:45:27 -05:00
|
|
|
|
2022-01-25 20:02:16 -05:00
|
|
|
if (_overlayVisible) {
|
2022-01-09 19:51:38 -05:00
|
|
|
_overlayVisible = false;
|
2021-11-12 03:45:27 -05:00
|
|
|
} else {
|
2022-01-25 20:02:16 -05:00
|
|
|
updateStreamInfo();
|
2021-12-09 23:05:44 -05:00
|
|
|
|
2022-01-09 19:51:38 -05:00
|
|
|
_overlayVisible = true;
|
2022-01-25 20:02:16 -05:00
|
|
|
_overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
|
2021-11-24 04:39:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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());
|
2022-05-24 00:21:01 -04:00
|
|
|
|
|
|
|
|
_overlayTimer.cancel();
|
2022-05-25 14:19:33 -04:00
|
|
|
_streamInfo = null;
|
|
|
|
|
_paused = true;
|
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() {
|
2021-12-28 03:02:58 -05:00
|
|
|
settingsStore.expandInfo = !settingsStore.expandInfo;
|
2022-01-25 20:02:16 -05:00
|
|
|
|
|
|
|
|
_overlayTimer.cancel();
|
|
|
|
|
_overlayTimer = Timer(const Duration(seconds: 5), () => _overlayVisible = false);
|
2021-12-26 03:19:08 -05:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2022-06-27 23:23:09 -04:00
|
|
|
HapticFeedback.mediumImpact();
|
|
|
|
|
|
2022-01-23 20:13:48 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-05 17:24:51 -04:00
|
|
|
@action
|
|
|
|
|
Future<void> initVideo() async {
|
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"));');
|
2021-12-30 04:07:49 -05:00
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
}
|
2022-06-05 17:24:51 -04:00
|
|
|
|
|
|
|
|
// Determine whether the device is an iPad or not.
|
|
|
|
|
// Used to show or hide the rotate button on the overlay.
|
|
|
|
|
// Flutter doesn't allow programmatic rotation on iPad unless multitasking is disabled.
|
2022-06-05 18:29:13 -04:00
|
|
|
if (Platform.isIOS) {
|
|
|
|
|
final deviceInfo = DeviceInfoPlugin();
|
|
|
|
|
final info = await deviceInfo.iosInfo;
|
|
|
|
|
if (info.model?.toLowerCase().contains('ipad') == true) {
|
|
|
|
|
_isIPad = true;
|
|
|
|
|
} else {
|
|
|
|
|
_isIPad = false;
|
|
|
|
|
}
|
2022-06-05 17:24:51 -04:00
|
|
|
}
|
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
|
|
|
}
|
2022-03-22 17:07:49 -04:00
|
|
|
|
|
|
|
|
/// Updates the sleep timer with [sleepHours] and [sleepMinutes].
|
2022-03-23 19:55:01 -04:00
|
|
|
/// Calls [onTimerFinished] when the sleep timer completes.
|
|
|
|
|
void updateSleepTimer({required void Function() onTimerFinished}) {
|
2022-03-22 17:07:49 -04:00
|
|
|
// 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();
|
2022-03-23 19:55:01 -04:00
|
|
|
onTimerFinished();
|
2022-03-22 17:07:49 -04:00
|
|
|
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
|
|
|
}
|