Skip to content

Commit

Permalink
Update CommunityView.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf committed Jan 1, 2024
1 parent c534e38 commit 58aacff
Showing 1 changed file with 77 additions and 41 deletions.
118 changes: 77 additions & 41 deletions Mlem/Views/Tabs/Feeds/CommunityView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,16 @@ struct CommunityView: View {
.toolbar {
ToolbarItem(placement: .principal) {
Text(community.name)
.font(.headline)
.opacity(scrollToTopAppeared ? 0 : 1)
.animation(.easeOut(duration: 0.2), value: scrollToTopAppeared)
.font(.headline)
.opacity(scrollToTopAppeared ? 0 : 1)
.animation(.easeOut(duration: 0.2), value: scrollToTopAppeared)
}
ToolbarItemGroup(placement: .secondaryAction) {
ForEach(
community.menuFunctions({ community = $0 },
editorTracker: editorTracker,
postTracker: postTracker
)
)
) { menuFunction in
MenuButton(menuFunction: menuFunction, confirmDestructive: confirmDestructive)
}
Expand Down Expand Up @@ -240,43 +240,7 @@ struct CommunityView: View {
}
.buttonStyle(.plain)
Spacer()
if let subscribed = community.subscribed {
let foregroundColor: Color = subscribed ? .green : .secondary
Button {
hapticManager.play(haptic: .lightSuccess, priority: .low)
Task {
var community = community
do {
if subscribed {
confirmDestructive(destructiveFunction: try community.subscribeMenuFunction { self.community = $0 })
} else {
try await community.toggleSubscribe { item in
DispatchQueue.main.async { self.community = item }
}
}
} catch {
errorHandler.handle(error)
}
}
} label: {
HStack(spacing: 4) {
if let subscriberCount = community.subscriberCount {
Text(abbreviateNumber(subscriberCount))
}
Image(systemName: subscribed ? Icons.successCircle : Icons.personFill)
.aspectRatio(contentMode: .fit)
}
.foregroundStyle(foregroundColor)
.padding(.vertical, 5)
.padding(.horizontal, 10)
.background(
Capsule()
.strokeBorder(foregroundColor, style: .init(lineWidth: 1))
.background(Capsule().fill(subscribed ? .green.opacity(0.1) : .clear))
)
}
.buttonStyle(.plain)
}
subscribeButton
}
.padding(.horizontal, AppConstants.postAndCommentSpacing)
.padding(.bottom, 3)
Expand All @@ -290,6 +254,78 @@ struct CommunityView: View {
.background(Color.secondarySystemBackground)
}
}

var subscribeButtonColor: Color {
if community.favorited {
return .blue
} else if community.subscribed ?? false {
return .green
}
return .secondary
}

var subscribeButtonIcon: String {
if community.favorited {
return Icons.favoriteFill
} else if community.subscribed ?? false {
return Icons.successCircle
}
return Icons.personFill
}

@ViewBuilder
var subscribeButton: some View {
let foregroundColor = subscribeButtonColor
if let subscribed = community.subscribed {
HStack(spacing: 4) {
if let subscriberCount = community.subscriberCount {
Text(abbreviateNumber(subscriberCount))
}
Image(systemName: subscribeButtonIcon)
.aspectRatio(contentMode: .fit)
}
.foregroundStyle(foregroundColor)
.padding(.vertical, 5)
.padding(.horizontal, 10)
.background(
Capsule()
.strokeBorder(foregroundColor, style: .init(lineWidth: 1))
.background(Capsule().fill(subscribed ? .green.opacity(0.1) : .clear))
)
.onTapGesture {
hapticManager.play(haptic: .lightSuccess, priority: .low)
Task {
var community = community
do {
if community.favorited {
confirmDestructive(destructiveFunction: community.favoriteMenuFunction { self.community = $0 })
} else if subscribed {
confirmDestructive(destructiveFunction: try community.subscribeMenuFunction { self.community = $0 })
} else {
try await community.toggleSubscribe { item in
DispatchQueue.main.async { self.community = item }
}
}
} catch {
errorHandler.handle(error)
}
}
}
.simultaneousGesture(LongPressGesture().onEnded { _ in
hapticManager.play(haptic: .lightSuccess, priority: .low)
Task {
var community = community
do {
try await community.toggleFavorite { item in
DispatchQueue.main.async { self.community = item }
}
} catch {
errorHandler.handle(error)
}
}
})
}
}
}

// swiftlint:enable type_body_length

0 comments on commit 58aacff

Please sign in to comment.