Skip to content

Commit

Permalink
Merge pull request #1517 from ardriveapp/PE-4994
Browse files Browse the repository at this point in the history
PE-4994: Prompt Users to Create a Snapshot
  • Loading branch information
thiagocarvalhodev authored Jan 24, 2024
2 parents eec8c05 + e643868 commit 8d4cdef
Show file tree
Hide file tree
Showing 21 changed files with 1,099 additions and 133 deletions.
18 changes: 16 additions & 2 deletions lib/app_shell.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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/components/profile_card.dart';
import 'package:ardrive/components/side_bar.dart';
import 'package:ardrive/gift/reedem_button.dart';
Expand Down Expand Up @@ -60,9 +62,21 @@ class AppShellState extends State<AppShell> {

@override
Widget build(BuildContext context) => BlocBuilder<DrivesCubit, DrivesState>(
builder: (context, _) {
builder: (context, drivesState) {
Widget buildPage(scaffold) => Material(
child: BlocBuilder<SyncCubit, SyncState>(
child: BlocConsumer<SyncCubit, SyncState>(
listener: (context, syncState) async {
if (drivesState is DrivesLoadSuccess) {
if (syncState is! SyncInProgress) {
final promptToSnapshotBloc =
context.read<PromptToSnapshotBloc>();

promptToSnapshotBloc.add(SelectedDrive(
driveId: drivesState.selectedDriveId,
));
}
}
},
builder: (context, syncState) => syncState is SyncInProgress
? Stack(
children: [
Expand Down
57 changes: 42 additions & 15 deletions lib/blocs/drives/drives_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'dart:async';

import 'package:ardrive/authentication/ardrive_auth.dart';
import 'package:ardrive/blocs/blocs.dart';
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/utils/user_utils.dart';
Expand All @@ -17,6 +19,7 @@ part 'drives_state.dart';
/// It works even if the user profile is unavailable.
class DrivesCubit extends Cubit<DrivesState> {
final ProfileCubit _profileCubit;
final PromptToSnapshotBloc _promptToSnapshotBloc;
final DriveDao _driveDao;
final ArDriveAuth _auth;

Expand All @@ -26,9 +29,11 @@ class DrivesCubit extends Cubit<DrivesState> {
required ArDriveAuth auth,
this.initialSelectedDriveId,
required ProfileCubit profileCubit,
required PromptToSnapshotBloc promptToSnapshotBloc,
required DriveDao driveDao,
required ActivityTracker activityTracker,
}) : _profileCubit = profileCubit,
_promptToSnapshotBloc = promptToSnapshotBloc,
_driveDao = driveDao,
_auth = auth,
super(DrivesLoadInProgress()) {
Expand All @@ -52,7 +57,7 @@ class DrivesCubit extends Cubit<DrivesState> {
).listen((drives) async {
final state = this.state;

final profile = _profileCubit.state;
final profileState = _profileCubit.state;

String? selectedDriveId;

Expand All @@ -64,22 +69,26 @@ class DrivesCubit extends Cubit<DrivesState> {
}

final walletAddress =
profile is ProfileLoggedIn ? profile.walletAddress : null;
profileState is ProfileLoggedIn ? profileState.walletAddress : null;

final ghostFolders = await _driveDao.ghostFolders().get();

final sharedDrives =
drives.where((d) => !isDriveOwner(auth, d.ownerAddress)).toList();

final userDrives = drives
.where((d) => profileState is ProfileLoggedIn
? d.ownerAddress == walletAddress
: false)
.toList();

_promptToSnapshotBloc.add(SelectedDrive(driveId: selectedDriveId));

emit(
DrivesLoadSuccess(
selectedDriveId: selectedDriveId,
// If the user is not logged in, all drives are considered shared ones.
userDrives: drives
.where((d) => profile is ProfileLoggedIn
? d.ownerAddress == walletAddress
: false)
.toList(),
userDrives: userDrives,
sharedDrives: sharedDrives,
drivesWithAlerts: ghostFolders.map((e) => e.driveId).toList(),
canCreateNewDrive: _profileCubit.state is ProfileLoggedIn,
Expand All @@ -89,16 +98,28 @@ class DrivesCubit extends Cubit<DrivesState> {
}

void selectDrive(String driveId) {
final canCreateNewDrive = _profileCubit.state is ProfileLoggedIn;
final state = this.state is DrivesLoadSuccess
? (this.state as DrivesLoadSuccess).copyWith(selectedDriveId: driveId)
: DrivesLoadedWithNoDrivesFound(canCreateNewDrive: canCreateNewDrive);
final profileIsLoggedIn = _profileCubit.state is ProfileLoggedIn;
final canCreateNewDrive = profileIsLoggedIn;
final DrivesState state;
if (this.state is DrivesLoadSuccess) {
state = (this.state as DrivesLoadSuccess).copyWith(
selectedDriveId: driveId,
);
_promptToSnapshotBloc.add(SelectedDrive(driveId: driveId));
} else {
state = DrivesLoadedWithNoDrivesFound(
canCreateNewDrive: canCreateNewDrive,
);
_promptToSnapshotBloc.add(const SelectedDrive(driveId: null));
}
emit(state);
}

void cleanDrives() {
initialSelectedDriveId = null;

_promptToSnapshotBloc.add(const SelectedDrive(driveId: null));

final state = DrivesLoadSuccess(
selectedDriveId: null,
userDrives: const [],
Expand All @@ -110,21 +131,27 @@ class DrivesCubit extends Cubit<DrivesState> {
}

void _resetDriveSelection(DriveID detachedDriveId) {
final canCreateNewDrive = _profileCubit.state is ProfileLoggedIn;
final profileIsLoggedIn = _profileCubit.state is ProfileLoggedIn;
final canCreateNewDrive = profileIsLoggedIn;
if (state is DrivesLoadSuccess) {
final state = this.state as DrivesLoadSuccess;
state.userDrives.removeWhere((drive) => drive.id == detachedDriveId);
state.sharedDrives.removeWhere((drive) => drive.id == detachedDriveId);
final firstOrNullDrive = state.userDrives.isNotEmpty
final firstOrNullDriveId = state.userDrives.isNotEmpty
? state.userDrives.first.id
: state.sharedDrives.isNotEmpty
? state.sharedDrives.first.id
: null;
if (firstOrNullDrive != null) {
emit(state.copyWith(selectedDriveId: firstOrNullDrive));
_promptToSnapshotBloc.add(SelectedDrive(
driveId: firstOrNullDriveId,
));
if (firstOrNullDriveId != null) {
emit(state.copyWith(selectedDriveId: firstOrNullDriveId));
return;
}
}

_promptToSnapshotBloc.add(const SelectedDrive(driveId: null));
emit(DrivesLoadedWithNoDrivesFound(canCreateNewDrive: canCreateNewDrive));
}

Expand Down
1 change: 0 additions & 1 deletion lib/blocs/feedback_survey/feedback_survey_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class FeedbackSurveyCubit extends Cubit<FeedbackSurveyState> {

FeedbackSurveyCubit(
FeedbackSurveyState initialState, {

/// takes a KeyValueStore for testing purposes
KeyValueStore? store,
}) : super(initialState) {
Expand Down
Loading

0 comments on commit 8d4cdef

Please sign in to comment.