Skip to content

Commit

Permalink
LA-1267 Clean cache after canceling media capture
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim committed Nov 12, 2024
1 parent 02981c0 commit 4ac482d
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 22 deletions.
3 changes: 2 additions & 1 deletion domain/lib/domain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,5 @@ export 'src/usecases/workgroup/get_all_workgroups_offline_interactor.dart';
export 'src/usecases/workgroup/workgroup_exception.dart';
export 'src/usecases/workgroup/workgroup_view_state.dart';
export 'src/usecases/media_picker_from_camera/media_picker_viewstate.dart';
export 'src/usecases/audio_recorder/audio_recorder_viewstate.dart';
export 'src/usecases/audio_recorder/audio_recorder_viewstate.dart';
export 'src/usecases/cache/remove_file_from_cache_interactor.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'dart:developer';
import 'dart:io';

class RemoveFileFromCacheInteractor {
Future<void> execute(String file) async {
try {
await File(file).delete();
} catch (exception) {
log('RemoveFileFromCacheInteractor Failed');
}
}
}
1 change: 1 addition & 0 deletions lib/presentation/di/module/app_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ class AppModule {
getIt.registerFactory(() => SaveQuotaInteractor(getIt<QuotaRepository>()));
getIt.registerFactory(() => SaveLastLoginInteractor(getIt<AuditUserRepository>()));
getIt.registerFactory(() => SearchRecipientsUploadRequestInteractor());
getIt.registerFactory(() => RemoveFileFromCacheInteractor());
}

void _provideSharePreference() {
Expand Down
4 changes: 3 additions & 1 deletion lib/presentation/di/module/widget_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ class WidgetModule {
getIt.get<AppNavigation>(),
getIt.get<UploadShareFileManager>(),
getIt.get<GetAutoCompleteSharingInteractor>(),
getIt.get<GetAutoCompleteSharingWithDeviceContactInteractor>()
getIt.get<GetAutoCompleteSharingWithDeviceContactInteractor>(),
getIt.get<RemoveFileFromCacheInteractor>()
));
}

