Skip to content

Commit

Permalink
Structure image with user id (#52)
Browse files Browse the repository at this point in the history
* structure image with user id

* remove unused code

* fix condition
  • Loading branch information
cp-sidhdhi-p authored Jun 20, 2024
1 parent 4e6ed25 commit d7194ca
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 276 deletions.
32 changes: 7 additions & 25 deletions data/lib/service/file_upload/file_upload_service.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dart:io';
import 'package:data/errors/app_error.dart';
import 'package:data/utils/constant/firestore_constant.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:path/path.dart' as path;

final fileUploadServiceProvider = Provider(
(ref) => FileUploadService(FirebaseStorage.instance),
Expand All @@ -14,21 +12,15 @@ class FileUploadService {

FileUploadService(this._firebaseStorage);

Future<String> uploadProfileImage(
String imagePath,
ImageUploadType type,
) async {
var file = File(imagePath);
Future<String> uploadProfileImage({
required String filePath,
required String uploadPath,
}) async {
var file = File(filePath);
if (await file.exists()) {
try {
String fileExtension = path.extension(file.path);
DateTime currentDate = DateTime.now();
String imgName =
"IMG_${currentDate.year}${currentDate.month}${currentDate.day}${currentDate.hour}${currentDate.minute}${currentDate.second}${currentDate.millisecond}$fileExtension";
var snapshot = await _firebaseStorage
.ref()
.child('${type.value}/$imgName')
.putFile(file);
var snapshot =
await _firebaseStorage.ref().child(uploadPath).putFile(file);
var downloadUrl = await snapshot.ref.getDownloadURL();
return downloadUrl;
} catch (error, stack) {
Expand All @@ -47,13 +39,3 @@ class FileUploadService {
}
}
}

enum ImageUploadType {
user(FireStoreConst.userProfileImagesFolder),
team(FireStoreConst.teamProfileImagesFolder),
support(FireStoreConst.supportImagesFolder);

const ImageUploadType(this.value);

final String value;
}
13 changes: 13 additions & 0 deletions data/lib/service/team/team_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ class TeamService {
}
}

Future<void> updateProfileImageUrl(String teamId, String? imageUrl) async {
try {
DocumentReference teamRef =
_firestore.collection(FireStoreConst.teamsCollection).doc(teamId);

await teamRef.set({
FireStoreConst.profileImageUrl: imageUrl,
}, SetOptions(merge: true));
} catch (error, stack) {
throw AppError.fromError(error, stack);
}
}

Future<void> deleteTeam(String teamId) async {
try {
await _firestore
Expand Down
20 changes: 20 additions & 0 deletions data/lib/utils/constant/firebase_storage_constant.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class StorageConst {
static String rootDirectory = "images";

static String userProfileUploadPath({
required String userId,
}) =>
"$rootDirectory/$userId/profile";

static String teamProfileUploadPath({
required String userId,
required String teamId,
}) =>
"$rootDirectory/$userId/team_profile_images/$teamId";

static String supportAttachmentUploadPath({
required String userId,
required String imageName,
}) =>
"$rootDirectory/$userId/support_attachment_images/$imageName";
}
4 changes: 1 addition & 3 deletions data/lib/utils/constant/firestore_constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ class FireStoreConst {
static const String ballScoresCollection = "ball_scores";
static const String usersCollection = "users";
static const String supportCollection = "contact_support";
static const String userProfileImagesFolder = "UserProfileImages";
static const String teamProfileImagesFolder = "TeamProfileImages";
static const String supportImagesFolder = "SupportImages";

// matches field const
static const String id = "id";
Expand Down Expand Up @@ -41,4 +38,5 @@ class FireStoreConst {
static const String players = "players";
static const String createdBy = "created_by";
static const String nameLowercase = "name_lowercase";
static const String profileImageUrl = "profile_img_url";
}
46 changes: 30 additions & 16 deletions khelo/lib/components/profile_image_avatar.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Expand All @@ -10,6 +12,7 @@ import 'package:style/indicator/progress_indicator.dart';
class ProfileImageAvatar extends StatelessWidget {
final double size;
final String? imageUrl;
final String? filePath;
final String? placeHolderImage;
final bool isLoading;
final Function() onEditButtonTap;
Expand All @@ -18,6 +21,7 @@ class ProfileImageAvatar extends StatelessWidget {
super.key,
required this.size,
this.imageUrl,
this.filePath,
this.placeHolderImage,
required this.isLoading,
required this.onEditButtonTap,
Expand Down Expand Up @@ -45,23 +49,33 @@ class ProfileImageAvatar extends StatelessWidget {
width: size,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle, color: context.colorScheme.primary),
child: (imageUrl != null && !isLoading)
? CachedNetworkImage(
imageUrl: imageUrl!,
fit: BoxFit.cover,
errorWidget: (context, url, error) => _placeHolderIcon(context),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
shape: BoxShape.circle,
color: context.colorScheme.primary,
image: (filePath != null)
? DecorationImage(
image: FileImage(File(filePath!)),
fit: BoxFit.cover,
)
: null),
child: (filePath != null)
? null
: (imageUrl != null && !isLoading)
? CachedNetworkImage(
imageUrl: imageUrl!,
fit: BoxFit.cover,
errorWidget: (context, url, error) =>
_placeHolderIcon(context),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
),
),
),
)
: _imagePlaceHolder(context),
)
: _imagePlaceHolder(context),
);
}

Expand Down

This file was deleted.

13 changes: 13 additions & 0 deletions khelo/lib/domain/extensions/file_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'dart:io';

extension FileExtension on File {
Future<bool> isFileUnderMaxSize() async {
try {
const maxAllowedSizeInBytes = 25 * 1024 * 1024;
final sizeInBytes = await length();
return sizeInBytes <= maxAllowedSizeInBytes;
} catch (e) {
rethrow;
}
}
}
2 changes: 1 addition & 1 deletion khelo/lib/ui/flow/profile/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class ProfileScreen extends ConsumerWidget {
context,
icon: Assets.images.icContactSupport,
title: context.l10n.contact_support_title,
onTap: () =>AppRoute.contactSupport().push(context),
onTap: () => AppRoute.contactSupport().push(context),
),
_settingItem(
context,
Expand Down
116 changes: 56 additions & 60 deletions khelo/lib/ui/flow/settings/edit_profile/edit_profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,66 +41,62 @@ class EditProfileScreen extends ConsumerWidget {
_observeActionError(context, ref);
_observeIsSaved(context, ref);

return PopScope(
onPopInvoked: (didPop) async {
await notifier.onBackBtnPressed();
},
child: AppPage(
title: context.l10n.edit_profile_screen_title,
actions: [
if (!isToCreateAccount) ...[
actionButton(context,
onPressed: () => showConfirmationDialog(context,
title: context.l10n.common_delete_title,
message: context.l10n.alert_confirm_default_message(
context.l10n.common_delete_title.toLowerCase()),
confirmBtnText: context.l10n.common_delete_title,
onConfirm: notifier.onDeleteTap),
icon: SvgPicture.asset(
Assets.images.icBin,
height: 24,
width: 24,
fit: BoxFit.contain,
colorFilter: ColorFilter.mode(
context.colorScheme.primary, BlendMode.srcATop),
)),
]
],
body: Builder(
builder: (context) {
return Stack(
children: [
ListView(
padding: context.mediaQueryPadding +
const EdgeInsets.all(16.0) +
BottomStickyOverlay.padding,
children: [
ProfileImageAvatar(
size: profileViewHeight,
placeHolderImage: Assets.images.icProfileThin,
imageUrl: state.imageUrl,
isLoading: state.isImageUploading,
onEditButtonTap: () async {
final imagePath = await ImagePickerSheet.show<String>(
context, true);
if (imagePath != null) {
notifier.onImageChange(imagePath);
}
}),
const SizedBox(height: 24),
_userContactDetailsView(context, notifier, state),
const SizedBox(height: 24),
_userPersonalDetailsView(context, notifier, state),
const SizedBox(height: 24),
_userPlayStyleView(context, notifier, state),
const SizedBox(height: 24),
],
),
_stickyButton(context, notifier, state)
],
);
},
),
return AppPage(
title: context.l10n.edit_profile_screen_title,
actions: [
if (!isToCreateAccount) ...[
actionButton(context,
onPressed: () => showConfirmationDialog(context,
title: context.l10n.common_delete_title,
message: context.l10n.alert_confirm_default_message(
context.l10n.common_delete_title.toLowerCase()),
confirmBtnText: context.l10n.common_delete_title,
onConfirm: notifier.onDeleteTap),
icon: SvgPicture.asset(
Assets.images.icBin,
height: 24,
width: 24,
fit: BoxFit.contain,
colorFilter: ColorFilter.mode(
context.colorScheme.primary, BlendMode.srcATop),
)),
]
],
body: Builder(
builder: (context) {
return Stack(
children: [
ListView(
padding: context.mediaQueryPadding +
const EdgeInsets.all(16.0) +
BottomStickyOverlay.padding,
children: [
ProfileImageAvatar(
size: profileViewHeight,
placeHolderImage: Assets.images.icProfileThin,
imageUrl: state.imageUrl,
filePath: state.filePath,
isLoading: state.isImageUploading,
onEditButtonTap: () async {
final imagePath =
await ImagePickerSheet.show<String>(context, true);
if (imagePath != null) {
notifier.onImageChange(imagePath);
}
}),
const SizedBox(height: 24),
_userContactDetailsView(context, notifier, state),
const SizedBox(height: 24),
_userPersonalDetailsView(context, notifier, state),
const SizedBox(height: 24),
_userPlayStyleView(context, notifier, state),
const SizedBox(height: 24),
],
),
_stickyButton(context, notifier, state)
],
);
},
),
);
}
Expand Down
Loading

0 comments on commit d7194ca

Please sign in to comment.