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-04-01 12:10:59 +02: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
|
|
|
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-03-07 16:45:44 +01:00
|
|
|
@Environment(\.dismiss) private var dismiss
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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()
|
2023-04-01 12:10:59 +02:00
|
|
|
@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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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:
|
2023-01-09 10:56:20 +01:00
|
|
|
if let account = self.account {
|
2023-03-29 09:35:01 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-29 09:35:01 +02:00
|
|
|
private func accountView(account: Account) -> some View {
|
|
|
|
ScrollView {
|
2023-10-10 13:30:53 +02:00
|
|
|
UserProfileHeaderView(account: account, relationship: relationship, boostsDisabled: $boostsDisabled)
|
2023-03-29 09:35:01 +02:00
|
|
|
.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-03-29 09:35:01 +02:00
|
|
|
}
|
2023-05-25 17:33:04 +02:00
|
|
|
.gallery { galleryProperties in
|
|
|
|
self.imageColumns = galleryProperties.imageColumns
|
|
|
|
self.containerWidth = galleryProperties.containerWidth
|
|
|
|
self.containerHeight = galleryProperties.containerHeight
|
|
|
|
}
|
2023-03-29 09:35:01 +02:00
|
|
|
.onAppear {
|
|
|
|
if let updatedProfile = self.applicationState.updatedProfile {
|
|
|
|
self.account = nil
|
|
|
|
self.account = updatedProfile
|
|
|
|
self.applicationState.updatedProfile = nil
|
|
|
|
self.viewId = UUID().uuidString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02: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()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-07 16:45:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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 {
|
|
|
|
self.boostsDisabled = AccountRelationshipHandler.shared.isBoostedStatusesMuted(for: signedInAccountId, relation: self.accountId)
|
|
|
|
}
|
2023-03-27 14:52:53 +02:00
|
|
|
|
|
|
|
self.account = accountFromApi
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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-12 13:59:13 +01:00
|
|
|
}
|
2023-01-09 08:29:55 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-23 09:10:58 +01:00
|
|
|
@ToolbarContentBuilder
|
|
|
|
private func getTrailingAccountToolbar(account: Account) -> some ToolbarContent {
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
2023-04-01 12:10:59 +02:00
|
|
|
Menu(content: {
|
2023-01-23 09:10:58 +01:00
|
|
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-23 09:10:58 +01:00
|
|
|
Divider()
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-23 09:10:58 +01:00
|
|
|
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")
|
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
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-23 09:10:58 +01:00
|
|
|
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")
|
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
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
boostedStatusesMuted: self.boostsDisabled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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-01 12:10:59 +02:00
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-04-01 12:10:59 +02:00
|
|
|
Menu(content: {
|
2023-01-23 09:10:58 +01:00
|
|
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-23 09:10:58 +01:00
|
|
|
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-01 12:10:59 +02:00
|
|
|
|
2023-04-25 15:47:21 +02:00
|
|
|
self.accountsMenuView(account: account)
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-26 09:40:05 +02:00
|
|
|
Divider()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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-01-23 09:10:58 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02: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-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-04-01 12:10:59 +02:00
|
|
|
|
2023-03-26 08:21:22 +02:00
|
|
|
Divider()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-26 08:21:22 +02:00
|
|
|
NavigationLink(value: RouteurDestinations.editProfile) {
|
|
|
|
Label(NSLocalizedString("userProfile.title.edit", comment: "Edit profile"), systemImage: "pencil")
|
2023-04-01 12:10:59 +02:00
|
|
|
}
|
2023-01-23 09:10:58 +01:00
|
|
|
}, label: {
|
|
|
|
Image(systemName: "gear")
|
|
|
|
.tint(.mainTextColor)
|
|
|
|
})
|
|
|
|
.tint(.accentColor)
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.locked {
|
|
|
|
NavigationLink(value: RouteurDestinations.followRequests) {
|
|
|
|
Label(NSLocalizedString("userProfile.title.followRequests", comment: "FollowRequests"), systemImage: "person.crop.circle.badge.checkmark")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 09:10:58 +01:00
|
|
|
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
|
|
|
|
}
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
} 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
|
|
|
|
}
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-23 09:10:58 +01:00
|
|
|
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
|
|
|
|
}
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
} 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
|
|
|
|
}
|
2023-01-23 09:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|