Skip to content

Commit

Permalink
Fix preview
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Dec 20, 2024
1 parent af334ce commit ba0b822
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 151 deletions.
182 changes: 89 additions & 93 deletions app/lib/ui/flow/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,108 +122,104 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
),
);

return Scrollbar(
return ListView.builder(
controller: _scrollController,
interactive: true,
child: ListView.builder(
controller: _scrollController,
itemCount: state.medias.length + 2,
itemBuilder: (context, index) {
if (index == 0) {
return Column(
children: [
const HomeScreenHints(),
const NoInternetConnectionHint(),
],
);
} else if (index == state.medias.length + 1) {
return FadeInSwitcher(
child: state.loading
? const Center(
child: Padding(
padding: EdgeInsets.all(16),
child: AppCircularProgressIndicator(
size: 20,
),
itemCount: state.medias.length + 2,
itemBuilder: (context, index) {
if (index == 0) {
return Column(
children: [
const HomeScreenHints(),
const NoInternetConnectionHint(),
],
);
} else if (index == state.medias.length + 1) {
return FadeInSwitcher(
child: state.loading
? const Center(
child: Padding(
padding: EdgeInsets.all(16),
child: AppCircularProgressIndicator(
size: 20,
),
)
: const SizedBox(),
);
} else {
final gridEntry = state.medias.entries.elementAt(index - 1);
return Column(
children: [
Container(
height: 45,
padding: const EdgeInsets.only(left: 16, top: 5),
margin: EdgeInsets.zero,
alignment: Alignment.centerLeft,
decoration: BoxDecoration(
color: context.colorScheme.surface,
),
child: Text(
gridEntry.key.format(context, DateFormatType.relative),
style: AppTextStyles.subtitle1.copyWith(
color: context.colorScheme.textPrimary,
),
),
)
: const SizedBox(),
);
} else {
final gridEntry = state.medias.entries.elementAt(index - 1);
return Column(
children: [
Container(
height: 45,
padding: const EdgeInsets.only(left: 16, top: 5),
margin: EdgeInsets.zero,
alignment: Alignment.centerLeft,
decoration: BoxDecoration(
color: context.colorScheme.surface,
),
GridView.builder(
padding: const EdgeInsets.all(4),
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: context.mediaQuerySize.width > 600
? context.mediaQuerySize.width ~/ 180
: context.mediaQuerySize.width ~/ 100,
crossAxisSpacing: 4,
mainAxisSpacing: 4,
child: Text(
gridEntry.key.format(context, DateFormatType.relative),
style: AppTextStyles.subtitle1.copyWith(
color: context.colorScheme.textPrimary,
),
itemCount: gridEntry.value.entries.length,
itemBuilder: (context, index) {
final media =
gridEntry.value.entries.elementAt(index).value;
),
),
GridView.builder(
padding: const EdgeInsets.all(4),
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: context.mediaQuerySize.width > 600
? context.mediaQuerySize.width ~/ 180
: context.mediaQuerySize.width ~/ 100,
crossAxisSpacing: 4,
mainAxisSpacing: 4,
),
itemCount: gridEntry.value.entries.length,
itemBuilder: (context, index) {
final media =
gridEntry.value.entries.elementAt(index).value;

if (media.id == state.lastLocalMediaId) {
runPostFrame(() {
_notifier.loadMedias();
});
}
if (media.id == state.lastLocalMediaId) {
runPostFrame(() {
_notifier.loadMedias();
});
}

return AppMediaItem(
key: ValueKey(media.id),
onTap: () async {
if (state.selectedMedias.isNotEmpty) {
_notifier.toggleMediaSelection(media);
HapticFeedback.lightImpact();
} else {
await MediaPreviewRoute(
$extra: MediaPreviewRouteData(
medias: state.medias.values
.expand((element) => element.values)
.toList(),
startFrom: media.id,
),
).push(context);
}
},
onLongTap: () {
return AppMediaItem(
key: ValueKey(media.id),
onTap: () async {
if (state.selectedMedias.isNotEmpty) {
_notifier.toggleMediaSelection(media);
HapticFeedback.lightImpact();
},
isSelected: state.selectedMedias.containsKey(media.id),
uploadMediaProcess: state.uploadMediaProcesses[media.id],
downloadMediaProcess:
state.downloadMediaProcesses[media.id],
media: media,
);
},
),
],
);
}
},
),
} else {
await MediaPreviewRoute(
$extra: MediaPreviewRouteData(
medias: state.medias.values
.expand((element) => element.values)
.toList(),
startFrom: media.id,
),
).push(context);
}
},
onLongTap: () {
_notifier.toggleMediaSelection(media);
HapticFeedback.lightImpact();
},
isSelected: state.selectedMedias.containsKey(media.id),
uploadMediaProcess: state.uploadMediaProcesses[media.id],
downloadMediaProcess:
state.downloadMediaProcesses[media.id],
media: media,
);
},
),
],
);
}
},
);
}
}
Expand Down
99 changes: 54 additions & 45 deletions app/lib/ui/flow/home/home_screen_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,38 @@ final homeViewStateNotifier =
ref.read(localMediaServiceProvider),
ref.read(googleDriveServiceProvider),
ref.read(dropboxServiceProvider),
ref.read(authServiceProvider),
ref.read(mediaProcessRepoProvider),
ref.read(loggerProvider),
ref.read(connectivityHandlerProvider),
ref.read(AppPreferences.dropboxCurrentUserAccount),
ref.read(googleUserAccountProvider),
);
ref.listen(AppPreferences.dropboxCurrentUserAccount, (previous, next) {
final dropboxAccountSubscription =
ref.listen(AppPreferences.dropboxCurrentUserAccount, (previous, next) {
notifier.updateDropboxAccount(next);
});
final googleAccountSubscription =
ref.listen(googleUserAccountProvider, (previous, next) {
notifier.updateGoogleAccount(next);
});

ref.onDispose(() async {
dropboxAccountSubscription.close();
googleAccountSubscription.close();
});

return notifier;
});

