Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix attach drives #1910

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/fastlane/metadata/android/en-US/changelogs/160.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixes an issue attaching drives
20 changes: 13 additions & 7 deletions lib/blocs/drive_attach/drive_attach_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DriveAttachCubit extends Cubit<DriveAttachState> {
final SyncCubit _syncBloc;
final DrivesCubit _drivesBloc;
final SecretKey? _profileKey;
final DriveDetailCubit _driveDetailCubit;

final driveNameController = TextEditingController();
final driveKeyController = TextEditingController();
Expand All @@ -40,11 +41,13 @@ class DriveAttachCubit extends Cubit<DriveAttachState> {
required DriveDao driveDao,
required SyncCubit syncBloc,
required DrivesCubit drivesBloc,
required DriveDetailCubit driveDetailCubit,
}) : _arweave = arweave,
_driveDao = driveDao,
_syncBloc = syncBloc,
_drivesBloc = drivesBloc,
_profileKey = profileKey,
_driveDetailCubit = driveDetailCubit,
super(DriveAttachInitial()) {
initializeForm(
driveId: initialDriveId,
Expand Down Expand Up @@ -141,15 +144,18 @@ class DriveAttachCubit extends Cubit<DriveAttachState> {
profileKey: _profileKey,
);

emit(DriveAttachSuccess());

/// Wait for the sync to finish before syncing the newly attached drive.
await _syncBloc.waitCurrentSync();
_syncBloc.waitCurrentSync().then((value) {
logger.d('after sync drives attach');

/// Then, sync and select the newly attached drive.
unawaited(_syncBloc.startSync());

/// Then, sync and select the newly attached drive.
unawaited(_syncBloc
.startSync()
.then((value) => _drivesBloc.selectDrive(driveId)));
_drivesBloc.selectDrive(driveId);
_driveDetailCubit.changeDrive(driveId);
});

emit(DriveAttachSuccess());

PlausibleEventTracker.trackAttachDrive(
drivePrivacy: drivePrivacy,
Expand Down
19 changes: 14 additions & 5 deletions lib/blocs/drive_detail/drive_detail_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
}

Future<void> changeDrive(String driveId) async {
logger.d('changeDrive on cubit: $driveId');

final drive = await _driveDao.driveById(driveId: driveId).getSingleOrNull();

if (drive == null) {
Expand Down Expand Up @@ -138,15 +140,22 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
_folderSubscription =
Rx.combineLatest3<Drive?, FolderWithContents, ProfileState, void>(
_driveRepository.watchDrive(driveId: driveId),
_driveDao.watchFolderContents(
_driveDao
.watchFolderContents(
driveId,
orderBy: contentOrderBy,
orderingMode: contentOrderingMode,
folderId: folderId,
),
)
.handleError((e) {
if (e is DriveNotFoundException) {
emit(DriveDetailLoadNotFound());
}
}),
_profileCubit.stream.startWith(ProfileCheckingAvailability()),
(drive, folderContents, _) async {
if (isClosed) {
logger.d('drive detail cubit is closed');
return;
}

Expand Down Expand Up @@ -290,11 +299,11 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
emit(DriveDetailLoadNotFound());
return;
}

logger.e('An error occured mouting the drive explorer', e);
});

await _folderSubscription?.asFuture();
await _folderSubscription?.asFuture().catchError((e, stacktrace) {
emit(DriveDetailLoadNotFound());
});
}

List<ArDriveDataTableItem> parseEntitiesToDatatableItem({
Expand Down
4 changes: 4 additions & 0 deletions lib/blocs/drives/drives_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:ardrive/blocs/prompt_to_snapshot/prompt_to_snapshot_event.dart';
import 'package:ardrive/core/activity_tracker.dart';
import 'package:ardrive/models/models.dart';
import 'package:ardrive/user/repositories/user_preferences_repository.dart';
import 'package:ardrive/utils/logger.dart';
import 'package:ardrive/utils/user_utils.dart';
import 'package:ardrive_utils/ardrive_utils.dart';
import 'package:drift/drift.dart';
Expand Down Expand Up @@ -125,6 +126,7 @@ class DrivesCubit extends Cubit<DrivesState> {
}

void selectDrive(String driveId) {
logger.d('selectDrive: $driveId');
final profileIsLoggedIn = _profileCubit.state is ProfileLoggedIn;
final canCreateNewDrive = profileIsLoggedIn;
final DrivesState state;
Expand All @@ -141,6 +143,8 @@ class DrivesCubit extends Cubit<DrivesState> {
}

_userPreferencesRepository.saveLastSelectedDriveId(driveId);

logger.d('selectDrive: $driveId, state: $state');
emit(state);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/components/drive_attach_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Future<void> attachDrive({
DriveID? driveId,
String? driveName,
SecretKey? driveKey,
required DriveDetailCubit driveDetailCubit,
}) {
final profileState = context.read<ProfileCubit>().state;
final profileKey =
Expand All @@ -30,6 +31,7 @@ Future<void> attachDrive({
context,
content: BlocProvider<DriveAttachCubit>(
create: (context) => DriveAttachCubit(
driveDetailCubit: driveDetailCubit,
initialDriveId: driveId,
initialDriveName: driveName,
initialDriveKey: driveKey,
Expand Down
15 changes: 12 additions & 3 deletions lib/components/new_button/new_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ class NewButton extends StatelessWidget {

return [
ArDriveNewButtonItem(
onClick: () => attachDrive(context: context),
onClick: () => attachDrive(
context: context,
driveDetailCubit: context.read<DriveDetailCubit>(),
),
name: appLocalizations.attachDrive,
icon: ArDriveIcons.iconAttachDrive(size: defaultIconSize),
),
Expand Down Expand Up @@ -440,7 +443,10 @@ class NewButton extends StatelessWidget {
} else {
return [
ArDriveNewButtonItem(
onClick: () => attachDrive(context: context),
onClick: () => attachDrive(
context: context,
driveDetailCubit: context.read<DriveDetailCubit>(),
),
name: appLocalizations.attachDrive,
icon: ArDriveIcons.iconAttachDrive(size: defaultIconSize),
),
Expand Down Expand Up @@ -538,7 +544,10 @@ class NewButton extends StatelessWidget {
} else {
return [
ArDriveNewButtonItem(
onClick: () => attachDrive(context: context),
onClick: () => attachDrive(
context: context,
driveDetailCubit: context.read<DriveDetailCubit>(),
),
name: appLocalizations.attachDrive,
icon: ArDriveIcons.iconAttachDrive(size: defaultIconSize),
),
Expand Down
1 change: 1 addition & 0 deletions lib/pages/app_router_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoutePath>
driveId: driveId,
driveName: driveName,
driveKey: sharedDriveKey,
driveDetailCubit: context.read<DriveDetailCubit>(),
).then((_) {
sharedDriveKey = null;
sharedRawDriveKey = null;
Expand Down
4 changes: 4 additions & 0 deletions lib/pages/drive_detail/drive_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,18 @@ class _DriveDetailPageState extends State<DriveDetailPage> {
child: BlocListener<DrivesCubit, DrivesState>(
listener: (context, state) {
if (state is DrivesLoadSuccess) {
logger.d('drive detail listener: ${state.selectedDriveId}');
if (state.userDrives.isNotEmpty) {
logger.d('drive detail listener: ${state.selectedDriveId}');
final driveDetailState = context.read<DriveDetailCubit>().state;

if (driveDetailState is DriveDetailLoadSuccess &&
driveDetailState.currentDrive.id == state.selectedDriveId) {
return;
}

logger.d('changeDrive on listener: ${state.selectedDriveId}');

context
.read<DriveDetailCubit>()
.changeDrive(state.selectedDriveId!);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Secure, permanent storage

publish_to: 'none'

version: 2.57.0
version: 2.57.1

environment:
sdk: '>=3.2.0 <4.0.0'
Expand Down
7 changes: 6 additions & 1 deletion test/blocs/drive_attach_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void main() {
late SyncCubit syncBloc;
late DrivesCubit drivesBloc;
late DriveAttachCubit driveAttachCubit;
late MockDriveDetailCubit driveDetailCubit;

const validPrivateDriveId = 'valid-private-drive-id';
const validPrivateDriveKeyBase64 =
Expand Down Expand Up @@ -54,7 +55,9 @@ void main() {
arweave = MockArweaveService();
syncBloc = MockSyncBloc();
drivesBloc = MockDrivesCubit();
when(() => arweave.getLatestDriveEntityWithId(validDriveId)).thenAnswer(
driveDetailCubit = MockDriveDetailCubit();

when(() => arweave.getLatestDriveEntityWithId(validDriveId)).thenAnswer(
(_) => Future.value(
DriveEntity(
id: validDriveId,
Expand Down Expand Up @@ -102,6 +105,7 @@ void main() {
syncBloc: syncBloc,
drivesBloc: drivesBloc,
profileKey: profileKey,
driveDetailCubit: driveDetailCubit,
);
});

Expand Down Expand Up @@ -193,6 +197,7 @@ void main() {
initialDriveId: validPrivateDriveId,
initialDriveName: validDriveName,
initialDriveKey: validPrivateDriveKey,
driveDetailCubit: driveDetailCubit,
),
expect: () => [
DriveAttachPrivate(),
Expand Down
Loading