Skip to content

Commit

Permalink
✨ Add caching system
Browse files Browse the repository at this point in the history
  • Loading branch information
wrngwrld committed Apr 12, 2023
1 parent 10babad commit 0eedbdd
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 333 deletions.
6 changes: 3 additions & 3 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 0.7.4;
MARKETING_VERSION = 0.7.5;
PRODUCT_BUNDLE_IDENTIFIER = com.wrngwrld.Chanyan;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -506,7 +506,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 0.7.4;
MARKETING_VERSION = 0.7.5;
PRODUCT_BUNDLE_IDENTIFIER = com.wrngwrld.Chanyan;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -535,7 +535,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 0.7.4;
MARKETING_VERSION = 0.7.5;
PRODUCT_BUNDLE_IDENTIFIER = com.wrngwrld.Chanyan;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
128 changes: 32 additions & 96 deletions lib/API/save_videos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:ffmpeg_kit_flutter_full/ffmpeg_session.dart';
import 'package:ffmpeg_kit_flutter_full/return_code.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_chan/Models/saved_attachment.dart';
import 'package:flutter_chan/blocs/saved_attachments_model.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
Expand All @@ -14,6 +15,8 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';

import '../services/show_snackbar.dart';

Future<bool> _requestPermission(Permission permission) async {
if (await permission.isGranted) {
return true;
Expand Down Expand Up @@ -79,7 +82,6 @@ Future<void> saveVideo(
savedAttachmentsProvider.pauseVideo();

Directory directory = Directory('');
final dio = Dio();

if (isSaved) {
directory = await requestDirectory(directory);
Expand Down Expand Up @@ -140,13 +142,8 @@ Future<void> saveVideo(
directory = await requestDirectory(directory);

if (await directory.exists()) {
final File saveFile = File('${directory.path}/$fileName');
final File saveFileVideo = File('${directory.path}/$fileName');

await dio.download(
url,
saveFile.path,
);
final File fileDownloadPath = File('${directory.path}/$fileName');
final File videoCache = await DefaultCacheManager().getSingleFile(url);

if (Platform.isIOS) {
if (ext == '.webm') {
Expand All @@ -159,12 +156,11 @@ Future<void> saveVideo(
);

final ReturnCode? returnCode =
(await convertWebMToMP4(saveFile, saveFileVideo))
as ReturnCode?;
await convertWebMToMP4(videoCache, fileDownloadPath);

if (ReturnCode.isSuccess(returnCode)) {
await ImageGallerySaver.saveFile(
saveFileVideo.path.replaceAll('.webm', '.mp4'),
fileDownloadPath.path.replaceAll('.webm', '.mp4'),
isReturnPathOfIOS: true,
).then((value) => {
if (showSnackBar) Navigator.pop(context),
Expand All @@ -188,7 +184,7 @@ Future<void> saveVideo(
);
}
} else {
await ImageGallerySaver.saveFile(saveFile.path,
await ImageGallerySaver.saveFile(videoCache.path,
isReturnPathOfIOS: true)
.then((value) => {
if (showSnackBar) Navigator.pop(context),
Expand All @@ -213,41 +209,13 @@ Future<void> saveVideo(
savedAttachmentsProvider.startVideo();
}

Future<void> saveAllMedia(
String url, List<String> fileNames, BuildContext context) async {
showCupertinoSnackbar(
null,
false,
context,
'Downloading...',
);

for (final String fileName in fileNames) {
await saveVideo(
url + fileName,
fileName,
context,
);
}

Navigator.of(context).pop(true);

showCupertinoSnackbar(
const Duration(milliseconds: 1800),
true,
context,
'All files downloaded!',
);
}

Future<void> shareMedia(
String url,
String fileName,
BuildContext context, {
bool isSaved = false,
}) async {
Directory directory = Directory('');
final dio = Dio();

final SavedAttachmentsProvider savedAttachmentsProvider =
Provider.of<SavedAttachmentsProvider>(context, listen: false);
Expand All @@ -257,9 +225,11 @@ Future<void> shareMedia(
if (isSaved) {
directory = await requestDirectory(directory);

Share.shareFiles([
'${directory.path}/savedAttachments/$fileName'
.replaceAll('.webm', '.mp4'),
Share.shareXFiles([
XFile(
'${directory.path}/savedAttachments/$fileName'
.replaceAll('.webm', '.mp4'),
)
]);
} else {
Navigator.pop(context);
Expand All @@ -281,13 +251,8 @@ Future<void> shareMedia(
directory = await requestDirectory(directory);

if (await directory.exists()) {
final File saveFile = File('${directory.path}/$fileName');
final File saveFileVideo = File('${directory.path}/$fileName');

await dio.download(
url,
saveFile.path,
);
final File fileDownloadPath = File('${directory.path}/$fileName');
final File videoCache = await DefaultCacheManager().getSingleFile(url);

final String ext = '.${fileName.split('.').last}';

Expand All @@ -302,8 +267,7 @@ Future<void> shareMedia(
);

final ReturnCode? returnCode =
(await convertWebMToMP4(saveFile, saveFileVideo))
as ReturnCode?;
await convertWebMToMP4(videoCache, fileDownloadPath);

if (ReturnCode.isSuccess(returnCode)) {
Navigator.pop(context);
Expand All @@ -313,8 +277,10 @@ Future<void> shareMedia(
context,
'File downloaded!',
).then((value) => {
Share.shareFiles([
saveFileVideo.path.replaceAll('.webm', '.mp4'),
Share.shareXFiles([
XFile(
fileDownloadPath.path.replaceAll('.webm', '.mp4'),
)
]),
});
} else {
Expand All @@ -334,12 +300,16 @@ Future<void> shareMedia(
context,
'File downloaded!',
).then((value) => {
Share.shareFiles([saveFile.path])
Share.shareXFiles([
XFile(videoCache.path),
])
});
}
} else {
Navigator.pop(context);
Share.shareFiles([saveFile.path]);
Share.shareXFiles([
XFile(videoCache.path),
]);
}
}
} catch (e) {
Expand Down Expand Up @@ -376,8 +346,9 @@ Future<SavedAttachment?> saveAttachment(
directory = await requestDirectory(directory);

if (await directory.exists()) {
final File saveFile = File('${directory.path}/$fileName');
File saveFileVideo;
final File fileDownloadPath =
File('${directory.path}/savedAttachments/$fileName');
final File videoCache = await DefaultCacheManager().getSingleFile(url);

final Directory savedAttachmentsDirectory =
Directory('${directory.path}/savedAttachments');
Expand All @@ -386,13 +357,6 @@ Future<SavedAttachment?> saveAttachment(
await savedAttachmentsDirectory.create(recursive: true);
}

saveFileVideo = File('${directory.path}/savedAttachments/$fileName');

await dio.download(
url,
saveFile.path,
);

final String ext = '.${fileName.split('.').last}';

if (Platform.isIOS) {
Expand All @@ -406,7 +370,7 @@ Future<SavedAttachment?> saveAttachment(
);

final ReturnCode? returnCode =
(await convertWebMToMP4(saveFile, saveFileVideo)) as ReturnCode?;
await convertWebMToMP4(videoCache, fileDownloadPath);

if (ReturnCode.isSuccess(returnCode)) {
final String thumbnailPath = await downloadThumbnail(
Expand Down Expand Up @@ -445,7 +409,7 @@ Future<SavedAttachment?> saveAttachment(
return null;
}
} else {
saveFile.copy(saveFileVideo.path);
videoCache.copy(fileDownloadPath.path);

if (ext == '.mp4') {
final String thumbnailPath = await downloadThumbnail(
Expand Down Expand Up @@ -489,7 +453,7 @@ Future<SavedAttachment?> saveAttachment(
}
}
} else {
saveFile.copy(saveFileVideo.path);
videoCache.copy(fileDownloadPath.path);

Navigator.pop(context);

Expand Down Expand Up @@ -522,34 +486,6 @@ Future<SavedAttachment?> saveAttachment(
}
}

Future<dynamic> showCupertinoSnackbar(
Duration? duration,
bool dismissable,
BuildContext context,
String message,
) {
return showCupertinoDialog(
context: context,
builder: (context) {
if (duration != null) {
Future.delayed(duration, () {
Navigator.of(context).pop(true);
});
}
return GestureDetector(
onTap: () {
if (dismissable) {
Navigator.of(context).pop(true);
}
},
child: CupertinoAlertDialog(
title: Text(message),
),
);
},
);
}

Future<String> downloadThumbnail(
String fileName,
String thumbnailUrl,
Expand Down
5 changes: 3 additions & 2 deletions lib/pages/board/grid_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter_chan/blocs/bookmarks_model.dart';
import 'package:flutter_chan/blocs/theme.dart';
import 'package:flutter_chan/pages/replies_row.dart';
import 'package:flutter_chan/pages/thread/thread_page.dart';
import 'package:flutter_chan/widgets/image_viewer.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';

Expand Down Expand Up @@ -100,8 +101,8 @@ class _GridPostState extends State<GridPost> {
},
child: Stack(
children: [
Image.network(
'https://i.4cdn.org/${widget.board}/${widget.post.tim}s.jpg',
ImageViewer(
url: 'https://i.4cdn.org/${widget.board}/${widget.post.tim}s.jpg',
fit: BoxFit.cover,
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
Expand Down
6 changes: 4 additions & 2 deletions lib/pages/board/list_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter_chan/blocs/theme.dart';
import 'package:flutter_chan/pages/replies_row.dart';
import 'package:flutter_chan/pages/thread/thread_page.dart';
import 'package:flutter_chan/services/string.dart';
import 'package:flutter_chan/widgets/image_viewer.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:intl/intl.dart';
Expand Down Expand Up @@ -120,8 +121,9 @@ class _ListPostState extends State<ListPost> {
padding: const EdgeInsets.all(10),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.network(
'https://i.4cdn.org/${widget.board}/${widget.post.tim}s.jpg',
child: ImageViewer(
url:
'https://i.4cdn.org/${widget.board}/${widget.post.tim}s.jpg',
fit: BoxFit.cover,
),
),
Expand Down
18 changes: 10 additions & 8 deletions lib/pages/bookmarks/bookmarks_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_chan/enums/enums.dart';
import 'package:flutter_chan/pages/replies_row.dart';
import 'package:flutter_chan/pages/thread/thread_page.dart';
import 'package:flutter_chan/services/string.dart';
import 'package:flutter_chan/widgets/image_viewer.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -76,8 +77,9 @@ class _BookmarksPostState extends State<BookmarksPost> {
padding: const EdgeInsets.all(10),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.network(
'https://i.4cdn.org/${widget.favorite.board}/${widget.favorite.imageUrl}',
child: ImageViewer(
url:
'https://i.4cdn.org/${widget.favorite.board}/${widget.favorite.imageUrl}',
fit: BoxFit.cover,
),
),
Expand Down Expand Up @@ -212,12 +214,12 @@ class _BookmarksPostState extends State<BookmarksPost> {
child: Padding(
padding: const EdgeInsets.all(10),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.network(
'https://i.4cdn.org/${widget.favorite.board}/${widget.favorite.imageUrl}',
fit: BoxFit.cover,
),
),
borderRadius: BorderRadius.circular(10),
child: ImageViewer(
url:
'https://i.4cdn.org/${widget.favorite.board}/${widget.favorite.imageUrl}',
fit: BoxFit.cover,
)),
),
)
else
Expand Down
7 changes: 6 additions & 1 deletion lib/pages/media_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:preload_page_view/preload_page_view.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

class MediaPage extends StatefulWidget {
const MediaPage({
Expand Down Expand Up @@ -71,6 +71,11 @@ class _MediaPageState extends State<MediaPage> {
);
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeChanger>(context);
Expand Down
Loading

0 comments on commit 0eedbdd

Please sign in to comment.