Compare commits

...
42 Commits
Author SHA1 Message Date
edde746 8e00ad0551 chore: bump version 2025-11-04 14:57:34 +01:00
edde746 deadbd1be7 fix: improve subtitle selection 2025-11-04 14:51:58 +01:00
edde746 3d3358e4f9 fix: library chip alignment 2025-11-04 14:43:31 +01:00
edde746 51cb83cef9 fix: request utf-8 encoding 2025-11-04 14:29:49 +01:00
edde746 0e2fe8a9f3 fix: sort not applying after restart 2025-11-04 13:35:33 +01:00
edde746 63fa08798c ci: notarization 2025-11-04 12:15:39 +01:00
edde746 94e6c4109a ci: macOS signing 2025-11-04 11:49:41 +01:00
edde746 4dd2d7b011 chore: bump version 2025-11-04 11:01:51 +01:00
edde746 4696b5c7b0 fix: hardware decoding toggle functionality 2025-11-04 11:01:43 +01:00
edde746 852cee8967 ci: sign android build 2025-11-04 11:00:22 +01:00
edde746 2e7babaae5 ci: macOS 26 runners 2025-11-04 10:53:02 +01:00
edde746 65057750f7 feat: file version picker 2025-11-04 10:28:37 +01:00
edde746 e50bd46386 fix: limit hero dots count 2025-11-04 09:40:02 +01:00
edde746 1c67fa3998 feat: mobile double tap rewind/skip 2025-11-04 09:26:03 +01:00
edde746 d586c8c067 fix: allow connections to local only servers 2025-11-04 09:07:04 +01:00
edde746 f1a5493aeb feat: higher playback speed options
add 2.5x and 3x speed options
2025-11-04 08:52:47 +01:00
edde746 7efa96be74 fix: missing subtitles on android 2025-11-04 08:48:04 +01:00
edde746 770cb6b744 feat: customizable seek durations 2025-11-04 08:26:05 +01:00
edde746 5126b67c9c refactor: remove redundant library management in settings 2025-11-04 08:21:52 +01:00
edde746 f0035d1335 fix: seek time overflow 2025-11-04 08:02:00 +01:00
edde746 ca576110b8 Merge branch 'feature/library-reordering-6'
# Conflicts:
#	lib/screens/libraries_screen.dart
#	lib/services/storage_service.dart
2025-11-04 07:50:12 +01:00
edde746 1f59374f62 feat: library hiding 2025-11-04 07:45:24 +01:00
edde746 679e5d5c39 feat: sorting 2025-11-04 07:45:24 +01:00
edde746 73568ee3ae refactor: unify library management 2025-11-04 07:44:36 +01:00
edde746 aa9dd045de Merge branch 'main' into feature/library-reordering-6
# Conflicts:
#	lib/screens/libraries_screen.dart
#	lib/services/storage_service.dart
2025-11-04 06:56:21 +01:00
edde746andGitHub 69e7e796b4 Merge pull request #14 from TheLidlMan/fix/issue-13-poster-sizing
Fix poster sizing on home screen to match libraries view
2025-11-04 06:47:18 +01:00
edde746 95263b7625 feat: library hiding 2025-11-04 06:45:16 +01:00
Sam Matthews 236e3e7cfc Fix poster sizing on home screen to match libraries view
- Increase minimum poster width from 130px to 160px
- Adjust poster sizing to account for MediaCard padding (16px)
- Calculate poster dimensions based on actual display area
- Ensure consistent poster sizing across Home and Libraries screens

