Skip to content

Commit

Permalink
Implement drive upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Feb 26, 2024
1 parent b0811ba commit 831bca0
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 123 deletions.
66 changes: 46 additions & 20 deletions .idea/libraries/Dart_Packages.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 24 additions & 23 deletions app/lib/ui/flow/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,31 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
actions: [
if (selectedMedia.isNotEmpty)
TextButton(
onPressed: () {
showAppSheet(
context: context,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
AppSheetAction(
icon: SvgPicture.asset(
Assets.images.icons.googlePhotos,
height: 24,
width: 24,
),
title: context.l10n.back_up_on_google_drive_text,
onPressed: () {

},
onPressed: () {
showAppSheet(
context: context,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
AppSheetAction(
icon: SvgPicture.asset(
Assets.images.icons.googlePhotos,
height: 24,
width: 24,
),
],
));
},
child: Text(context.l10n.common_done,
style: AppTextStyles.button.copyWith(
color: context.colorScheme.primary,
)))
title: context.l10n.back_up_on_google_drive_text,
onPressed: notifier.uploadMediaOnGoogleDrive,
),
],
));
},
child: Text(
context.l10n.common_done,
style: AppTextStyles.button.copyWith(
color: context.colorScheme.primary,
),
),
)
],
body: Visibility(
visible: !isLoading,
Expand Down
32 changes: 26 additions & 6 deletions app/lib/ui/flow/home/home_screen_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'package:data/errors/app_error.dart';
import 'package:data/models/media/media.dart';
import 'package:data/services/auth_service.dart';
import 'package:data/services/google_drive_service.dart';
import 'package:data/services/local_media_service.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
Expand All @@ -9,15 +12,19 @@ final homeViewStateNotifier =
StateNotifierProvider.autoDispose<HomeViewStateNotifier, HomeViewState>(
(ref) => HomeViewStateNotifier(
ref.read(localMediaServiceProvider),
ref.read(googleDriveServiceProvider),
ref.read(authServiceProvider),
),
);

class HomeViewStateNotifier extends StateNotifier<HomeViewState> {
final LocalMediaService _localMediaService;
final GoogleDriveService _googleDriveService;
final AuthService _authService;

bool _loading = false;

HomeViewStateNotifier(this._localMediaService)
HomeViewStateNotifier(this._localMediaService, this._googleDriveService, this._authService)
: super(const HomeViewState());

Future<void> loadMediaCount() async {
Expand Down Expand Up @@ -65,19 +72,32 @@ class HomeViewStateNotifier extends StateNotifier<HomeViewState> {
}
}

void clearMediaSelection() {
state = state.copyWith(selectedMedias: []);
}

Future<void> uploadMediaOnGoogleDrive() async {

try {
if(_authService.getUser == null){
await _authService.signInWithGoogle();
}
state = state.copyWith(uploadingMedias: state.selectedMedias);
final folderId = await _googleDriveService.getBackupFolderId();
for (final media in state.selectedMedias) {
await _googleDriveService.uploadInGoogleDrive(media: media, folderID: folderId!);
}
state = state.copyWith(uploadingMedias: [], selectedMedias: []);
} catch (error) {
if(error is UserGoogleSignInAccountNotFound){
await _authService.signInWithGoogle();
await uploadMediaOnGoogleDrive();
}
state = state.copyWith(error: error, uploadingMedias: []);
}
}
}

@freezed
class HomeViewState with _$HomeViewState {
const factory HomeViewState({
@Default(false) bool loading,
@Default([]) List<AppMedia> uploadingMedias,
@Default([]) List<AppMedia> medias,
@Default([]) List<AppMedia> selectedMedias,
@Default(0) int mediaCount,
Expand Down
Loading

0 comments on commit 831bca0

Please sign in to comment.