Skip to content

Commit

Permalink
Merge pull request #1554 from ardriveapp/PE-5437-add-an-option-to-the…
Browse files Browse the repository at this point in the history
…-user-update-the-file-extension-when-renaming-a-file

PE-5437: add an option to the user update the file extension when renaming a file
  • Loading branch information
thiagocarvalhodev authored Jan 30, 2024
2 parents cd76603 + 86f4493 commit 5a920df
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
},
"bloc.newBlocTemplate": "equatable",
"bloc.newCubitTemplate": "equatable",
Expand Down
63 changes: 58 additions & 5 deletions lib/blocs/fs_entry_rename/fs_entry_rename_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:ardrive/models/models.dart';
import 'package:ardrive/services/services.dart';
import 'package:ardrive/turbo/services/upload_service.dart';
import 'package:ardrive/utils/logger.dart';
import 'package:ardrive_io/ardrive_io.dart';
import 'package:drift/drift.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:reactive_forms/reactive_forms.dart';
Expand All @@ -26,6 +28,8 @@ class FsEntryRenameCubit extends Cubit<FsEntryRenameState> {

bool get _isRenamingFolder => folderId != null;

bool _dontVerifyExtension = false;

FsEntryRenameCubit({
required this.driveId,
this.folderId,
Expand All @@ -47,7 +51,14 @@ class FsEntryRenameCubit extends Cubit<FsEntryRenameState> {
emit(FsEntryRenameInitialized(isRenamingFolder: _isRenamingFolder));
}

Future<void> submit({required String newName}) async {
Future<void> reset() async {
emit(FsEntryRenameInitialized(isRenamingFolder: _isRenamingFolder));
}

Future<void> submit({
required String newName,
bool updateExtension = false,
}) async {
try {
late bool hasEntityWithSameName;

Expand Down Expand Up @@ -114,13 +125,48 @@ class FsEntryRenameCubit extends Cubit<FsEntryRenameState> {

emit(const FolderEntryRenameSuccess());
} else {
var file = await _driveDao
.fileById(driveId: driveId, fileId: fileId!)
.getSingle();

if (!updateExtension && !_dontVerifyExtension) {
final newFileExtension =
getFileExtensionFromFileName(fileName: newName);

if (newFileExtension.isNotEmpty) {
bool hasExtensionChanged;

final currentExtension = getFileExtension(
name: file.name, contentType: file.dataContentType!);

hasExtensionChanged = currentExtension != newFileExtension;

if (hasExtensionChanged) {
emit(
UpdatingEntityExtension(
previousExtension: currentExtension,
entityName: newName,
newExtension: newFileExtension,
),
);

return;
}
}
}

emit(const FileEntryRenameInProgress());

await _driveDao.transaction(() async {
var file = await _driveDao
.fileById(driveId: driveId, fileId: fileId!)
.getSingle();
file = file.copyWith(name: newName, lastUpdated: DateTime.now());
file = file.copyWith(
name: newName,
lastUpdated: DateTime.now(),
);

if (updateExtension) {
file =
file.copyWith(dataContentType: Value(lookupMimeType(newName)));
}

final fileKey = driveKey != null
? await _crypto.deriveFileKey(driveKey, file.id)
Expand Down Expand Up @@ -148,6 +194,9 @@ class FsEntryRenameCubit extends Cubit<FsEntryRenameState> {
fileEntity.txId = fileTx.id;
}

logger.i(
'Updating file ${file.id} with txId ${fileEntity.txId}. Data content type: ${fileEntity.dataContentType}');

await _driveDao.writeToFile(file);

await _driveDao.insertFileRevision(fileEntity.toRevisionCompanion(
Expand Down Expand Up @@ -188,6 +237,10 @@ class FsEntryRenameCubit extends Cubit<FsEntryRenameState> {
return entityWithSameNameExists;
}

void dontVerifyExtension() {
_dontVerifyExtension = true;
}

@override
void onError(Object error, StackTrace stackTrace) {
if (_isRenamingFolder) {
Expand Down
12 changes: 12 additions & 0 deletions lib/blocs/fs_entry_rename/fs_entry_rename_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ class EntityAlreadyExists extends FsEntryRenameState {
final String entityName;
}

class UpdatingEntityExtension extends FsEntryRenameState {
const UpdatingEntityExtension({
required this.entityName,
required this.newExtension,
required this.previousExtension,
}) : super(isRenamingFolder: false);

final String entityName;
final String newExtension;
final String previousExtension;
}

class FolderEntryRenameWalletMismatch extends FsEntryRenameState {
const FolderEntryRenameWalletMismatch() : super(isRenamingFolder: true);
}
Expand Down
36 changes: 33 additions & 3 deletions lib/components/fs_entry_rename_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:ardrive/turbo/services/upload_service.dart';
import 'package:ardrive/utils/app_localizations_wrapper.dart';
import 'package:ardrive/utils/show_general_dialog.dart';
import 'package:ardrive/utils/validate_folder_name.dart';
import 'package:ardrive_io/ardrive_io.dart';
import 'package:ardrive_ui/ardrive_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down Expand Up @@ -105,6 +106,33 @@ class _FsEntryRenameFormState extends State<FsEntryRenameForm> {
),
),
);
} else if (state is UpdatingEntityExtension) {
showArDriveDialog(
context,
content: ArDriveStandardModal(
title: 'Do you want to change the file extension?',
description: 'The file extension will be changed from '
'${state.previousExtension} to ${getFileExtension(name: state.entityName, contentType: state.newExtension)}',
actions: [
ModalAction(
action: () {
context.read<FsEntryRenameCubit>().reset();
Navigator.of(context).pop(context);
},
title: appLocalizationsOf(context).cancelEmphasized,
),
ModalAction(
action: () {
context.read<FsEntryRenameCubit>().submit(
newName: _nameController.text, updateExtension: true);
Navigator.of(context).pop();
},
title: 'Submit',
isEnable: _validForm,
),
],
),
);
}
},
builder: (context, state) {
Expand Down Expand Up @@ -145,9 +173,11 @@ class _FsEntryRenameFormState extends State<FsEntryRenameForm> {
title: appLocalizationsOf(context).cancelEmphasized,
),
ModalAction(
action: () => context
.read<FsEntryRenameCubit>()
.submit(newName: _nameController.text),
action: () {
context
.read<FsEntryRenameCubit>()
.submit(newName: _nameController.text);
},
title: appLocalizationsOf(context).renameEmphasized,
isEnable: _validForm,
),
Expand Down
20 changes: 10 additions & 10 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "v1.4.5"
resolved-ref: "286d6366ceffe44e21d1257507090f8b6ec8beaa"
ref: PE-5529-txt-file-extension-changes-incorrectly-in-edit-modal
resolved-ref: "9e43c831ed30c0a2985bc6288c8ae3a4ef4f290d"
url: "https://github.com/ar-io/ardrive_io.git"
source: git
version: "1.4.5"
Expand Down Expand Up @@ -1255,10 +1255,10 @@ packages:
dependency: transitive
description:
name: jovial_svg
sha256: "5fd52a96fe3d2b69082c53374364649f51cb1b3e06ca6be3601ed8cae537bc7b"
sha256: "7162d9fce53e284782f6178e175c9e07dde695779392eaa2542c1f795658eb3d"
url: "https://pub.dev"
source: hosted
version: "1.1.17"
version: "1.1.20"
js:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1431,10 +1431,10 @@ packages:
dependency: transitive
description:
name: mutex
sha256: "03116a4e46282a671b46c12de649d72c0ed18188ffe12a8d0fc63e83f4ad88f4"
sha256: "8827da25de792088eb33e572115a5eb0d61d61a3c01acbc8bcbe76ed78f1a1f2"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
version: "3.1.0"
nested:
dependency: transitive
description:
Expand Down Expand Up @@ -1915,18 +1915,18 @@ packages:
dependency: transitive
description:
name: sqflite
sha256: "591f1602816e9c31377d5f008c2d9ef7b8aca8941c3f89cc5fd9d84da0c38a9a"
sha256: c2c32eb0c74021d987336522acc3b6bf0082fbd0c540c36a9cf4ddb8ba891ddc
url: "https://pub.dev"
source: hosted
version: "2.3.0"
version: "2.3.1"
sqflite_common:
dependency: transitive
description:
name: sqflite_common
sha256: "1b92f368f44b0dee2425bb861cfa17b6f6cf3961f762ff6f941d20b33355660a"
sha256: "28d8c66baee4968519fb8bd6cdbedad982d6e53359091f0b74544a9f32ec72d5"
url: "https://pub.dev"
source: hosted
version: "2.5.0"
version: "2.5.3"
sqlite3:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ dependency_overrides:
ardrive_io:
git:
url: https://github.com/ar-io/ardrive_io.git
ref: v1.4.5
ref: PE-5529-txt-file-extension-changes-incorrectly-in-edit-modal

dev_dependencies:
integration_test:
Expand Down

0 comments on commit 5a920df

Please sign in to comment.