Skip to content

Commit

Permalink
Fixed default sidebar avatars (#689)
Browse files Browse the repository at this point in the history
Commit:
1d83d19 [1d83d19]
  • Loading branch information
boscojwho committed Oct 11, 2023
1 parent f322e89 commit 8185f66
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 92 deletions.
46 changes: 30 additions & 16 deletions Mlem.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions Mlem/Enums/AvatarType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// AvatarType.swift
// Mlem
//
// Created by Eric Andrews on 2023-10-02.
//

import Foundation

/// Enum of things that can have avatars
enum AvatarType {
case user, community
}

extension AvatarType: AssociatedIcon {
var iconName: String {
switch self {
case .user:
return Icons.user
case .community:
return Icons.community
}
}

var iconNameFill: String {
switch self {
case .user:
return Icons.userFill
case .community:
return Icons.communityFill
}
}
}
21 changes: 21 additions & 0 deletions Mlem/Views/Shared/Avatars/DefaultAvatarView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DefaultAvatarView.swift
// Mlem
//
// Created by Eric Andrews on 2023-10-02.
//

import Foundation
import SwiftUI

struct DefaultAvatarView: View {
let avatarType: AvatarType

var body: some View {
Image(systemName: avatarType.iconNameFill)
.resizable()
.scaledToFill()
.background(.white)
.foregroundStyle(Color.gray.gradient)
}
}
45 changes: 8 additions & 37 deletions Mlem/Views/Shared/Links/AvatarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ struct AvatarView: View {
// Don't clip the avatars of communities from these instances
static let unclippedInstances = ["beehaw.org"]

enum AvatarType { case community, user }

let type: AvatarType
let url: URL?
let avatarSize: CGFloat
Expand Down Expand Up @@ -48,21 +46,15 @@ struct AvatarView: View {

return !unclippedInstances.contains(hostString)
}

var body: some View {
Group {
if let url {
CachedImage(
url: url.withIcon64Parameters,
shouldExpand: false,
fixedSize: CGSize(width: avatarSize, height: avatarSize),
imageNotFound: defaultAvatar,
contentMode: .fill
)
} else {
defaultAvatar()
}
}
CachedImage(
url: url?.withIcon64Parameters,
shouldExpand: false,
fixedSize: CGSize(width: avatarSize, height: avatarSize),
imageNotFound: { AnyView(DefaultAvatarView(avatarType: type)) },
contentMode: .fill
)
.frame(width: avatarSize, height: avatarSize)
.accessibilityHidden(true)
.blur(radius: blurAvatar ? 4 : 0)
Expand All @@ -73,25 +65,4 @@ struct AvatarView: View {
lineWidth: clipAvatar ? 1 : 0
))
}

private func defaultAvatar() -> AnyView {
switch type {
case .community:
return AnyView(
Image(systemName: Icons.communityFill)
.resizable()
.scaledToFill()
.background(.white)
.foregroundStyle(Color.gray.gradient)
)
case .user:
return AnyView(
Image(systemName: Icons.userFill)
.resizable()
.scaledToFill()
.background(.white)
.foregroundStyle(Color.gray.gradient)
)
}
}
}
47 changes: 17 additions & 30 deletions Mlem/Views/Tabs/Feeds/Components/Sidebar Header Avatar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,25 @@ import Foundation

import SwiftUI

