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-03-25 11:37:02 +01:00
|
|
|
@State private var viewId = UUID().uuidString
|
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:
|
2023-01-09 10:56:20 +01:00
|
|
|
if let account = self.account {
|
|
|
|
ScrollView {
|
2023-02-21 07:05:06 +01:00
|
|
|
UserProfileHeaderView(account: account, relationship: relationship)
|
2023-03-25 11:37:02 +01:00
|
|
|
.id(self.viewId)
|
2023-02-21 07:05:06 +01:00
|
|
|
UserProfileStatusesView(accountId: account.id)
|
2023-01-09 10:56:20 +01:00
|
|
|
}
|
2023-03-25 11:37:02 +01:00
|
|
|
.onAppear {
|
|
|
|
if let updatedProfile = self.applicationState.updatedProfile {
|
|
|
|
self.account = nil
|
|
|
|
self.account = updatedProfile
|
|
|
|
self.applicationState.updatedProfile = nil
|
|
|
|
self.viewId = UUID().uuidString
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2023-03-13 13:53:36 +01:00
|
|
|
ToastrService.shared.showError(title: "userProfile.error.notExists", imageSystemName: "exclamationmark.octagon")
|
2023-03-07 16:45:44 +01:00
|
|
|
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 {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "userProfile.error.loadingAccountFailed", showToastr: !Task.isCancelled)
|
2023-02-01 18:40:28 +01:00
|
|
|
self.state = .error(error)
|
2023-01-12 13:59:13 +01:00
|
|
|
}
|
2023-01-09 08:29:55 +01:00
|
|
|
}
|
2023-01-23 09:10:58 +01:00
|
|
|
|
|
|
|
@ToolbarContentBuilder
|
|
|
|
private func getTrailingAccountToolbar(account: Account) -> some ToolbarContent {
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
|
|
Menu (content: {
|
|
|
|
if let accountUrl = account.url {
|
|
|
|
Link(destination: accountUrl) {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.openInBrowser", comment: "Open in browser"), systemImage: "safari")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ShareLink(item: accountUrl) {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.share", comment: "Share"), systemImage: "square.and.arrow.up")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Divider()
|
|
|
|
}
|
|
|
|
|
|
|
|
Button {
|
|
|
|
Task {
|
|
|
|
await onMuteAccount(account: account)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
if self.relationship?.muting == true {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.unmute", comment: "Unute"), systemImage: "message.and.waveform.fill")
|
2023-01-23 09:10:58 +01:00
|
|
|
} else {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.mute", comment: "Mute"), systemImage: "message.and.waveform")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Button {
|
|
|
|
Task {
|
|
|
|
await onBlockAccount(account: account)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
if self.relationship?.blocking == true {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.unblock", comment: "Unblock"), systemImage: "hand.raised.fill")
|
2023-01-23 09:10:58 +01:00
|
|
|
} else {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.block", comment: "Block"), systemImage: "hand.raised")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 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) {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.openInBrowser", comment: "Open in browser"), systemImage: "safari")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ShareLink(item: accountUrl) {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.share", comment: "Share"), systemImage: "square.and.arrow.up")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Divider()
|
|
|
|
}
|
|
|
|
|
2023-02-01 20:01:18 +01:00
|
|
|
NavigationLink(value: RouteurDestinations.favourites) {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.favourites", comment: "Favourites"), systemImage: "hand.thumbsup")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 20:01:18 +01:00
|
|
|
NavigationLink(value: RouteurDestinations.bookmarks) {
|
2023-03-13 13:53:36 +01:00
|
|
|
Label(NSLocalizedString("userProfile.title.bookmarks", comment: "Bookmarks"), systemImage: "bookmark")
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
2023-01-23 18:01:27 +01:00
|
|
|
|
2023-03-26 08:21:22 +02:00
|
|
|
Divider()
|
2023-03-24 17:45:27 +01:00
|
|
|
|
2023-03-26 08:21:22 +02:00
|
|
|
NavigationLink(value: RouteurDestinations.editProfile) {
|
|
|
|
Label(NSLocalizedString("userProfile.title.edit", comment: "Edit profile"), systemImage: "pencil")
|
|
|
|
}
|
2023-01-23 09:10:58 +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) {
|
2023-01-23 09:10:58 +01:00
|
|
|
self.relationship = relationship
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-03 15:16:30 +01:00
|
|
|
if let relationship = try await self.client.accounts?.mute(account: account.id) {
|
2023-01-23 09:10:58 +01:00
|
|
|
self.relationship = relationship
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "userProfile.error.mute", showToastr: true)
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-01-23 09:10:58 +01:00
|
|
|
self.relationship = relationship
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-03 15:16:30 +01:00
|
|
|
if let relationship = try await self.client.accounts?.block(account: account.id) {
|
2023-01-23 09:10:58 +01:00
|
|
|
self.relationship = relationship
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "userProfile.error.block", showToastr: true)
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-04 20:56:26 +01:00
|
|
|
}
|