Skip to content

Commit

Permalink
add loader on image uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed Jan 29, 2024
1 parent 9ca9e84 commit 3716946
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
17 changes: 11 additions & 6 deletions khelo/lib/ui/app.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:data/storage/app_preferences.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
Expand All @@ -26,12 +27,16 @@ class _AppState extends ConsumerState<App> {
super.initState();

final AppRoute initialRoute;
initialRoute = AppRoute.intro;
// if (ref.read(hasUserSession)) {
// initialRoute = AppRoute.main;
// } else {
// initialRoute = AppRoute.intro;
// }
if (ref.read(hasUserSession)) {
final currentUser = ref.read(currentUserPod);
if (currentUser?.name == null || currentUser!.name!.isEmpty) {
initialRoute = AppRoute.editProfile(isToCreateAccount: true);
} else {
initialRoute = AppRoute.main;
}
} else {
initialRoute = AppRoute.intro;
}

_router = GoRouter(
initialLocation: initialRoute.path,
Expand Down
13 changes: 9 additions & 4 deletions khelo/lib/ui/flow/settings/profile/edit_profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:khelo/ui/flow/settings/profile/edit_profile_view_model.dart';
import 'package:style/animations/on_tap_scale.dart';
import 'package:style/button/primary_button.dart';
import 'package:style/extensions/context_extensions.dart';
import 'package:style/indicator/progress_indicator.dart';
import 'package:style/text/app_text_style.dart';

class EditProfileScreen extends ConsumerWidget {
Expand Down Expand Up @@ -182,7 +183,7 @@ class EditProfileScreen extends ConsumerWidget {
context.l10n.edit_profile_save_title,
expanded: false,
progress: state.isSaveInProgress,
enabled: state.isButtonEnable,
enabled: state.isButtonEnable && !state.isImageUploading,
onPressed: () => notifier.onSubmitTap(),
)
],
Expand All @@ -208,19 +209,23 @@ class EditProfileScreen extends ConsumerWidget {
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: state.imageUrl != null
image: state.imageUrl != null && !state.isImageUploading
? DecorationImage(
image: CachedNetworkImageProvider(state.imageUrl!),
fit: BoxFit.cover)
: null,
color: context.colorScheme.primary),
child: state.imageUrl == null
child: state.imageUrl == null && !state.isImageUploading
? Icon(
Icons.person,
size: profileViewHeight / 2,
color: context.colorScheme.textSecondary,
)
: null,
: state.isImageUploading
? AppProgressIndicator(
color: context.colorScheme.surface,
)
: null,
),
OnTapScale(
onTap: () {
Expand Down
29 changes: 16 additions & 13 deletions khelo/lib/ui/flow/settings/profile/edit_profile_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,33 @@ class EditProfileViewNotifier extends StateNotifier<EditProfileState> {

Future<void> onImageChange(String path) async {
try {
state = state.copyWith(isImageUploading: true);
final imageUrl = await fileUploadService.uploadProfileImage(path);
final prevUrl = state.imageUrl;

if (prevUrl != null && prevUrl != state.currentUser?.profile_img_url) {
await fileUploadService.deleteUploadedProfileImage(prevUrl);
}
state = state.copyWith(imageUrl: imageUrl);
state = state.copyWith(imageUrl: imageUrl, isImageUploading: false);
onValueChange();
} catch (e) {
state = state.copyWith(isImageUploading: false);
debugPrint("EditProfileViewNotifier: error while image upload -> $e");
}
}

Future<void> onBackBtnPressed() async {
try {
if (state.imageUrl != state.currentUser?.profile_img_url &&
state.imageUrl != null) {
await fileUploadService.deleteUploadedProfileImage(state.imageUrl!);
}
} catch (e) {
debugPrint(
"EditProfileViewNotifier: error while delete image on back btn press -> $e");
}
}

Future<void> onSubmitTap() async {
if (state.currentUser == null) {
return;
Expand Down Expand Up @@ -176,18 +190,6 @@ class EditProfileViewNotifier extends StateNotifier<EditProfileState> {
"EditProfileViewNotifier: error in reAuthenticate And Delete -> $e");
}
}

void onBackBtnPressed() {
try {
if (state.imageUrl != state.currentUser?.profile_img_url &&
state.imageUrl != null) {
fileUploadService.deleteUploadedProfileImage(state.imageUrl!);
}
} catch (e) {
debugPrint(
"EditProfileViewNotifier: error while delete image on back btn press -> $e");
}
}
}

@freezed
Expand All @@ -203,6 +205,7 @@ class EditProfileState with _$EditProfileState {
@Default(null) BowlingStyle? bowlingStyle,
@Default(null) PlayerRole? playerRole,
@Default(false) bool isButtonEnable,
@Default(false) bool isImageUploading,
@Default(false) bool isSaved,
@Default(false) bool isSaveInProgress,
UserModel? currentUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mixin _$EditProfileState {
BowlingStyle? get bowlingStyle => throw _privateConstructorUsedError;
PlayerRole? get playerRole => throw _privateConstructorUsedError;
bool get isButtonEnable => throw _privateConstructorUsedError;
bool get isImageUploading => throw _privateConstructorUsedError;
bool get isSaved => throw _privateConstructorUsedError;
bool get isSaveInProgress => throw _privateConstructorUsedError;
UserModel? get currentUser => throw _privateConstructorUsedError;
Expand All @@ -55,6 +56,7 @@ abstract class $EditProfileStateCopyWith<$Res> {
BowlingStyle? bowlingStyle,
PlayerRole? playerRole,
bool isButtonEnable,
bool isImageUploading,
bool isSaved,
bool isSaveInProgress,
UserModel? currentUser});
Expand Down Expand Up @@ -85,6 +87,7 @@ class _$EditProfileStateCopyWithImpl<$Res, $Val extends EditProfileState>
Object? bowlingStyle = freezed,
Object? playerRole = freezed,
Object? isButtonEnable = null,
Object? isImageUploading = null,
Object? isSaved = null,
Object? isSaveInProgress = null,
Object? currentUser = freezed,
Expand Down Expand Up @@ -130,6 +133,10 @@ class _$EditProfileStateCopyWithImpl<$Res, $Val extends EditProfileState>
? _value.isButtonEnable
: isButtonEnable // ignore: cast_nullable_to_non_nullable
as bool,
isImageUploading: null == isImageUploading
? _value.isImageUploading
: isImageUploading // ignore: cast_nullable_to_non_nullable
as bool,
isSaved: null == isSaved
? _value.isSaved
: isSaved // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -177,6 +184,7 @@ abstract class _$$EditProfileStateImplCopyWith<$Res>
BowlingStyle? bowlingStyle,
PlayerRole? playerRole,
bool isButtonEnable,
bool isImageUploading,
bool isSaved,
bool isSaveInProgress,
UserModel? currentUser});
Expand Down Expand Up @@ -206,6 +214,7 @@ class __$$EditProfileStateImplCopyWithImpl<$Res>
Object? bowlingStyle = freezed,
Object? playerRole = freezed,
Object? isButtonEnable = null,
Object? isImageUploading = null,
Object? isSaved = null,
Object? isSaveInProgress = null,
Object? currentUser = freezed,
Expand Down Expand Up @@ -251,6 +260,10 @@ class __$$EditProfileStateImplCopyWithImpl<$Res>
? _value.isButtonEnable
: isButtonEnable // ignore: cast_nullable_to_non_nullable
as bool,
isImageUploading: null == isImageUploading
? _value.isImageUploading
: isImageUploading // ignore: cast_nullable_to_non_nullable
as bool,
isSaved: null == isSaved
? _value.isSaved
: isSaved // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -281,6 +294,7 @@ class _$EditProfileStateImpl implements _EditProfileState {
this.bowlingStyle = null,
this.playerRole = null,
this.isButtonEnable = false,
this.isImageUploading = false,
this.isSaved = false,
this.isSaveInProgress = false,
this.currentUser});
Expand Down Expand Up @@ -313,6 +327,9 @@ class _$EditProfileStateImpl implements _EditProfileState {
final bool isButtonEnable;
@override
@JsonKey()
final bool isImageUploading;
@override
@JsonKey()
final bool isSaved;
@override
@JsonKey()
Expand All @@ -322,7 +339,7 @@ class _$EditProfileStateImpl implements _EditProfileState {

@override
String toString() {
return 'EditProfileState(dob: $dob, nameController: $nameController, emailController: $emailController, locationController: $locationController, imageUrl: $imageUrl, gender: $gender, battingStyle: $battingStyle, bowlingStyle: $bowlingStyle, playerRole: $playerRole, isButtonEnable: $isButtonEnable, isSaved: $isSaved, isSaveInProgress: $isSaveInProgress, currentUser: $currentUser)';
return 'EditProfileState(dob: $dob, nameController: $nameController, emailController: $emailController, locationController: $locationController, imageUrl: $imageUrl, gender: $gender, battingStyle: $battingStyle, bowlingStyle: $bowlingStyle, playerRole: $playerRole, isButtonEnable: $isButtonEnable, isImageUploading: $isImageUploading, isSaved: $isSaved, isSaveInProgress: $isSaveInProgress, currentUser: $currentUser)';
}

@override
Expand All @@ -348,6 +365,8 @@ class _$EditProfileStateImpl implements _EditProfileState {
other.playerRole == playerRole) &&
(identical(other.isButtonEnable, isButtonEnable) ||
other.isButtonEnable == isButtonEnable) &&
(identical(other.isImageUploading, isImageUploading) ||
other.isImageUploading == isImageUploading) &&
(identical(other.isSaved, isSaved) || other.isSaved == isSaved) &&
(identical(other.isSaveInProgress, isSaveInProgress) ||
other.isSaveInProgress == isSaveInProgress) &&
Expand All @@ -368,6 +387,7 @@ class _$EditProfileStateImpl implements _EditProfileState {
bowlingStyle,
playerRole,
isButtonEnable,
isImageUploading,
isSaved,
isSaveInProgress,
currentUser);
Expand All @@ -392,6 +412,7 @@ abstract class _EditProfileState implements EditProfileState {
final BowlingStyle? bowlingStyle,
final PlayerRole? playerRole,
final bool isButtonEnable,
final bool isImageUploading,
final bool isSaved,
final bool isSaveInProgress,
final UserModel? currentUser}) = _$EditProfileStateImpl;
Expand All @@ -417,6 +438,8 @@ abstract class _EditProfileState implements EditProfileState {
@override
bool get isButtonEnable;
@override
bool get isImageUploading;
@override
bool get isSaved;
@override
bool get isSaveInProgress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class PhoneVerificationViewNotifier
await userService.updateUser(user);
state = state.copyWith(verifying: false, credential: credential);
} else {
// get user
await userService.getUser(credential.user?.uid ?? "INVALID ID");
state = state.copyWith(verifying: false, credential: credential);
}
Expand Down

0 comments on commit 3716946

Please sign in to comment.