369 lines
13 KiB
Dart
369 lines
13 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:flutter_cache_manager/file.dart';
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
import 'package:f0ckapp/widgets/tagfooter.dart';
|
|
import 'package:f0ckapp/utils/animatedtransition.dart';
|
|
import 'package:f0ckapp/controller/authcontroller.dart';
|
|
import 'package:f0ckapp/widgets/actiontag.dart';
|
|
import 'package:f0ckapp/widgets/favoritesection.dart';
|
|
import 'package:f0ckapp/screens/fullscreen.dart';
|
|
import 'package:f0ckapp/widgets/end_drawer.dart';
|
|
import 'package:f0ckapp/controller/mediacontroller.dart';
|
|
import 'package:f0ckapp/models/item.dart';
|
|
import 'package:f0ckapp/widgets/video_widget.dart';
|
|
|
|
enum ShareAction { media, directLink, postLink }
|
|
|
|
class MediaDetailScreen extends StatefulWidget {
|
|
final int initialId;
|
|
const MediaDetailScreen({super.key, required this.initialId});
|
|
|
|
@override
|
|
State<MediaDetailScreen> createState() => _MediaDetailScreenState();
|
|
}
|
|
|
|
class _MediaDetailScreenState extends State<MediaDetailScreen> {
|
|
PageController? _pageController;
|
|
final MediaController mediaController = Get.find<MediaController>();
|
|
final AuthController authController = Get.find<AuthController>();
|
|
final RxInt _currentIndex = 0.obs;
|
|
final MethodChannel _mediaSaverChannel = const MethodChannel('MediaShit');
|
|
final Map<int, bool> _expandedTags = {};
|
|
|
|
bool _isLoading = true;
|
|
bool _itemNotFound = false;
|
|
final Set<int> _readyItemIds = {};
|
|
|
|
final List<PopupMenuEntry<ShareAction>> _shareMenuItems = const [
|
|
PopupMenuItem(
|
|
value: ShareAction.media,
|
|
child: ListTile(leading: Icon(Icons.image), title: Text('Als Datei')),
|
|
),
|
|
PopupMenuItem(
|
|
value: ShareAction.directLink,
|
|
child: ListTile(leading: Icon(Icons.link), title: Text('Link zur Datei')),
|
|
),
|
|
PopupMenuItem(
|
|
value: ShareAction.postLink,
|
|
child: ListTile(
|
|
leading: Icon(Icons.article),
|
|
title: Text('Link zum f0ck'),
|
|
),
|
|
),
|
|
];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadInitialItem();
|
|
}
|
|
|
|
Future<void> _loadInitialItem() async {
|
|
int initialIndex = mediaController.items.indexWhere(
|
|
(item) => item.id == widget.initialId,
|
|
);
|
|
|
|
if (initialIndex < 0) {
|
|
await mediaController.fetchInitial(id: widget.initialId + 20);
|
|
initialIndex = mediaController.items.indexWhere(
|
|
(item) => item.id == widget.initialId,
|
|
);
|
|
}
|
|
|
|
if (initialIndex < 0) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_itemNotFound = true;
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (mounted) {
|
|
_currentIndex.value = initialIndex;
|
|
_pageController = PageController(initialPage: initialIndex);
|
|
if (mediaController.items[initialIndex].mime.startsWith('image/')) {
|
|
_readyItemIds.add(mediaController.items[initialIndex].id);
|
|
}
|
|
setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
|
|
void _showMsg(String message) {
|
|
if (!mounted) return;
|
|
Get
|
|
..closeAllSnackbars()
|
|
..snackbar('hehe', message, snackPosition: SnackPosition.BOTTOM);
|
|
}
|
|
|
|
void _onPageChanged(int idx) {
|
|
if (idx != _currentIndex.value) {
|
|
_currentIndex.value = idx;
|
|
final MediaItem item = mediaController.items[idx];
|
|
if (item.mime.startsWith('image/') && !_readyItemIds.contains(item.id)) {
|
|
setState(() => _readyItemIds.add(item.id));
|
|
}
|
|
}
|
|
if (idx >= mediaController.items.length - 2 &&
|
|
!mediaController.loading.value &&
|
|
!mediaController.atEnd.value) {
|
|
mediaController.fetchMore();
|
|
} else if (idx <= 1 &&
|
|
!mediaController.loading.value &&
|
|
!mediaController.atStart.value) {
|
|
mediaController.fetchNewer();
|
|
}
|
|
}
|
|
|
|
Future<void> _downloadMedia(MediaItem item) async {
|
|
try {
|
|
final File file = await DefaultCacheManager().getSingleFile(
|
|
item.mediaUrl,
|
|
);
|
|
|
|
final bool? success = await _mediaSaverChannel.invokeMethod<bool>(
|
|
'saveFile',
|
|
{'filePath': file.path, 'fileName': item.dest},
|
|
);
|
|
|
|
success == true
|
|
? _showMsg('${item.dest} wurde in Downloads/fApp neigespeichert.')
|
|
: _showMsg('${item.dest} konnte nicht heruntergeladen werden.');
|
|
} catch (e) {
|
|
_showMsg('Fehler beim Download: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> _handleShareAction(ShareAction value, MediaItem item) async {
|
|
try {
|
|
if (value == ShareAction.media) {
|
|
final File file = await DefaultCacheManager().getSingleFile(
|
|
item.mediaUrl,
|
|
);
|
|
final Uint8List bytes = await file.readAsBytes();
|
|
final params = ShareParams(
|
|
files: [XFile.fromData(bytes, mimeType: item.mime)],
|
|
);
|
|
await SharePlus.instance.share(params);
|
|
return;
|
|
}
|
|
|
|
final String textToShare;
|
|
switch (value) {
|
|
case ShareAction.directLink:
|
|
textToShare = item.mediaUrl;
|
|
break;
|
|
case ShareAction.postLink:
|
|
textToShare = item.postUrl;
|
|
break;
|
|
case ShareAction.media:
|
|
return;
|
|
}
|
|
await SharePlus.instance.share(ShareParams(text: textToShare));
|
|
} catch (e) {
|
|
_showMsg('Fehler beim Teilen: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_pageController?.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Widget _buildMedia(MediaItem item, bool isActive) {
|
|
if (item.mime.startsWith('image/')) {
|
|
return CachedNetworkImage(
|
|
imageUrl: item.mediaUrl,
|
|
fit: BoxFit.contain,
|
|
errorWidget: (c, e, s) => const Icon(Icons.broken_image, size: 100),
|
|
);
|
|
} else if (item.mime.startsWith('video/') ||
|
|
item.mime.startsWith('audio/')) {
|
|
return VideoWidget(
|
|
details: item,
|
|
isActive: isActive,
|
|
onInitialized: () {
|
|
if (mounted && !_readyItemIds.contains(item.id)) {
|
|
setState(() => _readyItemIds.add(item.id));
|
|
}
|
|
},
|
|
);
|
|
} else {
|
|
return const Icon(Icons.help_outline, size: 100);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_isLoading) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text('Lade f0ck #${widget.initialId}...')),
|
|
body: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
|
|
if (_itemNotFound) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Fehler')),
|
|
body: const Center(child: Text('f0ck nicht gefunden.')),
|
|
);
|
|
}
|
|
|
|
return Obx(
|
|
() => 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 bool areTagsExpanded = _expandedTags[item.id] ?? false;
|
|
final List<Tag> allTags = item.tags ?? [];
|
|
final bool hasMoreTags = allTags.length > 5;
|
|
final List<Tag> tagsToShow = areTagsExpanded
|
|
? allTags
|
|
: allTags.take(5).toList();
|
|
|
|
return Obx(
|
|
() => Scaffold(
|
|
endDrawer: EndDrawer(),
|
|
endDrawerEnableOpenDragGesture:
|
|
mediaController.drawerSwipeEnabled.value,
|
|
appBar: AppBar(
|
|
title: Text('f0ck #${item.id}'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.fullscreen),
|
|
onPressed: () {
|
|
Get.to(
|
|
FullScreenMediaView(item: item),
|
|
fullscreenDialog: true,
|
|
);
|
|
},
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.download),
|
|
onPressed: () async {
|
|
await _downloadMedia(item);
|
|
},
|
|
),
|
|
PopupMenuButton<ShareAction>(
|
|
onSelected: (value) => _handleShareAction(value, item),
|
|
itemBuilder: (context) => _shareMenuItems,
|
|
icon: const Icon(Icons.share),
|
|
),
|
|
Builder(
|
|
builder: (context) => IconButton(
|
|
icon: const Icon(Icons.menu),
|
|
onPressed: () {
|
|
Scaffold.of(context).openEndDrawer();
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
AnimatedBuilder(
|
|
animation: _pageController!,
|
|
builder: (context, child) {
|
|
return buildAnimatedTransition(
|
|
context: context,
|
|
pageController: _pageController!,
|
|
index: index,
|
|
controller: mediaController,
|
|
child: child!,
|
|
);
|
|
},
|
|
child: Obx(
|
|
() => _buildMedia(item, index == _currentIndex.value),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: GestureDetector(
|
|
onTap: () => mediaController.hideVideoControls(),
|
|
behavior: HitTestBehavior.translucent,
|
|
child: Visibility(
|
|
visible: isReady,
|
|
child: SingleChildScrollView(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
Wrap(
|
|
spacing: 6.0,
|
|
runSpacing: 4.0,
|
|
alignment: WrapAlignment.center,
|
|
children: [
|
|
...tagsToShow.map(
|
|
(tag) => ActionTag(
|
|
tag,
|
|
(tag.tag == 'sfw' || tag.tag == 'nsfw')
|
|
? (onTagTap) => {}
|
|
: (onTagTap) {
|
|
mediaController.setTag(
|
|
onTagTap,
|
|
);
|
|
Get.offAllNamed('/');
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
if (hasMoreTags)
|
|
TextButton(
|
|
onPressed: () {
|
|
setState(
|
|
() => _expandedTags[item.id] =
|
|
!areTagsExpanded,
|
|
);
|
|
},
|
|
child: Text(
|
|
areTagsExpanded
|
|
? 'Weniger anzeigen'
|
|
: 'Alle ${allTags.length} Tags anzeigen',
|
|
),
|
|
),
|
|
Obx(
|
|
() => Visibility(
|
|
visible: authController.isLoggedIn,
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(top: 20.0),
|
|
child: FavoriteSection(
|
|
item: item,
|
|
index: index,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SafeArea(child: SizedBox.shrink()),
|
|
],
|
|
),
|
|
persistentFooterButtons: mediaController.tag.value != null
|
|
? [TagFooter()]
|
|
: null,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|