Add inline account badge for status context favorites / boosts

This commit is contained in:
Thomas Ricouard 2023-02-05 10:07:45 +01:00
parent fedfa1f1c7
commit 2bd5c26c6f
3 changed files with 46 additions and 10 deletions

View file

@ -7,7 +7,7 @@ public struct AvatarView: View {
@EnvironmentObject private var theme: Theme @EnvironmentObject private var theme: Theme
public enum Size { public enum Size {
case account, status, embed, badge, boost case account, status, embed, badge, list, boost
public var size: CGSize { public var size: CGSize {
switch self { switch self {
@ -22,6 +22,8 @@ public struct AvatarView: View {
return .init(width: 34, height: 34) return .init(width: 34, height: 34)
case .badge: case .badge:
return .init(width: 28, height: 28) return .init(width: 28, height: 28)
case .list:
return .init(width: 20, height: 20)
case .boost: case .boost:
return .init(width: 12, height: 12) return .init(width: 12, height: 12)
} }
@ -29,7 +31,7 @@ public struct AvatarView: View {
var cornerRadius: CGFloat { var cornerRadius: CGFloat {
switch self { switch self {
case .badge, .boost: case .badge, .boost, .list:
return size.width / 2 return size.width / 2
default: default:
return 4 return 4

View file

@ -92,6 +92,9 @@ struct StatusActionsView: View {
} }
if viewModel.isFocused { if viewModel.isFocused {
summaryView summaryView
.task {
await viewModel.fetchActionsAccounts()
}
} }
} }
} }
@ -137,20 +140,40 @@ struct StatusActionsView: View {
if viewModel.favoritesCount > 0 { if viewModel.favoritesCount > 0 {
Divider() Divider()
NavigationLink(value: RouterDestinations.favoritedBy(id: viewModel.status.id)) { NavigationLink(value: RouterDestinations.favoritedBy(id: viewModel.status.id)) {
HStack {
Text("status.summary.n-favorites \(viewModel.favoritesCount)") Text("status.summary.n-favorites \(viewModel.favoritesCount)")
.font(.scaledCallout) .font(.scaledCallout)
Spacer() Spacer()
makeAccountsScrollView(accounts: viewModel.favoriters)
Image(systemName: "chevron.right") Image(systemName: "chevron.right")
} }
.frame(height: 20)
}
} }
if viewModel.reblogsCount > 0 { if viewModel.reblogsCount > 0 {
Divider() Divider()
NavigationLink(value: RouterDestinations.rebloggedBy(id: viewModel.status.id)) { NavigationLink(value: RouterDestinations.rebloggedBy(id: viewModel.status.id)) {
HStack {
Text("status.summary.n-boosts \(viewModel.reblogsCount)") Text("status.summary.n-boosts \(viewModel.reblogsCount)")
.font(.scaledCallout) .font(.scaledCallout)
Spacer() Spacer()
makeAccountsScrollView(accounts: viewModel.rebloggers)
Image(systemName: "chevron.right") Image(systemName: "chevron.right")
} }
.frame(height: 20)
}
}
}
private func makeAccountsScrollView(accounts: [Account]) -> some View {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 0) {
ForEach(accounts) { account in
AvatarView(url: account.avatar, size: .list)
.padding(.leading, -4)
}
}
.padding(.leading, .layoutPadding)
} }
} }

View file

@ -27,6 +27,9 @@ public class StatusRowViewModel: ObservableObject {
@Published var translation: String? @Published var translation: String?
@Published var isLoadingTranslation: Bool = false @Published var isLoadingTranslation: Bool = false
@Published var favoriters: [Account] = []
@Published var rebloggers: [Account] = []
var seen = false var seen = false
var filter: Filtered? { var filter: Filtered? {
@ -251,6 +254,14 @@ public class StatusRowViewModel: ObservableObject {
} catch {} } catch {}
} }
func fetchActionsAccounts() async {
guard let client else { return }
do {
favoriters = try await client.get(endpoint: Statuses.favoritedBy(id: status.id, maxId: nil))
rebloggers = try await client.get(endpoint: Statuses.rebloggedBy(id: status.id, maxId: nil))
} catch { }
}
private func updateFromStatus(status: Status) { private func updateFromStatus(status: Status) {
if let reblog = status.reblog { if let reblog = status.reblog {
isFavorited = reblog.favourited == true isFavorited = reblog.favourited == true