Impressia/Vernissage/Views/UserProfileView/UserProfileView.swift

263 lines
10 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.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-04 20:56:26 +01:00
//
2023-01-04 20:56:26 +01:00
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-04-04 09:14:07 +02:00
@EnvironmentObject private var routerPath: RouterPath
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-03-27 14:52:53 +02:00
@StateObject private var relationship = RelationshipModel()
@State private var account: Account?
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)
}
}
}
}
2023-02-01 18:40:28 +01:00
@ViewBuilder
private func mainBody() -> some View {
switch state {
case .loading:
LoadingIndicator()
.task {
await self.loadData()
}
case .loaded:
if let account = self.account {
self.accountView(account: account)
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
}
}
private func accountView(account: Account) -> some View {
ScrollView {
UserProfileHeaderView(account: account, relationship: relationship)
.id(self.viewId)
UserProfileStatusesView(accountId: account.id)
}
.onAppear {
if let updatedProfile = self.applicationState.updatedProfile {
self.account = nil
self.account = updatedProfile
self.applicationState.updatedProfile = nil
self.viewId = UUID().uuidString
}
}
}
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()
2023-03-07 16:45:44 +01:00
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.
2023-03-27 14:52:53 +02:00
let (relationshipFromApi, accountFromApi) = try await (relationshipTask, accountTask)
2023-03-27 14:52:53 +02:00
if let relationshipFromApi {
self.relationship.update(relationship: relationshipFromApi)
} else {
self.relationship.update(relationship: RelationshipModel())
}
self.account = accountFromApi
2023-02-01 18:40:28 +01:00
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-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) {
2023-03-13 13:53:36 +01:00
Label(NSLocalizedString("userProfile.title.openInBrowser", comment: "Open in browser"), systemImage: "safari")
}
ShareLink(item: accountUrl) {
2023-03-13 13:53:36 +01:00
Label(NSLocalizedString("userProfile.title.share", comment: "Share"), systemImage: "square.and.arrow.up")
}
Divider()
}
Button {
Task {
await onMuteAccount(account: account)
}
} label: {
2023-03-27 14:52:53 +02:00
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")
} else {
2023-03-13 13:53:36 +01:00
Label(NSLocalizedString("userProfile.title.mute", comment: "Mute"), systemImage: "message.and.waveform")
}
}
Button {
Task {
await onBlockAccount(account: account)
}
} label: {
2023-03-27 14:52:53 +02:00
if self.relationship.blocking == true {
2023-03-13 13:53:36 +01:00
Label(NSLocalizedString("userProfile.title.unblock", comment: "Unblock"), systemImage: "hand.raised.fill")
} else {
2023-03-13 13:53:36 +01:00
Label(NSLocalizedString("userProfile.title.block", comment: "Block"), systemImage: "hand.raised")
}
}
2023-04-04 09:14:07 +02:00
Button {
self.routerPath.presentedSheet = .report(objectType: .user, objectId: self.accountId)
} label: {
Label(NSLocalizedString("userProfile.title.report", comment: "Report"), systemImage: "exclamationmark.triangle")
}
}, 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")
}
ShareLink(item: accountUrl) {
2023-03-13 13:53:36 +01:00
Label(NSLocalizedString("userProfile.title.share", comment: "Share"), systemImage: "square.and.arrow.up")
}
Divider()
}
2023-03-26 09:40:05 +02:00
NavigationLink(value: RouteurDestinations.instance) {
Label(NSLocalizedString("userProfile.title.instance", comment: "Instance information"), systemImage: "server.rack")
}
2023-03-27 14:52:53 +02:00
Divider()
2023-03-27 14:52:53 +02:00
NavigationLink(value: RouteurDestinations.accounts(listType: .blocks)) {
Label(NSLocalizedString("userProfile.title.blocks", comment: "Blocked accounts"), systemImage: "hand.raised.fill")
}
2023-03-27 14:52:53 +02:00
NavigationLink(value: RouteurDestinations.accounts(listType: .mutes)) {
Label(NSLocalizedString("userProfile.title.mutes", comment: "Muted accounts"), systemImage: "message.and.waveform.fill")
}
2023-03-26 09:40:05 +02:00
Divider()
2023-02-01 20:01:18 +01:00
NavigationLink(value: RouteurDestinations.favourites) {
2023-04-06 13:28:34 +02:00
Label(NSLocalizedString("userProfile.title.favourites", comment: "Favourites"), systemImage: "star")
}
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-03-26 08:21:22 +02:00
Divider()
2023-03-26 08:21:22 +02:00
NavigationLink(value: RouteurDestinations.editProfile) {
Label(NSLocalizedString("userProfile.title.edit", comment: "Edit profile"), systemImage: "pencil")
}
}, label: {
Image(systemName: "gear")
.tint(.mainTextColor)
})
.tint(.accentColor)
}
}
private func onMuteAccount(account: Account) async {
do {
2023-03-27 14:52:53 +02:00
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-03-27 14:52:53 +02:00
ToastrService.shared.showSuccess("userProfile.title.unmuted", imageSystemName: "message.and.waveform")
withAnimation(.linear) {
self.relationship.muting = relationship.muting
}
}
} else {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.mute(account: account.id) {
2023-03-27 14:52:53 +02:00
ToastrService.shared.showSuccess("userProfile.title.muted", imageSystemName: "message.and.waveform.fill")
withAnimation(.linear) {
self.relationship.muting = relationship.muting
}
}
}
} catch {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "userProfile.error.mute", showToastr: true)
}
}
private func onBlockAccount(account: Account) async {
do {
2023-03-27 14:52:53 +02:00
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-03-27 14:52:53 +02:00
ToastrService.shared.showSuccess("userProfile.title.unblocked", imageSystemName: "hand.raised")
withAnimation(.linear) {
self.relationship.blocking = relationship.blocking
}
}
} else {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.block(account: account.id) {
2023-03-27 14:52:53 +02:00
ToastrService.shared.showSuccess("userProfile.title.blocked", imageSystemName: "hand.raised.fill")
withAnimation(.linear) {
self.relationship.blocking = relationship.blocking
}
}
}
} catch {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "userProfile.error.block", showToastr: true)
}
}
2023-01-04 20:56:26 +01:00
}