Skip to content

Commit

Permalink
use ProfileImage component
Browse files Browse the repository at this point in the history
  • Loading branch information
petabite committed Jun 14, 2023
1 parent e3011e7 commit 3ef63ee
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 79 deletions.
9 changes: 6 additions & 3 deletions ALUM/ALUM/Components/EditProfileImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ struct EditProfileImage: View {
}.onAppear(perform: {
Task {
/// Get user's current image
/// We cannot use the loading state of ProfileImage because
/// we are loading the image ourself to get a UIImage
do {
loading = true
image = try await ImageService.shared.getImage(imageId: imageId)
Expand All @@ -66,8 +64,13 @@ struct EditProfileImage: View {
if loading {
ProgressView()
.frame(width: 112, height: 112)
} else if image != nil {
Image(uiImage: image!)
.resizable()
.frame(width: 112, height: 112)
.clipShape(Circle())
} else {
ProfileImage(image: $image, size: 112)
Image("DefaultProfileImage")
}
}
.padding(20)
Expand Down
15 changes: 2 additions & 13 deletions ALUM/ALUM/Components/HorizontalMenteeCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct HorizontalMenteeCard: View {
var menteeId: String

var school: String = "NHS"
@State var isEmpty = true
@StateObject private var viewModel = MenteeProfileViewmodel()

var body: some View {
Expand Down Expand Up @@ -43,18 +42,8 @@ struct HorizontalMenteeCard: View {
RoundedRectangle(cornerRadius: 12.0)
.frame(width: 358, height: 118)
.foregroundColor(Color("ALUM Primary Purple"))
if isEmpty {
Circle()
.frame(width: 85)
.foregroundColor(Color("NeutralGray1"))
.offset(x: -112.5)
} else {
Image(mentee.imageId)
.resizable()
.clipShape(Circle())
.frame(width: 85, height: 85)
.offset(x: -112.5)
}
ProfileImage(imageId: mentee.imageId, size: 85)
.offset(x: -112.5)
VStack {
HStack {
Text(mentee.name)
Expand Down
15 changes: 2 additions & 13 deletions ALUM/ALUM/Components/MentorCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct MentorCard: View {
@State var isEmpty = false
@State var uID: String = ""
@StateObject private var viewModel = MentorProfileViewModel()

Expand Down Expand Up @@ -36,18 +35,8 @@ struct MentorCard: View {
RoundedRectangle(cornerRadius: 12.0)
.frame(width: 358, height: 118)
.foregroundColor(Color("ALUM Primary Purple"))
if isEmpty {
Circle()
.frame(width: 85, height: 85)
.foregroundColor(Color("NeutralGray1"))
.offset(x: -112.5)
} else {
Image("ALUMLogoBlue")
.resizable()
.clipShape(Circle())
.frame(width: 85, height: 85)
.offset(x: -112.5)
}
ProfileImage(imageId: mentor.imageId, size: 85)
.offset(x: -112.5)
VStack {
HStack {
Text(mentor.name)
Expand Down
44 changes: 18 additions & 26 deletions ALUM/ALUM/Components/ProfileImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,33 @@
import SwiftUI

struct ProfileImage: View {
let imageId: String
let size: CGFloat
@State var loading = false
@Binding var imageId: String?
@Binding var image: UIImage?
var size: CGFloat

init(image: Binding<UIImage?> = .constant(nil), imageId: Binding<String?> = .constant(nil), size: CGFloat = 100) {
_image = image
_imageId = imageId
self.size = size
}
@State var image: UIImage?

var body: some View {
Group {
if loading {
ProgressView()
.frame(width: size, height: size)
} else if image != nil {
Image(uiImage: image!)
.resizable()
.frame(width: size, height: size)
.clipShape(Circle())
} else {
Image("DefaultProfileImage")
(
image != nil ? Image(uiImage: image!) : Image("DefaultProfileImage")
)
.resizable()
.clipShape(Circle())
.frame(width: size, height: size)
}
}.onAppear(perform: {
Task {
if imageId != nil {
do {
loading = true
image = try await ImageService.shared.getImage(imageId: imageId!)
loading = false
} catch {
/// User has no image
loading = false
}
do {
loading = true
image = try await ImageService.shared.getImage(imageId: imageId)
loading = false
} catch {
/// User has no image
loading = false
}
}
})
Expand All @@ -51,8 +43,8 @@ struct ProfileImage: View {

struct ProfileImage_Previews: PreviewProvider {
static var previews: some View {
@State var imageId: String? = "64893922bc50c5db8870cc59"
@State var imageId = "64893922bc50c5db8870cc59"

return ProfileImage(imageId: $imageId, size: 100)
return ProfileImage(imageId: imageId, size: 100)
}
}
7 changes: 2 additions & 5 deletions ALUM/ALUM/Views/HomeScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ struct HomeScreen: View {
.padding(.bottom, 5)

NavigationLink(destination: MenteeProfileScreen(uID: pairedMenteeId)) {
HorizontalMenteeCard(
menteeId: pairedMenteeId,
isEmpty: true
)
HorizontalMenteeCard(menteeId: pairedMenteeId)
.padding(.bottom, 28)
}
}
Expand All @@ -79,7 +76,7 @@ struct HomeScreen: View {
.padding(.bottom, 5)

NavigationLink(destination: MentorProfileScreen(uID: pairedMentorId)) {
MentorCard(isEmpty: true, uID: pairedMentorId)
MentorCard(uID: pairedMentorId)
.padding(.bottom, 28)
}
}
Expand Down
11 changes: 2 additions & 9 deletions ALUM/ALUM/Views/MenteeProfileScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ struct MenteeProfileScreen: View {
}

var content: some View {
let mentee = viewModel.mentee!

return
GeometryReader { grr in
VStack(spacing: 0) {
ScrollView {
Expand Down Expand Up @@ -95,11 +92,7 @@ extension MenteeProfileScreen {
Circle()
.frame(width: 135, height: 145)
.foregroundColor(Color("ALUM White2"))
Image("ALUMLogoBlue")
.resizable()
.frame(width: 135, height: 135)
.clipShape(Circle())
.scaledToFit()
ProfileImage(imageId: viewModel.mentee!.imageId, size: 135)
}
.padding(.top, 57)
}
Expand Down Expand Up @@ -159,7 +152,7 @@ extension MenteeProfileScreen {
})
.customNavigationTitle("Mentor Profile")
) {
MentorCard(isEmpty: true, uID: viewModel.mentee!.mentorId ?? "")
MentorCard(uID: viewModel.mentee!.mentorId ?? "")
.padding(.bottom, 10)
}
}
Expand Down
6 changes: 1 addition & 5 deletions ALUM/ALUM/Views/MentorProfileScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ extension MentorProfileScreen {
Circle()
.frame(width: 135, height: 145)
.foregroundColor(Color("ALUM White2"))
Image("ALUMLogoBlue")
.resizable()
.frame(width: 135, height: 135)
.clipShape(Circle())
.scaledToFit()
ProfileImage(imageId: viewModel.mentor!.imageId, size: 135)
}
.padding(.top, 57)
}
Expand Down
7 changes: 2 additions & 5 deletions ALUM/ALUM/Views/SessionDetailsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,7 @@ extension SessionDetailsScreen {
uID: session.menteeId
).customNavigationTitle("Mentee Profile")
) {
HorizontalMenteeCard(
menteeId: session.menteeId,
isEmpty: true
)
HorizontalMenteeCard(menteeId: session.menteeId)
.padding(.bottom, 28)
}
dateTimeDisplaySection
Expand Down Expand Up @@ -371,7 +368,7 @@ extension SessionDetailsScreen {
MentorProfileScreen(uID: session.mentorId)
.customNavigationTitle("Mentor Profile")
) {
MentorCard(isEmpty: true, uID: session.mentorId)
MentorCard(uID: session.mentorId)
.padding(.bottom, 28)
}
dateTimeDisplaySection
Expand Down

0 comments on commit 3ef63ee

Please sign in to comment.