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';
|
2023-05-30 21:10:04 -04:00
|
|
|
import 'package:flutter/material.dart';
|
2022-01-07 16:14:59 -05:00
|
|
|
import 'package:flutter/services.dart';
|
2022-08-31 14:11:45 -04:00
|
|
|
import 'package:frosty/apis/twitch_api.dart';
|
2021-11-24 04:39:05 -05:00
|
|
|
import 'package:frosty/models/stream.dart';
|
2022-08-31 14:11:45 -04:00
|
|
|
import 'package:frosty/screens/settings/stores/auth_store.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';
|
2022-12-27 23:47:55 -05:00
|
|
|
import 'package:simple_pip_mode/simple_pip.dart';
|
2021-11-11 21:32:05 -05:00
|
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
2023-05-30 21:10:04 -04:00
|
|
|
import 'package:webview_flutter_android/webview_flutter_android.dart';
|
|
|
|
|
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
|
2021-11-11 21:32:05 -05:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// The userlogin of the current channel.
|
|
|
|
|
final String userLogin;
|
|
|
|
|
|
|
|
|
|
final AuthStore authStore;
|
|
|
|
|
|
|
|
|
|
final SettingsStore settingsStore;
|
|
|
|
|
|
2022-12-27 23:47:55 -05:00
|
|
|
/// The [SimplePip] instance used for initiating PiP on Android.
|
|
|
|
|
final pip = SimplePip();
|
2022-07-28 01:25:56 -04:00
|
|
|
|
2023-05-30 21:10:04 -04:00
|
|
|
/// The video web view params used for enabling auto play.
|
|
|
|
|
late final PlatformWebViewControllerCreationParams _videoWebViewParams;
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// The webview controller used for injecting JavaScript to control the webview and video player.
|
2023-05-30 21:10:04 -04:00
|
|
|
late final WebViewController videoWebViewController =
|
|
|
|
|
WebViewController.fromPlatformCreationParams(_videoWebViewParams)
|
|
|
|
|
..setBackgroundColor(Colors.black)
|
|
|
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
2023-10-22 18:38:32 -04:00
|
|
|
..addJavaScriptChannel(
|
|
|
|
|
'VideoPause',
|
|
|
|
|
onMessageReceived: (message) {
|
|
|
|
|
_paused = true;
|
|
|
|
|
if (Platform.isAndroid) pip.setIsPlaying(false);
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
..addJavaScriptChannel(
|
|
|
|
|
'VideoPlaying',
|
|
|
|
|
onMessageReceived: (message) {
|
|
|
|
|
_paused = false;
|
|
|
|
|
if (Platform.isAndroid) pip.setIsPlaying(true);
|
|
|
|
|
videoWebViewController.runJavaScript(
|
|
|
|
|
'document.getElementsByTagName("video")[0].muted = false;',
|
|
|
|
|
);
|
|
|
|
|
videoWebViewController.runJavaScript(
|
|
|
|
|
'document.getElementsByTagName("video")[0].volume = 1.0;',
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
)
|
2023-05-30 21:10:04 -04:00
|
|
|
..setNavigationDelegate(
|
|
|
|
|
NavigationDelegate(
|
|
|
|
|
onPageFinished: (_) => initVideo(),
|
|
|
|
|
),
|
2023-06-19 22:02:18 -04:00
|
|
|
);
|
2021-11-11 21:32:05 -05:00
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// The timer that handles hiding the overlay automatically
|
2022-01-25 20:02:16 -05:00
|
|
|
late Timer _overlayTimer;
|
2021-11-11 21:32:05 -05:00
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// Disposes the overlay reactions.
|
|
|
|
|
late final ReactionDisposer _disposeOverlayReaction;
|
2022-03-22 17:07:49 -04:00
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// If the video is currently paused.
|
|
|
|
|
///
|
|
|
|
|
/// Does not pause or play the video, only used for rendering state of the overlay.
|
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-07-28 01:25:56 -04:00
|
|
|
/// If the overlay is should be visible.
|
2022-01-23 20:13:48 -05:00
|
|
|
@readonly
|
|
|
|
|
var _overlayVisible = true;
|
2021-11-11 21:32:05 -05:00
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// If the current device is iPad.
|
2022-06-05 17:24:51 -04:00
|
|
|
@readonly
|
|
|
|
|
var _isIPad = false;
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// The current stream info, used for displaying relevant info on the overlay.
|
2021-12-09 23:05:44 -05:00
|
|
|
@readonly
|
|
|
|
|
StreamTwitch? _streamInfo;
|
2021-11-24 04:39:05 -05:00
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// The video URL to use for the webview. Controls will be disabled when custom overlay is enabled.
|
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-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,
|
|
|
|
|
}) {
|
2023-06-19 22:02:18 -04:00
|
|
|
// Initialize the video webview params for iOS to enable video autoplay.
|
|
|
|
|
if (WebViewPlatform.instance is WebKitWebViewPlatform) {
|
|
|
|
|
_videoWebViewParams = WebKitWebViewControllerCreationParams(
|
|
|
|
|
allowsInlineMediaPlayback: true,
|
|
|
|
|
mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
_videoWebViewParams = const PlatformWebViewControllerCreationParams();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the video webview params for Android to enable video autoplay.
|
|
|
|
|
if (videoWebViewController.platform is AndroidWebViewController) {
|
|
|
|
|
(videoWebViewController.platform as AndroidWebViewController)
|
|
|
|
|
.setMediaPlaybackRequiresUserGesture(false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 23:47:55 -05:00
|
|
|
// On Android, enable auto PiP mode (setAutoEnterEnabled) if the device supports it.
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
SimplePip.isAutoPipAvailable.then((isAutoPipAvailable) {
|
|
|
|
|
if (isAutoPipAvailable) pip.setAutoPipMode();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 18:08:03 -04:00
|
|
|
// Initialize the [_overlayTimer] to hide the overlay automatically after 5 seconds.
|
2023-04-29 11:34:28 -04:00
|
|
|
_overlayTimer =
|
|
|
|
|
Timer(const Duration(seconds: 5), () => _overlayVisible = false);
|
2022-07-28 01:25:56 -04:00
|
|
|
|
|
|
|
|
// Initialize a reaction that will reload the webview whenever the overlay is toggled.
|
|
|
|
|
_disposeOverlayReaction = reaction(
|
|
|
|
|
(_) => settingsStore.showOverlay,
|
2023-05-30 21:10:04 -04:00
|
|
|
(_) => videoWebViewController.loadRequest(Uri.parse(videoUrl)),
|
2022-07-28 01:25:56 -04:00
|
|
|
);
|
|
|
|
|
|
2021-11-24 04:39:05 -05:00
|
|
|
updateStreamInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// Initializes the video webview.
|
|
|
|
|
@action
|
|
|
|
|
Future<void> initVideo() async {
|
2023-06-19 22:02:18 -04:00
|
|
|
if (await videoWebViewController.currentUrl() == videoUrl) {
|
|
|
|
|
// Add event listeners to notify the JavaScript channels when the video plays and pauses.
|
|
|
|
|
try {
|
|
|
|
|
videoWebViewController.runJavaScript(
|
2023-10-22 18:38:32 -04:00
|
|
|
'document.getElementsByTagName("video")[0].addEventListener("pause", () => VideoPause.postMessage("video paused"));',
|
|
|
|
|
);
|
2023-06-19 22:02:18 -04:00
|
|
|
videoWebViewController.runJavaScript(
|
2023-10-22 18:38:32 -04:00
|
|
|
'document.getElementsByTagName("video")[0].addEventListener("playing", () => VideoPlaying.postMessage("video playing"));',
|
|
|
|
|
);
|
2023-06-19 22:02:18 -04:00
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
}
|
2022-07-28 01:25:56 -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.
|
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
|
final deviceInfo = DeviceInfoPlugin();
|
|
|
|
|
final info = await deviceInfo.iosInfo;
|
2023-10-22 18:38:32 -04:00
|
|
|
if (info.model.toLowerCase().contains('ipad')) {
|
2022-07-28 01:25:56 -04:00
|
|
|
_isIPad = true;
|
|
|
|
|
} else {
|
|
|
|
|
_isIPad = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Called whenever the video/overlay is tapped.
|
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;
|
2023-04-29 11:34:28 -04:00
|
|
|
_overlayTimer =
|
|
|
|
|
Timer(const Duration(seconds: 5), () => _overlayVisible = false);
|
2021-11-24 04:39:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// Updates the stream info from the Twitch API.
|
|
|
|
|
///
|
|
|
|
|
/// If the stream is offline, disables the overlay.
|
2021-11-24 04:39:05 -05:00
|
|
|
@action
|
|
|
|
|
Future<void> updateStreamInfo() async {
|
2022-01-15 13:37:43 -05:00
|
|
|
try {
|
2023-04-29 11:34:28 -04:00
|
|
|
_streamInfo = await twitchApi.getStream(
|
2023-10-22 18:38:32 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// Handles the toggle overlay options.
|
|
|
|
|
///
|
|
|
|
|
/// The toggle overlay option allows switching between the custom and Twitch's overlay by long-pressing the overlay.
|
2022-01-23 20:13:48 -05:00
|
|
|
@action
|
2022-07-28 01:25:56 -04:00
|
|
|
void handleToggleOverlay() {
|
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
|
|
|
|
|
|
|
|
if (settingsStore.showOverlay) {
|
|
|
|
|
_overlayVisible = true;
|
|
|
|
|
|
|
|
|
|
_overlayTimer.cancel();
|
2023-04-29 11:34:28 -04:00
|
|
|
_overlayTimer =
|
|
|
|
|
Timer(const Duration(seconds: 3), () => _overlayVisible = false);
|
2022-01-25 20:02:16 -05:00
|
|
|
}
|
2022-01-23 20:13:48 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// Refreshes the stream webview and updates the stream info.
|
2022-07-27 01:19:25 -04:00
|
|
|
@action
|
2022-01-25 20:02:16 -05:00
|
|
|
void handleRefresh() {
|
2022-01-07 16:14:59 -05:00
|
|
|
HapticFeedback.lightImpact();
|
2023-05-30 21:10:04 -04:00
|
|
|
videoWebViewController.reload();
|
2022-07-27 01:19:25 -04:00
|
|
|
updateStreamInfo();
|
2022-01-07 16:14:59 -05:00
|
|
|
}
|
|
|
|
|
|
2022-07-28 18:08:03 -04:00
|
|
|
/// Play or pause the video depending on the current state of [_paused].
|
|
|
|
|
void handlePausePlay() {
|
|
|
|
|
try {
|
|
|
|
|
if (_paused) {
|
2023-05-30 21:10:04 -04:00
|
|
|
videoWebViewController
|
|
|
|
|
.runJavaScript('document.getElementsByTagName("video")[0].play();');
|
2022-07-28 18:08:03 -04:00
|
|
|
} else {
|
2023-05-30 21:10:04 -04:00
|
|
|
videoWebViewController.runJavaScript(
|
2023-10-22 18:38:32 -04:00
|
|
|
'document.getElementsByTagName("video")[0].pause();',
|
|
|
|
|
);
|
2022-07-28 18:08:03 -04:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
/// Initiate picture in picture if available.
|
|
|
|
|
///
|
|
|
|
|
/// On Android, this will utilize the native Android PiP API.
|
2022-12-27 23:47:55 -05:00
|
|
|
/// On iOS, this will utilize the web picture-in-picture API.
|
2022-07-28 01:25:56 -04:00
|
|
|
void requestPictureInPicture() {
|
|
|
|
|
try {
|
|
|
|
|
if (Platform.isAndroid) {
|
2022-12-27 23:47:55 -05:00
|
|
|
pip.enterPipMode(autoEnter: true);
|
2022-07-28 01:25:56 -04:00
|
|
|
} else if (Platform.isIOS) {
|
2023-05-30 21:10:04 -04:00
|
|
|
videoWebViewController.runJavaScript(
|
2023-10-22 18:38:32 -04:00
|
|
|
'document.getElementsByTagName("video")[0].requestPictureInPicture();',
|
|
|
|
|
);
|
2022-07-28 01:25:56 -04:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint(e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@action
|
2022-07-25 16:31:14 -04:00
|
|
|
void dispose() {
|
2022-12-27 23:47:55 -05:00
|
|
|
// Disable auto PiP when leaving so that we don't enter PiP on other screens.
|
2023-01-26 22:20:02 -05:00
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
SimplePip.isAutoPipAvailable.then((isAutoPipAvailable) {
|
|
|
|
|
if (isAutoPipAvailable) pip.setAutoPipMode(autoEnter: false);
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-12-27 23:47:55 -05:00
|
|
|
|
2022-07-28 01:25:56 -04:00
|
|
|
_disposeOverlayReaction();
|
2022-07-25 16:31:14 -04:00
|
|
|
}
|
2021-11-11 21:32:05 -05:00
|
|
|
}
|