code review changes from @shenlong-tanwen

This commit is contained in:
bwees 2025-05-28 10:25:10 -05:00
parent 9b42e1b561
commit f999c77079
No known key found for this signature in database
9 changed files with 85 additions and 87 deletions

View File

@ -5,8 +5,6 @@ abstract interface class ICastDestinationService {
Future<bool> initialize();
CastDestinationType getType();
bool isAvailable();
void Function(bool)? onConnectionState;
void Function(Duration)? onCurrentTime;
@ -15,12 +13,14 @@ abstract interface class ICastDestinationService {
void Function(String)? onReceiverName;
void Function(CastState)? onCastState;
Future<void> connect(dynamic device);
void loadMedia(Asset asset, bool reload);
void play();
void pause();
void seekTo(Duration position);
void disconnect();
Future<void> disconnect();
Future<List<(String, CastDestinationType, dynamic)>> getDevices();
}

View File

@ -11,7 +11,7 @@ class CastManagerState {
final Duration currentTime;
final Duration duration;
CastManagerState({
const CastManagerState({
required this.isCasting,
required this.receiverName,
required this.castState,
@ -49,11 +49,12 @@ class CastManagerState {
factory CastManagerState.fromMap(Map<String, dynamic> map) {
return CastManagerState(
isCasting: map['isCasting'] ?? false,
receiverName: map['receiverName'] ?? '',
castState: map['castState'] ?? CastState.idle,
currentTime: Duration(seconds: map['currentTime']?.toInt() ?? 0),
duration: Duration(seconds: map['duration']?.toInt() ?? 0),);
isCasting: map['isCasting'] ?? false,
receiverName: map['receiverName'] ?? '',
castState: map['castState'] ?? CastState.idle,
currentTime: Duration(seconds: map['currentTime']?.toInt() ?? 0),
duration: Duration(seconds: map['duration']?.toInt() ?? 0),
);
}
String toJson() => json.encode(toMap());

View File

@ -8,7 +8,7 @@ class SessionCreateResponse {
final String token;
final String updatedAt;
SessionCreateResponse({
const SessionCreateResponse({
required this.createdAt,
required this.current,
required this.deviceOS,

View File

@ -61,7 +61,7 @@ class NativeVideoViewerPage extends HookConsumerWidget {
final log = Logger('NativeVideoViewerPage');
final cast = ref.watch(castProvider);
final isCasting = ref.watch(castProvider.select((c) => c.isCasting));
Future<VideoSource?> createSource() async {
if (!context.mounted) {
@ -394,7 +394,7 @@ class NativeVideoViewerPage extends HookConsumerWidget {
// This remains under the video to avoid flickering
// For motion videos, this is the image portion of the asset
Center(key: ValueKey(asset.id), child: image),
if (aspectRatio.value != null && !cast.isCasting)
if (aspectRatio.value != null && !isCasting)
Visibility.maintain(
key: ValueKey(asset),
visible: isVisible.value,

View File

@ -1,5 +1,6 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'package:immich_mobile/interfaces/cast_destination_service.interface.dart';
import 'package:immich_mobile/models/cast/cast_manager_state.dart';
import 'package:immich_mobile/services/gcast.service.dart';
@ -8,13 +9,14 @@ final castProvider = StateNotifierProvider<CastNotifier, CastManagerState>(
);
class CastNotifier extends StateNotifier<CastManagerState> {
final GCastService _gCastService;
// more cast providers can be added here (ie Fcast)
final ICastDestinationService _gCastService;
List<(String, CastDestinationType, dynamic)> discovered = List.empty();
CastNotifier(this._gCastService)
: super(
CastManagerState(
const CastManagerState(
isCasting: false,
currentTime: Duration.zero,
duration: Duration.zero,

View File

@ -14,6 +14,7 @@ import 'package:immich_mobile/repositories/gcast.repository.dart';
import 'package:immich_mobile/repositories/sessions_api.repository.dart';
import 'package:immich_mobile/utils/image_url_builder.dart';
import 'package:immich_mobile/utils/url_helper.dart';
// ignore: import_rule_openapi, we are only using the AssetMediaSize enum
import 'package:openapi/api.dart';
final gCastServiceProvider = Provider(
@ -34,7 +35,6 @@ class GCastService implements ICastDestinationService {
bool isConnected = false;
int? _sessionId;
Timer? _mediaStatusPollingTimer;
CastState? castState;
@override
void Function(bool)? onConnectionState;
@ -67,55 +67,58 @@ class GCastService implements ICastDestinationService {
}
void _onCastMessageCallback(Map<String, dynamic> message) {
final msgType = message['type'];
switch (message['type']) {
case "MEDIA_STATUS":
_handleMediaStatus(message);
break;
}
}
if (msgType == "MEDIA_STATUS") {
final statusList = (message['status'] as List)
.whereType<Map<String, dynamic>>()
.toList();
void _handleMediaStatus(Map<String, dynamic> message) {
final statusList =
(message['status'] as List).whereType<Map<String, dynamic>>().toList();
if (statusList.isEmpty) {
return;
}
if (statusList.isEmpty) {
return;
}
final status = statusList[0];
switch (status['playerState']) {
case "PLAYING":
onCastState?.call(CastState.playing);
break;
case "PAUSED":
onCastState?.call(CastState.paused);
break;
case "BUFFERING":
onCastState?.call(CastState.buffering);
break;
case "IDLE":
onCastState?.call(CastState.idle);
final status = statusList[0];
switch (status['playerState']) {
case "PLAYING":
onCastState?.call(CastState.playing);
break;
case "PAUSED":
onCastState?.call(CastState.paused);
break;
case "BUFFERING":
onCastState?.call(CastState.buffering);
break;
case "IDLE":
onCastState?.call(CastState.idle);
// stop polling for media status if the video finished playing
if (status["idleReason"] == "FINISHED") {
_mediaStatusPollingTimer?.cancel();
}
// stop polling for media status if the video finished playing
if (status["idleReason"] == "FINISHED") {
_mediaStatusPollingTimer?.cancel();
}
break;
}
break;
}
if (status["media"] != null && status["media"]["duration"] != null) {
final duration = Duration(
milliseconds: (status["media"]["duration"] * 1000 ?? 0).toInt(),
);
onDuration?.call(duration);
}
if (status["media"] != null && status["media"]["duration"] != null) {
final duration = Duration(
milliseconds: (status["media"]["duration"] * 1000 ?? 0).toInt(),
);
onDuration?.call(duration);
}
if (status["mediaSessionId"] != null) {
_sessionId = status["mediaSessionId"];
}
if (status["mediaSessionId"] != null) {
_sessionId = status["mediaSessionId"];
}
if (status["currentTime"] != null) {
final currentTime =
Duration(milliseconds: (status["currentTime"] * 1000 ?? 0).toInt());
onCurrentTime?.call(currentTime);
}
if (status["currentTime"] != null) {
final currentTime =
Duration(milliseconds: (status["currentTime"] * 1000 ?? 0).toInt());
onCurrentTime?.call(currentTime);
}
}
@ -139,20 +142,11 @@ class GCastService implements ICastDestinationService {
}
@override
void disconnect() {
_gCastRepository.disconnect();
Future<void> disconnect() async {
await _gCastRepository.disconnect();
onReceiverName?.call("");
}
@override
bool isAvailable() {
// check if server URL is https
final serverUrl = punycodeDecodeUrl(Store.tryGet(StoreKey.serverEndpoint));
return serverUrl?.startsWith("https://") ?? false;
}
bool isSessionValid() {
// check if we already have a session token
// we should always have a expiration date
@ -220,6 +214,8 @@ class GCastService implements ICastDestinationService {
"autoplay": true,
});
currentAssetId = asset.remoteId;
// we need to poll for media status since the cast device does not
// send a message when the media is loaded for whatever reason
// only do this on videos

View File

@ -78,14 +78,15 @@ class CustomVideoPlayerControls extends HookConsumerWidget {
}
ref.read(castProvider.notifier).loadMedia(asset, true);
}
return;
}
if (state == VideoPlaybackState.playing) {
ref.read(videoPlayerControlsProvider.notifier).pause();
} else if (state == VideoPlaybackState.completed) {
ref.read(videoPlayerControlsProvider.notifier).restart();
} else {
if (state == VideoPlaybackState.playing) {
ref.read(videoPlayerControlsProvider.notifier).pause();
} else if (state == VideoPlaybackState.completed) {
ref.read(videoPlayerControlsProvider.notifier).restart();
} else {
ref.read(videoPlayerControlsProvider.notifier).play();
}
ref.read(videoPlayerControlsProvider.notifier).play();
}
}

View File

@ -46,7 +46,7 @@ class TopControlAppBar extends HookConsumerWidget {
const double iconSize = 22.0;
final a = ref.watch(assetWatcher(asset)).value ?? asset;
final album = ref.watch(currentAlbumProvider);
final castManager = ref.watch(castProvider);
final isCasting = ref.watch(castProvider.select((c) => c.isCasting));
final comments = album != null &&
album.remoteId != null &&
asset.remoteId != null
@ -181,9 +181,7 @@ class TopControlAppBar extends HookConsumerWidget {
);
},
icon: Icon(
castManager.isCasting
? Icons.cast_connected_rounded
: Icons.cast_rounded,
isCasting ? Icons.cast_connected_rounded : Icons.cast_rounded,
size: 20.0,
color: Colors.grey[200],
),

View File

@ -71,16 +71,16 @@ class VideoPosition extends HookConsumerWidget {
ref
.read(castProvider.notifier)
.seekTo(seekToDuration);
} else {
ref
.read(videoPlayerControlsProvider.notifier)
.position = seekToDuration.inSeconds.toDouble();
// This immediately updates the slider position without waiting for the video to update
ref
.read(videoPlaybackValueProvider.notifier)
.position = seekToDuration;
return;
}
ref
.read(videoPlayerControlsProvider.notifier)
.position = seekToDuration.inSeconds.toDouble();
// This immediately updates the slider position without waiting for the video to update
ref.read(videoPlaybackValueProvider.notifier).position =
seekToDuration;
},
),
),