Skip to content

Commit

Permalink
Merge pull request #1892 from ardriveapp/PE-6752-hide-drives
Browse files Browse the repository at this point in the history
PE-6752: hide drives
  • Loading branch information
thiagocarvalhodev authored Oct 28, 2024
2 parents 705172e + 9f47a53 commit 0a482a6
Show file tree
Hide file tree
Showing 49 changed files with 1,808 additions and 716 deletions.
17 changes: 10 additions & 7 deletions lib/app_shell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ class AppShellState extends State<AppShell> {
}
}
},
builder: (context, syncState) => syncState is SyncInProgress
? Stack(
builder: (context, syncState) {
return Stack(children: [
scaffold,
if (syncState is SyncInProgress)
Stack(
children: [
AbsorbPointer(
child: scaffold,
),
SizedBox.expand(
child: Container(
color: Colors.black.withOpacity(0.5),
Expand Down Expand Up @@ -181,8 +181,9 @@ class AppShellState extends State<AppShell> {
),
),
],
)
: scaffold,
),
]);
},
),
);
return ScreenTypeLayout.builder(
Expand Down Expand Up @@ -367,6 +368,8 @@ class MobileAppBar extends StatelessWidget implements PreferredSizeWidget {
),
),
const Spacer(),
const GlobalHideToggleButton(),
const SizedBox(width: 8),
const SyncButton(),
const SizedBox(
width: 24,
Expand Down
4 changes: 2 additions & 2 deletions lib/authentication/ardrive_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ class ArDriveAuthImpl implements ArDriveAuth {
await _secureKeyValueStore.remove('biometricEnabled');
currentUser = null;
await _disconnectFromArConnect();
_userStreamController.add(null);
}

await _userRepository.deleteUser();
await _databaseHelpers.deleteAllTables();
await (await _metadataCache).clear();
(await _metadataCache).clear();
_userStreamController.add(null);
} catch (e, stacktrace) {
logger.e('Failed to logout user', e, stacktrace);
throw AuthenticationFailedException('Failed to logout user');
Expand Down
82 changes: 54 additions & 28 deletions lib/blocs/drive_detail/drive_detail_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import 'package:rxdart/rxdart.dart';
part 'drive_detail_state.dart';

class DriveDetailCubit extends Cubit<DriveDetailState> {
final String driveId;
String _driveId;
final ProfileCubit _profileCubit;
final DriveDao _driveDao;
final ConfigService _configService;
Expand All @@ -49,10 +49,8 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {

bool _refreshSelectedItem = false;

bool _showHiddenFiles = false;

DriveDetailCubit({
required this.driveId,
required String driveId,
String? initialFolderId,
required ProfileCubit profileCubit,
required DriveDao driveDao,
Expand All @@ -70,6 +68,7 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
_breadcrumbBuilder = breadcrumbBuilder,
_syncCubit = syncCubit,
_driveRepository = driveRepository,
_driveId = driveId,
super(DriveDetailLoadInProgress()) {
if (driveId.isEmpty) {
return;
Expand All @@ -95,26 +94,38 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
}
}

void toggleHiddenFiles() {
_showHiddenFiles = !_showHiddenFiles;
void showEmptyDriveDetail() async {
await _syncCubit.waitCurrentSync();

emit(DriveDetailLoadEmpty());
}

Future<void> changeDrive(String driveId) async {
final drive = await _driveDao.driveById(driveId: driveId).getSingleOrNull();

if (drive == null) {
return;
}

_driveId = driveId;

refreshDriveDataTable();
openFolder(folderId: drive.rootFolderId);
}

void openFolder({
Future<void> openFolder({
String? folderId,
String? otherDriveId,
String? selectedItemId,
DriveOrder contentOrderBy = DriveOrder.name,
OrderingMode contentOrderingMode = OrderingMode.asc,
}) async {
/// always wait for the current sync to finish before opening a new folder
await _syncCubit.waitCurrentSync();

try {
_selectedItem = null;
_allImagesOfCurrentFolder = null;

String driveId = otherDriveId ?? this.driveId;
String driveId = otherDriveId ?? _driveId;

emit(DriveDetailLoadInProgress());

Expand All @@ -131,6 +142,10 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
),
_profileCubit.stream.startWith(ProfileCheckingAvailability()),
(drive, folderContents, _) async {
if (isClosed) {
return;
}

await _syncCubit.waitCurrentSync();

if (drive == null) {
Expand Down Expand Up @@ -195,6 +210,12 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
isOwner: isDriveOwner(_auth, drive.ownerAddress),
);

if (selectedItemId != null) {
_selectedItem = currentFolderContents.firstWhere(
(element) => element.id == selectedItemId,
);
}

final List<BreadCrumbRowInfo> pathSegments =
await _breadcrumbBuilder.buildForFolder(
folderId: folderContents.folder.id,
Expand All @@ -215,10 +236,10 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
rowsPerPage: availableRowsPerPage.first,
availableRowsPerPage: availableRowsPerPage,
currentFolderContents: currentFolderContents,
isShowingHiddenFiles: _showHiddenFiles,
pathSegments: pathSegments,
driveIsEmpty: folderContents.files.isEmpty &&
folderContents.subfolders.isEmpty,
showSelectedItemDetails: _selectedItem != null,
),
);
} else {
Expand All @@ -241,7 +262,7 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
multiselect: false,
currentFolderContents: currentFolderContents,
columnVisibility: columnsVisibility,
isShowingHiddenFiles: _showHiddenFiles,
showSelectedItemDetails: _selectedItem != null,
),
);
}
Expand Down Expand Up @@ -447,53 +468,58 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
final state = this.state as DriveDetailLoadSuccess;
emit(state.copyWith(
forceRebuildKey: UniqueKey(),
isShowingHiddenFiles: _showHiddenFiles,
));
}
}

bool canNavigateThroughImages() {
final numberOfImages = getAllImagesOfCurrentFolder().length;
bool canNavigateThroughImages(bool showHiddenImages) {
final numberOfImages = getAllImagesOfCurrentFolder(showHiddenImages).length;
return numberOfImages > 1;
}

Future<void> selectNextImage() => _selectImageRelativeToCurrent(1);
Future<void> selectPreviousImage() => _selectImageRelativeToCurrent(-1);
Future<void> selectNextImage(bool showHiddenImages) =>
_selectImageRelativeToCurrent(1, showHiddenImages);
Future<void> selectPreviousImage(bool showHiddenImages) =>
_selectImageRelativeToCurrent(-1, showHiddenImages);

Future<void> _selectImageRelativeToCurrent(int offset) async {
final currentIndex = getIndexForImage(_selectedItem as FileDataTableItem);
Future<void> _selectImageRelativeToCurrent(
int offset, bool showHiddenImages) async {
final currentIndex = getIndexForImage(
_selectedItem as FileDataTableItem,
showHiddenImages,
);
final nextIndex = currentIndex + offset;
final nextImage = getImageForIndex(nextIndex);
final nextImage = getImageForIndex(nextIndex, showHiddenImages);

await selectDataItem(nextImage);
}

FileDataTableItem getImageForIndex(int index) {
final allImagesOfCurrentFolder = getAllImagesOfCurrentFolder();
FileDataTableItem getImageForIndex(int index, bool showHiddenImages) {
final allImagesOfCurrentFolder =
getAllImagesOfCurrentFolder(showHiddenImages);
final cyclicIndex = index % allImagesOfCurrentFolder.length;
final image = allImagesOfCurrentFolder[cyclicIndex];

return image;
}

int getIndexForImage(FileDataTableItem image) {
final allImagesOfCurrentFolder = getAllImagesOfCurrentFolder();
int getIndexForImage(FileDataTableItem image, bool showHiddenImages) {
final allImagesOfCurrentFolder =
getAllImagesOfCurrentFolder(showHiddenImages);
final index = allImagesOfCurrentFolder.indexWhere(
(element) => element.id == image.id,
);

return index;
}

List<FileDataTableItem> getAllImagesOfCurrentFolder() {
List<FileDataTableItem> getAllImagesOfCurrentFolder(bool showHiddenImages) {
if (_allImagesOfCurrentFolder != null) {
return _allImagesOfCurrentFolder!;
}

final state = this.state as DriveDetailLoadSuccess;

final isShowingHiddenFiles = state.isShowingHiddenFiles;

final List<FileDataTableItem> allImagesForFolder =
state.currentFolderContents.whereType<FileDataTableItem>().where(
(element) {
Expand All @@ -502,7 +528,7 @@ class DriveDetailCubit extends Cubit<DriveDetailState> {
);

return supportedImageType &&
(isShowingHiddenFiles ? true : !element.isHidden);
(showHiddenImages ? true : !element.isHidden);
},
).toList();

Expand Down
8 changes: 2 additions & 6 deletions lib/blocs/drive_detail/drive_detail_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class DriveDetailLoadSuccess extends DriveDetailState {
final Map<int, bool> columnVisibility;
final Key? forceRebuildKey;

final bool isShowingHiddenFiles;

DriveDetailLoadSuccess({
required this.currentDrive,
required this.hasWritePermissions,
Expand All @@ -61,7 +59,6 @@ class DriveDetailLoadSuccess extends DriveDetailState {
required this.currentFolderContents,
required this.columnVisibility,
this.forceRebuildKey,
required this.isShowingHiddenFiles,
required this.pathSegments,
this.selectedPage,
});
Expand All @@ -83,7 +80,6 @@ class DriveDetailLoadSuccess extends DriveDetailState {
ArDriveDataTableItem? selectedItem,
List<ArDriveDataTableItem>? currentFolderContents,
Key? forceRebuildKey,
bool? isShowingHiddenFiles,
List<BreadCrumbRowInfo>? pathSegments,
int? selectedPage,
}) =>
Expand All @@ -109,7 +105,6 @@ class DriveDetailLoadSuccess extends DriveDetailState {
driveIsEmpty: driveIsEmpty ?? this.driveIsEmpty,
currentFolderContents:
currentFolderContents ?? this.currentFolderContents,
isShowingHiddenFiles: isShowingHiddenFiles ?? this.isShowingHiddenFiles,
pathSegments: pathSegments ?? this.pathSegments,
);

Expand All @@ -119,7 +114,6 @@ class DriveDetailLoadSuccess extends DriveDetailState {
hasWritePermissions,
folderInView,
currentFolderContents,
isShowingHiddenFiles,
contentOrderBy,
contentOrderingMode,
showSelectedItemDetails,
Expand All @@ -144,4 +138,6 @@ class DriveDetailLoadSuccess extends DriveDetailState {
/// the user's profile.
class DriveDetailLoadNotFound extends DriveDetailState {}

class DriveDetailLoadEmpty extends DriveDetailState {}

class DriveInitialLoading extends DriveDetailState {}
31 changes: 27 additions & 4 deletions lib/blocs/drives/drives_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:ardrive/blocs/prompt_to_snapshot/prompt_to_snapshot_bloc.dart';
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/user_utils.dart';
import 'package:ardrive_utils/ardrive_utils.dart';
import 'package:drift/drift.dart';
Expand All @@ -22,6 +23,7 @@ class DrivesCubit extends Cubit<DrivesState> {
final PromptToSnapshotBloc _promptToSnapshotBloc;
final DriveDao _driveDao;
final ArDriveAuth _auth;
final UserPreferencesRepository _userPreferencesRepository;

late StreamSubscription _drivesSubscription;
String? initialSelectedDriveId;
Expand All @@ -32,10 +34,12 @@ class DrivesCubit extends Cubit<DrivesState> {
required PromptToSnapshotBloc promptToSnapshotBloc,
required DriveDao driveDao,
required ActivityTracker activityTracker,
required UserPreferencesRepository userPreferencesRepository,
}) : _profileCubit = profileCubit,
_promptToSnapshotBloc = promptToSnapshotBloc,
_driveDao = driveDao,
_auth = auth,
_userPreferencesRepository = userPreferencesRepository,
super(DrivesLoadInProgress()) {
_auth.onAuthStateChanged().listen((user) {
if (user == null) {
Expand Down Expand Up @@ -66,11 +70,28 @@ class DrivesCubit extends Cubit<DrivesState> {

String? selectedDriveId;

if (state is DrivesLoadSuccess && state.selectedDriveId != null) {
if (state is DrivesLoadSuccess) {
selectedDriveId = state.selectedDriveId;
} else {
selectedDriveId = initialSelectedDriveId ??
(drives.isNotEmpty ? drives.first.id : null);
}

if (selectedDriveId == null) {
if (initialSelectedDriveId != null &&
initialSelectedDriveId!.isNotEmpty) {
selectedDriveId = initialSelectedDriveId;
} else {
final userPreferences = await _userPreferencesRepository.load();

final userHasHiddenDrive = drives.any((d) => d.isHidden);
await _userPreferencesRepository
.saveUserHasHiddenItem(userHasHiddenDrive);

selectedDriveId = userPreferences.lastSelectedDriveId;

if (selectedDriveId == null ||
!drives.any((d) => d.id == selectedDriveId)) {
selectedDriveId = drives.isNotEmpty ? drives.first.id : null;
}
}
}

final walletAddress = profileState is ProfileLoggedIn
Expand Down Expand Up @@ -118,6 +139,8 @@ class DrivesCubit extends Cubit<DrivesState> {
);
_promptToSnapshotBloc.add(const SelectedDrive(driveId: null));
}

_userPreferencesRepository.saveLastSelectedDriveId(driveId);
emit(state);
}

Expand Down
Loading

0 comments on commit 0a482a6

Please sign in to comment.