Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf committed Oct 7, 2023
1 parent fc64d25 commit cb69ddd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
32 changes: 16 additions & 16 deletions Mlem/Models/Content/User/UserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,33 @@ struct UserModel {
}

// Once we've done other model types we should stop this from relying on API types
func getFlair(
func getFlairs(
postContext: APIPost? = nil,
commentContext: APIComment? = nil,
communityContext: GetCommunityResponse? = nil
) -> UserFlair? {
) -> [UserFlair] {
var ret: [UserFlair] = .init()
if let post = postContext, post.creatorId == self.userId {
ret.append(.op)
}
if isAdmin {
return .admin
ret.append(.admin)
}
if isBot {
return .bot
if UserModel.developerNames.contains(profileUrl.absoluteString) {
ret.append(.developer)
}
if let comment = commentContext, comment.distinguished {
return .moderator
}
if let community = communityContext, community.moderators.contains(where: { $0.moderator.id == person.id }) {
return .moderator
ret.append(.moderator)
} else if let community = communityContext, community.moderators.contains(where: { $0.moderator.id == userId }) {
ret.append(.moderator)
}
if let post = postContext, post.creatorId == self.userId {
return .op
if isBot {
ret.append(.bot)
}
if banned {
return .banned
}
if UserModel.developerNames.contains(profileUrl.absoluteString) {
return .developer
ret.append(.banned)
}
return nil
return ret
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mlem/Models/UserFlair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum UserFlair {
var color: Color {
switch self {
case .admin:
return .pink
return .teal
case .moderator:
return .green
case .op:
Expand Down
57 changes: 46 additions & 11 deletions Mlem/Views/Shared/Links/User/UserLabelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,43 +82,78 @@ struct UserLabelView: View {

@ViewBuilder
private var userName: some View {
let flair = user.getFlair(
let flairs = user.getFlairs(
postContext: postContext,
commentContext: commentContext,
communityContext: communityContext
)

HStack(spacing: 4) {
if let flair = flair {
Image(systemName: flair.icon)
.font(.footnote)
.imageScale(serverInstanceLocation == .bottom ? .large : .small)
.foregroundColor(flair.color)
if serverInstanceLocation == .bottom {
if flairs.count == 1, let first = flairs.first {
userFlairIcon(with: first)
.imageScale(.large)
} else if !flairs.isEmpty {
HStack(spacing: 2) {
LazyHGrid(rows: [GridItem(), GridItem()], alignment: .center) {
ForEach(flairs.dropLast(flairs.count % 2), id: \.self) { flair in
userFlairIcon(with: flair)
.imageScale(.medium)
}
}
if flairs.count % 2 != 0 {
userFlairIcon(with: flairs.last!)
.imageScale(.medium)
}
}
.padding(2)
.padding(.trailing, 4)
}

} else {
if flairs.count == 1, let first = flairs.first {
userFlairIcon(with: first)
.imageScale(.small)
} else if !flairs.isEmpty {
ForEach(flairs, id: \.self) { flair in
userFlairIcon(with: flair)
.imageScale(.small)
}
.padding(.trailing, 4)
}
}

switch serverInstanceLocation {
case .disabled:
userName(with: flair)
userName(with: flairs)
case .bottom:
VStack(alignment: .leading) {
userName(with: flair)
userName(with: flairs)
userInstance
}
case .trailing:
HStack(spacing: 0) {
userName(with: flair)
userName(with: flairs)
userInstance
}
}
}
}

@ViewBuilder
private func userName(with flair: UserFlair?) -> some View {
private func userFlairIcon(with flair: UserFlair) -> some View {
Image(systemName: flair.icon)
.bold()
.font(.footnote)
.foregroundColor(flair.color)
}

@ViewBuilder
private func userName(with flairs: [UserFlair]) -> some View {
Text(user.displayName)
.bold()
.font(.footnote)
.foregroundColor(flair?.color ?? .gray)
.foregroundColor(flairs.count == 1 ? flairs.first!.color : .gray)
}

@ViewBuilder
Expand Down
7 changes: 4 additions & 3 deletions Mlem/Views/Tabs/Search/UserResultView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ struct UserResultView: View {
NavigationLink(value: NavigationRoute.userProfile(user)) {
HStack(spacing: 10) {
AvatarView(user: user, avatarSize: 48)
let flair = user.getFlair()
let flairs = user.getFlairs()
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 4) {
if let flair = flair {
ForEach(flairs, id: \.self) { flair in
Image(systemName: flair.icon)
.imageScale(.small)
.foregroundStyle(flair.color)
}
Text(user.name)
.foregroundStyle(flairs.count == 1 ? flairs.first!.color : .primary)
}
.foregroundStyle(flair?.color ?? .primary)
Text(caption)
.font(.footnote)
.foregroundStyle(.secondary)
Expand Down

0 comments on commit cb69ddd

Please sign in to comment.