Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf committed Jan 18, 2024
1 parent 70c1fe2 commit 900ffb6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
70 changes: 42 additions & 28 deletions Mlem/Models/Content/User/UserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ struct UserModel {
@Dependency(\.notifier) var notifier

@available(*, deprecated, message: "Use attributes of the UserModel directly instead.")
var person: APIPerson
var person: APIPerson!

// Ids
let userId: Int
let instanceId: Int
let matrixUserId: String?
var userId: Int!
var instanceId: Int!
var matrixUserId: String?

// Text
let name: String
let displayName: String
var name: String!
var displayName: String!
var bio: String?

// Images
let avatar: URL?
let banner: URL?
var avatar: URL?
var banner: URL?

// State
let banned: Bool
let local: Bool
let deleted: Bool
let isBot: Bool
var blocked: Bool
var banned: Bool!
var local: Bool!
var deleted: Bool!
var isBot: Bool!
var blocked: Bool!

// Dates
let creationDate: Date
let updatedDate: Date?
let banExpirationDate: Date?
var creationDate: Date!
var updatedDate: Date?
var banExpirationDate: Date?

// URLs
let profileUrl: URL
let sharedInboxUrl: URL?
var profileUrl: URL!
var sharedInboxUrl: URL?

// From APIPersonView
var isAdmin: Bool?
Expand All @@ -69,15 +69,28 @@ struct UserModel {
/// Creates a UserModel from an GetPersonDetailsResponse
/// - Parameter response: GetPersonDetailsResponse to create a UserModel representation of
init(from response: GetPersonDetailsResponse) {
self.init(from: response.personView)
self.moderatedCommunities = response.moderates.map { CommunityModel(from: $0.community) }
self.update(with: response)
}

/// Creates a UserModel from an APIPersonView
/// - Parameter apiPersonView: APIPersonView to create a UserModel representation of
init(from personView: APIPersonView) {
self.init(from: personView.person)

self.update(with: personView)
}

/// Creates a UserModel from an APIPerson. Note that using this initialiser nullifies count values, since
/// those are only accessable from APIPersonView.
/// - Parameter apiPerson: APIPerson to create a UserModel representation of
init(from person: APIPerson) {
update(with: person)
}

mutating func update(with response: GetPersonDetailsResponse) {
self.moderatedCommunities = response.moderates.map { CommunityModel(from: $0.community) }
self.update(with: response.personView)
}

mutating func update(with personView: APIPersonView) {
self.postCount = personView.counts.postCount
self.commentCount = personView.counts.commentCount

Expand All @@ -86,12 +99,11 @@ struct UserModel {
if (siteInformation.version ?? .infinity) > .init("0.19.0") {
self.isAdmin = personView.isAdmin
}

self.update(with: personView.person)
}

/// Creates a UserModel from an APIPerson. Note that using this initialiser nullifies count values, since
/// those are only accessable from APIPersonView.
/// - Parameter apiPerson: APIPerson to create a UserModel representation of
init(from person: APIPerson) {
mutating func update(with person: APIPerson) {
self.person = person

self.userId = person.id
Expand Down Expand Up @@ -121,7 +133,9 @@ struct UserModel {

// Annoyingly, PersonView doesn't include whether the user is blocked so we can't
// actually determine this without making extra requests...
self.blocked = false
if self.blocked == nil {
self.blocked = false
}
}

// Once we've done other model types we should stop this from relying on API types
Expand Down Expand Up @@ -179,7 +193,7 @@ struct UserModel {

var fullyQualifiedUsername: String? {
if let host = self.profileUrl.host() {
return "\(name)@\(host)"
return "\(name!)@\(host)"
}
return nil
}
Expand Down
25 changes: 2 additions & 23 deletions Mlem/Views/Tabs/Profile/Profile View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ struct ProfileView: View {
// appstorage
@Dependency(\.siteInformation) var siteInformation
@AppStorage("shouldShowUserHeaders") var shouldShowUserHeaders: Bool = true

@State var user: UserModel?

init() {
if let person = siteInformation.myUserInfo?.localUserView.person {
self._user = .init(wrappedValue: UserModel(from: person))
}
}

@StateObject private var profileTabNavigation: AnyNavigationPath<AppRoute> = .init()
@StateObject private var navigation: Navigation = .init()
Expand All @@ -30,8 +22,8 @@ struct ProfileView: View {
var body: some View {
ScrollViewReader { proxy in
NavigationStack(path: $profileTabNavigation.path) {
if let user {
UserView(user: user)
if let person = siteInformation.myUserInfo?.localUserView.person {
UserView(user: UserModel(from: person))
.handleLemmyViews()
.environmentObject(profileTabNavigation)
.tabBarNavigationEnabled(.profile, navigation)
Expand All @@ -56,12 +48,6 @@ struct ProfileView: View {
}
}
.sheet(isPresented: $isPresentingProfileEditor) {
if let person = siteInformation.myUserInfo?.localUserView.person {
self.user = UserModel(from: person)
} else {
self.user = nil
}
} content: {
NavigationStack {
ProfileSettingsView(showCloseButton: true)
}
Expand All @@ -75,13 +61,6 @@ struct ProfileView: View {
.environment(\.navigationPathWithRoutes, $profileTabNavigation.path)
.environment(\.scrollViewProxy, proxy)
.environment(\.navigation, navigation)
.onChange(of: siteInformation.myUserInfo?.localUserView.person) { newValue in
if let newValue {
self.user?.bio = newValue.bio
} else {
self.user = nil
}
}
}
}
}
7 changes: 7 additions & 0 deletions Mlem/Views/Tabs/Profile/UserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ struct UserView: View {
await tryReloadUser()
}
}
.onChange(of: siteInformation.myUserInfo?.localUserView.person) { newValue in
if isOwnProfile {
if let newValue {
self.user.update(with: newValue)
}
}
}
.hoistNavigation {
if navigationPath.isEmpty {
withAnimation {
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Views/Tabs/Search/Results/UserResultView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct UserResultView: View {

var title: String {
if user.blocked {
return "\(user.displayName) ∙ Blocked"
return "\(user.displayName!) ∙ Blocked"
} else {
return user.displayName
}
Expand Down

0 comments on commit 900ffb6

Please sign in to comment.