Expand Down Expand Up @@ -782,6 +783,7 @@ class WidgetModule {
getIt.get<AppNavigation>(),
getIt.get<AudioRecorder>(),
getIt.get<Stopwatch>(),
getIt.get<RemoveFileFromCacheInteractor>()
));
}
}
6 changes: 4 additions & 2 deletions lib/presentation/util/audio_recorder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import 'package:permission_handler/permission_handler.dart';
class AudioRecorder {
final RecorderController recorderController = RecorderController();
final PermissionService permissionService = PermissionService();
String? recordingPath;

Future<Either<Failure, Success>> startRecordingAudio() async {
try {
Expand All @@ -49,7 +50,8 @@ class AudioRecorder {
final tempPath = Directory.systemTemp.path;
final currentTime = DateTime.now().millisecondsSinceEpoch;
final fileName = 'audio_$currentTime.m4a';
await recorderController.record('$tempPath/$fileName');
recordingPath = '$tempPath/$fileName';
await recorderController.record(recordingPath);
return Right(AudioRecorderStarted());
} else {
return Left(
Expand Down Expand Up @@ -103,7 +105,7 @@ class AudioRecorder {
Either<Failure, Success> resumeRecording() {
try {
recorderController.record();
return Right(AudioRecorderPaused());
return Right(AudioRecorderStarted());
} catch (exception) {
return Left(AudioRecorderFailed());
}
Expand Down
25 changes: 15 additions & 10 deletions lib/presentation/util/media_picker_from_camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ import 'package:wechat_camera_picker/wechat_camera_picker.dart';

class MediaPickerFromCamera {
var _phoneStateSubscription;
FileInfo? fileInfo;
FileInfo? fileInfo;

void _listenToPhoneState(Function onCallReceived,CameraPickerState cameraPickerState,
BuildContext context,) {
void _listenToPhoneState(
Function onCallReceived,
CameraPickerState cameraPickerState,
BuildContext context,
) {
_phoneStateSubscription = PhoneState.phoneStateStream.listen(
(status) {
if (status == PhoneStateStatus.CALL_INCOMING) {
onCallReceived.call(cameraPickerState,context);
onCallReceived.call(cameraPickerState, context);
}
},
);
Expand All @@ -64,15 +67,16 @@ class MediaPickerFromCamera {
await PermissionService().tryToGetPermissionForCamera();
final microphonePermission =
await PermissionService().tryToGetPermissionForAudioRecording();

final phonePermission= await PermissionService().tryToGetPermissionForPhoneState();
final phonePermission =
await PermissionService().tryToGetPermissionForPhoneState();

if (cameraPermission.isGranted && microphonePermission.isGranted) {

List<FileInfo> pickedFiles = [];
CameraPickerState cameraPickerState = CameraPickerState();
if(phonePermission.isGranted){
_listenToPhoneState(handleIncomingPhoneCallWhileRecordingCallback,cameraPickerState,context);
fileInfo = null;
if (phonePermission.isGranted) {
_listenToPhoneState(handleIncomingPhoneCallWhileRecordingCallback,
cameraPickerState, context);
}
await CameraPicker.pickFromCamera(
createPickerState: () => cameraPickerState,
Expand All @@ -91,6 +95,7 @@ class MediaPickerFromCamera {
CameraPickerViewer.pushToViewer(
context,
pickerConfig: CameraPickerConfig(
shouldDeletePreviewFile: true,
onEntitySaving: ((context, viewType, file) {
fileInfo = FileInfo(
file.path.split('/').last,
Expand Down Expand Up @@ -155,10 +160,10 @@ class MediaPickerFromCamera {
) async {
if (cameraPickerState.controller.value.isRecordingVideo) {
final xFile = await cameraPickerState.controller.stopVideoRecording();

await CameraPickerViewer.pushToViewer(
context,
pickerConfig: CameraPickerConfig(
shouldDeletePreviewFile: true,
onEntitySaving: ((context, viewType, file) {
fileInfo = FileInfo(
file.path.split('/').last,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class CustomCameraPickerViewer extends CameraPickerViewerState {
Widget buildConfirmButton(BuildContext context) {
return WillPopScope(
onWillPop: () async {
return await DiscardConfirmationDialog()
var popAndDiscard = await DiscardConfirmationDialog()
.showExitConfirmationDialog(context) ??
false;
if (popAndDiscard) {
await previewFile.delete();
}
return popAndDiscard;
},
child: super.buildConfirmButton(context),
);
Expand Down
3 changes: 2 additions & 1 deletion lib/presentation/widget/myspace/my_space_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,8 @@ class MySpaceViewModel extends BaseViewModel {
? success.pickedFiles
: success is MediaPickerSuccessViewState
? success.file
: []));
: [],
cleanUpCacheFile: success is MediaPickerSuccessViewState));
};
}

Expand Down
18 changes: 15 additions & 3 deletions lib/presentation/widget/record_audio/record_audio_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ class RecordAudioViewModel extends BaseViewModel {
UploadFileArguments? uploadFileArguments;
var _phoneStateSubscription;
var duration;
final RemoveFileFromCacheInteractor removeFileFromCacheInteractor;

RecordAudioViewModel(Store<AppState> store, this._appNavigation,
this.audioRecorder, this.stopwatch)
: super(store) {
RecordAudioViewModel(
Store<AppState> store,
this._appNavigation,
this.audioRecorder,
this.stopwatch,
this.removeFileFromCacheInteractor,
) : super(store) {
_listenToPhoneState();
}

Expand Down Expand Up @@ -119,6 +124,7 @@ class RecordAudioViewModel extends BaseViewModel {
_appNavigation.popBack();
var uploadArgs = UploadFileArguments(
success.file,
cleanUpCacheFile: true,
);
if (uploadFileArguments != null) {
uploadArgs.shareType = uploadFileArguments!.shareType;
Expand Down Expand Up @@ -245,6 +251,12 @@ class RecordAudioViewModel extends BaseViewModel {
);
}

void removeFileFromCache() {
if (audioRecorder.recordingPath != null) {
removeFileFromCacheInteractor.execute(audioRecorder.recordingPath!);
}
}

@override
void onDisposed() {
super.onDisposed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ class RecordAudioWidgetState extends State<RecordAudioWidget> {
} else if (success is AudioRecorderStarted) {
recordAudioViewModel.pauseAudioRecording();
}
return await DiscardConfirmationDialog()
final popAndDiscard = await DiscardConfirmationDialog()
.showExitConfirmationDialog(context) ??
false;
if (popAndDiscard) {
recordAudioViewModel.removeFileFromCache();
}
return popAndDiscard;
});
},
child: Center(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ class UploadFileArguments extends RouterArguments {
ShareDestination? shareDestination;
List<Document>? documents;
late WorkGroupDocumentUploadInfo? workGroupDocumentUploadInfo;
final bool cleanUpCacheFile;

UploadFileArguments(this.uploadFiles, {
ShareType shareType = ShareType.uploadAndShare,
List<Document>? documents,
ShareDestination? shareDestination,
WorkGroupDocumentUploadInfo? workGroupDocumentUploadInfo
WorkGroupDocumentUploadInfo? workGroupDocumentUploadInfo,
this.cleanUpCacheFile = false
}) {
this.shareType = shareType;
this.shareDestination = shareDestination;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
// the Additional Terms applicable to LinShare software.

import 'dart:async';

import 'package:domain/domain.dart';
import 'package:flutter/material.dart';
import 'package:linshare_flutter_app/presentation/localizations/app_localizations.dart';
Expand Down Expand Up @@ -63,10 +62,12 @@ class UploadFileViewModel extends BaseViewModel {
final UploadShareFileManager _uploadShareFileManager;
final GetAutoCompleteSharingInteractor _getAutoCompleteSharingInteractor;
final GetAutoCompleteSharingWithDeviceContactInteractor _getAutoCompleteSharingWithDeviceContactInteractor;
final RemoveFileFromCacheInteractor _removeFileFromCacheInteractor;
late StreamSubscription _autoCompleteResultListSubscription;
ContactSuggestionSource _contactSuggestionSource = ContactSuggestionSource.linShareContact;

List<FileInfo>? _uploadFilesArgument;
bool cleanUpCacheFile = false;

ShareType _shareTypeArgument = ShareType.uploadAndShare;
ShareType get shareTypeArgument => _shareTypeArgument;
Expand Down Expand Up @@ -94,6 +95,7 @@ class UploadFileViewModel extends BaseViewModel {
this._uploadShareFileManager,
this._getAutoCompleteSharingInteractor,
this._getAutoCompleteSharingWithDeviceContactInteractor,
this._removeFileFromCacheInteractor
) : super(store) {
_autoCompleteResultListSubscription = _autoCompleteResultListObservable.listen((shareMails) {
switch (_shareTypeArgument) {
Expand All @@ -120,6 +122,10 @@ class UploadFileViewModel extends BaseViewModel {
}

void backToMySpace() {
if (cleanUpCacheFile && _uploadFilesArgument!=null) {
var filepath= '${_uploadFilesArgument![0].filePath}${_uploadFilesArgument![0].fileName}';
_removeFileFromCacheInteractor.execute(filepath);
}
_appNavigation.popBack();
store.dispatch(CleanUploadStateAction());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class _UploadFileWidgetState extends State<UploadFileWidget> {
uploadFileViewModel.setUploadFilesArgument(arguments.uploadFiles);
uploadFileViewModel.setShareTypeArgument(arguments.shareType);
uploadFileViewModel.setShareDestinationArgument(arguments.shareDestination);
uploadFileViewModel.cleanUpCacheFile = arguments.cleanUpCacheFile;
if (arguments.documents != null) {
uploadFileViewModel.setDocumentsArgument(arguments.documents);
}
Expand Down

0 comments on commit 4ac482d

Please sign in to comment.