Impressia/Vernissage/Views/UserProfileView/UserProfileView.swift

328 lines
14 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-04-07 14:20:12 +02:00
import ClientKit
2023-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-01-04 20:56:26 +01:00
2023-10-19 13:24:02 +02:00
@MainActor
2023-01-04 20:56:26 +01:00
struct UserProfileView: View {
2023-10-19 13:24:02 +02:00
@Environment(ApplicationState.self) var applicationState
@Environment(Client.self) var client
@Environment(RouterPath.self) var routerPath
2023-10-20 07:45:18 +02:00
@Environment(\.modelContext) private var modelContext
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-10-19 13:24:02 +02:00
@State 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-10-10 10:10:24 +02:00
@State private var boostsDisabled = false
2023-05-25 17:33:04 +02:00
// Gallery parameters.
@State private var imageColumns = 3
@State private var containerWidth: Double = UIScreen.main.bounds.width
@State private var containerHeight: Double = UIScreen.main.bounds.height
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 {
2023-10-10 13:30:53 +02:00
UserProfileHeaderView(account: account, relationship: relationship, boostsDisabled: $boostsDisabled)
.id(self.viewId)
2023-04-25 15:47:21 +02:00
if self.applicationState.account?.id == account.id || self.relationship.haveAccessToPhotos(account: account) {
2023-05-25 17:33:04 +02:00
UserProfileStatusesView(accountId: account.id, imageColumns: $imageColumns, containerWidth: $containerWidth, containerHeight: $containerHeight)
2023-04-25 15:47:21 +02:00
} else {
UserProfilePrivateAccountView()
}
}
2023-05-25 17:33:04 +02:00
.gallery { galleryProperties in
self.imageColumns = galleryProperties.imageColumns
self.containerWidth = galleryProperties.containerWidth
self.containerHeight = galleryProperties.containerHeight
}
.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())
}
2023-10-10 10:10:24 +02:00
if let signedInAccountId = self.applicationState.account?.id {
2023-10-20 07:45:18 +02:00
self.boostsDisabled = AccountRelationshipHandler.shared.isBoostedStatusesMuted(for: signedInAccountId, relation: self.accountId, modelContext: modelContext)
2023-10-10 10:10:24 +02:00
}
2023-03-27 14:52:53 +02:00
self.account = accountFromApi
withAnimation {
self.state = .loaded
}
2023-02-01 18:40:28 +01:00
} 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-10-10 10:10:24 +02:00
Button {
Task {
if let signedInAccoountId = self.applicationState.account?.id {
self.boostsDisabled.toggle()
AccountRelationshipHandler.shared.setBoostedStatusesMuted(for: signedInAccoountId,
relation: self.accountId,
2023-10-20 07:45:18 +02:00
boostedStatusesMuted: self.boostsDisabled,
modelContext: modelContext)
2023-10-10 10:10:24 +02:00
}
}
} label: {
if self.boostsDisabled == true {
Label(NSLocalizedString("userProfile.title.enableBoosts", comment: "Enable boosts"), image: "custom.rocket.fill")
} else {
Label(NSLocalizedString("userProfile.title.disableBoosts", comment: "Disable boosts"), image: "custom.rocket")
}
}
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-04-25 15:47:21 +02:00
self.accountsMenuView(account: account)
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-09-26 20:01:58 +02:00
NavigationLink(value: RouteurDestinations.hashtags(listType: .followed)) {
Label(NSLocalizedString("userProfile.title.followedTags", comment: "Followed tags"), systemImage: "number.square")
}
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)
}
}
2023-04-25 15:47:21 +02:00
@ViewBuilder
private func accountsMenuView(account: Account) -> some View {
NavigationLink(value: RouteurDestinations.accounts(listType: .blocks)) {
Label(NSLocalizedString("userProfile.title.blocks", comment: "Blocked accounts"), systemImage: "hand.raised.fill")
}
NavigationLink(value: RouteurDestinations.accounts(listType: .mutes)) {
Label(NSLocalizedString("userProfile.title.mutes", comment: "Muted accounts"), systemImage: "message.and.waveform.fill")
}
2023-10-23 08:47:36 +02:00
NavigationLink(value: RouteurDestinations.accounts(listType: .disabledBoosts)) {
Label(NSLocalizedString("userProfile.title.disabledBoosts", comment: "Disabled boosts"), image: "custom.rocket.fill")
}
2023-04-25 15:47:21 +02:00
if account.locked {
NavigationLink(value: RouteurDestinations.followRequests) {
Label(NSLocalizedString("userProfile.title.followRequests", comment: "FollowRequests"), systemImage: "person.crop.circle.badge.checkmark")
}
}
}
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-10-18 11:14:56 +02:00
ErrorService.shared.handle(error, message: "userProfile.error.muting", 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
}