IceCubesApp/Packages/Account/Sources/Account/AccountDetailView.swift

400 lines
12 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import DesignSystem
import EmojiText
import Env
2022-11-29 11:18:06 +00:00
import Models
import Network
2022-12-18 19:30:19 +00:00
import Shimmer
2023-01-17 10:36:01 +00:00
import Status
import SwiftUI
2022-11-29 11:18:06 +00:00
2023-01-17 10:36:01 +00:00
public struct AccountDetailView: View {
2022-12-20 19:33:45 +00:00
@Environment(\.redactionReasons) private var reasons
@EnvironmentObject private var watcher: StreamWatcher
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var preferences: UserPreferences
2022-12-24 14:09:17 +00:00
@EnvironmentObject private var theme: Theme
2022-11-29 11:18:06 +00:00
@EnvironmentObject private var client: Client
@EnvironmentObject private var routeurPath: RouterPath
2023-01-17 10:36:01 +00:00
2022-11-29 11:18:06 +00:00
@StateObject private var viewModel: AccountDetailViewModel
2022-12-20 08:37:07 +00:00
@State private var scrollOffset: CGFloat = 0
2022-12-21 19:53:23 +00:00
@State private var isFieldsSheetDisplayed: Bool = false
2022-12-27 12:49:54 +00:00
@State private var isCurrentUser: Bool = false
@State private var isCreateListAlertPresented: Bool = false
@State private var createListTitle: String = ""
2023-01-10 07:24:05 +00:00
@State private var isEditingAccount: Bool = false
2023-01-17 10:36:01 +00:00
/// When coming from a URL like a mention tap in a status.
2022-11-29 11:18:06 +00:00
public init(accountId: String) {
_viewModel = StateObject(wrappedValue: .init(accountId: accountId))
}
2023-01-17 10:36:01 +00:00
/// When the account is already fetched by the parent caller.
2022-12-27 12:49:54 +00:00
public init(account: Account) {
_viewModel = StateObject(wrappedValue: .init(account: account))
2022-12-17 12:37:46 +00:00
}
2023-01-17 10:36:01 +00:00
2022-11-29 11:18:06 +00:00
public var body: some View {
2022-12-27 08:11:12 +00:00
ScrollViewReader { proxy in
ScrollViewOffsetReader { offset in
self.scrollOffset = offset
} content: {
LazyVStack(alignment: .leading) {
makeHeaderView(proxy: proxy)
familliarFollowers
.offset(y: -36)
featuredTagsView
.offset(y: -36)
Group {
Picker("", selection: $viewModel.selectedTab) {
ForEach(isCurrentUser ? AccountDetailViewModel.Tab.currentAccountTabs : AccountDetailViewModel.Tab.accountTabs,
id: \.self) { tab in
2023-01-09 18:26:56 +00:00
Image(systemName: tab.iconName)
.tag(tab)
2022-12-27 08:11:12 +00:00
}
}
.pickerStyle(.segmented)
.padding(.horizontal, .layoutPadding)
.offset(y: -20)
}
2022-12-27 08:11:12 +00:00
.id("status")
2023-01-17 10:36:01 +00:00
2022-12-27 08:11:12 +00:00
switch viewModel.tabState {
case .statuses:
2023-01-03 17:22:08 +00:00
if viewModel.selectedTab == .statuses {
pinnedPostsView
}
2022-12-27 08:11:12 +00:00
StatusesListView(fetcher: viewModel)
2023-01-04 17:37:58 +00:00
case .followedTags:
tagsListView
case .lists:
listsListView
2022-12-27 08:11:12 +00:00
}
}
2023-01-09 20:13:00 +00:00
.frame(maxWidth: .maxColumnWidth)
2022-11-29 11:18:06 +00:00
}
2022-12-31 11:28:27 +00:00
.scrollContentBackground(.hidden)
.background(theme.primaryBackgroundColor)
2022-11-29 11:18:06 +00:00
}
.onAppear {
Task {
guard reasons != .placeholder else { return }
isCurrentUser = currentAccount.account?.id == viewModel.accountId
viewModel.isCurrentUser = isCurrentUser
viewModel.client = client
await viewModel.fetchAccount()
if viewModel.statuses.isEmpty {
await viewModel.fetchStatuses()
}
2022-12-20 15:08:09 +00:00
}
}
.refreshable {
Task {
await viewModel.fetchAccount()
await viewModel.fetchStatuses()
}
2022-12-18 19:30:19 +00:00
}
2023-01-17 10:36:01 +00:00
.onChange(of: watcher.latestEvent?.id) { _ in
if let latestEvent = watcher.latestEvent,
2023-01-17 10:36:01 +00:00
viewModel.accountId == currentAccount.account?.id
{
viewModel.handleEvent(event: latestEvent, currentAccount: currentAccount)
}
}
2023-01-10 07:24:05 +00:00
.onChange(of: isEditingAccount, perform: { isEditing in
if !isEditing {
Task {
await viewModel.fetchAccount()
await preferences.refreshServerPreferences()
}
}
})
.sheet(isPresented: $isEditingAccount, content: {
EditAccountView()
})
2022-12-20 08:37:07 +00:00
.edgesIgnoringSafeArea(.top)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
2023-01-04 17:37:58 +00:00
toolbarContent
}
2022-12-18 19:30:19 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-18 19:30:19 +00:00
@ViewBuilder
2022-12-27 08:11:12 +00:00
private func makeHeaderView(proxy: ScrollViewProxy?) -> some View {
switch viewModel.accountState {
2022-12-18 19:30:19 +00:00
case .loading:
AccountDetailHeaderView(viewModel: viewModel,
2022-12-20 16:11:12 +00:00
account: .placeholder(),
2022-12-27 08:11:12 +00:00
scrollViewProxy: proxy,
2022-12-21 11:47:07 +00:00
scrollOffset: $scrollOffset)
2022-12-18 19:30:19 +00:00
.redacted(reason: .placeholder)
.shimmering()
2022-12-18 19:30:19 +00:00
case let .data(account):
AccountDetailHeaderView(viewModel: viewModel,
2022-12-20 16:11:12 +00:00
account: account,
2022-12-27 08:11:12 +00:00
scrollViewProxy: proxy,
2022-12-21 11:47:07 +00:00
scrollOffset: $scrollOffset)
2022-12-18 19:30:19 +00:00
case let .error(error):
Text("Error: \(error.localizedDescription)")
}
}
2023-01-17 10:36:01 +00:00
2022-12-21 19:26:38 +00:00
@ViewBuilder
private var featuredTagsView: some View {
2022-12-21 19:53:23 +00:00
if !viewModel.featuredTags.isEmpty || !viewModel.fields.isEmpty {
2022-12-21 19:26:38 +00:00
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 4) {
2022-12-21 19:53:23 +00:00
if !viewModel.fields.isEmpty {
2022-12-21 19:26:38 +00:00
Button {
2022-12-21 19:53:23 +00:00
isFieldsSheetDisplayed.toggle()
2022-12-21 19:26:38 +00:00
} label: {
VStack(alignment: .leading, spacing: 0) {
2022-12-21 19:53:23 +00:00
Text("About")
2022-12-21 19:26:38 +00:00
.font(.callout)
2022-12-21 19:53:23 +00:00
Text("\(viewModel.fields.count) fields")
2022-12-21 19:26:38 +00:00
.font(.caption2)
}
2022-12-21 19:53:23 +00:00
}
.buttonStyle(.bordered)
.sheet(isPresented: $isFieldsSheetDisplayed) {
fieldSheetView
}
}
if !viewModel.featuredTags.isEmpty {
ForEach(viewModel.featuredTags) { tag in
Button {
routeurPath.navigate(to: .hashTag(tag: tag.name, account: viewModel.accountId))
} label: {
VStack(alignment: .leading, spacing: 0) {
Text("#\(tag.name)")
.font(.callout)
Text("\(tag.statusesCount) posts")
.font(.caption2)
}
}.buttonStyle(.bordered)
}
2022-12-21 19:26:38 +00:00
}
}
.padding(.leading, .layoutPadding)
2022-12-21 19:26:38 +00:00
}
}
}
2023-01-17 10:36:01 +00:00
@ViewBuilder
private var familliarFollowers: some View {
if !viewModel.familliarFollowers.isEmpty {
2022-12-23 15:21:31 +00:00
VStack(alignment: .leading, spacing: 2) {
Text("Also followed by")
.font(.headline)
.padding(.leading, .layoutPadding)
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 0) {
ForEach(viewModel.familliarFollowers) { account in
AvatarView(url: account.avatar, size: .badge)
.onTapGesture {
routeurPath.navigate(to: .accountDetailWithAccount(account: account))
}
.padding(.leading, -4)
}
}
.padding(.leading, .layoutPadding + 4)
}
}
.padding(.top, 2)
.padding(.bottom, 12)
}
}
2023-01-17 10:36:01 +00:00
2022-12-21 19:53:23 +00:00
private var fieldSheetView: some View {
NavigationStack {
List {
ForEach(viewModel.fields) { field in
VStack(alignment: .leading, spacing: 2) {
Text(field.name)
.font(.headline)
HStack {
if field.verifiedAt != nil {
Image(systemName: "checkmark.seal")
.foregroundColor(Color.green.opacity(0.80))
}
EmojiTextApp(field.value.asMarkdown, emojis: viewModel.account?.emojis ?? [])
2022-12-24 14:09:17 +00:00
.foregroundColor(theme.tintColor)
2022-12-21 19:53:23 +00:00
}
.font(.body)
}
2022-12-29 09:39:34 +00:00
.listRowBackground(field.verifiedAt != nil ? Color.green.opacity(0.15) : theme.primaryBackgroundColor)
2022-12-21 19:53:23 +00:00
}
}
2022-12-29 09:39:34 +00:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
2022-12-21 19:53:23 +00:00
.navigationTitle("About")
2023-01-08 18:45:11 +00:00
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
isFieldsSheetDisplayed = false
} label: {
Image(systemName: "xmark")
.imageScale(.small)
.font(.body.weight(.semibold))
.frame(width: 30, height: 30)
.background(theme.primaryBackgroundColor.opacity(0.5))
.clipShape(Circle())
}
.foregroundColor(theme.tintColor)
}
}
2022-12-21 19:53:23 +00:00
}
}
2023-01-17 10:36:01 +00:00
2023-01-04 17:37:58 +00:00
private var tagsListView: some View {
Group {
2023-01-04 17:37:58 +00:00
ForEach(currentAccount.tags) { tag in
HStack {
TagRowView(tag: tag)
Spacer()
Image(systemName: "chevron.right")
}
.padding(.horizontal, .layoutPadding)
.padding(.vertical, 8)
}
2023-01-04 17:37:58 +00:00
}.task {
await currentAccount.fetchFollowedTags()
}
}
2023-01-17 10:36:01 +00:00
private var listsListView: some View {
Group {
ForEach(currentAccount.lists) { list in
NavigationLink(value: RouteurDestinations.list(list: list)) {
HStack {
Text(list.title)
Spacer()
Image(systemName: "chevron.right")
}
.padding(.vertical, 8)
.padding(.horizontal, .layoutPadding)
.font(.headline)
.foregroundColor(theme.labelColor)
}
.contextMenu {
Button("Delete list", role: .destructive) {
Task {
await currentAccount.deleteList(list: list)
}
}
}
}
Button("Create a new list") {
isCreateListAlertPresented = true
}
.padding(.horizontal, .layoutPadding)
}
2023-01-04 17:37:58 +00:00
.task {
await currentAccount.fetchLists()
}
.alert("Create a new list", isPresented: $isCreateListAlertPresented) {
TextField("List name", text: $createListTitle)
Button("Cancel") {
isCreateListAlertPresented = false
createListTitle = ""
}
Button("Create List") {
guard !createListTitle.isEmpty else { return }
isCreateListAlertPresented = false
Task {
await currentAccount.createList(title: createListTitle)
createListTitle = ""
}
}
} message: {
Text("Enter the name for your list")
}
}
2023-01-17 10:36:01 +00:00
2023-01-03 17:22:08 +00:00
@ViewBuilder
private var pinnedPostsView: some View {
if !viewModel.pinned.isEmpty {
ForEach(viewModel.pinned) { status in
VStack(alignment: .leading) {
Label("Pinned post", systemImage: "pin.fill")
.font(.footnote)
.foregroundColor(.gray)
.fontWeight(.semibold)
StatusRowView(viewModel: .init(status: status))
}
.padding(.horizontal, .layoutPadding)
Divider()
.padding(.vertical, .dividerPadding)
}
}
}
2023-01-17 10:36:01 +00:00
2023-01-04 17:37:58 +00:00
@ToolbarContentBuilder
private var toolbarContent: some ToolbarContent {
ToolbarItem(placement: .principal) {
if scrollOffset < -200 {
switch viewModel.accountState {
case let .data(account):
EmojiTextApp(account.safeDisplayName.asMarkdown, emojis: account.emojis)
.font(.headline)
2023-01-04 17:37:58 +00:00
default:
EmptyView()
}
}
}
2023-01-17 10:36:01 +00:00
2023-01-04 17:37:58 +00:00
ToolbarItem(placement: .navigationBarTrailing) {
Menu {
if let account = viewModel.account {
Section(account.acct) {
if !viewModel.isCurrentUser {
Button {
routeurPath.presentedSheet = .mentionStatusEditor(account: account,
visibility: preferences.serverPreferences?.postVisibility ?? .pub)
2023-01-04 17:37:58 +00:00
} label: {
Label("Mention", systemImage: "at")
}
Button {
routeurPath.presentedSheet = .mentionStatusEditor(account: account, visibility: .direct)
} label: {
Label("Message", systemImage: "tray.full")
}
Divider()
}
2023-01-17 10:36:01 +00:00
2023-01-04 17:37:58 +00:00
if viewModel.relationship?.following == true {
Button {
routeurPath.presentedSheet = .listAddAccount(account: account)
} label: {
Label("Add/Remove from lists", systemImage: "list.bullet")
}
}
2023-01-17 10:36:01 +00:00
2023-01-04 17:37:58 +00:00
if let url = account.url {
ShareLink(item: url)
}
2023-01-17 10:36:01 +00:00
2023-01-10 07:24:05 +00:00
Divider()
2023-01-17 10:36:01 +00:00
2023-01-10 07:24:05 +00:00
if isCurrentUser {
Button {
isEditingAccount = true
} label: {
Label("Edit Info", systemImage: "pencil")
}
}
2023-01-04 17:37:58 +00:00
}
}
} label: {
Image(systemName: "ellipsis")
}
}
}
2022-12-17 12:37:46 +00:00
}
struct AccountDetailView_Previews: PreviewProvider {
static var previews: some View {
AccountDetailView(account: .placeholder())
2022-11-29 11:18:06 +00:00
}
}