294 lines
9.4 KiB
Dart
294 lines
9.4 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
import 'package:f0ckapp/service/media_service.dart';
|
|
import 'package:f0ckapp/utils/animatedtransition.dart';
|
|
import 'package:f0ckapp/utils/smartrefreshindicator.dart';
|
|
import 'package:f0ckapp/controller/media_controller.dart';
|
|
import 'package:f0ckapp/models/media_item.dart';
|
|
import 'package:f0ckapp/screens/media_grid.dart';
|
|
import 'package:f0ckapp/screens/fullscreen_screen.dart';
|
|
import 'package:f0ckapp/widgets/end_drawer.dart';
|
|
import 'package:f0ckapp/widgets/detailmediacontent.dart';
|
|
|
|
class DetailView extends StatefulWidget {
|
|
final int initialId;
|
|
const DetailView({super.key, required this.initialId});
|
|
|
|
@override
|
|
State<DetailView> createState() => _DetailViewState();
|
|
}
|
|
|
|
class _DetailViewState extends State<DetailView> {
|
|
final MediaController controller = Get.find<MediaController>();
|
|
MediaItem? item;
|
|
bool isLoading = false;
|
|
PageController? _pageController;
|
|
int _currentPage = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_setupInitialView();
|
|
|
|
ever(controller.drawerSwipeEnabled, (_) {
|
|
setState(() {});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_pageController?.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _setupInitialView() async {
|
|
bool itemExists = controller.mediaItems.any(
|
|
(media) => media.id == widget.initialId,
|
|
);
|
|
|
|
if (!itemExists) {
|
|
await _initializeDetail(widget.initialId);
|
|
}
|
|
_initializePageController();
|
|
}
|
|
|
|
void _initializePageController() {
|
|
final page = controller.mediaItems.indexWhere(
|
|
(media) => media.id == widget.initialId,
|
|
);
|
|
setState(() {
|
|
_currentPage = page < 0 ? 0 : page;
|
|
_pageController = PageController(initialPage: _currentPage)
|
|
..addListener(() {
|
|
setState(() => _currentPage = _pageController!.page!.round());
|
|
});
|
|
});
|
|
}
|
|
|
|
Future<void> _downloadMedia(MediaItem item) async {
|
|
final File file = await DefaultCacheManager().getSingleFile(item.mediaUrl);
|
|
final MethodChannel methodChannel = const MethodChannel('MediaShit');
|
|
|
|
bool? success = await methodChannel.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.');
|
|
}
|
|
|
|
void _showMsg(String message) {
|
|
if (!mounted) return;
|
|
Get
|
|
..closeAllSnackbars()
|
|
..snackbar('hehe', message, snackPosition: SnackPosition.BOTTOM);
|
|
}
|
|
|
|
Future<void> _initializeDetail(int deepLinkId) async {
|
|
item = controller.mediaItems.firstWhereOrNull(
|
|
(element) => element.id == deepLinkId,
|
|
);
|
|
|
|
if (item == null) {
|
|
setState(() => isLoading = true);
|
|
await controller.loadMediaItems(older: deepLinkId + 50);
|
|
item = controller.mediaItems.firstWhereOrNull(
|
|
(element) => element.id == deepLinkId,
|
|
);
|
|
if (item == null) {
|
|
Get.offAll(() => const MediaGrid());
|
|
}
|
|
setState(() => isLoading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final MediaService mediaService = Get.find<MediaService>();
|
|
|
|
if (isLoading || controller.mediaItems.isEmpty || _pageController == null) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("f0ck"),
|
|
leading: Navigator.canPop(context)
|
|
? null
|
|
: IconButton(
|
|
icon: const Icon(Icons.arrow_back),
|
|
onPressed: () {
|
|
Get.offAll(() => const MediaGrid());
|
|
},
|
|
),
|
|
),
|
|
body: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
|
|
final MediaItem currentItem = controller.mediaItems[_currentPage];
|
|
|
|
return Scaffold(
|
|
endDrawer: const EndDrawer(),
|
|
endDrawerEnableOpenDragGesture: controller.drawerSwipeEnabled.value,
|
|
persistentFooterButtons: controller.tag.value != null
|
|
? [
|
|
Center(
|
|
child: InputChip(
|
|
label: Text(controller.tag.value!),
|
|
onDeleted: () {
|
|
controller.setTag(null);
|
|
Get.offAll(() => const MediaGrid());
|
|
},
|
|
),
|
|
),
|
|
]
|
|
: null,
|
|
body: CustomScrollView(
|
|
slivers: [
|
|
SliverAppBar(
|
|
floating: true,
|
|
pinned: true,
|
|
snap: true,
|
|
centerTitle: true,
|
|
title: Text('f0ck #${currentItem.id.toString()}'),
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.arrow_back),
|
|
onPressed: () {
|
|
Navigator.popUntil(
|
|
context,
|
|
(route) => route.settings.name == '/' || route.isFirst,
|
|
);
|
|
},
|
|
),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.fullscreen),
|
|
onPressed: () {
|
|
Get.to(
|
|
FullScreenMediaView(item: currentItem),
|
|
fullscreenDialog: true,
|
|
);
|
|
},
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.download),
|
|
onPressed: () async {
|
|
await _downloadMedia(currentItem);
|
|
},
|
|
),
|
|
PopupMenuButton<String>(
|
|
onSelected: (value) async {
|
|
switch (value) {
|
|
case 'media':
|
|
File file = await DefaultCacheManager().getSingleFile(
|
|
currentItem.mediaUrl,
|
|
);
|
|
Uint8List bytes = await file.readAsBytes();
|
|
final params = ShareParams(
|
|
files: [
|
|
XFile.fromData(bytes, mimeType: currentItem.mime),
|
|
],
|
|
);
|
|
await SharePlus.instance.share(params);
|
|
break;
|
|
case 'direct_link':
|
|
await SharePlus.instance.share(
|
|
ShareParams(text: currentItem.mediaUrl),
|
|
);
|
|
break;
|
|
case 'post_link':
|
|
await SharePlus.instance.share(
|
|
ShareParams(text: currentItem.postUrl),
|
|
);
|
|
break;
|
|
}
|
|
},
|
|
itemBuilder: (context) => [
|
|
PopupMenuItem(
|
|
value: 'media',
|
|
child: ListTile(
|
|
leading: const Icon(Icons.image),
|
|
title: const Text('Als Datei'),
|
|
),
|
|
),
|
|
PopupMenuItem(
|
|
value: 'direct_link',
|
|
child: ListTile(
|
|
leading: const Icon(Icons.link),
|
|
title: const Text('Link zur Datei'),
|
|
),
|
|
),
|
|
PopupMenuItem(
|
|
value: 'post_link',
|
|
child: ListTile(
|
|
leading: const Icon(Icons.article),
|
|
title: const Text('Link zum f0ck'),
|
|
),
|
|
),
|
|
],
|
|
icon: const Icon(Icons.share),
|
|
),
|
|
Builder(
|
|
builder: (context) => IconButton(
|
|
icon: const Icon(Icons.menu),
|
|
onPressed: () {
|
|
Scaffold.of(context).openEndDrawer();
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
SliverFillRemaining(
|
|
child: PageView.builder(
|
|
controller: _pageController,
|
|
itemCount: controller.mediaItems.length,
|
|
itemBuilder: (context, index) {
|
|
final MediaItem pageItem = controller.mediaItems[index];
|
|
return AnimatedBuilder(
|
|
animation: _pageController!,
|
|
builder: (context, child) {
|
|
return buildAnimatedTransition(
|
|
context: context,
|
|
child: child!,
|
|
pageController: _pageController!,
|
|
index: index,
|
|
controller: controller,
|
|
);
|
|
},
|
|
child: SmartRefreshIndicator(
|
|
onRefresh: () async {
|
|
final MediaItem? refreshed = await mediaService.fetchItem(
|
|
pageItem.id,
|
|
);
|
|
if (refreshed != null) {
|
|
controller.mediaItems[index] = refreshed;
|
|
controller.mediaItems.refresh();
|
|
}
|
|
},
|
|
child: DetailMediaContent(
|
|
currentPage: _currentPage,
|
|
index: index,
|
|
onTagTap: (tag) {
|
|
if (tag == 'sfw' || tag == 'nsfw') return;
|
|
controller.setTag(tag);
|
|
Get.offAllNamed('/');
|
|
},
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|