Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Mar 21, 2024
1 parent 604b85d commit e3cb620
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 93 deletions.
168 changes: 81 additions & 87 deletions lib/pages/chat/events/message_download_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:dartz/dartz.dart' hide State, OpenFile;
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/presentation/model/chat/downloading_state_presentation_model.dart';
import 'package:fluffychat/utils/manager/download_manager/download_file_state.dart';
import 'package:fluffychat/utils/manager/download_manager/download_manager.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/download_file_extension.dart';
Expand Down Expand Up @@ -35,10 +36,10 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>
with HandleDownloadAndPreviewFileMixin {
final downloadManager = getIt.get<DownloadManager>();

final downloadProgressNotifier = ValueNotifier<double?>(0);

final downloadFileStateNotifier =
ValueNotifier(DownloadFileState.notDownload);
ValueNotifier<DownloadStatePresentationModel>(
const NotDownloadStatePresentationModel(),
);

StreamSubscription<Either<Failure, Success>>? streamSubscription;

Expand All @@ -49,26 +50,25 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>
@override
void initState() {
super.initState();
startListenToDownloading();
loadFilePath();
}

void loadFilePath() async {
final filePath = await widget.event.getFileNameInAppDownload();
if (await File(filePath).exists() && streamSubscription == null) {
onDownloadedState(filePath);
} else if (streamSubscription != null) {
downloadFileStateNotifier.value = DownloadFileState.downloading;
downloadProgressNotifier.value = null;
}
checkDownloadFileState();
}

void startListenToDownloading() {
void checkDownloadFileState() async {
streamSubscription = downloadManager
.getDownloadStateStream(widget.event.eventId)
?.listen((event) {
onDownloadingProcess(event);
});

if (streamSubscription != null) {
downloadFileStateNotifier.value =
const DownloadingStatePresentationModel();
} else {
final filePath = await widget.event.getFileNameInAppDownload();
if (await File(filePath).exists()) {
onDownloadedState(filePath);
}
}
}

void onDownloadingProcess(Either<Failure, Success> event) {
Expand All @@ -80,29 +80,33 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>
(success) {
if (success is DownloadingFileState) {
if (success.total != 0) {
downloadFileStateNotifier.value = DownloadFileState.downloading;
downloadProgressNotifier.value = success.receive / success.total;
downloadFileStateNotifier.value = DownloadingStatePresentationModel(
receive: success.receive,
total: success.total,
);
}
} else if (success is DownloadNativeFileSuccessState) {
onDownloadedState(success.filePath);
onDownloadedState(
success.filePath,
receive: success.receive,
total: success.total,
);
}
},
);
}

void onNotDownloadState() {
downloadProgressNotifier.value = 0;
downloadFileStateNotifier.value = DownloadFileState.notDownload;
downloadFileStateNotifier.value = const NotDownloadStatePresentationModel();
}

void onDownloadedState(String filePath) {
downloadFileStateNotifier.value = DownloadFileState.downloaded;
void onDownloadedState(String filePath, {int? receive, int? total}) {
downloadFileStateNotifier.value = const DownloadedStatePresentationModel();
filePathNotifier.value = filePath;
}

void onStartDownloading() {
downloadFileStateNotifier.value = DownloadFileState.downloading;
downloadProgressNotifier.value = null;
downloadFileStateNotifier.value = const DownloadingStatePresentationModel();
}

@override
Expand All @@ -118,71 +122,61 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>
final sizeString = widget.event.sizeString;
return ValueListenableBuilder(
valueListenable: downloadFileStateNotifier,
builder: (context, DownloadFileState state, child) {
switch (state) {
case DownloadFileState.notDownload:
return InkWell(
onTap: () async {
downloadManager.download(
savePath: await widget.event.getFileNameInAppDownload(),
event: widget.event,
);
onStartDownloading();
startListenToDownloading();
},
child: DownloadFileTileWidget(
builder: (context, DownloadStatePresentationModel state, child) {
if (state is NotDownloadStatePresentationModel) {
return InkWell(
onTap: () async {
downloadManager.download(
savePath: await widget.event.getFileNameInAppDownload(),
event: widget.event,
);
onStartDownloading();
checkDownloadFileState();
},
child: DownloadFileTileWidget(
mimeType: widget.event.mimeType,
fileType: filetype,
filename: filename,
highlightText: widget.highlightText,
sizeString: sizeString,
downloadFileStateNotifier: downloadFileStateNotifier,
style: const MessageFileTileStyle(),
),
);
} else if (state is DownloadingStatePresentationModel) {
return DownloadFileTileWidget(
mimeType: widget.event.mimeType,
fileType: filetype,
filename: filename,
highlightText: widget.highlightText,
sizeString: sizeString,
style: const MessageFileTileStyle(),
downloadFileStateNotifier: downloadFileStateNotifier,
onCancelDownload: () {
onNotDownloadState();
downloadManager.cancelDownload(widget.event.eventId);
},
);
} else if (state is DownloadedStatePresentationModel) {
return InkWell(
onTap: () async {
openDownloadedFileForPreview(
filePath: filePathNotifier.value,
mimeType: widget.event.mimeType,
fileType: filetype,
filename: filename,
highlightText: widget.highlightText,
sizeString: sizeString,
downloadProgressNotifier: downloadProgressNotifier,
style: const MessageFileTileStyle(),
),
);
case DownloadFileState.downloading:
return Stack(
children: [
DownloadFileTileWidget(
mimeType: widget.event.mimeType,
fileType: filetype,
filename: filename,
highlightText: widget.highlightText,
sizeString: sizeString,
style: const MessageFileTileStyle(),
downloadProgressNotifier: downloadProgressNotifier,
onCancelDownload: () {
onNotDownloadState();
downloadManager.cancelDownload(widget.event.eventId);
},
),
],
);
case DownloadFileState.downloaded:
return InkWell(
onTap: () async {
openDownloadedFileForPreview(
filePath: filePathNotifier.value,
mimeType: widget.event.mimeType,
);
},
child: FileTileWidget(
mimeType: widget.event.mimeType,
fileType: filetype,
filename: filename,
highlightText: widget.highlightText,
sizeString: sizeString,
style: const MessageFileTileStyle(),
),
);
);
},
child: FileTileWidget(
mimeType: widget.event.mimeType,
fileType: filetype,
filename: filename,
highlightText: widget.highlightText,
sizeString: sizeString,
style: const MessageFileTileStyle(),
),
);
}
return const SizedBox.shrink();
},
);
}
}

enum DownloadFileState {
notDownload,
downloading,
downloaded,
}
5 changes: 5 additions & 0 deletions lib/presentation/enum/chat/download_file_state_enum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum DownloadFilePresentationState {
notDownload,
downloading,
downloaded,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:equatable/equatable.dart';
import 'package:fluffychat/presentation/enum/chat/download_file_state_enum.dart';

abstract class DownloadStatePresentationModel with EquatableMixin {
const DownloadStatePresentationModel({
required this.downloadFilePresentationState,
this.receive,
this.total,
});

final DownloadFilePresentationState downloadFilePresentationState;

final int? receive;

final int? total;

@override
List<Object?> get props => [downloadFilePresentationState, receive, total];
}

class NotDownloadStatePresentationModel extends DownloadStatePresentationModel {
const NotDownloadStatePresentationModel()
: super(
receive: 0,
total: 0,
downloadFilePresentationState:
DownloadFilePresentationState.notDownload,
);
}

class DownloadedStatePresentationModel extends DownloadStatePresentationModel {
const DownloadedStatePresentationModel()
: super(
downloadFilePresentationState:
DownloadFilePresentationState.downloaded,
);
}

class DownloadingStatePresentationModel extends DownloadStatePresentationModel {
const DownloadingStatePresentationModel({
int? receive,
int? total,
}) : super(
downloadFilePresentationState:
DownloadFilePresentationState.downloading,
receive: receive,
total: total,
);
}
12 changes: 10 additions & 2 deletions lib/utils/manager/download_manager/download_file_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ class DownloadingFileState extends DownloadFileState {
}

class DownloadNativeFileSuccessState extends DownloadFileState {
const DownloadNativeFileSuccessState({required this.filePath});
const DownloadNativeFileSuccessState({
required this.filePath,
required this.receive,
required this.total,
});

final String filePath;

final int receive;

final int total;

@override
List<Object?> get props => [filePath];
List<Object?> get props => [filePath, receive, total];
}

class DecryptingFileState extends DownloadFileState {
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/manager/download_manager/download_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class DownloadManager {
event,
savePath,
streamController,
receive,
total,
);
} else {
streamController.add(
Expand Down Expand Up @@ -151,6 +153,8 @@ class DownloadManager {
Event event,
String savePath,
StreamController<Either<Failure, Success>> streamController,
int receive,
int total,
) async {
final fileInfo = FileInfo(
event.filename,
Expand All @@ -163,12 +167,16 @@ class DownloadManager {
event,
fileInfo,
savePath,
receive,
total,
);
} else {
streamController.add(
Right(
DownloadNativeFileSuccessState(
filePath: fileInfo.filePath,
receive: receive,
total: total,
),
),
);
Expand All @@ -181,6 +189,8 @@ class DownloadManager {
Event event,
FileInfo fileInfo,
String savePath,
int receive,
int total,
) async {
streamController.add(
const Right(
Expand All @@ -203,6 +213,8 @@ class DownloadManager {
Right(
DownloadNativeFileSuccessState(
filePath: saveFile.path,
receive: receive,
total: total,
),
),
);
Expand Down
Loading

0 comments on commit e3cb620

Please sign in to comment.