struct CommunitySidebarHeaderAvatar: View {
struct SidebarHeaderAvatar: View {
@State var shouldClipAvatar: Bool = false
@State var imageUrl: URL?

var body: some View {
avatar
.frame(width: AppConstants.hugeAvatarSize, height: AppConstants.hugeAvatarSize)
.clipShape(Circle())
.overlay(Circle()
.stroke(.secondary, lineWidth: shouldClipAvatar ? 2 : 0))
.shadow(radius: 10)
.background(shouldClipAvatar ? Circle()
.foregroundColor(.systemBackground) : nil)
}
let avatarType: AvatarType

@ViewBuilder
var avatar: some View {
if let avatarURL = imageUrl {
CachedImage(
url: avatarURL,
shouldExpand: false,
fixedSize: CGSize(width: AppConstants.hugeAvatarSize, height: AppConstants.hugeAvatarSize),
contentMode: .fill
)
} else {
VStack(alignment: .center) {
Spacer()
.frame(height: 20)
Image(systemName: Icons.user)
.font(.system(size: AppConstants.hugeAvatarSize)) // SF Symbols are apparently font
.foregroundColor(.secondary)
}
}
var body: some View {
CachedImage(
url: imageUrl,
shouldExpand: false,
fixedSize: CGSize(width: AppConstants.hugeAvatarSize, height: AppConstants.hugeAvatarSize),
imageNotFound: { AnyView(DefaultAvatarView(avatarType: avatarType)) },
contentMode: .fill
)
.frame(width: AppConstants.hugeAvatarSize, height: AppConstants.hugeAvatarSize)
.clipShape(Circle())
.overlay(Circle()
.stroke(.secondary, lineWidth: shouldClipAvatar ? 2 : 0))
.shadow(radius: 10)
.background(shouldClipAvatar ? Circle()
.foregroundColor(.systemBackground) : nil)
}
}
22 changes: 15 additions & 7 deletions Mlem/Views/Tabs/Feeds/Components/Sidebar Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct CommunitySidebarHeader: View {
var label1: String?
var label2: String?

let avatarType: AvatarType

var body: some View {
ZStack(alignment: .top) {
// Banner
Expand All @@ -38,9 +40,10 @@ struct CommunitySidebarHeader: View {
}
HStack(alignment: .top) {
VStack(alignment: .leading) {
CommunitySidebarHeaderAvatar(
SidebarHeaderAvatar(
shouldClipAvatar: AvatarView.shouldClipCommunityAvatar(url: avatarUrl),
imageUrl: avatarUrl
imageUrl: avatarUrl,
avatarType: avatarType
)

Button {
Expand Down Expand Up @@ -96,7 +99,8 @@ struct SidebarHeaderPreview: PreviewProvider {
bannerURL: URL(string: "https://picsum.photos/seed/mlem-banner/2001/300"),
avatarUrl: URL(string: "https://picsum.photos/seed/mlem-avatar/200"),
label1: "Label 1",
label2: "Label 2"
label2: "Label 2",
avatarType: .community
)
Divider()
CommunitySidebarHeader(
Expand All @@ -106,7 +110,8 @@ struct SidebarHeaderPreview: PreviewProvider {
bannerURL: URL(string: "https://picsum.photos/seed/mlem-banner/200/300"),
avatarUrl: URL(string: "https://picsum.photos/seed/mlem-avatar/200"),
label1: "Label 1",
label2: "Label 2"
label2: "Label 2",
avatarType: .community
)
Divider()
CommunitySidebarHeader(
Expand All @@ -116,7 +121,8 @@ struct SidebarHeaderPreview: PreviewProvider {
bannerURL: URL(string: "https://picsum.photos/seed/mlem-banner/200/300"),
avatarUrl: nil,
label1: "Label 1",
label2: "Label 2"
label2: "Label 2",
avatarType: .community
)
Divider()
CommunitySidebarHeader(
Expand All @@ -126,7 +132,8 @@ struct SidebarHeaderPreview: PreviewProvider {
bannerURL: nil,
avatarUrl: URL(string: "https://picsum.photos/seed/mlem-avatar/200"),
label1: "Label 1",
label2: "Label 2"
label2: "Label 2",
avatarType: .community
)
Divider()
CommunitySidebarHeader(
Expand All @@ -136,7 +143,8 @@ struct SidebarHeaderPreview: PreviewProvider {
bannerURL: nil,
avatarUrl: nil,
label1: "Label 1",
label2: "Label 2"
label2: "Label 2",
avatarType: .community
)
Spacer()
}
Expand Down
3 changes: 2 additions & 1 deletion Mlem/Views/Tabs/Feeds/Components/Sidebar View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ struct CommunitySidebarView: View {
avatarSubtext: .constant("Created \(getRelativeTime(date: communityDetails.communityView.community.published))"),
bannerURL: shouldShowCommunityHeaders ? communityDetails.communityView.community.bannerUrl : nil,
avatarUrl: communityDetails.communityView.community.iconUrl,
label1: "\(communityDetails.communityView.counts.subscribers) Subscribers"
label1: "\(communityDetails.communityView.counts.subscribers) Subscribers",
avatarType: .community
)

Picker(selection: $selectionSection, label: Text("Profile Section")) {
Expand Down
3 changes: 2 additions & 1 deletion Mlem/Views/Tabs/Profile/User View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ struct UserView: View {
bannerURL: shouldShowUserHeaders ? userDetails.person.bannerUrl : nil,
avatarUrl: userDetails.person.avatarUrl,
label1: "\(userDetails.counts.commentCount) Comments",
label2: "\(userDetails.counts.postCount) Posts"
label2: "\(userDetails.counts.postCount) Posts",
avatarType: .user
)
}

Expand Down

0 comments on commit 8185f66

Please sign in to comment.