Fixes #13
2025-11-04 05:06:55 +00:00
Sam Matthews f06deba0cc feat: add library reordering functionality (#6)
- Add reorder mode toggle button in Libraries screen app bar
- Implement drag & drop reordering for library chips
- Persist library order to local storage using SharedPreferences
- Restore saved library order on app launch
- Update selected library index when libraries are reordered
- Add visual feedback with drag handle icon in reorder mode

Fixes #6
2025-11-04 04:29:26 +00:00
edde746 dcb4f3ee8a feat: sorting 2025-11-04 05:25:29 +01:00
edde746andGitHub 4246d7689d Merge pull request #11 from TheLidlMan/fix/content-rating-display
Fix content rating display to remove country prefixes
2025-11-04 04:45:10 +01:00
edde746 781aa4640e fix: support pin protected profiles 2025-11-04 04:44:22 +01:00
Sam Matthews ed668ba0e4 Fix content rating display to remove country prefixes
- Add formatContentRating utility function to strip country codes (gb/, us/, de/, etc.)
- Apply formatter in discover_screen.dart hero section
- Apply formatter in media_detail_screen.dart (badge and info row)
- Ratings now display as '12', 'PG', 'PG-13' instead of 'gb/12', 'us/PG-13'
- Fixes #10
2025-11-04 03:32:28 +00:00
edde746andGitHub fffe342dd1 Merge pull request #9 from TheLidlMan/fix/rating-display-issue-7
Fix rating display showing 0.7 instead of 7.0
2025-11-04 04:22:36 +01:00
edde746 b5349d7b3d feat: library density settings
also add season/show poster toggle for episodes
2025-11-04 04:17:39 +01:00
Sam Matthews f7d98fbe44 Fix rating display showing 0.7 instead of 7.0
Fixes #7

- Changed rating field type from int to double in PlexMetadata model
- Removed incorrect division by 10 in discover screen rating display
- Regenerated JSON serialization to properly parse rating as double
- Rating now displays correctly (e.g., 7.0 instead of 0.7)
2025-11-04 03:12:42 +00:00
edde746 3a5b812a22 refactor: slow down hero scroll 2025-11-04 02:55:44 +01:00
edde746 b01f9ce3f9 fix: crashing when scrolling large libraries 2025-11-04 02:50:39 +01:00
edde746 25ff8f1d1f feat: horizontal scroll arrows on desktop 2025-11-04 02:14:50 +01:00
edde746 ce8d384e7c feat: file info 2025-11-03 15:08:48 +01:00
edde746 bae7b70feb docs: mobile store links 2025-11-02 07:21:41 +01:00
edde746 26e7dba0d0 fix: plex track preference 2025-11-01 12:12:16 +01:00
43 changed files with 4040 additions and 477 deletions
+93 -6
View File
@@ -2,9 +2,6 @@ name: Build Flutter App
on:
workflow_dispatch:
push:
tags:
- "v*"
jobs:
build-android:
@@ -50,9 +47,23 @@ jobs:
- name: Install dependencies
run: flutter pub get
- name: Configure Android signing
run: |
echo "${{ secrets.ANDROID_KEYSTORE_BASE64 }}" | base64 --decode > android/app/upload-keystore.jks
cat > android/key.properties << EOF
storePassword=${{ secrets.ANDROID_STORE_PASSWORD }}
keyPassword=${{ secrets.ANDROID_KEY_PASSWORD }}
keyAlias=${{ secrets.ANDROID_KEY_ALIAS }}
storeFile=upload-keystore.jks
EOF
- name: Build APK
run: flutter build apk --release
- name: Clean up keystore
if: always()
run: rm -f android/app/upload-keystore.jks android/key.properties
- name: Rename APK
run: |
cp build/app/outputs/flutter-apk/app-release.apk plezy-android.apk
@@ -69,7 +80,7 @@ jobs:
path: plezy-android.apk
build-ios:
runs-on: macos-latest
runs-on: macos-26
permissions:
id-token: write
attestations: write
@@ -127,7 +138,7 @@ jobs:
path: plezy-ios.ipa
build-macos:
runs-on: macos-latest
runs-on: macos-26
permissions:
id-token: write
attestations: write
@@ -167,10 +178,86 @@ jobs:
- name: Build macOS
run: flutter build macos --release
- name: Import Code Signing Certificate
env:
MACOS_CERTIFICATE_BASE64: ${{ secrets.MACOS_CERTIFICATE_BASE64 }}
MACOS_CERTIFICATE_PASSWORD: ${{ secrets.MACOS_CERTIFICATE_PASSWORD }}
KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }}
run: |
# Create temporary keychain
KEYCHAIN_PATH=$RUNNER_TEMP/build.keychain
security create-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH
security set-keychain-settings -lut 21600 $KEYCHAIN_PATH
security unlock-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH
# Import certificate to keychain
CERTIFICATE_PATH=$RUNNER_TEMP/certificate.p12
echo "$MACOS_CERTIFICATE_BASE64" | base64 --decode -o $CERTIFICATE_PATH
security import $CERTIFICATE_PATH -k $KEYCHAIN_PATH -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH
# Add keychain to search list
security list-keychains -d user -s $KEYCHAIN_PATH login.keychain
- name: Sign Application
env:
APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
run: |
# Find the identity
IDENTITY=$(security find-identity -v -p codesigning | grep "Developer ID Application" | head -1 | grep -o '".*"' | tr -d '"')
echo "Signing with identity: $IDENTITY"
APP_PATH="build/macos/Build/Products/Release/plezy.app"
# Sign all frameworks and dylibs first (inside-out signing)
find "$APP_PATH/Contents/Frameworks" -name "*.framework" -o -name "*.dylib" | while read framework; do
echo "Signing: $framework"
codesign --force --sign "$IDENTITY" --timestamp --options runtime "$framework"
done
# Sign the app bundle itself
echo "Signing app bundle: $APP_PATH"
codesign --force --sign "$IDENTITY" --timestamp --options runtime --entitlements macos/Runner/Release.entitlements "$APP_PATH"
- name: Verify Signature
run: |
APP_PATH="build/macos/Build/Products/Release/plezy.app"
echo "Verifying signature..."
codesign -dvvv "$APP_PATH"
codesign --verify --deep --strict --verbose=2 "$APP_PATH"
- name: Notarize Application
env:
APPLE_ID: ${{ secrets.APPLE_ID }}
APPLE_APP_SPECIFIC_PASSWORD: ${{ secrets.APPLE_APP_SPECIFIC_PASSWORD }}
APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
run: |
APP_PATH="build/macos/Build/Products/Release/plezy.app"
# Create a temporary ZIP for notarization submission
echo "Creating ZIP for notarization..."
ditto -c -k --keepParent "$APP_PATH" notarization.zip
# Submit for notarization
echo "Submitting to Apple's notary service..."
xcrun notarytool submit notarization.zip \
--apple-id "$APPLE_ID" \
--password "$APPLE_APP_SPECIFIC_PASSWORD" \
--team-id "$APPLE_TEAM_ID" \
--wait
# Staple the notarization ticket to the app
echo "Stapling notarization ticket..."
xcrun stapler staple "$APP_PATH"
# Verify stapling
echo "Verifying notarization..."
xcrun stapler validate "$APP_PATH"
- name: Create ZIP
run: |
cd build/macos/Build/Products/Release
zip -r $GITHUB_WORKSPACE/plezy-macos.zip plezy.app
ditto -c -k --sequesterRsrc --keepParent plezy.app $GITHUB_WORKSPACE/plezy-macos.zip
- name: Attest macOS ZIP
uses: actions/attest-build-provenance@v2
+4 -1
View File
@@ -14,7 +14,10 @@ Plezy is a modern Plex media client that provides a seamless streaming experienc
## Download
### Mobile
Coming soon
<a href='https://apps.apple.com/us/app/id6754315964'><img height='60' alt='Download on the App Store' src='./assets/app-store-badge.png'/></a>
<a href='https://play.google.com/store/apps/details?id=com.edde746.plezy'><img height='60' alt='Get it on Google Play' src='./assets/play-store-badge.png'/></a>
> Google Play version is in closed testing ([required by Google](https://support.google.com/googleplay/android-developer/answer/14151465#overview)). Join the [Google Group](https://groups.google.com/g/plezy-testers) to get access.
### Desktop
- [Windows (x64)](https://github.com/edde746/plezy/releases/latest/download/plezy-windows-installer.exe)
Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

+174 -4
View File
@@ -3,7 +3,10 @@ import '../config/plex_config.dart';
import '../models/plex_library.dart';
import '../models/plex_metadata.dart';
import '../models/plex_media_info.dart';
import '../models/plex_file_info.dart';
import '../models/plex_filter.dart';
import '../models/plex_sort.dart';
import '../models/plex_media_version.dart';
import '../utils/app_logger.dart';
/// Result of testing a connection, including success status and latency
@@ -26,6 +29,8 @@ class PlexClient {
connectTimeout: const Duration(seconds: 10),
receiveTimeout: const Duration(seconds: 30),
validateStatus: (status) => status != null && status < 500,
responseType: ResponseType.json,
contentType: 'application/json; charset=utf-8',
),
);
@@ -74,6 +79,8 @@ class PlexClient {
connectTimeout: timeout,
receiveTimeout: timeout,
validateStatus: (status) => status != null && status < 500,
responseType: ResponseType.json,
contentType: 'application/json; charset=utf-8',
),
);
@@ -370,14 +377,22 @@ class PlexClient {
}
/// Get video URL for direct playback
Future<String?> getVideoUrl(String ratingKey) async {
/// [mediaIndex] specifies which Media item to use (defaults to 0 - first version)
Future<String?> getVideoUrl(String ratingKey, {int mediaIndex = 0}) async {
final response = await _dio.get('/library/metadata/$ratingKey');
final metadataJson = _getFirstMetadataJson(response);
if (metadataJson != null &&
metadataJson['Media'] != null &&
(metadataJson['Media'] as List).isNotEmpty) {
final media = metadataJson['Media'][0];
final mediaList = metadataJson['Media'] as List;
// Ensure the requested index is valid
if (mediaIndex < 0 || mediaIndex >= mediaList.length) {
mediaIndex = 0;
}
final media = mediaList[mediaIndex];
if (media['Part'] != null && (media['Part'] as List).isNotEmpty) {
final part = media['Part'][0];
final partKey = part['key'] as String?;
@@ -418,14 +433,22 @@ class PlexClient {
}
/// Get detailed media info including chapters and tracks
Future<PlexMediaInfo?> getMediaInfo(String ratingKey) async {
/// [mediaIndex] specifies which Media item to use (defaults to 0 - first version)
Future<PlexMediaInfo?> getMediaInfo(String ratingKey, {int mediaIndex = 0}) async {
final response = await _dio.get('/library/metadata/$ratingKey');
final metadataJson = _getFirstMetadataJson(response);
if (metadataJson != null &&
metadataJson['Media'] != null &&
(metadataJson['Media'] as List).isNotEmpty) {
final media = metadataJson['Media'][0];
final mediaList = metadataJson['Media'] as List;
// Ensure the requested index is valid
if (mediaIndex < 0 || mediaIndex >= mediaList.length) {
mediaIndex = 0;
}
final media = mediaList[mediaIndex];
if (media['Part'] != null && (media['Part'] as List).isNotEmpty) {
final part = media['Part'][0];
final partKey = part['key'] as String?;
@@ -504,6 +527,92 @@ class PlexClient {
return null;
}
/// Get all available media versions for a media item
/// Returns a list of PlexMediaVersion objects representing different quality/format options
Future<List<PlexMediaVersion>> getMediaVersions(String ratingKey) async {
final response = await _dio.get('/library/metadata/$ratingKey');
final metadataJson = _getFirstMetadataJson(response);
if (metadataJson != null &&
metadataJson['Media'] != null &&
(metadataJson['Media'] as List).isNotEmpty) {
final mediaList = metadataJson['Media'] as List;
return mediaList
.map((media) => PlexMediaVersion.fromJson(media as Map<String, dynamic>))
.toList();
}
return [];
}
/// Get file information for a media item
Future<PlexFileInfo?> getFileInfo(String ratingKey) async {
try {
final response = await _dio.get('/library/metadata/$ratingKey');
final metadataJson = _getFirstMetadataJson(response);
if (metadataJson != null &&
metadataJson['Media'] != null &&
(metadataJson['Media'] as List).isNotEmpty) {
final media = metadataJson['Media'][0];
final part = media['Part'] != null && (media['Part'] as List).isNotEmpty
? media['Part'][0]
: null;
// Extract video stream details
final streams = part?['Stream'] as List<dynamic>? ?? [];
Map<String, dynamic>? videoStream;
Map<String, dynamic>? audioStream;
for (var stream in streams) {
final streamType = stream['streamType'] as int?;
if (streamType == 1 && videoStream == null) {
videoStream = stream;
} else if (streamType == 2 && audioStream == null) {
audioStream = stream;
}
}
return PlexFileInfo(
// Media level properties
container: media['container'] as String?,
videoCodec: media['videoCodec'] as String?,
videoResolution: media['videoResolution'] as String?,
videoFrameRate: media['videoFrameRate'] as String?,
videoProfile: media['videoProfile'] as String?,
width: media['width'] as int?,
height: media['height'] as int?,
aspectRatio: (media['aspectRatio'] as num?)?.toDouble(),
bitrate: media['bitrate'] as int?,
duration: media['duration'] as int?,
audioCodec: media['audioCodec'] as String?,
audioProfile: media['audioProfile'] as String?,
audioChannels: media['audioChannels'] as int?,
optimizedForStreaming: media['optimizedForStreaming'] as bool?,
has64bitOffsets: media['has64bitOffsets'] as bool?,
// Part level properties (file)
filePath: part?['file'] as String?,
fileSize: part?['size'] as int?,
// Video stream details
colorSpace: videoStream?['colorSpace'] as String?,
colorRange: videoStream?['colorRange'] as String?,
colorPrimaries: videoStream?['colorPrimaries'] as String?,
colorTrc: videoStream?['colorTrc'] as String?,
chromaSubsampling: videoStream?['chromaSubsampling'] as String?,
frameRate: (videoStream?['frameRate'] as num?)?.toDouble(),
bitDepth: videoStream?['bitDepth'] as int?,
// Audio stream details
audioChannelLayout: audioStream?['audioChannelLayout'] as String?,
);
}
return null;
} catch (e) {
appLogger.e('Failed to get file info: $e');
return null;
}
}
/// Mark media as watched
Future<void> markAsWatched(String ratingKey) async {
await _dio.get(
@@ -573,6 +682,67 @@ class PlexClient {
return _extractDirectoryList(response, PlexFilterValue.fromJson);
}
/// Get available sort options for a library section
Future<List<PlexSort>> getLibrarySorts(String sectionId) async {
try {
// Fetch library content with minimal data to get Sort metadata
final response = await _dio.get(
'/library/sections/$sectionId/all',
queryParameters: {'X-Plex-Container-Size': 0},
);
final container = _getMediaContainer(response);
if (container != null && container['Sort'] != null) {
return (container['Sort'] as List)
.map((json) => PlexSort.fromJson(json as Map<String, dynamic>))
.toList();
}
// Fallback: return common sort options if API doesn't provide them
return [
PlexSort(
key: 'titleSort',
title: 'Title',
defaultDirection: 'asc',
),
PlexSort(
key: 'addedAt',
descKey: 'addedAt:desc',
title: 'Date Added',
defaultDirection: 'desc',
),
PlexSort(
key: 'originallyAvailableAt',
descKey: 'originallyAvailableAt:desc',
title: 'Release Date',
defaultDirection: 'desc',
),
PlexSort(
key: 'rating',
descKey: 'rating:desc',
title: 'Rating',
defaultDirection: 'desc',
),
];
} catch (e) {
appLogger.e('Failed to get library sorts: $e');
// Return fallback sort options on error
return [
PlexSort(
key: 'titleSort',
title: 'Title',
defaultDirection: 'asc',
),
PlexSort(
key: 'addedAt',
descKey: 'addedAt:desc',
title: 'Date Added',
defaultDirection: 'desc',
),
];
}
}
/// Find adjacent episode in a given direction
///
/// [direction]: +1 for next episode, -1 for previous episode
+1
View File
@@ -51,6 +51,7 @@ class PlexConfig {
'X-Plex-Platform': platform,
if (device != null) 'X-Plex-Device': device!,
if (acceptJson) 'Accept': 'application/json',
'Accept-Charset': 'utf-8',
};
if (token != null) {
+15 -8
View File
@@ -13,6 +13,8 @@ import 'services/fullscreen_state_manager.dart';
import 'providers/user_profile_provider.dart';
import 'providers/plex_client_provider.dart';
import 'providers/theme_provider.dart';
import 'providers/settings_provider.dart';
import 'providers/hidden_libraries_provider.dart';
import 'utils/language_codes.dart';
import 'utils/app_logger.dart';
import 'utils/provider_extensions.dart';
@@ -21,6 +23,10 @@ import 'utils/orientation_helper.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Configure image cache for large libraries
PaintingBinding.instance.imageCache.maximumSizeBytes = 500 << 20; // 500MB
PaintingBinding.instance.imageCache.maximumSize = 500; // 500 images
// Initialize window_manager for desktop platforms
if (Platform.isMacOS || Platform.isWindows || Platform.isLinux) {
await windowManager.ensureInitialized();
@@ -62,6 +68,8 @@ class MainApp extends StatelessWidget {
create: (context) => UserProfileProvider()..initialize(),
),
ChangeNotifierProvider(create: (context) => ThemeProvider()),
ChangeNotifierProvider(create: (context) => SettingsProvider()),
ChangeNotifierProvider(create: (context) => HiddenLibrariesProvider()),
],
child: Consumer<ThemeProvider>(
builder: (context, themeProvider, child) {
@@ -169,15 +177,14 @@ class _SetupScreenState extends State<SetupScreen> {
if (mounted) {
context.plexClient.setClient(result.client!);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => MainScreen(
client: result.client!,
userProfile: result.userProfile,
if (mounted) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => MainScreen(client: result.client!),
),
),
);
);
}
}
return;
} else {
+151
View File
@@ -0,0 +1,151 @@
class PlexFileInfo {
// Media level properties
final String? container;
final String? videoCodec;
final String? videoResolution;
final String? videoFrameRate;
final String? videoProfile;
final int? width;
final int? height;
final double? aspectRatio;
final int? bitrate;
final int? duration;
final String? audioCodec;
final String? audioProfile;
final int? audioChannels;
final bool? optimizedForStreaming;
final bool? has64bitOffsets;
// Part level properties (file)
final String? filePath;
final int? fileSize;
// Stream level properties (video stream details)
final String? colorSpace;
final String? colorRange;
final String? colorPrimaries;
final String? colorTrc;
final String? chromaSubsampling;
final double? frameRate;
final int? bitDepth;
final String? audioChannelLayout;
PlexFileInfo({
this.container,
this.videoCodec,
this.videoResolution,
this.videoFrameRate,
this.videoProfile,
this.width,
this.height,
this.aspectRatio,
this.bitrate,
this.duration,
this.audioCodec,
this.audioProfile,
this.audioChannels,
this.optimizedForStreaming,
this.has64bitOffsets,
this.filePath,
this.fileSize,
this.colorSpace,
this.colorRange,
this.colorPrimaries,
this.colorTrc,
this.chromaSubsampling,
this.frameRate,
this.bitDepth,
this.audioChannelLayout,
});
/// Format file size in human-readable format (GB, MB, KB, bytes)
String get fileSizeFormatted {
if (fileSize == null) return 'Unknown';
const kb = 1024;
const mb = kb * 1024;
const gb = mb * 1024;
if (fileSize! >= gb) {
return '${(fileSize! / gb).toStringAsFixed(2)} GB';
} else if (fileSize! >= mb) {
return '${(fileSize! / mb).toStringAsFixed(2)} MB';
} else if (fileSize! >= kb) {
return '${(fileSize! / kb).toStringAsFixed(2)} KB';
} else {
return '$fileSize bytes';
}
}
/// Format duration in HH:MM:SS or MM:SS format
String get durationFormatted {
if (duration == null) return 'Unknown';
final seconds = duration! ~/ 1000;
final hours = seconds ~/ 3600;
final minutes = (seconds % 3600) ~/ 60;
final secs = seconds % 60;
if (hours > 0) {
return '${hours}h ${minutes}m ${secs}s';
} else {
return '${minutes}m ${secs}s';
}
}
/// Format bitrate in Mbps or Kbps
String get bitrateFormatted {
if (bitrate == null) return 'Unknown';
const kbps = 1000;
const mbps = kbps * 1000;
if (bitrate! >= mbps) {
return '${(bitrate! / mbps).toStringAsFixed(2)} Mbps';
} else if (bitrate! >= kbps) {
return '${(bitrate! / kbps).toStringAsFixed(2)} Kbps';
} else {
return '$bitrate bps';
}
}
/// Format resolution as widthxheight
String get resolutionFormatted {
if (width != null && height != null) {
return '${width}x$height';
} else if (videoResolution != null) {
return videoResolution!;
}
return 'Unknown';
}
/// Format aspect ratio
String get aspectRatioFormatted {
if (aspectRatio != null) {
return aspectRatio!.toStringAsFixed(2);
}
return 'Unknown';
}
/// Format frame rate
String get frameRateFormatted {
if (frameRate != null) {
return '${frameRate!.toStringAsFixed(3)} fps';
} else if (videoFrameRate != null) {
return videoFrameRate!;
}
return 'Unknown';
}
/// Format audio channels (e.g., "2 channels (stereo)")
String get audioChannelsFormatted {
if (audioChannels != null) {
String channelText = '$audioChannels channel${audioChannels! > 1 ? 's' : ''}';
if (audioChannelLayout != null) {
channelText += ' ($audioChannelLayout)';
}
return channelText;
}
return 'Unknown';
}
}
+9 -9
View File
@@ -32,18 +32,18 @@ class PlexHomeUser {
factory PlexHomeUser.fromJson(Map<String, dynamic> json) {
return PlexHomeUser(
id: (json['id'] as num?)?.toInt() ?? 0,
uuid: json['uuid'] as String,
title: json['title'] as String,
uuid: json['uuid'] as String? ?? '',
title: json['title'] as String? ?? 'Unknown',
username: json['username'] as String?,
email: json['email'] as String?,
friendlyName: json['friendlyName'] as String?,
thumb: json['thumb'] as String,
hasPassword: json['hasPassword'] as bool,
restricted: json['restricted'] as bool,
thumb: json['thumb'] as String? ?? '',
hasPassword: json['hasPassword'] as bool? ?? false,
restricted: json['restricted'] as bool? ?? false,
updatedAt: (json['updatedAt'] as num?)?.toInt(),
admin: json['admin'] as bool,
guest: json['guest'] as bool,
protected: json['protected'] as bool,
admin: json['admin'] as bool? ?? false,
guest: json['guest'] as bool? ?? false,
protected: json['protected'] as bool? ?? false,
);
}
@@ -70,5 +70,5 @@ class PlexHomeUser {
bool get isAdminUser => admin;
bool get isRestrictedUser => restricted;
bool get isGuestUser => guest;
bool get requiresPassword => hasPassword;
bool get requiresPassword => protected;
}
+77
View File
@@ -0,0 +1,77 @@
class PlexMediaVersion {
final int id;
final String? videoResolution;
final String? videoCodec;
final int? bitrate;
final int? width;
final int? height;
final String? container;
final String partKey;
PlexMediaVersion({
required this.id,
this.videoResolution,
this.videoCodec,
this.bitrate,
this.width,
this.height,
this.container,
required this.partKey,
});
/// Creates a PlexMediaVersion from Plex API Media object
factory PlexMediaVersion.fromJson(Map<String, dynamic> json) {
// Get the first Part key for playback
final parts = json['Part'] as List<dynamic>?;
final partKey = parts != null && parts.isNotEmpty
? parts[0]['key'] as String? ?? ''
: '';
return PlexMediaVersion(
id: json['id'] as int? ?? 0,
videoResolution: json['videoResolution'] as String?,
videoCodec: json['videoCodec'] as String?,
bitrate: json['bitrate'] as int?,
width: json['width'] as int?,
height: json['height'] as int?,
container: json['container'] as String?,
partKey: partKey,
);
}
/// Display label with detailed information: "1080p H.264 MKV (8.5 Mbps)"
String get displayLabel {
final parts = <String>[];
// Add resolution
if (videoResolution != null && videoResolution!.isNotEmpty) {
parts.add('${videoResolution}p');
} else if (height != null) {
parts.add('${height}p');
}
// Add codec
if (videoCodec != null && videoCodec!.isNotEmpty) {
parts.add(videoCodec!.toUpperCase());
}
// Add container
if (container != null && container!.isNotEmpty) {
parts.add(container!.toUpperCase());
}
// Build main label
String label = parts.isNotEmpty ? parts.join(' ') : 'Unknown';
// Add bitrate in parentheses
if (bitrate != null && bitrate! > 0) {
final bitrateInMbps = (bitrate! / 1000).toStringAsFixed(1);
label += ' ($bitrateInMbps Mbps)';
}
return label;
}
@override
String toString() => displayLabel;
}
+84 -5
View File
@@ -12,7 +12,7 @@ class PlexMetadata {
final String title;
final String? contentRating;
final String? summary;
final int? rating;
final double? rating;
final int? year;
final String? thumb;
final String? art;
@@ -24,6 +24,7 @@ class PlexMetadata {
final String? grandparentArt; // Show art for episodes
final String? grandparentRatingKey; // Show rating key for episodes
final String? parentTitle; // Season title for episodes
final String? parentThumb; // Season poster for episodes
final String? parentRatingKey; // Season rating key for episodes
final int? parentIndex; // Season number
final int? index; // Episode number
@@ -58,6 +59,7 @@ class PlexMetadata {
this.grandparentArt,
this.grandparentRatingKey,
this.parentTitle,
this.parentThumb,
this.parentRatingKey,
this.parentIndex,
this.index,
@@ -68,6 +70,74 @@ class PlexMetadata {
this.viewedLeafCount,
});
/// Create a copy of this metadata with optional field overrides
PlexMetadata copyWith({
String? ratingKey,
String? key,
String? guid,
String? studio,
String? type,
String? title,
String? contentRating,
String? summary,
double? rating,
int? year,
String? thumb,
String? art,
int? duration,
int? addedAt,
int? updatedAt,
String? grandparentTitle,
String? grandparentThumb,
String? grandparentArt,
String? grandparentRatingKey,
String? parentTitle,
String? parentThumb,
String? parentRatingKey,
int? parentIndex,
int? index,
String? grandparentTheme,
int? viewOffset,
int? viewCount,
int? leafCount,
int? viewedLeafCount,
}) {
final copy = PlexMetadata(
ratingKey: ratingKey ?? this.ratingKey,
key: key ?? this.key,
guid: guid ?? this.guid,
studio: studio ?? this.studio,
type: type ?? this.type,
title: title ?? this.title,
contentRating: contentRating ?? this.contentRating,
summary: summary ?? this.summary,
rating: rating ?? this.rating,
year: year ?? this.year,
thumb: thumb ?? this.thumb,
art: art ?? this.art,
duration: duration ?? this.duration,
addedAt: addedAt ?? this.addedAt,
updatedAt: updatedAt ?? this.updatedAt,
grandparentTitle: grandparentTitle ?? this.grandparentTitle,
grandparentThumb: grandparentThumb ?? this.grandparentThumb,
grandparentArt: grandparentArt ?? this.grandparentArt,
grandparentRatingKey: grandparentRatingKey ?? this.grandparentRatingKey,
parentTitle: parentTitle ?? this.parentTitle,
parentThumb: parentThumb ?? this.parentThumb,
parentRatingKey: parentRatingKey ?? this.parentRatingKey,
parentIndex: parentIndex ?? this.parentIndex,
index: index ?? this.index,
grandparentTheme: grandparentTheme ?? this.grandparentTheme,
viewOffset: viewOffset ?? this.viewOffset,
viewCount: viewCount ?? this.viewCount,
leafCount: leafCount ?? this.leafCount,
viewedLeafCount: viewedLeafCount ?? this.viewedLeafCount,
);
// Preserve clearLogo
copy._clearLogo = _clearLogo;
return copy;
}
// Extract clearLogo from Image array in raw JSON
void _extractClearLogo(Map<String, dynamic> json) {
if (!json.containsKey('Image')) return;
@@ -121,12 +191,21 @@ class PlexMetadata {
}
// Helper to get the poster (show poster for episodes/seasons, thumb otherwise)
String? get posterThumb {
// If useSeasonPoster is true, episodes will use season poster instead of series poster
String? posterThumb({bool useSeasonPoster = false}) {
final itemType = type.toLowerCase();
// For episodes and seasons, prefer grandparent thumb (show poster)
if ((itemType == 'episode' || itemType == 'season') &&
grandparentThumb != null) {
if (itemType == 'episode') {
// If season poster is enabled and available, use it
if (useSeasonPoster && parentThumb != null) {
return parentThumb!;
}
// Otherwise fall back to series poster, then item thumb
if (grandparentThumb != null) {
return grandparentThumb!;
}
} else if (itemType == 'season' && grandparentThumb != null) {
// For seasons, always use series poster
return grandparentThumb!;
}
return thumb;
+3 -1
View File
@@ -15,7 +15,7 @@ PlexMetadata _$PlexMetadataFromJson(Map<String, dynamic> json) => PlexMetadata(
title: json['title'] as String,
contentRating: json['contentRating'] as String?,
summary: json['summary'] as String?,
rating: (json['rating'] as num?)?.toInt(),
rating: (json['rating'] as num?)?.toDouble(),
year: (json['year'] as num?)?.toInt(),
thumb: json['thumb'] as String?,
art: json['art'] as String?,
@@ -27,6 +27,7 @@ PlexMetadata _$PlexMetadataFromJson(Map<String, dynamic> json) => PlexMetadata(
grandparentArt: json['grandparentArt'] as String?,
grandparentRatingKey: json['grandparentRatingKey'] as String?,
parentTitle: json['parentTitle'] as String?,
parentThumb: json['parentThumb'] as String?,
parentRatingKey: json['parentRatingKey'] as String?,
parentIndex: (json['parentIndex'] as num?)?.toInt(),
index: (json['index'] as num?)?.toInt(),
@@ -59,6 +60,7 @@ Map<String, dynamic> _$PlexMetadataToJson(PlexMetadata instance) =>
'grandparentArt': instance.grandparentArt,
'grandparentRatingKey': instance.grandparentRatingKey,
'parentTitle': instance.parentTitle,
'parentThumb': instance.parentThumb,
'parentRatingKey': instance.parentRatingKey,
'parentIndex': instance.parentIndex,
'index': instance.index,
+53
View File
@@ -0,0 +1,53 @@
class PlexSort {
final String key;
final String? descKey;
final String title;
final String? defaultDirection;
PlexSort({
required this.key,
this.descKey,
required this.title,
this.defaultDirection,
});
factory PlexSort.fromJson(Map<String, dynamic> json) {
return PlexSort(
key: json['key'] as String,
descKey: json['descKey'] as String?,
title: json['title'] as String,
defaultDirection: json['defaultDirection'] as String?,
);
}
/// Gets the full sort key with direction
/// If [descending] is true, returns the descKey or key:desc
/// Otherwise returns the key for ascending sort
String getSortKey({bool descending = false}) {
if (!descending) {
return key;
}
// Use descKey if available, otherwise append :desc to key
return descKey ?? '$key:desc';
}
/// Returns true if this sort's default direction is descending
bool get isDefaultDescending {
return defaultDirection?.toLowerCase() == 'desc';
}
@override
String toString() {
return 'PlexSort(key: $key, title: $title, defaultDirection: $defaultDirection)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is PlexSort && other.key == key;
}
@override
int get hashCode => key.hashCode;
}
@@ -0,0 +1,61 @@
import 'package:flutter/foundation.dart';
import '../services/storage_service.dart';
/// Provider for managing hidden library state across the app.
/// This ensures that when a library is hidden/unhidden in one screen,
/// all other screens are automatically updated.
class HiddenLibrariesProvider extends ChangeNotifier {
late StorageService _storageService;
Set<String> _hiddenLibraryKeys = {};
bool _isInitialized = false;
/// Get an unmodifiable copy of hidden library keys
Set<String> get hiddenLibraryKeys => Set.unmodifiable(_hiddenLibraryKeys);
/// Check if the provider has completed initialization
bool get isInitialized => _isInitialized;
HiddenLibrariesProvider() {
_initialize();
}
/// Initialize the provider by loading hidden libraries from storage
Future<void> _initialize() async {
_storageService = await StorageService.getInstance();
_hiddenLibraryKeys = _storageService.getHiddenLibraries();
_isInitialized = true;
notifyListeners();
}
/// Hide a library by its key
/// Updates both in-memory state and persistent storage
Future<void> hideLibrary(String libraryKey) async {
if (!_hiddenLibraryKeys.contains(libraryKey)) {
_hiddenLibraryKeys = Set.from(_hiddenLibraryKeys)..add(libraryKey);
await _storageService.saveHiddenLibraries(_hiddenLibraryKeys);
notifyListeners();
}
}
/// Unhide a library by its key
/// Updates both in-memory state and persistent storage
Future<void> unhideLibrary(String libraryKey) async {
if (_hiddenLibraryKeys.contains(libraryKey)) {
_hiddenLibraryKeys = Set.from(_hiddenLibraryKeys)..remove(libraryKey);
await _storageService.saveHiddenLibraries(_hiddenLibraryKeys);
notifyListeners();
}
}
/// Check if a specific library is hidden
bool isLibraryHidden(String libraryKey) {
return _hiddenLibraryKeys.contains(libraryKey);
}
/// Refresh hidden libraries from storage
/// Useful if storage was modified outside the provider
Future<void> refresh() async {
_hiddenLibraryKeys = _storageService.getHiddenLibraries();
notifyListeners();
}
}
+49
View File
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart';
import '../services/settings_service.dart';
class SettingsProvider extends ChangeNotifier {
late SettingsService _settingsService;
LibraryDensity _libraryDensity = LibraryDensity.normal;
bool _useSeasonPoster = false;
SettingsProvider() {
_initializeSettings();
}
Future<void> _initializeSettings() async {
_settingsService = await SettingsService.getInstance();
_libraryDensity = _settingsService.getLibraryDensity();
_useSeasonPoster = _settingsService.getUseSeasonPoster();
notifyListeners();
}
LibraryDensity get libraryDensity => _libraryDensity;
bool get useSeasonPoster => _useSeasonPoster;
Future<void> setLibraryDensity(LibraryDensity density) async {
if (_libraryDensity != density) {
_libraryDensity = density;
await _settingsService.setLibraryDensity(density);
notifyListeners();
}
}
Future<void> setUseSeasonPoster(bool value) async {
if (_useSeasonPoster != value) {
_useSeasonPoster = value;
await _settingsService.setUseSeasonPoster(value);
notifyListeners();
}
}
String get libraryDensityDisplayName {
switch (_libraryDensity) {
case LibraryDensity.compact:
return 'Compact';
case LibraryDensity.normal:
return 'Normal';
case LibraryDensity.comfortable:
return 'Comfortable';
}
}
}
+128 -2
View File
@@ -1,20 +1,25 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import '../models/plex_home.dart';
import '../models/plex_home_user.dart';
import '../models/plex_user_profile.dart';
import '../services/plex_auth_service.dart';
import '../services/storage_service.dart';
import '../utils/app_logger.dart';
import '../utils/provider_extensions.dart';
import '../widgets/pin_entry_dialog.dart';
import 'plex_client_provider.dart';
class UserProfileProvider extends ChangeNotifier {
PlexHome? _home;
PlexHomeUser? _currentUser;
PlexUserProfile? _profileSettings;
bool _isLoading = false;
String? _error;
PlexHome? get home => _home;
PlexHomeUser? get currentUser => _currentUser;
PlexUserProfile? get profileSettings => _profileSettings;
bool get isLoading => _isLoading;
String? get error => _error;
bool get hasMultipleUsers {
@@ -66,6 +71,19 @@ class UserProfileProvider extends ChangeNotifier {
// Don't set error here as it's not critical for app startup
}
}
// Fetch fresh profile settings from API
appLogger.d('UserProfileProvider: Fetching profile settings from API');
try {
await refreshProfileSettings();
} catch (e) {
appLogger.w(
'UserProfileProvider: Failed to fetch profile settings during initialization',
error: e,
);
// Don't set error here, cached profile (if any) was already loaded
}
appLogger.d('UserProfileProvider: Initialization complete');
} catch (e) {
appLogger.e(
@@ -98,9 +116,41 @@ class UserProfileProvider extends ChangeNotifier {
_currentUser = _home!.getUserByUUID(currentUserUUID);
}
// Profile settings are NOT cached - they will be fetched fresh from API
// in refreshProfileSettings()
notifyListeners();
}
/// Fetch the user's profile settings from the API
Future<void> refreshProfileSettings() async {
if (_authService == null || _storageService == null) {
appLogger.w('refreshProfileSettings: Services not initialized, skipping');
return;
}
appLogger.d('Fetching user profile settings from Plex API');
try {
final currentToken = _storageService!.getPlexToken();
if (currentToken == null) {
appLogger.w(
'refreshProfileSettings: No Plex token available, cannot fetch profile',
);
return;
}
final profile = await _authService!.getUserProfile(currentToken);
_profileSettings = profile;
appLogger.i('Successfully fetched user profile settings from API');
notifyListeners();
} catch (e) {
appLogger.w('Failed to fetch user profile settings from API', error: e);
// Don't set error state, profile will remain null or keep existing value
}
}
Future<void> loadHomeUsers({bool forceRefresh = false}) async {
appLogger.d('loadHomeUsers called - forceRefresh: $forceRefresh');
@@ -206,15 +256,41 @@ class UserProfileProvider extends ChangeNotifier {
_setLoading(true);
_clearError();
return await _attemptUserSwitch(user, context, clientProvider, null);
}
Future<bool> _attemptUserSwitch(
PlexHomeUser user,
BuildContext? context,
PlexClientProvider? clientProvider,
String? errorMessage,
) async {
try {
final currentToken = _storageService!.getPlexToken();
if (currentToken == null) {
throw Exception('No Plex.tv authentication token available');
}
// Check if user requires PIN
String? pin;
if (user.requiresPassword && context != null && context.mounted) {
pin = await showPinEntryDialog(
context,
user.displayName,
errorMessage: errorMessage,
);
// User cancelled the PIN dialog
if (pin == null) {
_setLoading(false);
return false;
}
}
final switchResponse = await _authService!.switchToUser(
user.uuid,
currentToken,
pin: pin,
);
// switchResponse.authToken is the new user's Plex.tv token
@@ -263,8 +339,17 @@ class UserProfileProvider extends ChangeNotifier {
// Update current user
_currentUser = user;
// Save user profile
await _storageService!.saveUserProfile(switchResponse.profile.toJson());
// Update user profile settings (fresh from API)
_profileSettings = switchResponse.profile;
appLogger.d(
'Updated profile settings for user: ${user.displayName}',
error: {
'defaultAudioLanguage':
_profileSettings?.defaultAudioLanguage ?? 'not set',
'defaultSubtitleLanguage':
_profileSettings?.defaultSubtitleLanguage ?? 'not set',
},
);
// Update PlexClient with proper server access token
if (clientProvider != null) {
@@ -291,6 +376,36 @@ class UserProfileProvider extends ChangeNotifier {
appLogger.i('Successfully switched to user: ${user.displayName}');
return true;
} catch (e) {
// Check if it's a PIN validation error
if (e is DioException && e.response?.statusCode == 403) {
final errors = e.response?.data['errors'] as List?;
if (errors != null && errors.isNotEmpty) {
final errorCode = errors[0]['code'] as int?;
final errorMessage = errors[0]['message'] as String?;
// Error code 1041 means invalid PIN
if (errorCode == 1041) {
appLogger.w('Invalid PIN for user: ${user.displayName}');
_clearError(); // Clear any previous error state
// Retry with error message if context is still available
if (context != null && context.mounted) {
return await _attemptUserSwitch(
user,
context,
clientProvider,
errorMessage ?? 'Incorrect PIN. Please try again.',
);
}
// If context not available, return false without showing error
appLogger.d('Cannot retry PIN entry - context not available');
return false;
}
}
}
// Only show error for non-PIN validation errors
_setError('Failed to switch user: $e');
appLogger.e('Failed to switch to user: ${user.displayName}', error: e);
return false;
@@ -322,6 +437,7 @@ class UserProfileProvider extends ChangeNotifier {
// Clear user-specific provider state but keep services for future sign-ins
_home = null;
_currentUser = null;
_profileSettings = null;
_onDataInvalidationRequested = null;
_clearError();
@@ -346,6 +462,7 @@ class UserProfileProvider extends ChangeNotifier {
// Clear cached data from previous server (both memory and storage)
_home = null;
_currentUser = null;
_profileSettings = null;
_clearError();
// Re-initialize services with current storage state
@@ -401,6 +518,15 @@ class UserProfileProvider extends ChangeNotifier {
appLogger.w(
'UserProfileProvider: Cannot perform complete profile switch - no context provided',
);
// Still try to fetch profile settings even without full switch
try {
await refreshProfileSettings();
} catch (e) {
appLogger.w(
'UserProfileProvider: Failed to refresh profile settings for new server',
error: e,
);
}
}
} catch (e) {
appLogger.w(
+149 -87
View File
@@ -4,13 +4,13 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:provider/provider.dart';
import '../client/plex_client.dart';
import '../models/plex_metadata.dart';
import '../models/plex_user_profile.dart';
import '../providers/plex_client_provider.dart';
import '../services/storage_service.dart';
import '../services/plex_auth_service.dart';
import '../widgets/media_card.dart';
import '../widgets/desktop_app_bar.dart';
import '../widgets/user_avatar_widget.dart';
import '../widgets/horizontal_scroll_with_arrows.dart';
import 'profile_switch_screen.dart';
import 'server_selection_screen.dart';
import '../providers/user_profile_provider.dart';
@@ -19,13 +19,13 @@ import '../mixins/item_updatable.dart';
import '../utils/app_logger.dart';
import '../utils/provider_extensions.dart';
import '../utils/video_player_navigation.dart';
import '../utils/content_rating_formatter.dart';
import 'auth_screen.dart';
class DiscoverScreen extends StatefulWidget {
final PlexUserProfile? userProfile;
final VoidCallback? onBecameVisible;
const DiscoverScreen({super.key, this.userProfile, this.onBecameVisible});
const DiscoverScreen({super.key, this.onBecameVisible});
@override
State<DiscoverScreen> createState() => _DiscoverScreenState();
@@ -33,6 +33,8 @@ class DiscoverScreen extends StatefulWidget {
class _DiscoverScreenState extends State<DiscoverScreen>
with Refreshable, ItemUpdatable, SingleTickerProviderStateMixin {
static const Duration _heroAutoScrollDuration = Duration(seconds: 8);
@override
PlexClient get client => context.clientSafe;
@@ -52,7 +54,7 @@ class _DiscoverScreenState extends State<DiscoverScreen>
super.initState();
_indicatorAnimationController = AnimationController(
vsync: this,
duration: const Duration(seconds: 5),
duration: _heroAutoScrollDuration,
);
_loadContent();
_startAutoScroll();
@@ -71,10 +73,15 @@ class _DiscoverScreenState extends State<DiscoverScreen>
if (_isAutoScrollPaused) return;
_indicatorAnimationController.forward(from: 0.0);
_autoScrollTimer = Timer.periodic(const Duration(seconds: 5), (timer) {
_autoScrollTimer = Timer.periodic(_heroAutoScrollDuration, (timer) {
if (_onDeck.isEmpty || !_heroController.hasClients || _isAutoScrollPaused)
return;
// Validate current index is within bounds before calculating next page
if (_currentHeroIndex >= _onDeck.length) {
_currentHeroIndex = 0;
}
final nextPage = (_currentHeroIndex + 1) % _onDeck.length;
_heroController.animateToPage(
nextPage,
@@ -110,6 +117,41 @@ class _DiscoverScreenState extends State<DiscoverScreen>
_startAutoScroll();
}
// Helper method to calculate visible dot range (max 5 dots)
({int start, int end}) _getVisibleDotRange() {
final totalDots = _onDeck.length;
if (totalDots <= 5) {
return (start: 0, end: totalDots - 1);
}
// Center the active dot when possible
final center = _currentHeroIndex;
int start = (center - 2).clamp(0, totalDots - 5);
int end = start + 4; // 5 dots total (0-4 inclusive)
return (start: start, end: end);
}
// Helper method to determine dot size based on position
double _getDotSize(int dotIndex, int start, int end) {
final totalDots = _onDeck.length;
// If we have 5 or fewer dots, all are full size (8px)
if (totalDots <= 5) {
return 8.0;
}
// First and last visible dots are smaller if there are more items beyond them
final isFirstVisible = dotIndex == start && start > 0;
final isLastVisible = dotIndex == end && end < totalDots - 1;
if (isFirstVisible || isLastVisible) {
return 5.0; // Smaller edge dots
}
return 8.0; // Normal size
}
Future<void> _loadContent() async {
appLogger.d('Loading discover content');
setState(() {
@@ -135,7 +177,16 @@ class _DiscoverScreenState extends State<DiscoverScreen>
_onDeck = onDeck;
_recentlyAdded = recentlyAdded;
_isLoading = false;
// Reset hero index to avoid sync issues
_currentHeroIndex = 0;
});
// Sync PageController to first page after data loads
if (_heroController.hasClients && onDeck.isNotEmpty) {
_heroController.jumpToPage(0);
}
appLogger.d('Discover content loaded successfully');
} catch (e) {
appLogger.e('Failed to load discover content', error: e);
@@ -450,10 +501,13 @@ class _DiscoverScreenState extends State<DiscoverScreen>
controller: _heroController,
itemCount: _onDeck.length,
onPageChanged: (index) {
setState(() {
_currentHeroIndex = index;
});
_resetAutoScrollTimer();
// Validate index is within bounds before updating
if (index >= 0 && index < _onDeck.length) {
setState(() {
_currentHeroIndex = index;
});
_resetAutoScrollTimer();
}
},
itemBuilder: (context, index) {
return _buildHeroItem(_onDeck[index]);
@@ -486,57 +540,67 @@ class _DiscoverScreenState extends State<DiscoverScreen>
),
// Spacer to separate indicators from button
const SizedBox(width: 8),
// Page indicators
...List.generate(_onDeck.length, (index) {
final isActive = _currentHeroIndex == index;
if (isActive) {
// Animated progress indicator for active page
return AnimatedBuilder(
animation: _indicatorAnimationController,
builder: (context, child) {
// Fill width animates from 8px to 24px
final fillWidth =
8.0 +
(16.0 * _indicatorAnimationController.value);
// Page indicators (limited to 5 dots)
...() {
final range = _getVisibleDotRange();
return List.generate(
range.end - range.start + 1,
(i) {
final index = range.start + i;
final isActive = _currentHeroIndex == index;
final dotSize = _getDotSize(index, range.start, range.end);
if (isActive) {
// Animated progress indicator for active page
return AnimatedBuilder(
animation: _indicatorAnimationController,
builder: (context, child) {
// Fill width animates based on dot size
final maxWidth = dotSize * 3; // 24px for normal, 15px for small
final fillWidth =
dotSize +
((maxWidth - dotSize) * _indicatorAnimationController.value);
return AnimatedContainer(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
margin: const EdgeInsets.symmetric(horizontal: 4),
width: maxWidth,
height: dotSize,
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(dotSize / 2),
),
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: fillWidth,
height: dotSize,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(dotSize / 2),
),
),
),
);
},
);
} else {
// Static indicator for inactive pages
return AnimatedContainer(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
margin: const EdgeInsets.symmetric(horizontal: 4),
width: 24,
height: 8,
width: dotSize,
height: dotSize,
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(4),
),
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: fillWidth,
height: 8,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(4),
),
),
borderRadius: BorderRadius.circular(dotSize / 2),
),
);
},
);
} else {
// Static indicator for inactive pages
return AnimatedContainer(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
margin: const EdgeInsets.symmetric(horizontal: 4),
width: 8,
height: 8,
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(4),
),
);
}
}),
}
},
);
}(),
],
),
),
@@ -569,11 +633,7 @@ class _DiscoverScreenState extends State<DiscoverScreen>
if (client == null) return;
appLogger.d('Navigating to VideoPlayerScreen for: ${heroItem.title}');
navigateToVideoPlayer(
context,
metadata: heroItem,
userProfile: widget.userProfile,
);
navigateToVideoPlayer(context, metadata: heroItem);
},
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
@@ -799,9 +859,9 @@ class _DiscoverScreenState extends State<DiscoverScreen>
[
contentTypeLabel,
if (heroItem.rating != null)
'★ ${(heroItem.rating! / 10).toStringAsFixed(1)}',
'★ ${heroItem.rating!.toStringAsFixed(1)}',
if (heroItem.contentRating != null)
heroItem.contentRating!,
formatContentRating(heroItem.contentRating!),
if (heroItem.year != null)
heroItem.year.toString(),
].join(' • '),
@@ -898,11 +958,7 @@ class _DiscoverScreenState extends State<DiscoverScreen>
if (client == null) return;
appLogger.d('Playing: ${heroItem.title}');
navigateToVideoPlayer(
context,
metadata: heroItem,
userProfile: widget.userProfile,
);
navigateToVideoPlayer(context, metadata: heroItem);
},
borderRadius: BorderRadius.circular(24),
child: Container(
@@ -968,41 +1024,47 @@ class _DiscoverScreenState extends State<DiscoverScreen>
child: LayoutBuilder(
builder: (context, constraints) {
// Responsive card width based on screen size
// Match Libraries screen sizing (190px baseline)
final screenWidth = constraints.maxWidth;
final cardWidth = screenWidth > 1600
? 220.0
: screenWidth > 1200
? 200.0
: screenWidth > 800
? 160.0
: 130.0;
? 190.0
: 160.0;
// MediaCard has 8px padding on all sides (16px total horizontally)
// So actual poster width is cardWidth - 16
final posterWidth = cardWidth - 16;
// 2:3 poster aspect ratio (height is 1.5x width)
final cardHeight = cardWidth * 1.5;
final posterHeight = posterWidth * 1.5;
// Container height = poster + padding + spacing + text
// 8px top padding + cardHeight + 4px spacing + ~26px text + 8px bottom padding
final containerHeight = cardHeight + 46;
// 8px top padding + posterHeight + 4px spacing + ~26px text + 8px bottom padding
final containerHeight = posterHeight + 46;
return SizedBox(
height: containerHeight,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 12),
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
child: MediaCard(
key: Key(item.ratingKey),
item: item,
width: cardWidth,
height: cardHeight,
onRefresh: updateItem,
userProfile: widget.userProfile,
),
);
},
child: HorizontalScrollWithArrows(
builder: (scrollController) => ListView.builder(
controller: scrollController,
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 12),
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
child: MediaCard(
key: Key(item.ratingKey),
item: item,
width: cardWidth,
height: posterHeight,
onRefresh: updateItem,
),
);
},
),
),
);
},
File diff suppressed because it is too large Load Diff
+3 -6
View File
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import '../client/plex_client.dart';
import '../models/plex_user_profile.dart';
import '../utils/app_logger.dart';
import '../utils/provider_extensions.dart';
import '../main.dart';
@@ -12,9 +11,8 @@ import 'settings_screen.dart';
class MainScreen extends StatefulWidget {
final PlexClient client;
final PlexUserProfile? userProfile;
const MainScreen({super.key, required this.client, this.userProfile});
const MainScreen({super.key, required this.client});
@override
State<MainScreen> createState() => _MainScreenState();
@@ -36,11 +34,10 @@ class _MainScreenState extends State<MainScreen> with RouteAware {
_screens = [
DiscoverScreen(
key: _discoverKey,
userProfile: widget.userProfile,
onBecameVisible: _onDiscoverBecameVisible,
),
LibrariesScreen(key: _librariesKey, userProfile: widget.userProfile),
SearchScreen(key: _searchKey, userProfile: widget.userProfile),
LibrariesScreen(key: _librariesKey),
SearchScreen(key: _searchKey),
SettingsScreen(key: _settingsKey),
];
+5 -16
View File
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:provider/provider.dart';
import '../models/plex_metadata.dart';
import '../models/plex_user_profile.dart';
import '../providers/plex_client_provider.dart';
import '../widgets/desktop_app_bar.dart';
import '../widgets/app_bar_back_button.dart';
@@ -10,18 +9,14 @@ import '../widgets/media_context_menu.dart';
import '../utils/app_logger.dart';
import '../utils/provider_extensions.dart';
import '../utils/video_player_navigation.dart';
import '../utils/content_rating_formatter.dart';
import '../theme/theme_helper.dart';
import 'season_detail_screen.dart';
class MediaDetailScreen extends StatefulWidget {
final PlexMetadata metadata;
final PlexUserProfile? userProfile;
const MediaDetailScreen({
super.key,
required this.metadata,
this.userProfile,
});
const MediaDetailScreen({super.key, required this.metadata});
@override
State<MediaDetailScreen> createState() => _MediaDetailScreenState();
@@ -214,11 +209,7 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
if (client == null) return;
appLogger.d('Playing first episode: ${firstEpisode.title}');
await navigateToVideoPlayer(
context,
metadata: firstEpisode,
userProfile: widget.userProfile,
);
await navigateToVideoPlayer(context, metadata: firstEpisode);
appLogger.d('Returned from playback, refreshing metadata');
// Refresh metadata when returning from video player
_loadFullMetadata();
@@ -471,7 +462,7 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
borderRadius: BorderRadius.circular(6),
),
child: Text(
metadata.contentRating!,
formatContentRating(metadata.contentRating!),
style: const TextStyle(
color: Colors.white,
fontSize: 13,
@@ -541,7 +532,6 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
await navigateToVideoPlayer(
context,
metadata: _onDeckEpisode!,
userProfile: widget.userProfile,
);
appLogger.d(
'Returned from playback, refreshing metadata',
@@ -562,7 +552,6 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
await navigateToVideoPlayer(
context,
metadata: metadata,
userProfile: widget.userProfile,
);
appLogger.d(
'Returned from playback, refreshing metadata',
@@ -727,7 +716,7 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
const SizedBox(height: 12),
],
if (metadata.contentRating != null) ...[
_buildInfoRow('Rating', metadata.contentRating!),
_buildInfoRow('Rating', formatContentRating(metadata.contentRating!)),
const SizedBox(height: 12),
],
],
+54 -7
View File
@@ -1,8 +1,10 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../client/plex_client.dart';
import '../models/plex_metadata.dart';
import '../models/plex_user_profile.dart';
import '../services/settings_service.dart';
import '../providers/settings_provider.dart';
import '../utils/provider_extensions.dart';
import '../widgets/media_card.dart';
import '../widgets/desktop_app_bar.dart';
@@ -10,9 +12,7 @@ import '../mixins/refreshable.dart';
import '../mixins/item_updatable.dart';
class SearchScreen extends StatefulWidget {
final PlexUserProfile? userProfile;
const SearchScreen({super.key, this.userProfile});
const SearchScreen({super.key});
@override
State<SearchScreen> createState() => _SearchScreenState();
@@ -216,8 +216,11 @@ class _SearchScreenState extends State<SearchScreen>
SliverPadding(
padding: const EdgeInsets.all(16),
sliver: SliverGrid(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 180,
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: _getMaxCrossAxisExtent(
context,
context.watch<SettingsProvider>().libraryDensity,
),
childAspectRatio: 2 / 3.3,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
@@ -228,7 +231,6 @@ class _SearchScreenState extends State<SearchScreen>
key: Key(item.ratingKey),
item: item,
onRefresh: updateItem,
userProfile: widget.userProfile,
);
}, childCount: _searchResults.length),
),
@@ -238,4 +240,49 @@ class _SearchScreenState extends State<SearchScreen>
),
);
}
double _getMaxCrossAxisExtent(BuildContext context, LibraryDensity density) {
final screenWidth = MediaQuery.of(context).size.width;
final padding = 32.0; // 16px left + 16px right from SliverPadding
final availableWidth = screenWidth - padding;
if (screenWidth >= 900) {
// Wide screens (desktop/large tablet landscape): Responsive division
double divisor;
double maxItemWidth;
switch (density) {
case LibraryDensity.comfortable:
divisor = 6.5;
maxItemWidth = 280;
break;
case LibraryDensity.normal:
divisor = 8.0;
maxItemWidth = 200;
break;
case LibraryDensity.compact:
divisor = 10.0;
maxItemWidth = 160;
break;
}
return (availableWidth / divisor).clamp(0, maxItemWidth);
} else if (screenWidth >= 600) {
// Medium screens (tablets): Fixed 4-5-6 items
int targetItemCount = switch (density) {
LibraryDensity.comfortable => 4,
LibraryDensity.normal => 5,
LibraryDensity.compact => 6,
};
return availableWidth / targetItemCount;
} else {
// Small screens (phones): Fixed 2-3-4 items
int targetItemCount = switch (density) {
LibraryDensity.comfortable => 2,
LibraryDensity.normal => 3,
LibraryDensity.compact => 4,
};
return availableWidth / targetItemCount;
}
}
}
+2 -8
View File
@@ -3,7 +3,6 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:provider/provider.dart';
import '../client/plex_client.dart';
import '../models/plex_metadata.dart';
import '../models/plex_user_profile.dart';
import '../providers/plex_client_provider.dart';
import '../utils/provider_extensions.dart';
import '../utils/video_player_navigation.dart';
@@ -14,9 +13,8 @@ import '../theme/theme_helper.dart';
class SeasonDetailScreen extends StatefulWidget {
final PlexMetadata season;
final PlexUserProfile? userProfile;
const SeasonDetailScreen({super.key, required this.season, this.userProfile});
const SeasonDetailScreen({super.key, required this.season});
@override
State<SeasonDetailScreen> createState() => _SeasonDetailScreenState();
@@ -136,11 +134,7 @@ class _SeasonDetailScreenState extends State<SeasonDetailScreen>
metadata: episode,
onRefresh: updateItem,
onTap: () async {
await navigateToVideoPlayer(
context,
metadata: episode,
userProfile: widget.userProfile,
);
await navigateToVideoPlayer(context, metadata: episode);
// Refresh episodes when returning from video player
_loadEpisodes();
},
+247
View File
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import '../providers/theme_provider.dart';
import '../providers/settings_provider.dart';
import '../services/settings_service.dart' as settings;
import '../services/keyboard_shortcuts_service.dart';
import '../widgets/desktop_app_bar.dart';
@@ -23,6 +24,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
bool _enableDebugLogging = false;
bool _enableHardwareDecoding = true;
int _bufferSize = 128;
int _seekTimeSmall = 10;
int _seekTimeLarge = 30;
@override
void initState() {
@@ -38,10 +41,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
_enableDebugLogging = _settingsService.getEnableDebugLogging();
_enableHardwareDecoding = _settingsService.getEnableHardwareDecoding();
_bufferSize = _settingsService.getBufferSize();
_seekTimeSmall = _settingsService.getSeekTimeSmall();
_seekTimeLarge = _settingsService.getSeekTimeLarge();
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
if (_isLoading) {
@@ -99,6 +105,32 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
},
),
Consumer<SettingsProvider>(
builder: (context, settingsProvider, child) {
return ListTile(
leading: const Icon(Icons.grid_view),
title: const Text('Library Density'),
subtitle: Text(settingsProvider.libraryDensityDisplayName),
trailing: const Icon(Icons.chevron_right),
onTap: () => _showLibraryDensityDialog(),
);
},
),
Consumer<SettingsProvider>(
builder: (context, settingsProvider, child) {
return SwitchListTile(
secondary: const Icon(Icons.image),
title: const Text('Use Season Posters'),
subtitle: const Text(
'Show season poster instead of series poster for episodes',
),
value: settingsProvider.useSeasonPoster,
onChanged: (value) async {
await settingsProvider.setUseSeasonPoster(value);
},
);
},
),
],
),
);
@@ -137,6 +169,20 @@ class _SettingsScreenState extends State<SettingsScreen> {
trailing: const Icon(Icons.chevron_right),
onTap: () => _showBufferSizeDialog(),
),
ListTile(
leading: const Icon(Icons.replay_10),
title: const Text('Small Skip Duration'),
subtitle: Text('$_seekTimeSmall seconds'),
trailing: const Icon(Icons.chevron_right),
onTap: () => _showSeekTimeSmallDialog(),
),
ListTile(
leading: const Icon(Icons.replay_30),
title: const Text('Large Skip Duration'),
subtitle: Text('$_seekTimeLarge seconds'),
trailing: const Icon(Icons.chevron_right),
onTap: () => _showSeekTimeLargeDialog(),
),
],
),
);
@@ -328,6 +374,134 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
}
void _showSeekTimeSmallDialog() {
final controller = TextEditingController(text: _seekTimeSmall.toString());
String? errorText;
showDialog(
context: context,
builder: (BuildContext dialogContext) {
return StatefulBuilder(
builder: (context, setDialogState) {
return AlertDialog(
title: const Text('Small Skip Duration'),
content: TextField(
controller: controller,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Seconds',
hintText: 'Enter duration (1-120)',
errorText: errorText,
suffixText: 's',
),
autofocus: true,
onChanged: (value) {
final parsed = int.tryParse(value);
setDialogState(() {
if (parsed == null) {
errorText = 'Please enter a valid number';
} else if (parsed < 1 || parsed > 120) {
errorText = 'Duration must be between 1 and 120 seconds';
} else {
errorText = null;
}
});
},
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: const Text('Cancel'),
),
TextButton(
onPressed: () async {
final parsed = int.tryParse(controller.text);
if (parsed != null && parsed >= 1 && parsed <= 120) {
setState(() {
_seekTimeSmall = parsed;
_settingsService.setSeekTimeSmall(parsed);
});
// Reload keyboard shortcuts service to use new settings
await _keyboardService.refreshFromStorage();
if (dialogContext.mounted) {
Navigator.pop(dialogContext);
}
}
},
child: const Text('Save'),
),
],
);
},
);
},
);
}
void _showSeekTimeLargeDialog() {
final controller = TextEditingController(text: _seekTimeLarge.toString());
String? errorText;
showDialog(
context: context,
builder: (BuildContext dialogContext) {
return StatefulBuilder(
builder: (context, setDialogState) {
return AlertDialog(
title: const Text('Large Skip Duration'),
content: TextField(
controller: controller,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Seconds',
hintText: 'Enter duration (1-120)',
errorText: errorText,
suffixText: 's',
),
autofocus: true,
onChanged: (value) {
final parsed = int.tryParse(value);
setDialogState(() {
if (parsed == null) {
errorText = 'Please enter a valid number';
} else if (parsed < 1 || parsed > 120) {
errorText = 'Duration must be between 1 and 120 seconds';
} else {
errorText = null;
}
});
},
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: const Text('Cancel'),
),
TextButton(
onPressed: () async {
final parsed = int.tryParse(controller.text);
if (parsed != null && parsed >= 1 && parsed <= 120) {
setState(() {
_seekTimeLarge = parsed;
_settingsService.setSeekTimeLarge(parsed);
});
// Reload keyboard shortcuts service to use new settings
await _keyboardService.refreshFromStorage();
if (dialogContext.mounted) {
Navigator.pop(dialogContext);
}
}
},
child: const Text('Save'),
),
],
);
},
);
},
);
}
void _showKeyboardShortcutsDialog() {
Navigator.push(
context,
@@ -410,6 +584,79 @@ class _SettingsScreenState extends State<SettingsScreen> {
},
);
}
void _showLibraryDensityDialog() {
final settingsProvider = context.read<SettingsProvider>();
showDialog(
context: context,
builder: (BuildContext context) {
return Consumer<SettingsProvider>(
builder: (context, provider, child) {
return AlertDialog(
title: const Text('Library Density'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
leading: Icon(
provider.libraryDensity == settings.LibraryDensity.compact
? Icons.radio_button_checked
: Icons.radio_button_unchecked,
),
title: const Text('Compact'),
subtitle: const Text('Smaller cards, more items visible'),
onTap: () async {
await settingsProvider.setLibraryDensity(
settings.LibraryDensity.compact,
);
if (context.mounted) Navigator.pop(context);
},
),
ListTile(
leading: Icon(
provider.libraryDensity == settings.LibraryDensity.normal
? Icons.radio_button_checked
: Icons.radio_button_unchecked,
),
title: const Text('Normal'),
subtitle: const Text('Default size'),
onTap: () async {
await settingsProvider.setLibraryDensity(
settings.LibraryDensity.normal,
);
if (context.mounted) Navigator.pop(context);
},
),
ListTile(
leading: Icon(
provider.libraryDensity ==
settings.LibraryDensity.comfortable
? Icons.radio_button_checked
: Icons.radio_button_unchecked,
),
title: const Text('Comfortable'),
subtitle: const Text('Larger cards, fewer items visible'),
onTap: () async {
await settingsProvider.setLibraryDensity(
settings.LibraryDensity.comfortable,
);
if (context.mounted) Navigator.pop(context);
},
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
],
);
},
);
},
);
}
}
class _KeyboardShortcutsScreen extends StatefulWidget {
+243 -70
View File
@@ -14,13 +14,14 @@ import '../services/settings_service.dart';
import '../utils/orientation_helper.dart';
import '../utils/video_player_navigation.dart';
import '../utils/platform_detector.dart';
import '../models/plex_media_version.dart';
class VideoPlayerScreen extends StatefulWidget {
final PlexMetadata metadata;
final AudioTrack? preferredAudioTrack;
final SubtitleTrack? preferredSubtitleTrack;
final double? preferredPlaybackRate;
final PlexUserProfile? userProfile;
final int selectedMediaIndex;
const VideoPlayerScreen({
super.key,
@@ -28,7 +29,7 @@ class VideoPlayerScreen extends StatefulWidget {
this.preferredAudioTrack,
this.preferredSubtitleTrack,
this.preferredPlaybackRate,
this.userProfile,
this.selectedMediaIndex = 0,
});
@override
@@ -46,15 +47,13 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
bool _showPlayNextDialog = false;
PlexClientProvider? _cachedClientProvider;
bool _isPhone = false;
List<PlexMediaVersion> _availableVersions = [];
@override
void initState() {
super.initState();
appLogger.d('VideoPlayerScreen initialized for: ${widget.metadata.title}');
if (widget.userProfile != null) {
appLogger.d('Using user profile for track selection');
}
if (widget.preferredAudioTrack != null) {
appLogger.d(
'Preferred audio track: ${widget.preferredAudioTrack!.title ?? widget.preferredAudioTrack!.id} (${widget.preferredAudioTrack!.language ?? "unknown"})',
@@ -102,15 +101,23 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
final settingsService = await SettingsService.getInstance();
final bufferSizeMB = settingsService.getBufferSize();
final bufferSizeBytes = bufferSizeMB * 1024 * 1024;
final enableHardwareDecoding = settingsService.getEnableHardwareDecoding();
// Create player with configuration
player = Player(
configuration: PlayerConfiguration(
libass: true,
libassAndroidFont: 'assets/droid-sans.ttf',
libassAndroidFontName: 'Droid Sans Fallback',
bufferSize: bufferSizeBytes,
),
);
controller = VideoController(player!);
controller = VideoController(
player!,
configuration: VideoControllerConfiguration(
enableHardwareAcceleration: enableHardwareDecoding,
),
);
// Notify that player is ready
if (mounted) {
@@ -122,6 +129,9 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
// Get the video URL and start playback
_startPlayback();
// Load available media versions
_loadMediaVersions();
// Set fullscreen mode and landscape orientation
if (mounted) {
try {
@@ -185,8 +195,11 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
throw Exception('No client available');
}
// Get the direct file URL from the server
final videoUrl = await client.getVideoUrl(widget.metadata.ratingKey);
// Get the direct file URL from the server using the selected media index
final videoUrl = await client.getVideoUrl(
widget.metadata.ratingKey,
mediaIndex: widget.selectedMediaIndex,
);
if (videoUrl != null) {
// Open video without auto-playing
@@ -229,6 +242,24 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
}
}
/// Load available media versions for this item
Future<void> _loadMediaVersions() async {
try {
final clientProvider = context.plexClient;
final client = clientProvider.client;
if (client == null) return;
final versions = await client.getMediaVersions(widget.metadata.ratingKey);
if (mounted) {
setState(() {
_availableVersions = versions;
});
}
} catch (e) {
appLogger.e('Error loading media versions: $e');
}
}
@override
void dispose() {
// Stop progress tracking
@@ -344,7 +375,7 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
) {
appLogger.d('Audio track selection using user profile');
appLogger.d(
'Profile settings - autoSelectAudio: ${profile.autoSelectAudio}, defaultAudioLanguage: ${profile.defaultAudioLanguage}',
'Profile settings - autoSelectAudio: ${profile.autoSelectAudio}, defaultAudioLanguage: ${profile.defaultAudioLanguage}, defaultAudioLanguages: ${profile.defaultAudioLanguages}',
);
if (availableTracks.isEmpty || !profile.autoSelectAudio) {
@@ -354,31 +385,43 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
return null;
}
final preferredLanguage = profile.defaultAudioLanguage;
if (preferredLanguage == null || preferredLanguage.isEmpty) {
appLogger.d('Cannot use profile: No defaultAudioLanguage specified');
// Build list of preferred languages
final preferredLanguages = <String>[];
if (profile.defaultAudioLanguage != null && profile.defaultAudioLanguage!.isNotEmpty) {
preferredLanguages.add(profile.defaultAudioLanguage!);
}
if (profile.defaultAudioLanguages != null) {
preferredLanguages.addAll(profile.defaultAudioLanguages!);
}
if (preferredLanguages.isEmpty) {
appLogger.d('Cannot use profile: No defaultAudioLanguage(s) specified');
return null;
}
// Get all possible language code variations (e.g., "en" → ["en", "eng"])
final languageVariations = LanguageCodes.getVariations(preferredLanguage);
appLogger.d(
'Checking language variations: ${languageVariations.join(", ")}',
);
appLogger.d('Preferred languages: ${preferredLanguages.join(", ")}');
// Try to find track matching any language variation
for (var track in availableTracks) {
final trackLang = track.language?.toLowerCase();
if (trackLang != null && languageVariations.contains(trackLang)) {
appLogger.d(
'Found audio track matching profile language "$preferredLanguage" (matched: "$trackLang"): ${track.title ?? "Track ${track.id}"}',
);
return track;
// Try to find track matching any preferred language
for (final preferredLanguage in preferredLanguages) {
// Get all possible language code variations (e.g., "en" → ["en", "eng"])
final languageVariations = LanguageCodes.getVariations(preferredLanguage);
appLogger.d(
'Checking language variations for "$preferredLanguage": ${languageVariations.join(", ")}',
);
for (var track in availableTracks) {
final trackLang = track.language?.toLowerCase();
if (trackLang != null && languageVariations.contains(trackLang)) {
appLogger.d(
'Found audio track matching profile language "$preferredLanguage" (matched: "$trackLang"): ${track.title ?? "Track ${track.id}"}',
);
return track;
}
}
}
appLogger.d(
'No audio track found matching profile language "$preferredLanguage" or its variations',
'No audio track found matching profile languages or their variations',
);
return null;
}
@@ -403,11 +446,12 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
SubtitleTrack? _findSubtitleTrackByProfile(
List<SubtitleTrack> availableTracks,
PlexUserProfile profile,
) {
PlexUserProfile profile, {
AudioTrack? selectedAudioTrack,
}) {
appLogger.d('Subtitle track selection using user profile');
appLogger.d(
'Profile settings - autoSelectSubtitle: ${profile.autoSelectSubtitle}, defaultSubtitleLanguage: ${profile.defaultSubtitleLanguage}, defaultSubtitleForced: ${profile.defaultSubtitleForced}',
'Profile settings - autoSelectSubtitle: ${profile.autoSelectSubtitle}, defaultSubtitleLanguage: ${profile.defaultSubtitleLanguage}, defaultSubtitleLanguages: ${profile.defaultSubtitleLanguages}, defaultSubtitleForced: ${profile.defaultSubtitleForced}, defaultSubtitleAccessibility: ${profile.defaultSubtitleAccessibility}',
);
if (availableTracks.isEmpty) {
@@ -415,68 +459,191 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
return null;
}
// If autoSelectSubtitle is 0, don't select any subtitle
if (!profile.shouldAutoSelectSubtitle) {
// Mode 0: Manually selected - return OFF
if (profile.autoSelectSubtitle == 0) {
appLogger.d(
'Profile specifies no auto-select (autoSelectSubtitle=0) - Subtitles OFF',
'Profile specifies manual mode (autoSelectSubtitle=0) - Subtitles OFF',
);
return SubtitleTrack.no();
}
final preferredLanguage = profile.defaultSubtitleLanguage;
if (preferredLanguage == null || preferredLanguage.isEmpty) {
appLogger.d('Cannot use profile: No defaultSubtitleLanguage specified');
// Mode 1: Shown with foreign audio
if (profile.autoSelectSubtitle == 1) {
appLogger.d('Profile specifies foreign audio mode (autoSelectSubtitle=1)');
// Check if audio language matches user's preferred subtitle language
if (selectedAudioTrack != null && profile.defaultSubtitleLanguage != null) {
final audioLang = selectedAudioTrack.language?.toLowerCase();
final prefLang = profile.defaultSubtitleLanguage!.toLowerCase();
final languageVariations = LanguageCodes.getVariations(prefLang);
appLogger.d('Checking if audio is foreign - audio: $audioLang, preferred subtitle lang: $prefLang');
// If audio matches preferred language, no subtitles needed
if (audioLang != null && languageVariations.contains(audioLang)) {
appLogger.d('Audio matches preferred language - Subtitles OFF');
return SubtitleTrack.no();
}
appLogger.d('Foreign audio detected - enabling subtitles');
}
// Foreign audio detected or cannot determine, enable subtitles
}
// Mode 2: Always enabled (or continuing from mode 1 with foreign audio)
appLogger.d('Selecting subtitle track based on preferences');
// Build list of preferred languages
final preferredLanguages = <String>[];
if (profile.defaultSubtitleLanguage != null && profile.defaultSubtitleLanguage!.isNotEmpty) {
preferredLanguages.add(profile.defaultSubtitleLanguage!);
}
if (profile.defaultSubtitleLanguages != null) {
preferredLanguages.addAll(profile.defaultSubtitleLanguages!);
}
if (preferredLanguages.isEmpty) {
appLogger.d('Cannot use profile: No defaultSubtitleLanguage(s) specified');
return null;
}
// Get all possible language code variations (e.g., "en" → ["en", "eng"])
final languageVariations = LanguageCodes.getVariations(preferredLanguage);
appLogger.d(
'Checking language variations: ${languageVariations.join(", ")}',
);
appLogger.d('Preferred languages: ${preferredLanguages.join(", ")}');
// If defaultSubtitleForced is 1, prefer forced subtitles
if (profile.preferForcedSubtitles) {
appLogger.d('Profile prefers forced subtitles (defaultSubtitleForced=1)');
// Try to find forced subtitle in preferred language
for (var track in availableTracks) {
// Apply filtering based on preferences
var candidateTracks = availableTracks;
// Filter by SDH (defaultSubtitleAccessibility: 0-3)
candidateTracks = _filterSubtitlesBySDH(candidateTracks, profile.defaultSubtitleAccessibility);
// Filter by forced subtitle preference (defaultSubtitleForced: 0-3)
candidateTracks = _filterSubtitlesByForced(candidateTracks, profile.defaultSubtitleForced);
// If no candidates after filtering, relax filters
if (candidateTracks.isEmpty) {
appLogger.d('No tracks match strict filters, relaxing filters');
candidateTracks = availableTracks;
}
// Try to find track matching any preferred language
for (final preferredLanguage in preferredLanguages) {
final languageVariations = LanguageCodes.getVariations(preferredLanguage);
appLogger.d(
'Checking language variations for "$preferredLanguage": ${languageVariations.join(", ")}',
);
for (var track in candidateTracks) {
final trackLang = track.language?.toLowerCase();
if (trackLang != null &&
languageVariations.contains(trackLang) &&
track.title?.toLowerCase().contains('forced') == true) {
if (trackLang != null && languageVariations.contains(trackLang)) {
appLogger.d(
'Found forced subtitle matching profile language "$preferredLanguage" (matched: "$trackLang"): ${track.title ?? "Track ${track.id}"}',
'Found subtitle matching profile language "$preferredLanguage" (matched: "$trackLang"): ${track.title ?? "Track ${track.id}"}',
);
return track;
}
}
appLogger.d(
'No forced subtitle found in "$preferredLanguage" or its variations, trying regular subtitles',
);
}
// Try to find regular subtitle in preferred language
for (var track in availableTracks) {
final trackLang = track.language?.toLowerCase();
if (trackLang != null && languageVariations.contains(trackLang)) {
appLogger.d(
'Found subtitle matching profile language "$preferredLanguage" (matched: "$trackLang"): ${track.title ?? "Track ${track.id}"}',
);
return track;
}
}
appLogger.d(
'No subtitle track found matching profile language "$preferredLanguage" or its variations',
'No subtitle track found matching profile languages or their variations',
);
return null;
}
/// Filters subtitle tracks based on SDH (Subtitles for Deaf or Hard-of-Hearing) preference
///
/// Values:
/// - 0: Prefer non-SDH subtitles
/// - 1: Prefer SDH subtitles
/// - 2: Only show SDH subtitles
/// - 3: Only show non-SDH subtitles
List<SubtitleTrack> _filterSubtitlesBySDH(
List<SubtitleTrack> tracks,
int preference,
) {
if (preference == 0 || preference == 1) {
// Prefer but don't require
final preferSDH = preference == 1;
final preferred = tracks.where((t) => _isSDH(t) == preferSDH).toList();
if (preferred.isNotEmpty) {
appLogger.d('Applying SDH preference: ${preferSDH ? "prefer SDH" : "prefer non-SDH"} (${preferred.length} tracks)');
return preferred;
}
appLogger.d('No tracks match SDH preference, using all tracks');
return tracks;
} else if (preference == 2) {
// Only SDH
final filtered = tracks.where(_isSDH).toList();
appLogger.d('Filtering to SDH only (${filtered.length} tracks)');
return filtered;
} else if (preference == 3) {
// Only non-SDH
final filtered = tracks.where((t) => !_isSDH(t)).toList();
appLogger.d('Filtering to non-SDH only (${filtered.length} tracks)');
return filtered;
}
return tracks;
}
/// Filters subtitle tracks based on forced subtitle preference
///
/// Values:
/// - 0: Prefer non-forced subtitles
/// - 1: Prefer forced subtitles
/// - 2: Only show forced subtitles
/// - 3: Only show non-forced subtitles
List<SubtitleTrack> _filterSubtitlesByForced(
List<SubtitleTrack> tracks,
int preference,
) {
if (preference == 0 || preference == 1) {
// Prefer but don't require
final preferForced = preference == 1;
final preferred = tracks.where((t) => _isForced(t) == preferForced).toList();
if (preferred.isNotEmpty) {
appLogger.d('Applying forced preference: ${preferForced ? "prefer forced" : "prefer non-forced"} (${preferred.length} tracks)');
return preferred;
}
appLogger.d('No tracks match forced preference, using all tracks');
return tracks;
} else if (preference == 2) {
// Only forced
final filtered = tracks.where(_isForced).toList();
appLogger.d('Filtering to forced only (${filtered.length} tracks)');
return filtered;
} else if (preference == 3) {
// Only non-forced
final filtered = tracks.where((t) => !_isForced(t)).toList();
appLogger.d('Filtering to non-forced only (${filtered.length} tracks)');
return filtered;
}
return tracks;
}
/// Checks if a subtitle track is SDH (Subtitles for Deaf or Hard-of-Hearing)
///
/// Since media_kit may not expose this directly, we infer from the title
bool _isSDH(SubtitleTrack track) {
final title = track.title?.toLowerCase() ?? '';
// Look for common SDH indicators
return title.contains('sdh') ||
title.contains('cc') ||
title.contains('hearing impaired') ||
title.contains('deaf');
}
/// Checks if a subtitle track is forced
bool _isForced(SubtitleTrack track) {
final title = track.title?.toLowerCase() ?? '';
return title.contains('forced');
}
void _waitForTracksAndApply() async {
// Helper function to process tracks
Future<void> processTracks(Tracks tracks) async {
appLogger.d('Starting track selection process');
// Get profile settings for track selection
final profileSettings = context.profileSettings;
// Get real tracks (excluding auto and no)
final realAudioTracks = tracks.audio
.where((t) => t.id != 'auto' && t.id != 'no')
@@ -523,11 +690,11 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
}
// Priority 2: If no preferred track matched, try user profile preferences
if (trackToSelect == null && widget.userProfile != null) {
if (trackToSelect == null && profileSettings != null) {
appLogger.d('Priority 2: Checking user profile preferences');
trackToSelect = _findAudioTrackByProfile(
realAudioTracks,
widget.userProfile!,
profileSettings,
);
} else if (trackToSelect == null) {
appLogger.d('Priority 2: No user profile available');
@@ -585,12 +752,18 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
// Priority 2: If no preferred match, apply user profile preferences
if (subtitleToSelect == null &&
widget.userProfile != null &&
profileSettings != null &&
realSubtitleTracks.isNotEmpty) {
appLogger.d('Priority 2: Checking user profile preferences');
// Get the currently selected audio track
final currentAudioTrack = realAudioTracks.firstWhere(
(t) => t.id == player!.state.track.audio.id,
orElse: () => realAudioTracks.first,
);
subtitleToSelect = _findSubtitleTrackByProfile(
realSubtitleTracks,
widget.userProfile!,
profileSettings,
selectedAudioTrack: currentAudioTrack,
);
} else if (subtitleToSelect == null && realSubtitleTracks.isNotEmpty) {
appLogger.d('Priority 2: No user profile available');
@@ -720,7 +893,6 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
navigateToVideoPlayer(
context,
metadata: episodeMetadata,
userProfile: widget.userProfile,
usePushReplacement: true,
);
}
@@ -745,7 +917,6 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
preferredAudioTrack: currentAudioTrack,
preferredSubtitleTrack: currentSubtitleTrack,
preferredPlaybackRate: currentRate,
userProfile: widget.userProfile,
usePushReplacement: true,
);
}
@@ -783,6 +954,8 @@ class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
widget.metadata,
onNext: _nextEpisode != null ? _playNext : null,
onPrevious: _previousEpisode != null ? _playPrevious : null,
availableVersions: _availableVersions,
selectedMediaIndex: widget.selectedMediaIndex,
),
),
),
+29 -12
View File
@@ -10,6 +10,8 @@ class KeyboardShortcutsService {
Map<String, String> _shortcuts =
{}; // Legacy string shortcuts for backward compatibility
Map<String, HotKey> _hotkeys = {}; // New HotKey objects
int _seekTimeSmall = 10; // Default, loaded from settings
int _seekTimeLarge = 30; // Default, loaded from settings
KeyboardShortcutsService._();
@@ -28,6 +30,8 @@ class KeyboardShortcutsService {
_shortcuts = _settingsService
.getKeyboardShortcuts(); // Keep for legacy compatibility
_hotkeys = await _settingsService.getKeyboardHotkeys(); // Primary method
_seekTimeSmall = _settingsService.getSeekTimeSmall();
_seekTimeLarge = _settingsService.getSeekTimeLarge();
}
Map<String, String> get shortcuts => Map.from(_shortcuts);
@@ -61,6 +65,8 @@ class KeyboardShortcutsService {
Future<void> refreshFromStorage() async {
_hotkeys = await _settingsService.getKeyboardHotkeys();
_seekTimeSmall = _settingsService.getSeekTimeSmall();
_seekTimeLarge = _settingsService.getSeekTimeLarge();
}
Future<void> resetToDefaults() async {
@@ -224,6 +230,21 @@ class KeyboardShortcutsService {
return KeyEventResult.ignored;
}
/// Seeks by the given offset (can be positive or negative) while clamping
/// the result between 0 and the video duration
void _seekWithClamping(Player player, Duration offset) {
final currentPosition = player.state.position;
final duration = player.state.duration;
final newPosition = currentPosition + offset;
// Clamp between 0 and video duration
final clampedPosition = newPosition.isNegative
? Duration.zero
: (newPosition > duration ? duration : newPosition);
player.seek(clampedPosition);
}
void _executeAction(
String action,
Player player,
@@ -247,20 +268,16 @@ class KeyboardShortcutsService {
player.setVolume(newVolume);
break;
case 'seek_forward':
final newPosition = player.state.position + const Duration(seconds: 10);
player.seek(newPosition);
_seekWithClamping(player, Duration(seconds: _seekTimeSmall));
break;
case 'seek_backward':
final newPosition = player.state.position - const Duration(seconds: 10);
player.seek(newPosition.isNegative ? Duration.zero : newPosition);
_seekWithClamping(player, Duration(seconds: -_seekTimeSmall));
break;
case 'seek_forward_large':
final newPosition = player.state.position + const Duration(seconds: 30);
player.seek(newPosition);
_seekWithClamping(player, Duration(seconds: _seekTimeLarge));
break;
case 'seek_backward_large':
final newPosition = player.state.position - const Duration(seconds: 30);
player.seek(newPosition.isNegative ? Duration.zero : newPosition);
_seekWithClamping(player, Duration(seconds: -_seekTimeLarge));
break;
case 'fullscreen_toggle':
onToggleFullscreen?.call();
@@ -307,13 +324,13 @@ class KeyboardShortcutsService {
case 'volume_down':
return 'Volume Down';
case 'seek_forward':
return 'Seek Forward';
return 'Seek Forward (${_seekTimeSmall}s)';
case 'seek_backward':
return 'Seek Backward';
return 'Seek Backward (${_seekTimeSmall}s)';
case 'seek_forward_large':
return 'Seek Forward (Large)';
return 'Seek Forward (${_seekTimeLarge}s)';
case 'seek_backward_large':
return 'Seek Backward (Large)';
return 'Seek Backward (${_seekTimeLarge}s)';
case 'fullscreen_toggle':
return 'Toggle Fullscreen';
case 'mute_toggle':
+115 -47
View File
@@ -179,8 +179,9 @@ class PlexAuthService {
/// Switch to a different user in the home
Future<UserSwitchResponse> switchToUser(
String userUUID,
String currentToken,
) async {
String currentToken, {
String? pin,
}) async {
final queryParams = {
'includeSubscriptions': '1',
'includeProviders': '1',
@@ -193,6 +194,7 @@ class PlexAuthService {
'X-Plex-Platform-Version': '3.8.1',
'X-Plex-Token': currentToken,
'X-Plex-Language': 'en',
if (pin != null) 'pin': pin,
};
final queryString = queryParams.entries
@@ -213,6 +215,15 @@ class PlexAuthService {
}
}
/// Helper class to track connection candidates during testing
class _ConnectionCandidate {
final PlexConnection connection;
final String url;
final bool isPlexDirectUri;
_ConnectionCandidate(this.connection, this.url, this.isPlexDirectUri);
}
/// Represents a Plex Media Server
class PlexServer {
final String name;
@@ -302,103 +313,156 @@ class PlexServer {
return _selectBest(connections);
}
PlexConnection? _findLowestLatency(
List<MapEntry<PlexConnection, ConnectionTestResult>> entries,
) {
if (entries.isEmpty) return null;
final bestEntry = entries.reduce(
(a, b) => a.value.latencyMs < b.value.latencyMs ? a : b,
);
return bestEntry.key;
}
PlexConnection? _selectBestWithLatency(
Map<PlexConnection, ConnectionTestResult> results,
) {
final localEntries = results.entries
.where((e) => e.key.local && !e.key.relay)
.toList();
final remoteEntries = results.entries
.where((e) => !e.key.local && !e.key.relay)
.toList();
final relayEntries = results.entries.where((e) => e.key.relay).toList();
return _findLowestLatency(localEntries) ??
_findLowestLatency(remoteEntries) ??
_findLowestLatency(relayEntries);
}
/// Find the best working connection by testing them
/// Returns a Stream that emits connections progressively:
/// 1. First emission: The first connection that responds successfully
/// 2. Second emission (optional): The best connection after latency testing
/// Priority: local > remote > relay (from successful connections)
/// Tests both plex.direct URI and direct IP for each connection
Stream<PlexConnection> findBestWorkingConnection() async* {
if (connections.isEmpty) return;
// Create candidates: test both uri and directUrl for each connection
final candidates = <_ConnectionCandidate>[];
for (final connection in connections) {
candidates.add(_ConnectionCandidate(connection, connection.uri, true));
candidates.add(_ConnectionCandidate(connection, connection.directUrl, false));
}
// Phase 1: Race to find first working connection
final completer = Completer<PlexConnection?>();
PlexConnection? firstConnection;
final completer = Completer<_ConnectionCandidate?>();
_ConnectionCandidate? firstCandidate;
int completedTests = 0;
// Start testing all connections simultaneously
for (final connection in connections) {
PlexClient.testConnectionWithLatency(connection.uri, accessToken).then((
// Start testing all candidates simultaneously
for (final candidate in candidates) {
PlexClient.testConnectionWithLatency(candidate.url, accessToken).then((
result,
) {
completedTests++;
// If this is the first successful connection, emit it immediately
if (result.success && !completer.isCompleted) {
completer.complete(connection);
completer.complete(candidate);
}
// If all tests complete without success, complete with null
if (completedTests == connections.length && !completer.isCompleted) {
if (completedTests == candidates.length && !completer.isCompleted) {
completer.complete(null);
}
});
}
// Wait for and emit the first successful connection
firstConnection = await completer.future;
if (firstConnection == null) {
firstCandidate = await completer.future;
if (firstCandidate == null) {
return; // No working connections found
}
// Update the connection object to use the working URL
final firstConnection = _updateConnectionUrl(
firstCandidate.connection,
firstCandidate.url,
);
yield firstConnection;
// Phase 2: Continue testing in background to find best connection
// Test each connection 2-3 times and average the latency
final connectionResults = <PlexConnection, ConnectionTestResult>{};
// Test each candidate 2-3 times and average the latency
final candidateResults = <_ConnectionCandidate, ConnectionTestResult>{};
await Future.wait(
connections.map((connection) async {
candidates.map((candidate) async {
final result = await PlexClient.testConnectionWithAverageLatency(
connection.uri,
candidate.url,
accessToken,
attempts: 2,
);
if (result.success) {
connectionResults[connection] = result;
candidateResults[candidate] = result;
}
}),
);
// If no connections succeeded, we're done
if (connectionResults.isEmpty) {
if (candidateResults.isEmpty) {
return;
}
// Find the best connection considering both priority and latency
final bestConnection = _selectBestWithLatency(connectionResults);
// Find the best connection considering priority, latency, and URL type
final bestCandidate = _selectBestCandidateWithLatency(candidateResults);
// Emit the best connection if it's different from the first one
if (bestConnection != null && bestConnection.uri != firstConnection.uri) {
yield bestConnection;
if (bestCandidate != null) {
final bestConnection = _updateConnectionUrl(
bestCandidate.connection,
bestCandidate.url,
);
if (bestConnection.uri != firstConnection.uri) {
yield bestConnection;
}
}
}
/// Update a connection's URI to use the specified URL
PlexConnection _updateConnectionUrl(PlexConnection connection, String url) {
// If the URL matches the original URI, return as-is
if (url == connection.uri) {
return connection;
}
// Otherwise, create a new connection with the directUrl as the uri
return PlexConnection(
protocol: connection.protocol,
address: connection.address,
port: connection.port,
uri: url,
local: connection.local,
relay: connection.relay,
ipv6: connection.ipv6,
);
}
/// Select the best candidate considering priority, latency, and URL type preference
_ConnectionCandidate? _selectBestCandidateWithLatency(
Map<_ConnectionCandidate, ConnectionTestResult> results,
) {
// Group candidates by connection type (local/remote/relay)
final localCandidates = results.entries
.where((e) => e.key.connection.local && !e.key.connection.relay)
.toList();
final remoteCandidates = results.entries
.where((e) => !e.key.connection.local && !e.key.connection.relay)
.toList();
final relayCandidates = results.entries
.where((e) => e.key.connection.relay)
.toList();
// Find best in each category
return _findLowestLatencyCandidate(localCandidates) ??
_findLowestLatencyCandidate(remoteCandidates) ??
_findLowestLatencyCandidate(relayCandidates);
}
/// Find the candidate with lowest latency, preferring plex.direct URI on tie
_ConnectionCandidate? _findLowestLatencyCandidate(
List<MapEntry<_ConnectionCandidate, ConnectionTestResult>> entries,
) {
if (entries.isEmpty) return null;
// Sort by latency first, then by URL type (prefer plex.direct)
entries.sort((a, b) {
final latencyCompare = a.value.latencyMs.compareTo(b.value.latencyMs);
if (latencyCompare != 0) return latencyCompare;
// If latencies are equal, prefer plex.direct URI (isPlexDirectUri = true)
if (a.key.isPlexDirectUri && !b.key.isPlexDirectUri) return -1;
if (!a.key.isPlexDirectUri && b.key.isPlexDirectUri) return 1;
return 0;
});
return entries.first.key;
}
}
/// Represents a connection to a Plex server
@@ -445,6 +509,10 @@ class PlexConnection {
};
}
/// Get the direct URL constructed from address and port
/// This bypasses plex.direct DNS and connects directly to the IP
String get directUrl => '$protocol://$address:$port';
String get displayType {
if (relay) return 'Relay';
if (local) return 'Local';
+5 -38
View File
@@ -87,7 +87,7 @@ class ServerConnectionService {
// Fetch user profile if requested
PlexUserProfile? userProfile;
if (fetchUserProfile && plexToken != null) {
userProfile = await _fetchAndCacheUserProfile(plexToken);
userProfile = await _fetchUserProfile(plexToken);
}
// Return success result
@@ -123,17 +123,15 @@ class ServerConnectionService {
}
}
/// Fetch user profile from Plex API and cache it locally
static Future<PlexUserProfile?> _fetchAndCacheUserProfile(
String plexToken,
) async {
/// Fetch user profile from Plex API
static Future<PlexUserProfile?> _fetchUserProfile(String plexToken) async {
appLogger.d('Fetching user profile from Plex API');
try {
final authService = await PlexAuthService.create();
final profile = await authService.getUserProfile(plexToken);
appLogger.i(
'Successfully fetched user profile',
'Successfully fetched user profile from API',
error: {
'autoSelectAudio': profile.autoSelectAudio,
'defaultAudioLanguage': profile.defaultAudioLanguage ?? 'not set',
@@ -144,40 +142,9 @@ class ServerConnectionService {
},
);
// Cache the profile
final storage = await StorageService.getInstance();
await storage.saveUserProfile(profile.toJson());
appLogger.d('User profile cached locally');
return profile;
} catch (e) {
appLogger.w(
'Failed to fetch user profile from API, attempting to load from cache',
error: e,
);
// Failed to fetch profile, try to load from cache
final storage = await StorageService.getInstance();
final cachedProfile = storage.getUserProfile();
if (cachedProfile != null) {
final profile = PlexUserProfile.fromJson(cachedProfile);
appLogger.i(
'Loaded user profile from cache',
error: {
'autoSelectAudio': profile.autoSelectAudio,
'defaultAudioLanguage': profile.defaultAudioLanguage ?? 'not set',
'autoSelectSubtitle': profile.autoSelectSubtitle,
'defaultSubtitleLanguage':
profile.defaultSubtitleLanguage ?? 'not set',
'defaultSubtitleForced': profile.defaultSubtitleForced,
},
);
return profile;
}
appLogger.w(
'No cached user profile available, track selection will use defaults',
);
appLogger.w('Failed to fetch user profile from API', error: e);
return null;
}
}
+97
View File
@@ -5,6 +5,8 @@ import 'package:hotkey_manager/hotkey_manager.dart';
enum ThemeMode { system, light, dark }
enum LibraryDensity { compact, normal, comfortable }
class SettingsService {
static const String _keyThemeMode = 'theme_mode';
static const String _keyEnableDebugLogging = 'enable_debug_logging';
@@ -14,6 +16,11 @@ class SettingsService {
static const String _keyEnableHardwareDecoding = 'enable_hardware_decoding';
static const String _keyPreferredVideoCodec = 'preferred_video_codec';
static const String _keyPreferredAudioCodec = 'preferred_audio_codec';
static const String _keyLibraryDensity = 'library_density';
static const String _keyUseSeasonPoster = 'use_season_poster';
static const String _keySeekTimeSmall = 'seek_time_small';
static const String _keySeekTimeLarge = 'seek_time_large';
static const String _keyMediaVersionPreferences = 'media_version_preferences';
static SettingsService? _instance;
late SharedPreferences _prefs;
@@ -91,6 +98,46 @@ class SettingsService {
return _prefs.getString(_keyPreferredAudioCodec) ?? 'auto';
}
// Library Density
Future<void> setLibraryDensity(LibraryDensity density) async {
await _prefs.setString(_keyLibraryDensity, density.name);
}
LibraryDensity getLibraryDensity() {
final densityString = _prefs.getString(_keyLibraryDensity);
return LibraryDensity.values.firstWhere(
(density) => density.name == densityString,
orElse: () => LibraryDensity.normal,
);
}
// Use Season Poster
Future<void> setUseSeasonPoster(bool enabled) async {
await _prefs.setBool(_keyUseSeasonPoster, enabled);
}
bool getUseSeasonPoster() {
return _prefs.getBool(_keyUseSeasonPoster) ?? false; // Default: false (use series poster)
}
// Seek Time Small (in seconds)
Future<void> setSeekTimeSmall(int seconds) async {
await _prefs.setInt(_keySeekTimeSmall, seconds);
}
int getSeekTimeSmall() {
return _prefs.getInt(_keySeekTimeSmall) ?? 10; // Default: 10 seconds
}
// Seek Time Large (in seconds)
Future<void> setSeekTimeLarge(int seconds) async {
await _prefs.setInt(_keySeekTimeLarge, seconds);
}
int getSeekTimeLarge() {
return _prefs.getInt(_keySeekTimeLarge) ?? 30; // Default: 30 seconds
}
// Keyboard Shortcuts (Legacy String-based)
Map<String, String> getDefaultKeyboardShortcuts() {
return {
@@ -542,6 +589,47 @@ class SettingsService {
}
}
// Media Version Preferences
/// Save media version preference for a series
/// [seriesRatingKey] is the grandparentRatingKey for TV series, or ratingKey for movies
/// [mediaIndex] is the index of the selected media version
Future<void> setMediaVersionPreference(String seriesRatingKey, int mediaIndex) async {
final preferences = _getMediaVersionPreferences();
preferences[seriesRatingKey] = mediaIndex;
final jsonString = json.encode(preferences);
await _prefs.setString(_keyMediaVersionPreferences, jsonString);
}
/// Get saved media version preference for a series
/// Returns null if no preference is saved
int? getMediaVersionPreference(String seriesRatingKey) {
final preferences = _getMediaVersionPreferences();
return preferences[seriesRatingKey];
}
/// Clear media version preference for a series
Future<void> clearMediaVersionPreference(String seriesRatingKey) async {
final preferences = _getMediaVersionPreferences();
preferences.remove(seriesRatingKey);
final jsonString = json.encode(preferences);
await _prefs.setString(_keyMediaVersionPreferences, jsonString);
}
/// Get all media version preferences
Map<String, int> _getMediaVersionPreferences() {
final jsonString = _prefs.getString(_keyMediaVersionPreferences);
if (jsonString == null) return {};
try {
final decoded = json.decode(jsonString) as Map<String, dynamic>;
return decoded.map((key, value) => MapEntry(key, value as int));
} catch (e) {
return {};
}
}
// Reset all settings to defaults
Future<void> resetAllSettings() async {
await Future.wait([
@@ -553,6 +641,11 @@ class SettingsService {
_prefs.remove(_keyEnableHardwareDecoding),
_prefs.remove(_keyPreferredVideoCodec),
_prefs.remove(_keyPreferredAudioCodec),
_prefs.remove(_keyLibraryDensity),
_prefs.remove(_keyUseSeasonPoster),
_prefs.remove(_keySeekTimeSmall),
_prefs.remove(_keySeekTimeLarge),
_prefs.remove(_keyMediaVersionPreferences),
]);
}
@@ -575,6 +668,10 @@ class SettingsService {
'enableHardwareDecoding': getEnableHardwareDecoding(),
'preferredVideoCodec': getPreferredVideoCodec(),
'preferredAudioCodec': getPreferredAudioCodec(),
'libraryDensity': getLibraryDensity().name,
'useSeasonPoster': getUseSeasonPoster(),
'seekTimeSmall': getSeekTimeSmall(),
'seekTimeLarge': getSeekTimeLarge(),
'keyboardShortcuts': getKeyboardShortcuts(),
'keyboardHotkeys': hotkeys.map(
(key, value) => MapEntry(key, _serializeHotKey(value)),
+67 -1
View File
@@ -8,11 +8,14 @@ class StorageService {
static const String _keyServerData = 'server_data';
static const String _keyClientId = 'client_identifier';
static const String _keySelectedLibraryIndex = 'selected_library_index';
static const String _keySelectedLibraryKey = 'selected_library_key';
static const String _keyLibraryFilters = 'library_filters';
static const String _keyLibraryOrder = 'library_order';
static const String _keyUserProfile = 'user_profile';
static const String _keyCurrentUserUUID = 'current_user_uuid';
static const String _keyHomeUsersCache = 'home_users_cache';
static const String _keyHomeUsersCacheExpiry = 'home_users_cache_expiry';
static const String _keyHiddenLibraries = 'hidden_libraries';
static StorageService? _instance;
late SharedPreferences _prefs;
@@ -135,7 +138,7 @@ class StorageService {
};
}
// Selected Library Index
// Selected Library Index (deprecated - use library key instead)
Future<void> saveSelectedLibraryIndex(int index) async {
await _prefs.setInt(_keySelectedLibraryIndex, index);
}
@@ -144,6 +147,15 @@ class StorageService {
return _prefs.getInt(_keySelectedLibraryIndex);
}
// Selected Library Key (replaces index-based selection)
Future<void> saveSelectedLibraryKey(String key) async {
await _prefs.setString(_keySelectedLibraryKey, key);
}
String? getSelectedLibraryKey() {
return _prefs.getString(_keySelectedLibraryKey);
}
// Library Filters (stored as JSON string)
Future<void> saveLibraryFilters(Map<String, String> filters) async {
final jsonString = json.encode(filters);
@@ -162,12 +174,66 @@ class StorageService {
}
}
// Library Sort (per-library, stored individually)
Future<void> saveLibrarySort(String sectionId, String sortKey) async {
await _prefs.setString('library_sort_$sectionId', sortKey);
}
String getLibrarySort(String sectionId) {
// Return saved sort or default to titleSort (alphabetical)
return _prefs.getString('library_sort_$sectionId') ?? 'titleSort';
}
// Hidden Libraries (stored as JSON array of library section IDs)
Future<void> saveHiddenLibraries(Set<String> libraryKeys) async {
final list = libraryKeys.toList();
final jsonString = json.encode(list);
await _prefs.setString(_keyHiddenLibraries, jsonString);
}
Set<String> getHiddenLibraries() {
final jsonString = _prefs.getString(_keyHiddenLibraries);
if (jsonString == null) return {};
try {
final list = json.decode(jsonString) as List<dynamic>;
return list.map((e) => e.toString()).toSet();
} catch (e) {
return {};
}
}
// Clear library preferences
Future<void> clearLibraryPreferences() async {
await Future.wait([
_prefs.remove(_keySelectedLibraryIndex),
_prefs.remove(_keyLibraryFilters),
_prefs.remove(_keyLibraryOrder),
_prefs.remove(_keyHiddenLibraries),
]);
// Also clear all library sort preferences
final keys = _prefs.getKeys();
final sortKeys = keys.where((key) => key.startsWith('library_sort_'));
await Future.wait(sortKeys.map((key) => _prefs.remove(key)));
}
// Library Order (stored as JSON list of library keys)
Future<void> saveLibraryOrder(List<String> libraryKeys) async {
final jsonString = json.encode(libraryKeys);
await _prefs.setString(_keyLibraryOrder, jsonString);
}
List<String>? getLibraryOrder() {
final jsonString = _prefs.getString(_keyLibraryOrder);
if (jsonString == null) return null;
try {
final decoded = json.decode(jsonString) as List<dynamic>;
return decoded.map((e) => e.toString()).toList();
} catch (e) {
return null;
}
}
// User Profile (stored as JSON string)
+269
View File
@@ -0,0 +1,269 @@
import '../models/plex_media_info.dart';
import '../models/plex_user_profile.dart';
/// Service for selecting audio and subtitle tracks based on user preferences
class TrackSelectionService {
/// Selects the best audio track based on user preferences
///
/// Returns the selected audio track, or null if no suitable track is found
static PlexAudioTrack? selectAudioTrack(
List<PlexAudioTrack> tracks,
PlexUserProfile profile,
) {
if (tracks.isEmpty) return null;
// If auto-select is disabled, use Plex's selected track
if (!profile.autoSelectAudio) {
return tracks.firstWhere(
(track) => track.selected,
orElse: () => tracks.first,
);
}
// Build list of preferred language codes
final preferredLanguages = <String>[];
if (profile.defaultAudioLanguage != null) {
preferredLanguages.add(profile.defaultAudioLanguage!);
}
if (profile.defaultAudioLanguages != null) {
preferredLanguages.addAll(profile.defaultAudioLanguages!);
}
// If no preferred languages, return first track
if (preferredLanguages.isEmpty) {
return tracks.first;
}
// Try to find a track matching preferred languages
for (final language in preferredLanguages) {
final matchingTrack = tracks.firstWhere(
(track) => _matchesLanguage(track.languageCode, language),
orElse: () => tracks.first,
);
if (matchingTrack != tracks.first ||
_matchesLanguage(matchingTrack.languageCode, language)) {
return matchingTrack;
}
}
// Fallback to first track
return tracks.first;
}
/// Selects the best subtitle track based on user preferences
///
/// Returns the selected subtitle track, or null if subtitles should be disabled
static PlexSubtitleTrack? selectSubtitleTrack(
List<PlexSubtitleTrack> tracks,
PlexUserProfile profile,
PlexAudioTrack? selectedAudioTrack,
) {
if (tracks.isEmpty) return null;
// Mode 0: Manually selected - return null to disable subtitles
if (profile.autoSelectSubtitle == 0) {
return null;
}
// Mode 1: Shown with foreign audio
if (profile.autoSelectSubtitle == 1) {
// Check if audio language matches user's preferred subtitle language
if (selectedAudioTrack != null && profile.defaultSubtitleLanguage != null) {
final audioLang = selectedAudioTrack.languageCode;
final prefLang = profile.defaultSubtitleLanguage;
// If audio matches preferred language, no subtitles needed
if (_matchesLanguage(audioLang, prefLang)) {
return null;
}
}
// Foreign audio detected, enable subtitles
return _findBestSubtitle(tracks, profile);
}
// Mode 2: Always enabled
if (profile.autoSelectSubtitle == 2) {
return _findBestSubtitle(tracks, profile);
}
return null;
}
/// Finds the best subtitle track matching user preferences
static PlexSubtitleTrack? _findBestSubtitle(
List<PlexSubtitleTrack> tracks,
PlexUserProfile profile,
) {
// Build list of preferred language codes
final preferredLanguages = <String>[];
if (profile.defaultSubtitleLanguage != null) {
preferredLanguages.add(profile.defaultSubtitleLanguage!);
}
if (profile.defaultSubtitleLanguages != null) {
preferredLanguages.addAll(profile.defaultSubtitleLanguages!);
}
// Filter tracks based on preferences
var candidateTracks = tracks;
// Apply SDH (hearing impaired) filtering
candidateTracks = _filterBySDH(candidateTracks, profile.defaultSubtitleAccessibility);
// Apply forced subtitle filtering
candidateTracks = _filterByForced(candidateTracks, profile.defaultSubtitleForced);
// If no candidates after filtering, relax filters
if (candidateTracks.isEmpty) {
candidateTracks = tracks;
}
// If no preferred languages, return first candidate
if (preferredLanguages.isEmpty) {
return candidateTracks.firstOrNull;
}
// Try to find a track matching preferred languages
for (final language in preferredLanguages) {
final matchingTrack = candidateTracks.firstWhere(
(track) => _matchesLanguage(track.languageCode, language),
orElse: () => candidateTracks.first,
);
if (matchingTrack != candidateTracks.first ||
_matchesLanguage(matchingTrack.languageCode, language)) {
return matchingTrack;
}
}
// Fallback to first candidate
return candidateTracks.firstOrNull;
}
/// Filters subtitle tracks based on SDH (hearing impaired) preference
///
/// Values:
/// - 0: Prefer non-SDH subtitles
/// - 1: Prefer SDH subtitles
/// - 2: Only show SDH subtitles
/// - 3: Only show non-SDH subtitles
static List<PlexSubtitleTrack> _filterBySDH(
List<PlexSubtitleTrack> tracks,
int preference,
) {
if (preference == 0 || preference == 1) {
// Prefer but don't require
final preferSDH = preference == 1;
final preferred = tracks.where((t) => _isSDH(t) == preferSDH).toList();
return preferred.isNotEmpty ? preferred : tracks;
} else if (preference == 2) {
// Only SDH
return tracks.where(_isSDH).toList();
} else if (preference == 3) {
// Only non-SDH
return tracks.where((t) => !_isSDH(t)).toList();
}
return tracks;
}
/// Filters subtitle tracks based on forced subtitle preference
///
/// Values:
/// - 0: Prefer non-forced subtitles
/// - 1: Prefer forced subtitles
/// - 2: Only show forced subtitles
/// - 3: Only show non-forced subtitles
static List<PlexSubtitleTrack> _filterByForced(
List<PlexSubtitleTrack> tracks,
int preference,
) {
if (preference == 0 || preference == 1) {
// Prefer but don't require
final preferForced = preference == 1;
final preferred = tracks.where((t) => t.forced == preferForced).toList();
return preferred.isNotEmpty ? preferred : tracks;
} else if (preference == 2) {
// Only forced
return tracks.where((t) => t.forced).toList();
} else if (preference == 3) {
// Only non-forced
return tracks.where((t) => !t.forced).toList();
}
return tracks;
}
/// Checks if a subtitle track is SDH (Subtitles for Deaf or Hard-of-Hearing)
///
/// Since Plex API may not expose this directly, we infer from the title/displayTitle
static bool _isSDH(PlexSubtitleTrack track) {
final title = track.title?.toLowerCase() ?? '';
final displayTitle = track.displayTitle?.toLowerCase() ?? '';
// Look for common SDH indicators
return title.contains('sdh') ||
displayTitle.contains('sdh') ||
title.contains('cc') ||
displayTitle.contains('cc') ||
title.contains('hearing impaired') ||
displayTitle.contains('hearing impaired');
}
/// Checks if a language code matches a preferred language
///
/// Handles both 2-letter (ISO 639-1) and 3-letter (ISO 639-2) codes
static bool _matchesLanguage(String? trackLanguage, String? preferredLanguage) {
if (trackLanguage == null || preferredLanguage == null) {
return false;
}
final track = trackLanguage.toLowerCase();
final preferred = preferredLanguage.toLowerCase();
// Direct match
if (track == preferred) return true;
// Handle common 2-letter to 3-letter mappings
final languageMap = {
'en': 'eng',
'es': 'spa',
'fr': 'fra',
'de': 'deu',
'it': 'ita',
'pt': 'por',
'ja': 'jpn',
'ko': 'kor',
'zh': 'zho',
'ru': 'rus',
'ar': 'ara',
'hi': 'hin',
'nl': 'nld',
'pl': 'pol',
'tr': 'tur',
'sv': 'swe',
'no': 'nor',
'da': 'dan',
'fi': 'fin',
'cs': 'ces',
'hu': 'hun',
'ro': 'ron',
'th': 'tha',
'vi': 'vie',
'id': 'ind',
'uk': 'ukr',
'el': 'ell',
'he': 'heb',
};
// Try mapping preferred to 3-letter and compare
if (languageMap[preferred] == track) return true;
// Try mapping track to 3-letter and compare with preferred 3-letter
if (languageMap[track] == preferred) return true;
// Try reverse mapping (3-letter to 2-letter)
final reverseMap = languageMap.map((k, v) => MapEntry(v, k));
if (reverseMap[preferred] == track) return true;
if (reverseMap[track] == preferred) return true;
return false;
}
}
+1 -11
View File
@@ -11,14 +11,4 @@ import 'package:logger/logger.dart';
/// appLogger.w('Warning message');
/// appLogger.e('Error message', error: e, stackTrace: stackTrace);
/// ```
final appLogger = Logger(
printer: PrettyPrinter(
methodCount: 2,
errorMethodCount: 8,
lineLength: 120,
colors: true,
printEmojis: true,
dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
),
level: Level.debug,
);
final appLogger = Logger(printer: SimplePrinter(), level: Level.debug);
+17
View File
@@ -0,0 +1,17 @@
/// Utility function to format content ratings by removing country prefixes
String formatContentRating(String? contentRating) {
if (contentRating == null || contentRating.isEmpty) {
return '';
}
// Remove common country prefixes like "gb/", "us/", "de/", etc.
// The pattern matches: lowercase letters followed by a forward slash
final regex = RegExp(r'^[a-z]{2,3}/(.+)$', caseSensitive: false);
final match = regex.firstMatch(contentRating);
if (match != null && match.groupCount >= 1) {
return match.group(1) ?? contentRating;
}
return contentRating;
}
+11
View File
@@ -2,7 +2,9 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/plex_client_provider.dart';
import '../providers/user_profile_provider.dart';
import '../providers/hidden_libraries_provider.dart';
import '../client/plex_client.dart';
import '../models/plex_user_profile.dart';
extension ProviderExtensions on BuildContext {
PlexClientProvider get plexClient =>
@@ -17,9 +19,18 @@ extension ProviderExtensions on BuildContext {
UserProfileProvider watchUserProfile() =>
Provider.of<UserProfileProvider>(this, listen: true);
HiddenLibrariesProvider get hiddenLibraries =>
Provider.of<HiddenLibrariesProvider>(this, listen: false);
HiddenLibrariesProvider watchHiddenLibraries() =>
Provider.of<HiddenLibrariesProvider>(this, listen: true);
// Direct client access (nullable)
PlexClient? get client => plexClient.client;
// Null-safe client access
PlexClient get clientSafe => plexClient.client!;
// Direct profile settings access (nullable)
PlexUserProfile? get profileSettings => userProfile.profileSettings;
}
+20 -4
View File
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import '../models/plex_metadata.dart';
import '../models/plex_user_profile.dart';
import '../screens/video_player_screen.dart';
import '../services/settings_service.dart';
/// Navigates to the VideoPlayerScreen with instant transitions to prevent white flash.
///
@@ -13,10 +13,11 @@ import '../screens/video_player_screen.dart';
/// Parameters:
/// - [context]: The build context for navigation
/// - [metadata]: The Plex metadata for the content to play
/// - [userProfile]: Optional user profile for track selection preferences
/// - [preferredAudioTrack]: Optional audio track to select on playback start
/// - [preferredSubtitleTrack]: Optional subtitle track to select on playback start
/// - [preferredPlaybackRate]: Optional playback speed to set on playback start
/// - [selectedMediaIndex]: Optional media version index to use; if not provided,
/// loads the saved preference for the series/movie. Defaults to 0 if no preference exists.
/// - [usePushReplacement]: If true, replaces current route instead of pushing;
/// useful for episode-to-episode navigation. Defaults to false.
///
@@ -25,19 +26,34 @@ import '../screens/video_player_screen.dart';
Future<bool?> navigateToVideoPlayer(
BuildContext context, {
required PlexMetadata metadata,
PlexUserProfile? userProfile,
AudioTrack? preferredAudioTrack,
SubtitleTrack? preferredSubtitleTrack,
double? preferredPlaybackRate,
int? selectedMediaIndex,
bool usePushReplacement = false,
}) async {
// Load saved media version preference if not explicitly provided
int mediaIndex = selectedMediaIndex ?? 0;
if (selectedMediaIndex == null) {
try {
final settingsService = await SettingsService.getInstance();
final seriesKey = metadata.grandparentRatingKey ?? metadata.ratingKey;
final savedPreference = settingsService.getMediaVersionPreference(seriesKey);
if (savedPreference != null) {
mediaIndex = savedPreference;
}
} catch (e) {
// Ignore errors loading preference, use default
}
}
final route = PageRouteBuilder<bool>(
pageBuilder: (context, animation, secondaryAnimation) => VideoPlayerScreen(
metadata: metadata,
userProfile: userProfile,
preferredAudioTrack: preferredAudioTrack,
preferredSubtitleTrack: preferredSubtitleTrack,
preferredPlaybackRate: preferredPlaybackRate,
selectedMediaIndex: mediaIndex,
),
transitionDuration: Duration.zero,
reverseTransitionDuration: Duration.zero,
+137
View File
@@ -0,0 +1,137 @@
import 'package:flutter/material.dart';
import '../utils/platform_detector.dart';
/// A menu action item for context menus
class ContextMenuItem {
final String value;
final IconData icon;
final String label;
const ContextMenuItem({
required this.value,
required this.icon,
required this.label,
});
}
/// A wrapper widget that shows context menus differently based on platform.
/// On mobile (iOS/Android): Shows a bottom sheet on long-press
/// On desktop (Windows/macOS/Linux): Shows a popup menu on right-click or long-press
class ContextMenuWrapper extends StatefulWidget {
final Widget child;
final List<ContextMenuItem> menuItems;
final Function(String)? onMenuItemSelected;
final VoidCallback? onTap;
final String? title;
const ContextMenuWrapper({
super.key,
required this.child,
required this.menuItems,
this.onMenuItemSelected,
this.onTap,
this.title,
});
@override
State<ContextMenuWrapper> createState() => _ContextMenuWrapperState();
}
class _ContextMenuWrapperState extends State<ContextMenuWrapper> {
Offset _tapPosition = Offset.zero;
void _storeTapPosition(TapDownDetails details) {
_tapPosition = details.globalPosition;
}
Future<void> _showContextMenu(BuildContext context) async {
final useBottomSheet = PlatformDetector.isMobile(context);
String? selected;
if (useBottomSheet) {
// Mobile: Show bottom sheet
selected = await showModalBottomSheet<String>(
context: context,
builder: (context) => SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (widget.title != null)
Padding(
padding: const EdgeInsets.all(16),
child: Text(
widget.title!,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
...widget.menuItems.map(
(item) => ListTile(
leading: Icon(item.icon),
title: Text(item.label),
onTap: () => Navigator.pop(context, item.value),
),
),
],
),
),
);
} else {
// Desktop: Show popup menu
final RenderBox overlay =
Overlay.of(context).context.findRenderObject() as RenderBox;
final overlayRect = Rect.fromPoints(
_tapPosition,
_tapPosition.translate(1, 1),
);
final menuItems = widget.menuItems
.map(
(item) => PopupMenuItem<String>(
value: item.value,
child: Row(
children: [
Icon(item.icon, size: 20),
const SizedBox(width: 12),
Expanded(child: Text(item.label)),
],
),
),
)
.toList();
selected = await showMenu<String>(
context: context,
position: RelativeRect.fromRect(
overlayRect,
Offset.zero & overlay.size,
),
items: menuItems,
elevation: 8,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
popUpAnimationStyle: AnimationStyle(
duration: const Duration(milliseconds: 150),
reverseDuration: const Duration(milliseconds: 100),
),
);
}
if (selected != null && widget.onMenuItemSelected != null) {
widget.onMenuItemSelected!(selected);
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap,
onTapDown: _storeTapPosition,
onLongPress: () => _showContextMenu(context),
onSecondaryTapDown: _storeTapPosition,
onSecondaryTap: () => _showContextMenu(context),
child: widget.child,
);
}
}
+179
View File
@@ -0,0 +1,179 @@
import 'package:flutter/material.dart';
import '../models/plex_file_info.dart';
class FileInfoBottomSheet extends StatelessWidget {
final PlexFileInfo fileInfo;
final String title;
const FileInfoBottomSheet({
super.key,
required this.fileInfo,
required this.title,
});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.grey[900],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(16),
topRight: Radius.circular(16),
),
),
child: SafeArea(
child: SizedBox(
height: MediaQuery.of(context).size.height * 0.75,
child: Column(
children: [
// Header
Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
const Icon(
Icons.info_outline,
color: Colors.white,
size: 24,
),
const SizedBox(width: 12),
Expanded(
child: Text(
'File Info',
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
IconButton(
icon: const Icon(Icons.close, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
],
),
),
const Divider(color: Colors.grey, height: 1),
// Content
Expanded(
child: ListView(
padding: const EdgeInsets.all(16),
children: [
// Title
if (title.isNotEmpty) ...[
Text(
title,
style: const TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 20),
],
// Video Section
_buildSectionHeader('Video'),
const SizedBox(height: 8),
_buildInfoRow('Codec', fileInfo.videoCodec ?? 'Unknown'),
_buildInfoRow('Resolution', fileInfo.resolutionFormatted),
_buildInfoRow('Bitrate', fileInfo.bitrateFormatted),
_buildInfoRow('Frame Rate', fileInfo.frameRateFormatted),
_buildInfoRow('Aspect Ratio', fileInfo.aspectRatioFormatted),
if (fileInfo.videoProfile != null)
_buildInfoRow('Profile', fileInfo.videoProfile!),
if (fileInfo.bitDepth != null)
_buildInfoRow('Bit Depth', '${fileInfo.bitDepth} bit'),
if (fileInfo.colorSpace != null)
_buildInfoRow('Color Space', fileInfo.colorSpace!),
if (fileInfo.colorRange != null)
_buildInfoRow('Color Range', fileInfo.colorRange!),
if (fileInfo.colorPrimaries != null)
_buildInfoRow('Color Primaries', fileInfo.colorPrimaries!),
if (fileInfo.chromaSubsampling != null)
_buildInfoRow('Chroma Subsampling', fileInfo.chromaSubsampling!),
const SizedBox(height: 20),
// Audio Section
_buildSectionHeader('Audio'),
const SizedBox(height: 8),
_buildInfoRow('Codec', fileInfo.audioCodec ?? 'Unknown'),
_buildInfoRow('Channels', fileInfo.audioChannelsFormatted),
if (fileInfo.audioProfile != null)
_buildInfoRow('Profile', fileInfo.audioProfile!),
const SizedBox(height: 20),
// File Section
_buildSectionHeader('File'),
const SizedBox(height: 8),
if (fileInfo.filePath != null)
_buildInfoRow('Path', fileInfo.filePath!, isMonospace: true),
_buildInfoRow('Size', fileInfo.fileSizeFormatted),
_buildInfoRow('Container', fileInfo.container ?? 'Unknown'),
_buildInfoRow('Duration', fileInfo.durationFormatted),
const SizedBox(height: 20),
// Advanced Section
_buildSectionHeader('Advanced'),
const SizedBox(height: 8),
_buildInfoRow(
'Optimized for Streaming',
fileInfo.optimizedForStreaming == true ? 'Yes' : 'No',
),
_buildInfoRow(
'64-bit Offsets',
fileInfo.has64bitOffsets == true ? 'Yes' : 'No',
),
],
),
),
],
),
),
),
);
}
Widget _buildSectionHeader(String title) {
return Text(
title,
style: const TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
);
}
Widget _buildInfoRow(String label, String value, {bool isMonospace = false}) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 140,
child: Text(
label,
style: TextStyle(
color: Colors.grey[400],
fontSize: 14,
),
),
),
Expanded(
child: Text(
value,
style: TextStyle(
color: Colors.white,
fontSize: 14,
fontFamily: isMonospace ? 'monospace' : null,
),
),
),
],
),
);
}
}
@@ -0,0 +1,194 @@
import 'package:flutter/material.dart';
import '../utils/platform_detector.dart';
/// A wrapper widget that adds hover-activated navigation arrows to horizontal scrolling content.
/// The arrows only appear on desktop/web platforms and hide at scroll boundaries.
///
/// This widget creates and manages its own ScrollController internally. Use the [builder]
/// constructor to access the ScrollController for the scrollable child widget.
class HorizontalScrollWithArrows extends StatefulWidget {
final Widget Function(ScrollController) builder;
final double scrollAmount;
const HorizontalScrollWithArrows({
super.key,
required this.builder,
this.scrollAmount = 0.8, // Scroll by 80% of viewport width by default
});
@override
State<HorizontalScrollWithArrows> createState() =>
_HorizontalScrollWithArrowsState();
}
class _HorizontalScrollWithArrowsState
extends State<HorizontalScrollWithArrows> {
late final ScrollController _scrollController;
bool _isHovering = false;
bool _canScrollLeft = false;
bool _canScrollRight = false;
@override
void initState() {
super.initState();
_scrollController = ScrollController();
_scrollController.addListener(_updateScrollState);
// Initial state update after first frame
WidgetsBinding.instance.addPostFrameCallback((_) => _updateScrollState());
}
@override
void dispose() {
_scrollController.removeListener(_updateScrollState);
_scrollController.dispose();
super.dispose();
}
void _updateScrollState() {
if (!mounted) return;
final position = _scrollController.position;
setState(() {
_canScrollLeft = position.pixels > 0;
_canScrollRight = position.pixels < position.maxScrollExtent;
});
}
void _scrollLeft() {
final position = _scrollController.position;
final targetScroll = (position.pixels -
(position.viewportDimension * widget.scrollAmount))
.clamp(0.0, position.maxScrollExtent);
_scrollController.animateTo(
targetScroll,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
void _scrollRight() {
final position = _scrollController.position;
final targetScroll = (position.pixels +
(position.viewportDimension * widget.scrollAmount))
.clamp(0.0, position.maxScrollExtent);
_scrollController.animateTo(
targetScroll,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
@override
Widget build(BuildContext context) {
final child = widget.builder(_scrollController);
if (!PlatformDetector.isDesktop(context)) {
// On mobile, just return the child without arrows
return child;
}
return MouseRegion(
onEnter: (_) => setState(() => _isHovering = true),
onExit: (_) => setState(() => _isHovering = false),
child: Stack(
children: [
child,
// Left arrow
Positioned(
left: 8,
top: 0,
bottom: 0,
child: Center(
child: AnimatedOpacity(
opacity: (_isHovering && _canScrollLeft) ? 1.0 : 0.0,
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
child: IgnorePointer(
ignoring: !(_isHovering && _canScrollLeft),
child: _NavigationArrow(
icon: Icons.chevron_left,
onPressed: _scrollLeft,
),
),
),
),
),
// Right arrow
Positioned(
right: 8,
top: 0,
bottom: 0,
child: Center(
child: AnimatedOpacity(
opacity: (_isHovering && _canScrollRight) ? 1.0 : 0.0,
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
child: IgnorePointer(
ignoring: !(_isHovering && _canScrollRight),
child: _NavigationArrow(
icon: Icons.chevron_right,
onPressed: _scrollRight,
),
),
),
),
),
],
),
);
}
}
class _NavigationArrow extends StatefulWidget {
final IconData icon;
final VoidCallback onPressed;
const _NavigationArrow({
required this.icon,
required this.onPressed,
});
@override
State<_NavigationArrow> createState() => _NavigationArrowState();
}
class _NavigationArrowState extends State<_NavigationArrow> {
bool _isPressed = false;
@override
Widget build(BuildContext context) {
return MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTapDown: (_) => setState(() => _isPressed = true),
onTapUp: (_) {
setState(() => _isPressed = false);
widget.onPressed();
},
onTapCancel: () => setState(() => _isPressed = false),
child: Container(
width: 48,
height: 48,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black.withValues(alpha: _isPressed ? 0.9 : 0.7),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.3),
blurRadius: 8,
spreadRadius: 0,
),
],
),
child: Icon(
widget.icon,
color: Colors.white,
size: 32,
),
),
),
);
}
}
+8 -20
View File
@@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:provider/provider.dart';
import '../models/plex_metadata.dart';
import '../models/plex_user_profile.dart';
import '../providers/plex_client_provider.dart';
import '../providers/settings_provider.dart';
import '../utils/provider_extensions.dart';
import '../utils/video_player_navigation.dart';
import '../screens/media_detail_screen.dart';
@@ -16,7 +16,6 @@ class MediaCard extends StatefulWidget {
final double? width;
final double? height;
final void Function(String ratingKey)? onRefresh;
final PlexUserProfile? userProfile;
const MediaCard({
super.key,
@@ -24,18 +23,13 @@ class MediaCard extends StatefulWidget {
this.width,
this.height,
this.onRefresh,
this.userProfile,
});
@override
State<MediaCard> createState() => _MediaCardState();
}
class _MediaCardState extends State<MediaCard>
with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
class _MediaCardState extends State<MediaCard> {
void _handleTap(BuildContext context) async {
final client = context.client;
if (client == null) return;
@@ -47,7 +41,6 @@ class _MediaCardState extends State<MediaCard>
final result = await navigateToVideoPlayer(
context,
metadata: widget.item,
userProfile: widget.userProfile,
);
// Refresh parent screen if result indicates it's needed
if (result == true) {
@@ -58,10 +51,7 @@ class _MediaCardState extends State<MediaCard>
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SeasonDetailScreen(
season: widget.item,
userProfile: widget.userProfile,
),
builder: (context) => SeasonDetailScreen(season: widget.item),
),
);
// Season screen doesn't return a refresh flag, but we can refresh anyway
@@ -71,10 +61,7 @@ class _MediaCardState extends State<MediaCard>
final result = await Navigator.push<bool>(
context,
MaterialPageRoute(
builder: (context) => MediaDetailScreen(
metadata: widget.item,
userProfile: widget.userProfile,
),
builder: (context) => MediaDetailScreen(metadata: widget.item),
),
);
// Refresh parent screen if result indicates it's needed
@@ -86,7 +73,6 @@ class _MediaCardState extends State<MediaCard>
@override
Widget build(BuildContext context) {
super.build(context); // Required for AutomaticKeepAliveClientMixin
return SizedBox(
width: widget.width,
child: MediaContextMenu(
@@ -187,7 +173,9 @@ class _MediaCardState extends State<MediaCard>
}
Widget _buildPosterImage(BuildContext context) {
if (widget.item.posterThumb != null) {
final useSeasonPoster = context.watch<SettingsProvider>().useSeasonPoster;
final posterUrl = widget.item.posterThumb(useSeasonPoster: useSeasonPoster);
if (posterUrl != null) {
return Consumer<PlexClientProvider>(
builder: (context, clientProvider, child) {
final client = clientProvider.client;
@@ -200,7 +188,7 @@ class _MediaCardState extends State<MediaCard>
}
return CachedNetworkImage(
imageUrl: client.getThumbnailUrl(widget.item.posterThumb),
imageUrl: client.getThumbnailUrl(posterUrl),
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
+71
View File
@@ -4,6 +4,7 @@ import '../models/plex_metadata.dart';
import '../utils/provider_extensions.dart';
import '../screens/media_detail_screen.dart';
import '../screens/season_detail_screen.dart';
import '../widgets/file_info_bottom_sheet.dart';
/// Helper class to store menu action data
class _MenuAction {
@@ -99,6 +100,17 @@ class _MediaContextMenuState extends State<MediaContextMenu> {
);
}
// File Info (for episodes and movies)
if (itemType == 'episode' || itemType == 'movie') {
menuActions.add(
_MenuAction(
value: 'fileinfo',
icon: Icons.info_outline,
label: 'File Info',
),
);
}
String? selected;
if (useBottomSheet) {
@@ -219,6 +231,10 @@ class _MediaContextMenuState extends State<MediaContextMenu> {
'Error loading season',
);
break;
case 'fileinfo':
await _showFileInfo(context);
break;
}
}
@@ -276,6 +292,61 @@ class _MediaContextMenuState extends State<MediaContextMenu> {
}
}
/// Show file info bottom sheet
Future<void> _showFileInfo(BuildContext context) async {
final client = context.client;
if (client == null) return;
try {
// Show loading indicator
if (context.mounted) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => const Center(
child: CircularProgressIndicator(),
),
);
}
// Fetch file info
final fileInfo = await client.getFileInfo(widget.metadata.ratingKey);
// Close loading indicator
if (context.mounted) {
Navigator.pop(context);
}
if (fileInfo != null && context.mounted) {
// Show file info bottom sheet
await showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => FileInfoBottomSheet(
fileInfo: fileInfo,
title: widget.metadata.title,
),
);
} else if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('File information not available')),
);
}
} catch (e) {
// Close loading indicator if it's still open
if (context.mounted && Navigator.canPop(context)) {
Navigator.pop(context);
}
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error loading file info: $e')),
);
}
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
+158
View File
@@ -0,0 +1,158 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// Dialog for entering a PIN to access a protected profile
class PinEntryDialog extends StatefulWidget {
final String userName;
final String? errorMessage;
const PinEntryDialog({super.key, required this.userName, this.errorMessage});
@override
State<PinEntryDialog> createState() => _PinEntryDialogState();
}
class _PinEntryDialogState extends State<PinEntryDialog>
with SingleTickerProviderStateMixin {
final _pinController = TextEditingController();
final _focusNode = FocusNode();
bool _obscureText = true;
late AnimationController _shakeController;
late Animation<double> _shakeAnimation;
@override
void initState() {
super.initState();
// Setup shake animation
_shakeController = AnimationController(
duration: const Duration(milliseconds: 600),
vsync: this,
);
// Create a shake effect that oscillates
_shakeAnimation =
TweenSequence<double>([
TweenSequenceItem(tween: Tween(begin: 0.0, end: 10.0), weight: 1),
TweenSequenceItem(tween: Tween(begin: 10.0, end: -10.0), weight: 1),
TweenSequenceItem(tween: Tween(begin: -10.0, end: 10.0), weight: 1),
TweenSequenceItem(tween: Tween(begin: 10.0, end: -10.0), weight: 1),
TweenSequenceItem(tween: Tween(begin: -10.0, end: 0.0), weight: 1),
]).animate(
CurvedAnimation(parent: _shakeController, curve: Curves.easeInOut),
);
// Auto-focus the PIN field when dialog opens
WidgetsBinding.instance.addPostFrameCallback((_) {
_focusNode.requestFocus();
// If there's an error message, trigger shake and clear field
if (widget.errorMessage != null) {
_pinController.clear();
_shakeController.forward(from: 0);
}
});
}
@override
void dispose() {
_pinController.dispose();
_focusNode.dispose();
_shakeController.dispose();
super.dispose();
}
void _submit() {
final pin = _pinController.text.trim();
if (pin.isEmpty) {
return;
}
Navigator.of(context).pop(pin);
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return AnimatedBuilder(
animation: _shakeAnimation,
builder: (context, child) {
return Transform.translate(
offset: Offset(_shakeAnimation.value, 0),
child: child,
);
},
child: AlertDialog(
title: Row(
children: [
Icon(
Icons.lock_outline,
size: 24,
color: theme.colorScheme.primary,
),
const SizedBox(width: 12),
Expanded(
child: Text(widget.userName, overflow: TextOverflow.ellipsis),
),
],
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
controller: _pinController,
focusNode: _focusNode,
obscureText: _obscureText,
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(10),
],
decoration: InputDecoration(
hintText: 'Enter PIN',
border: const OutlineInputBorder(),
errorText: widget.errorMessage,
errorMaxLines: 2,
suffixIcon: IconButton(
icon: Icon(
_obscureText ? Icons.visibility_off : Icons.visibility,
size: 20,
),
onPressed: () {
setState(() {
_obscureText = !_obscureText;
});
},
tooltip: _obscureText ? 'Show PIN' : 'Hide PIN',
),
),
onSubmitted: (_) => _submit(),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(null),
child: const Text('Cancel'),
),
FilledButton(onPressed: _submit, child: const Text('Submit')),
],
),
);
}
}
/// Shows the PIN entry dialog and returns the entered PIN, or null if cancelled
Future<String?> showPinEntryDialog(
BuildContext context,
String userName, {
String? errorMessage,
}) {
return showDialog<String>(
context: context,
barrierDismissible: false,
builder: (context) =>
PinEntryDialog(userName: userName, errorMessage: errorMessage),
);
}
+349 -26
View File
@@ -7,12 +7,15 @@ import 'package:window_manager/window_manager.dart';
import 'package:macos_window_utils/macos_window_utils.dart';
import '../models/plex_metadata.dart';
import '../models/plex_media_info.dart';
import '../models/plex_media_version.dart';
import '../providers/plex_client_provider.dart';
import '../services/fullscreen_state_manager.dart';
import '../services/keyboard_shortcuts_service.dart';
import '../services/settings_service.dart';
import '../utils/desktop_window_padding.dart';
import '../utils/platform_detector.dart';
import '../utils/provider_extensions.dart';
import '../screens/video_player_screen.dart';
import 'app_bar_back_button.dart';
/// Custom video controls builder for Plex with chapter, audio, and subtitle support
@@ -21,12 +24,16 @@ Widget plexVideoControlsBuilder(
PlexMetadata metadata, {
VoidCallback? onNext,
VoidCallback? onPrevious,
List<PlexMediaVersion>? availableVersions,
int? selectedMediaIndex,
}) {
return PlexVideoControls(
player: player,
metadata: metadata,
onNext: onNext,
onPrevious: onPrevious,
availableVersions: availableVersions ?? [],
selectedMediaIndex: selectedMediaIndex ?? 0,
);
}
@@ -35,6 +42,8 @@ class PlexVideoControls extends StatefulWidget {
final PlexMetadata metadata;
final VoidCallback? onNext;
final VoidCallback? onPrevious;
final List<PlexMediaVersion> availableVersions;
final int selectedMediaIndex;
const PlexVideoControls({
super.key,
@@ -42,6 +51,8 @@ class PlexVideoControls extends StatefulWidget {
required this.metadata,
this.onNext,
this.onPrevious,
this.availableVersions = const [],
this.selectedMediaIndex = 0,
});
@override
@@ -49,7 +60,7 @@ class PlexVideoControls extends StatefulWidget {
}
class _PlexVideoControlsState extends State<PlexVideoControls>
with WindowListener {
with WindowListener, WidgetsBindingObserver {
bool _showControls = true;
List<PlexChapter> _chapters = [];
bool _chaptersLoaded = false;
@@ -57,14 +68,24 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
bool _isFullscreen = false;
late final FocusNode _focusNode;
KeyboardShortcutsService? _keyboardService;
int _seekTimeSmall = 10; // Default, loaded from settings
int _seekTimeLarge = 30; // Default, loaded from settings
// Double-tap feedback state
bool _showDoubleTapFeedback = false;
double _doubleTapFeedbackOpacity = 0.0;
bool _lastDoubleTapWasForward = true;
Timer? _feedbackTimer;
@override
void initState() {
super.initState();
_focusNode = FocusNode();
_loadChapters();
_loadSeekTimes();
_startHideTimer();
_initKeyboardService();
// Add lifecycle observer to reload settings when app resumes
WidgetsBinding.instance.addObserver(this);
// Add window listener for tracking fullscreen state (for button icon)
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
windowManager.addListener(this);
@@ -75,6 +96,16 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
_keyboardService = await KeyboardShortcutsService.getInstance();
}
Future<void> _loadSeekTimes() async {
final settingsService = await SettingsService.getInstance();
if (mounted) {
setState(() {
_seekTimeSmall = settingsService.getSeekTimeSmall();
_seekTimeLarge = settingsService.getSeekTimeLarge();
});
}
}
void _toggleSubtitles() {
// Toggle subtitle visibility - this would need to be implemented based on your subtitle system
// For now, this is a placeholder
@@ -107,7 +138,10 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
@override
void dispose() {
_hideTimer?.cancel();
_feedbackTimer?.cancel();
_focusNode.dispose();
// Remove lifecycle observer
WidgetsBinding.instance.removeObserver(this);
// Remove window listener
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
windowManager.removeListener(this);
@@ -115,6 +149,14 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
// Reload seek times when app resumes (e.g., returning from settings)
_loadSeekTimes();
}
}
@override
void onWindowEnterFullScreen() {
if (mounted) {
@@ -257,6 +299,11 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
icon: const Icon(Icons.video_library, color: Colors.white),
onPressed: _showChapterBottomSheet,
),
if (widget.availableVersions.length > 1)
IconButton(
icon: const Icon(Icons.video_file, color: Colors.white),
onPressed: _showVersionBottomSheet,
),
],
);
},
@@ -265,9 +312,8 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
void _seekToPreviousChapter() {
if (_chapters.isEmpty) {
// No chapters - seek backward 10 seconds
final currentPosition = widget.player.state.position;
widget.player.seek(currentPosition - const Duration(seconds: 10));
// No chapters - seek backward by configured amount
_seekWithClamping(Duration(seconds: -_seekTimeSmall));
return;
}
@@ -289,9 +335,8 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
void _seekToNextChapter() {
if (_chapters.isEmpty) {
// No chapters - seek forward 10 seconds
final currentPosition = widget.player.state.position;
widget.player.seek(currentPosition + const Duration(seconds: 10));
// No chapters - seek forward by configured amount
_seekWithClamping(Duration(seconds: _seekTimeSmall));
return;
}
@@ -307,6 +352,116 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
}
}
/// Seeks by the given offset (can be positive or negative) while clamping
/// the result between 0 and the video duration
void _seekWithClamping(Duration offset) {
final currentPosition = widget.player.state.position;
final duration = widget.player.state.duration;
final newPosition = currentPosition + offset;
// Clamp between 0 and video duration
final clampedPosition = newPosition.isNegative
? Duration.zero
: (newPosition > duration ? duration : newPosition);
widget.player.seek(clampedPosition);
}
/// Get the replay icon based on the duration
/// Returns numbered icons (replay_5, replay_10, replay_30) when available,
/// otherwise returns generic replay icon
IconData _getReplayIcon(int seconds) {
switch (seconds) {
case 5:
return Icons.replay_5;
case 10:
return Icons.replay_10;
case 30:
return Icons.replay_30;
default:
return Icons.replay; // Generic icon for custom durations
}
}
/// Get the forward icon based on the duration
/// Returns numbered icons (forward_5, forward_10, forward_30) when available,
/// otherwise returns generic forward icon
IconData _getForwardIcon(int seconds) {
switch (seconds) {
case 5:
return Icons.forward_5;
case 10:
return Icons.forward_10;
case 30:
return Icons.forward_30;
default:
return Icons.forward; // Generic icon for custom durations
}
}
/// Handle double-tap skip forward or backward
void _handleDoubleTapSkip({required bool isForward}) {
// Perform the seek
_seekWithClamping(
Duration(seconds: isForward ? _seekTimeSmall : -_seekTimeSmall),
);
// Show visual feedback
_showSkipFeedback(isForward: isForward);
}
/// Show animated visual feedback for skip gesture
void _showSkipFeedback({required bool isForward}) {
_feedbackTimer?.cancel();
setState(() {
_lastDoubleTapWasForward = isForward;
_showDoubleTapFeedback = true;
_doubleTapFeedbackOpacity = 1.0;
});
// Fade out after delay
_feedbackTimer = Timer(const Duration(milliseconds: 500), () {
if (mounted) {
setState(() {
_doubleTapFeedbackOpacity = 0.0;
});
Timer(const Duration(milliseconds: 300), () {
if (mounted) {
setState(() {
_showDoubleTapFeedback = false;
});
}
});
}
});
}
/// Build the visual feedback widget for double-tap skip
Widget _buildDoubleTapFeedback() {
return Align(
alignment: _lastDoubleTapWasForward
? Alignment.centerRight
: Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 60),
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.black.withValues(alpha: 0.6),
shape: BoxShape.circle,
),
child: Icon(
_lastDoubleTapWasForward
? _getForwardIcon(_seekTimeSmall)
: _getReplayIcon(_seekTimeSmall),
color: Colors.white,
size: 48,
),
),
);
}
Future<void> _toggleFullscreen() async {
if (!PlatformDetector.isMobile(context)) {
// Query actual window state to determine what action to take
@@ -446,6 +601,63 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
},
),
),
// Mobile double-tap zones for skip forward/backward
if (isMobile)
Positioned.fill(
child: LayoutBuilder(
builder: (context, constraints) {
final height = constraints.maxHeight;
final width = constraints.maxWidth;
final topExclude = height * 0.15; // Exclude top 15% (top bar)
final bottomExclude = height * 0.15; // Exclude bottom 15% (seek slider)
final leftZoneWidth = width * 0.35; // Left 35%
return Stack(
children: [
// Left zone - skip backward
Positioned(
left: 0,
top: topExclude,
bottom: bottomExclude,
width: leftZoneWidth,
child: GestureDetector(
onTap: _toggleControls,
onDoubleTap: () =>
_handleDoubleTapSkip(isForward: false),
behavior: HitTestBehavior.translucent,
child: Container(color: Colors.transparent),
),
),
// Right zone - skip forward
Positioned(
right: 0,
top: topExclude,
bottom: bottomExclude,
width: leftZoneWidth,
child: GestureDetector(
onTap: _toggleControls,
onDoubleTap: () =>
_handleDoubleTapSkip(isForward: true),
behavior: HitTestBehavior.translucent,
child: Container(color: Colors.transparent),
),
),
],
);
},
),
),
// Visual feedback overlay for double-tap
if (isMobile && _showDoubleTapFeedback)
Positioned.fill(
child: IgnorePointer(
child: AnimatedOpacity(
opacity: _doubleTapFeedbackOpacity,
duration: const Duration(milliseconds: 300),
child: _buildDoubleTapFeedback(),
),
),
),
],
),
),
@@ -547,17 +759,14 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
shape: BoxShape.circle,
),
child: IconButton(
icon: const Icon(
Icons.replay_10,
icon: Icon(
_getReplayIcon(_seekTimeSmall),
color: Colors.white,
size: 48,
),
iconSize: 48,
onPressed: () {
final currentPosition = widget.player.state.position;
widget.player.seek(
currentPosition - const Duration(seconds: 10),
);
_seekWithClamping(Duration(seconds: -_seekTimeSmall));
},
),
),
@@ -592,17 +801,14 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
shape: BoxShape.circle,
),
child: IconButton(
icon: const Icon(
Icons.forward_10,
icon: Icon(
_getForwardIcon(_seekTimeSmall),
color: Colors.white,
size: 48,
),
iconSize: 48,
onPressed: () {
final currentPosition = widget.player.state.position;
widget.player.seek(
currentPosition + const Duration(seconds: 10),
);
_seekWithClamping(Duration(seconds: _seekTimeSmall));
},
),
),
@@ -824,10 +1030,10 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
),
onPressed: widget.onPrevious,
),
// Previous chapter (or -10s if no chapters)
// Previous chapter (or skip backward if no chapters)
IconButton(
icon: Icon(
_chapters.isEmpty ? Icons.replay_10 : Icons.fast_rewind,
_chapters.isEmpty ? _getReplayIcon(_seekTimeSmall) : Icons.fast_rewind,
color: Colors.white,
),
onPressed: _seekToPreviousChapter,
@@ -856,10 +1062,10 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
);
},
),
// Next chapter (or +10s if no chapters)
// Next chapter (or skip forward if no chapters)
IconButton(
icon: Icon(
_chapters.isEmpty ? Icons.forward_10 : Icons.fast_forward,
_chapters.isEmpty ? _getForwardIcon(_seekTimeSmall) : Icons.fast_forward,
color: Colors.white,
),
onPressed: _seekToNextChapter,
@@ -1097,7 +1303,9 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
initialData: widget.player.state.track,
builder: (context, selectedSnapshot) {
// Use snapshot data or fall back to current state
final currentTrack = selectedSnapshot.data ?? widget.player.state.track;
final currentTrack =
selectedSnapshot.data ??
widget.player.state.track;
final selectedTrack = currentTrack.audio;
final selectedId = selectedTrack?.id;
@@ -1221,7 +1429,9 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
initialData: widget.player.state.track,
builder: (context, selectedSnapshot) {
// Use snapshot data or fall back to current state
final currentTrack = selectedSnapshot.data ?? widget.player.state.track;
final currentTrack =
selectedSnapshot.data ??
widget.player.state.track;
final selectedTrack = currentTrack.subtitle;
final selectedId = selectedTrack?.id;
final isOffSelected = selectedId == 'no';
@@ -1516,7 +1726,7 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
final currentRate = snapshot.data ?? 1.0;
// Define available playback speeds
final speeds = [0.5, 0.75, 1.0, 1.25, 1.5, 2.0];
final speeds = [0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 2.5, 3.0];
return SafeArea(
child: SizedBox(
@@ -1585,6 +1795,119 @@ class _PlexVideoControlsState extends State<PlexVideoControls>
);
}
void _showVersionBottomSheet() {
showModalBottomSheet(
context: context,
backgroundColor: Colors.grey[900],
isScrollControlled: true,
constraints: _getBottomSheetConstraints(),
builder: (context) {
final versions = widget.availableVersions;
final currentIndex = widget.selectedMediaIndex;
return SafeArea(
child: SizedBox(
height: MediaQuery.of(context).size.height * 0.75,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
const Icon(Icons.video_file, color: Colors.white),
const SizedBox(width: 12),
const Text(
'Video Version',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const Spacer(),
IconButton(
icon: const Icon(Icons.close, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
],
),
),
const Divider(color: Colors.white24, height: 1),
Expanded(
child: ListView.builder(
itemCount: versions.length,
itemBuilder: (context, index) {
final version = versions[index];
final isSelected = index == currentIndex;
return ListTile(
title: Text(
version.displayLabel,
style: TextStyle(
color: isSelected ? Colors.blue : Colors.white,
),
),
trailing: isSelected
? const Icon(Icons.check, color: Colors.blue)
: null,
onTap: () {
Navigator.pop(context);
_switchMediaVersion(index);
},
);
},
),
),
],
),
),
);
},
);
}
/// Switch to a different media version
Future<void> _switchMediaVersion(int newMediaIndex) async {
if (newMediaIndex == widget.selectedMediaIndex) {
return; // Already using this version
}
try {
// Save current playback position
final currentPosition = widget.player.state.position;
// Save the preference
final settingsService = await SettingsService.getInstance();
final seriesKey = widget.metadata.grandparentRatingKey ??
widget.metadata.ratingKey;
await settingsService.setMediaVersionPreference(seriesKey, newMediaIndex);
// Navigate to new player screen with the selected version
// Use PageRouteBuilder with zero-duration transitions to prevent orientation reset
if (mounted) {
Navigator.pushReplacement(
context,
PageRouteBuilder<bool>(
pageBuilder: (context, animation, secondaryAnimation) => VideoPlayerScreen(
metadata: widget.metadata.copyWith(
viewOffset: currentPosition.inMilliseconds,
),
selectedMediaIndex: newMediaIndex,
),
transitionDuration: Duration.zero,
reverseTransitionDuration: Duration.zero,
),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error switching version: $e')),
);
}
}
}
String _formatDuration(Duration duration) {
final hours = duration.inHours;
final minutes = duration.inMinutes.remainder(60);
+2 -1
View File
@@ -1,7 +1,7 @@
name: plezy
description: "A beautiful Plex client for Flutter"
publish_to: "none"
version: 1.2.1+7
version: 1.3.1+10
environment:
sdk: ^3.8.1
@@ -44,6 +44,7 @@ flutter:
assets:
- lib/data/iso_639_codes.json
- assets/plezy.png
- assets/droid-sans.ttf
flutter_launcher_icons:
android: true