Impressia/Vernissage/Views/UserProfileView/UserProfileView.swift

202 lines
7.2 KiB
Swift
Raw Normal View History

2023-01-04 20:56:26 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
2023-02-19 10:32:38 +01:00
import PixelfedKit
2023-01-04 20:56:26 +01:00
struct UserProfileView: View {
2023-01-06 13:05:21 +01:00
@EnvironmentObject private var applicationState: ApplicationState
2023-02-03 15:16:30 +01:00
@EnvironmentObject private var client: Client
2023-01-06 13:05:21 +01:00
2023-03-07 16:45:44 +01:00
@Environment(\.dismiss) private var dismiss
2023-01-04 20:56:26 +01:00
@State public var accountId: String
@State public var accountDisplayName: String?
@State public var accountUserName: String
2023-02-01 18:40:28 +01:00
2023-01-04 20:56:26 +01:00
@State private var account: Account? = nil
2023-01-05 11:55:20 +01:00
@State private var relationship: Relationship? = nil
2023-02-01 18:40:28 +01:00
@State private var state: ViewState = .loading
2023-01-04 20:56:26 +01:00
var body: some View {
2023-02-01 18:40:28 +01:00
self.mainBody()
2023-02-21 08:36:14 +01:00
.navigationTitle(self.accountDisplayName ?? self.accountUserName)
2023-02-01 18:40:28 +01:00
.toolbar {
if let account = self.account {
if self.applicationState.account?.id != account.id {
self.getTrailingAccountToolbar(account: account)
} else {
self.getTrailingProfileToolbar(account: account)
}
}
}
}
@ViewBuilder
private func mainBody() -> some View {
switch state {
case .loading:
LoadingIndicator()
.task {
await self.loadData()
}
case .loaded:
if let account = self.account {
ScrollView {
2023-02-21 07:05:06 +01:00
UserProfileHeaderView(account: account, relationship: relationship)
UserProfileStatusesView(accountId: account.id)
}
2023-01-04 20:56:26 +01:00
}
2023-02-01 18:40:28 +01:00
case .error(let error):
ErrorView(error: error) {
self.state = .loading
await self.loadData()
2023-01-04 20:56:26 +01:00
}
2023-02-01 18:40:28 +01:00
.padding()
2023-01-04 20:56:26 +01:00
}
}
2023-01-09 08:29:55 +01:00
2023-02-01 18:40:28 +01:00
private func loadData() async {
do {
2023-03-07 16:45:44 +01:00
if self.accountId.isEmpty {
let accountsFromApi = try await self.client.search?.search(query: self.accountUserName, resultsType: .accounts)
if let accountFromApi = accountsFromApi?.accounts.first {
self.accountId = accountFromApi.id
} else {
ToastrService.shared.showError(title: "Account not exists", imageSystemName: "exclamationmark.octagon")
dismiss()
return
}
}
2023-02-03 15:16:30 +01:00
async let relationshipTask = self.client.accounts?.relationships(withId: self.accountId)
async let accountTask = self.client.accounts?.account(withId: self.accountId)
2023-02-01 18:40:28 +01:00
// Wait for download account and relationships.
(self.relationship, self.account) = try await (relationshipTask, accountTask)
self.state = .loaded
} catch {
ErrorService.shared.handle(error, message: "Error during download account from server.", showToastr: !Task.isCancelled)
self.state = .error(error)
}
2023-01-09 08:29:55 +01:00
}
@ToolbarContentBuilder
private func getTrailingAccountToolbar(account: Account) -> some ToolbarContent {
ToolbarItem(placement: .navigationBarTrailing) {
Menu (content: {
if let accountUrl = account.url {
Link(destination: accountUrl) {
Label("Open in browser", systemImage: "safari")
}
ShareLink(item: accountUrl) {
Label("Share", systemImage: "square.and.arrow.up")
}
Divider()
}
Button {
Task {
await onMuteAccount(account: account)
}
} label: {
if self.relationship?.muting == true {
Label("Unute", systemImage: "message.and.waveform.fill")
} else {
Label("Mute", systemImage: "message.and.waveform")
}
}
Button {
Task {
await onBlockAccount(account: account)
}
} label: {
if self.relationship?.blocking == true {
Label("Unblock", systemImage: "hand.raised.fill")
} else {
Label("Block", systemImage: "hand.raised")
}
}
}, label: {
Image(systemName: "gear")
.tint(.mainTextColor)
})
.tint(.accentColor)
}
}
@ToolbarContentBuilder
private func getTrailingProfileToolbar(account: Account) -> some ToolbarContent {
ToolbarItem(placement: .navigationBarTrailing) {
Menu (content: {
if let accountUrl = account.url {
Link(destination: accountUrl) {
Label("Open in browser", systemImage: "safari")
}
ShareLink(item: accountUrl) {
Label("Share", systemImage: "square.and.arrow.up")
}
Divider()
}
2023-02-01 20:01:18 +01:00
NavigationLink(value: RouteurDestinations.favourites) {
Label("Favourites", systemImage: "hand.thumbsup")
}
2023-02-01 20:01:18 +01:00
NavigationLink(value: RouteurDestinations.bookmarks) {
Label("Bookmarks", systemImage: "bookmark")
}
2023-01-23 18:01:27 +01:00
}, label: {
Image(systemName: "gear")
.tint(.mainTextColor)
})
.tint(.accentColor)
}
}
private func onMuteAccount(account: Account) async {
do {
if self.relationship?.muting == true {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.unmute(account: account.id) {
self.relationship = relationship
}
} else {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.mute(account: account.id) {
self.relationship = relationship
}
}
} catch {
ErrorService.shared.handle(error, message: "Muting/unmuting action failed.", showToastr: true)
}
}
private func onBlockAccount(account: Account) async {
do {
if self.relationship?.blocking == true {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.unblock(account: account.id) {
self.relationship = relationship
}
} else {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.block(account: account.id) {
self.relationship = relationship
}
}
} catch {
ErrorService.shared.handle(error, message: "Block/unblock action failed.", showToastr: true)
}
}
2023-01-04 20:56:26 +01:00
}