class HomeViewStateNotifier extends StateNotifier<HomeViewState>
with HomeViewModelHelperMixin {
final AuthService _authService;
final Logger _logger;
final GoogleDriveService _googleDriveService;
final DropboxService _dropboxService;
final LocalMediaService _localMediaService;
final MediaProcessRepo _mediaProcessRepo;
final ConnectivityHandler _connectivityHandler;

StreamSubscription? _googleAccountSubscription;

// Local
int _localMediaCount = 0;
bool _localMaxLoaded = false;
Expand All @@ -70,62 +78,62 @@ class HomeViewStateNotifier extends StateNotifier<HomeViewState>
this._localMediaService,
this._googleDriveService,
this._dropboxService,
this._authService,
this._mediaProcessRepo,
this._logger,
this._connectivityHandler,
DropboxAccount? _dropboxAccount,
GoogleSignInAccount? _googleAccount,
) : super(
HomeViewState(
dropboxAccount: _dropboxAccount,
googleAccount: _authService.googleAccount,
googleAccount: _googleAccount,
),
) {
_init();
}

Future<void> _init() async {
_mediaProcessRepo.addListener(_mediaProcessObserve);
_listenUserGoogleAccount();
loadMedias(reload: true);
await loadMedias(reload: true);
_mediaProcessObserve();
}

// ACCOUNT LISTENERS ---------------------------------------------------------

/// Listen to google account changes and update the state accordingly.
void _listenUserGoogleAccount() {
_googleAccountSubscription =
_authService.onGoogleAccountChange.listen((event) async {
if (event != null && state.googleAccount?.id != event.id) {
state = state.copyWith(googleAccount: event);
try {
_backUpFolderId = await _googleDriveService.getBackUpFolderId();
} catch (e, s) {
_logger.e(
"HomeViewStateNotifier: unable to get google drive back up folder id",
error: e,
stackTrace: s,
);
}
loadMedias(reload: true);
} else if (event == null) {
_backUpFolderId = null;
state = state.copyWith(
googleAccount: null,
medias: mediaMapUpdate(
update: (media) {
if (media.driveMediaRefId != null &&
media.sources.contains(AppMediaSource.googleDrive) &&
media.sources.length > 1) {
return media.removeGoogleDriveRef();
} else if (!media.sources.contains(AppMediaSource.googleDrive) &&
media.driveMediaRefId == null) {
return media;
}
return null;
},
medias: state.medias,
),
Future<void> updateGoogleAccount(GoogleSignInAccount? googleAccount) async {
if (googleAccount != null) {
state = state.copyWith(googleAccount: googleAccount);
try {
_backUpFolderId = await _googleDriveService.getBackUpFolderId();
} catch (e, s) {
_logger.e(
"HomeViewStateNotifier: unable to get google drive back up folder id",
error: e,
stackTrace: s,
);
}
});
loadMedias(reload: true, force: true);
} else {
_backUpFolderId = null;
state = state.copyWith(
googleAccount: null,
medias: mediaMapUpdate(
update: (media) {
if (media.driveMediaRefId != null &&
media.sources.contains(AppMediaSource.googleDrive) &&
media.sources.length > 1) {
return media.removeGoogleDriveRef();
} else if (!media.sources.contains(AppMediaSource.googleDrive) &&
media.driveMediaRefId == null) {
return media;
}
return null;
},
medias: state.medias,
),
);
}
}

/// Listen to dropbox account changes and update the state accordingly.
Expand Down Expand Up @@ -252,8 +260,9 @@ class HomeViewStateNotifier extends StateNotifier<HomeViewState>

/// Loads medias from local, google drive and dropbox.
/// it append the medias to the existing medias if reload is false.
Future<void> loadMedias({bool reload = false}) async {
if (state.loading || state.cloudLoading) return;
/// force will load media event its already loading
Future<void> loadMedias({bool reload = false, bool force: false}) async {
if (state.cloudLoading && !force) return;
state = state.copyWith(loading: true, cloudLoading: true, error: null);
try {
// Reset all the variables if reload is true
Expand Down
Loading

0 comments on commit ba0b822

Please sign in to comment.