From 52d217a89c3948739ff24fb68449b5e426b19cce Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 11 Mar 2021 00:13:05 +0100 Subject: [PATCH] Simplify conversation files appearance --- lib/helpers/conversation_files_helper.dart | 46 ------- lib/models/conversation_message.dart | 2 - lib/ui/widgets/conversation_file_tile.dart | 139 +-------------------- pubspec.lock | 7 -- pubspec.yaml | 3 - 5 files changed, 3 insertions(+), 194 deletions(-) delete mode 100644 lib/helpers/conversation_files_helper.dart diff --git a/lib/helpers/conversation_files_helper.dart b/lib/helpers/conversation_files_helper.dart deleted file mode 100644 index 386a381..0000000 --- a/lib/helpers/conversation_files_helper.dart +++ /dev/null @@ -1,46 +0,0 @@ -import 'dart:io'; - -import 'package:comunic/models/conversation_message.dart'; -import 'package:dio/dio.dart'; -import 'package:flutter/material.dart'; -import 'package:path/path.dart' as path; -import 'package:path_provider/path_provider.dart'; - -/// Conversation files helper -/// -/// @author Pierre Hubert - -class ConversationFilesHelper { - /// Get the path for chat file - static Future getPathForChatFile( - int msgID, ConversationMessageFile fileInfo) async { - Directory basePath = await getTemporaryDirectory(); - - final storageDir = path.join(basePath.absolute.path, "conversation-files"); - final fileName = "$msgID"; - - return File(path.join(storageDir, fileName)); - } - - /// Download chat file - static Future download({ - @required int msgID, - @required ConversationMessageFile fileInfo, - @required Function(double) onProgress, - @required CancelToken cancelToken, - }) async { - final target = await getPathForChatFile(msgID, fileInfo); - - // Create parent directory if required - if (!await target.parent.exists()) { - await target.parent.create(recursive: true); - } - - await Dio().download( - fileInfo.url, - target.path, - cancelToken: cancelToken, - onReceiveProgress: (p, t) => onProgress(p / fileInfo.size.toDouble()), - ); - } -} diff --git a/lib/models/conversation_message.dart b/lib/models/conversation_message.dart index 403df94..8ae2c1a 100644 --- a/lib/models/conversation_message.dart +++ b/lib/models/conversation_message.dart @@ -78,8 +78,6 @@ class ConversationMessageFile { bool get hasThumbnail => thumbnail != null; - bool get downloadable => fileType == ConversationMessageFileType.AUDIO; - Map toJson() => { "url": url, "size": size, diff --git a/lib/ui/widgets/conversation_file_tile.dart b/lib/ui/widgets/conversation_file_tile.dart index b5f9404..fc7f41f 100644 --- a/lib/ui/widgets/conversation_file_tile.dart +++ b/lib/ui/widgets/conversation_file_tile.dart @@ -1,19 +1,8 @@ /// Chat file tile /// /// @author Pierre Hubert -import 'dart:io'; - -import 'package:cached_network_image/cached_network_image.dart'; -import 'package:comunic/helpers/conversation_files_helper.dart'; import 'package:comunic/models/conversation_message.dart'; -import 'package:comunic/ui/widgets/async_screen_widget.dart'; -import 'package:comunic/ui/widgets/audio_player_widget.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart'; -import 'package:comunic/utils/intl_utils.dart'; -import 'package:comunic/utils/log_utils.dart'; -import 'package:comunic/utils/ui_utils.dart'; -import 'package:dio/dio.dart'; -import 'package:filesize/filesize.dart'; import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -36,131 +25,13 @@ class ConversationFileWidget extends StatefulWidget { } class _ConversationFileWidgetState extends State { - final _refreshKey = GlobalKey(); - - File _targetFile; - - bool _isDownloaded; - - bool _downloading = false; - var _downloadProgress = 0.0; - CancelToken _cancelDownloadToken; - ConversationMessageFile get file => widget.file; - Future _refresh() async { - _targetFile = await ConversationFilesHelper.getPathForChatFile( - widget.messageID, file); - - _isDownloaded = await _targetFile.exists(); - } - @override - Widget build(BuildContext context) { - return Container( - width: _AreaSize, - height: _AreaSize, - child: AsyncScreenWidget( - key: _refreshKey, - onReload: _refresh, - onBuild: _buildContent, - errorMessage: tr("Error!"), - ), - ); - } + Widget build(BuildContext context) => + Container(width: _AreaSize, height: _AreaSize, child: _buildContent()); - Widget _buildContent() => _isDownloaded || !file.downloadable - ? _buildFileWidget() - : _buildDownloadWidget(); - - Widget _buildDownloadWidget() => Stack( - children: [ - // Thumbnail, if possible - !file.hasThumbnail - ? Container() - : CachedNetworkImage( - imageUrl: file.thumbnail, - width: _AreaSize, - height: _AreaSize, - fit: BoxFit.fill, - ), - - Container( - width: _AreaSize, - color: Color(0x66000000), - child: DefaultTextStyle( - style: TextStyle(color: Colors.white), - child: Column( - children: [ - Spacer(), - Icon(file.icon, color: Colors.white), - Spacer(), - _buildDownloadArea(), - Spacer(), - Text(filesize(file.size)), - Spacer(), - ], - ), - ), - ) - ], - ); - - Widget _buildDownloadArea() => _downloading - ? _buildDownloadingWidget() - : Material( - borderRadius: BorderRadius.all(Radius.circular(2.0)), - color: Colors.green, - child: IconButton( - icon: Icon(Icons.file_download), - onPressed: _downloadFile, - color: Colors.white, - ), - ); - - Widget _buildDownloadingWidget() => Container( - width: 36, - height: 36, - child: Stack( - children: [ - CircularProgressIndicator(value: _downloadProgress), - Center( - child: InkWell( - onTap: () => _cancelDownloadToken.cancel(), - child: Icon(Icons.cancel, color: Colors.white), - ), - ) - ], - ), - ); - - Future _downloadFile() async { - try { - setState(() { - _cancelDownloadToken = CancelToken(); - _downloading = true; - _downloadProgress = 0.0; - }); - - await ConversationFilesHelper.download( - msgID: widget.messageID, - fileInfo: file, - onProgress: (p) => setState(() => _downloadProgress = p), - cancelToken: _cancelDownloadToken, - ); - - await _refreshKey.currentState.refresh(); - } catch (e, s) { - logError(e, s); - showSimpleSnack(context, tr("Failed to download file!")); - } - - setState(() { - _downloading = false; - }); - } - - Widget _buildFileWidget() { + Widget _buildContent() { switch (file.fileType) { // Images case ConversationMessageFileType.IMAGE: @@ -172,10 +43,6 @@ class _ConversationFileWidgetState extends State { ), ); - // Audio player - case ConversationMessageFileType.AUDIO: - return AudioPlayerWidget(_targetFile); - // The file is not downloadable, we open it in the browser default: return Center( diff --git a/pubspec.lock b/pubspec.lock index f3d400d..4673d0e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -22,13 +22,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.5.0-nullsafety.1" - audioplayers: - dependency: "direct main" - description: - name: audioplayers - url: "https://pub.dartlang.org" - source: hosted - version: "0.15.1" boolean_selector: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 262f079..cb2a015 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -91,9 +91,6 @@ dependencies: # Version manager version: ^1.2.0 - # Play audio files - audioplayers: ^0.15.1 - # Get path to temporary files path_provider: ^1.6.27