This commit is contained in:
2025-06-24 02:21:06 +02:00
parent 0d42fad708
commit 39fadc009f
7 changed files with 494 additions and 365 deletions

View File

@ -39,10 +39,12 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
final RxInt _currentIndex = 0.obs;
final MethodChannel _mediaSaverChannel = const MethodChannel('MediaShit');
final Map<int, PullexRefreshController> _refreshControllers = {};
final Map<int, GlobalKey<VideoWidgetState>> _videoWidgetKeys = {};
bool _isLoading = true;
bool _itemNotFound = false;
final Set<int> _readyItemIds = {};
final Map<int, bool> _showFavoriteAnimation = {};
final List<PopupMenuEntry<ShareAction>> _shareMenuItems = const [
PopupMenuItem(
@ -140,6 +142,18 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
setState(() => _readyItemIds.add(item.id));
}
}
if (idx + 1 < mediaController.items.length) {
DefaultCacheManager().downloadFile(
mediaController.items[idx + 1].mediaUrl,
);
}
if (idx - 1 >= 0) {
DefaultCacheManager().downloadFile(
mediaController.items[idx - 1].mediaUrl,
);
}
if (idx >= mediaController.items.length - 2 &&
!mediaController.loading.value &&
!mediaController.atEnd.value) {
@ -201,6 +215,37 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
}
}
Future<void> _handleFullScreen(MediaItem currentItem) async {
if (currentItem.mime.startsWith('image')) {
Get.to(
() => FullScreenMediaView(item: currentItem),
fullscreenDialog: true,
);
return;
}
final GlobalKey<VideoWidgetState>? key = _videoWidgetKeys[currentItem.id];
final VideoWidgetState? videoState = key?.currentState;
if (videoState == null || !videoState.videoController.value.isInitialized) {
return;
}
final Duration position = videoState.videoController.value.position;
await videoState.videoController.pause();
final Duration? newPosition = await Get.to<Duration?>(
() => FullScreenMediaView(item: currentItem, initialPosition: position),
fullscreenDialog: true,
);
if (mounted && videoState.mounted) {
if (newPosition != null) {
await videoState.videoController.seekTo(newPosition);
}
await videoState.videoController.play();
}
}
@override
void dispose() {
_pageController?.dispose();
@ -210,16 +255,49 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
super.dispose();
}
Future<void> _handleFavoriteToggle(MediaItem item, bool isFavorite) async {
if (!authController.isLoggedIn) return;
HapticFeedback.lightImpact();
final List<Favorite>? newFavorites = await mediaController.toggleFavorite(
item,
isFavorite,
);
final int index = mediaController.items.indexWhere((i) => i.id == item.id);
if (newFavorites != null && index != -1) {
mediaController.items[index] = item.copyWith(favorites: newFavorites);
mediaController.items.refresh();
}
if (!mounted) return;
setState(() => _showFavoriteAnimation[item.id] = true);
Future.delayed(const Duration(milliseconds: 700), () {
if (mounted) {
setState(() => _showFavoriteAnimation[item.id] = false);
}
});
}
Widget _buildMedia(MediaItem item, bool isActive) {
Widget mediaWidget;
if (item.mime.startsWith('image/')) {
return CachedNetworkImage(
mediaWidget = CachedNetworkImage(
imageUrl: item.mediaUrl,
fit: BoxFit.contain,
placeholder: (context, url) =>
const Center(child: CircularProgressIndicator()),
errorWidget: (c, e, s) => const Icon(Icons.broken_image, size: 100),
);
} else if (item.mime.startsWith('video/') ||
item.mime.startsWith('audio/')) {
return VideoWidget(
final key = _videoWidgetKeys.putIfAbsent(
item.id,
() => GlobalKey<VideoWidgetState>(),
);
mediaWidget = VideoWidget(
key: key,
details: item,
isActive: isActive,
onInitialized: () {
@ -229,8 +307,39 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
},
);
} else {
return const Icon(Icons.help_outline, size: 100);
mediaWidget = const Icon(Icons.help_outline, size: 100);
}
final bool isFavorite =
item.favorites?.any((f) => f.userId == authController.user.value?.id) ??
false;
return Hero(
tag: 'media_${item.id}',
child: GestureDetector(
onDoubleTap: () => _handleFavoriteToggle(item, isFavorite),
child: Stack(
alignment: Alignment.center,
children: [
mediaWidget,
AnimatedOpacity(
opacity: _showFavoriteAnimation[item.id] ?? false ? 1.0 : 0.0,
duration: const Duration(milliseconds: 200),
child: AnimatedScale(
scale: _showFavoriteAnimation[item.id] ?? false ? 1.0 : 0.5,
duration: const Duration(milliseconds: 400),
curve: Curves.easeOutBack,
child: Icon(
isFavorite ? Icons.favorite : Icons.favorite_outline,
color: Colors.red,
size: 100,
),
),
),
],
),
),
);
}
@override
@ -267,12 +376,7 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
actions: [
IconButton(
icon: const Icon(Icons.fullscreen),
onPressed: () {
Get.to(
FullScreenMediaView(item: currentItem),
fullscreenDialog: true,
);
},
onPressed: () => _handleFullScreen(currentItem),
),
IconButton(
icon: const Icon(Icons.download),
@ -291,28 +395,26 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
),
],
),
body: PageView.builder(
controller: _pageController!,
itemCount: mediaController.items.length,
onPageChanged: _onPageChanged,
itemBuilder: (context, index) {
final MediaItem item = mediaController.items[index];
final bool isReady = _readyItemIds.contains(item.id);
final ScrollController scrollController = ScrollController();
final PullexRefreshController refreshController =
_refreshControllers.putIfAbsent(
item.id,
() => PullexRefreshController(),
);
body: Stack(
children: [
PageView.builder(
controller: _pageController!,
itemCount: mediaController.items.length,
onPageChanged: _onPageChanged,
itemBuilder: (context, index) {
final MediaItem item = mediaController.items[index];
final bool isReady = _readyItemIds.contains(item.id);
final PullexRefreshController refreshController =
_refreshControllers.putIfAbsent(
item.id,
() => PullexRefreshController(),
);
return Stack(
children: [
PullexRefresh(
return PullexRefresh(
onRefresh: () => _onRefresh(item.id, refreshController),
header: const WaterDropHeader(),
controller: refreshController,
child: CustomScrollView(
controller: scrollController,
slivers: [
SliverToBoxAdapter(
child: AnimatedBuilder(
@ -335,15 +437,11 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
SliverFillRemaining(
hasScrollBody: false,
fillOverscroll: true,
child: GestureDetector(
onTap: () => settingsController.hideVideoControls(),
behavior: HitTestBehavior.translucent,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [TagSection(tags: item.tags ?? [])],
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [TagSection(tags: item.tags ?? [])],
),
),
),
@ -352,53 +450,54 @@ class _MediaDetailScreenState extends State<MediaDetailScreen> {
),
],
),
),
Obx(() {
if (!authController.isLoggedIn) {
return const SizedBox.shrink();
}
final MediaItem currentItem =
mediaController.items[_currentIndex.value];
);
},
),
Obx(() {
if (!authController.isLoggedIn) {
return const SizedBox.shrink();
}
final MediaItem currentItem =
mediaController.items[_currentIndex.value];
final bool hasSoftButtons = MediaQuery.of(context).padding.bottom > 24.0;
final bool hasSoftButtons =
MediaQuery.of(context).padding.bottom > 24.0;
return DraggableScrollableSheet(
initialChildSize: hasSoftButtons ? 0.11 : 0.2,
minChildSize: hasSoftButtons ? 0.11 : 0.2,
maxChildSize: hasSoftButtons ? 0.245 : 0.2,
snap: true,
builder: (context, scrollController) => ListView(
controller: scrollController,
padding: const EdgeInsets.only(left: 16, right: 16),
children: [
FavoriteSection(
item: currentItem,
index: _currentIndex.value,
),
const SizedBox(height: 16),
Text(
"Dateigröße: ${(currentItem.size / 1024).toStringAsFixed(1)} KB",
style: Theme.of(context).textTheme.bodySmall,
),
Text(
"Typ: ${currentItem.mime}",
style: Theme.of(context).textTheme.bodySmall,
),
Text(
"ID: ${currentItem.id}",
style: Theme.of(context).textTheme.bodySmall,
),
Text(
"Hochgeladen am: ${DateTime.fromMillisecondsSinceEpoch(currentItem.stamp * 1000)}",
style: Theme.of(context).textTheme.bodySmall,
),
],
return DraggableScrollableSheet(
initialChildSize: hasSoftButtons ? 0.11 : 0.2,
minChildSize: hasSoftButtons ? 0.11 : 0.2,
maxChildSize: hasSoftButtons ? 0.245 : 0.2,
snap: true,
builder: (context, scrollController) => ListView(
controller: scrollController,
padding: const EdgeInsets.only(left: 16, right: 16),
children: [
FavoriteSection(
item: currentItem,
index: _currentIndex.value,
),
);
}),
],
);
},
const SizedBox(height: 16),
Text(
"Dateigröße: ${(currentItem.size / 1024).toStringAsFixed(1)} KB",
style: Theme.of(context).textTheme.bodySmall,
),
Text(
"Typ: ${currentItem.mime}",
style: Theme.of(context).textTheme.bodySmall,
),
Text(
"ID: ${currentItem.id}",
style: Theme.of(context).textTheme.bodySmall,
),
Text(
"Hochgeladen am: ${DateTime.fromMillisecondsSinceEpoch(currentItem.stamp * 1000)}",
style: Theme.of(context).textTheme.bodySmall,
),
],
),
);
}),
],
),
persistentFooterButtons: mediaController.tag.value != null
? [TagFooter()]