purple: consolidate UserBadgeInfo with Account

Rename get_account to fetch_account to make it clear that it is always a
call to the server.

Add get_maybe_cached_account method that checks cached before calling
fetch_account.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-01-30 17:30:12 -08:00
parent fe177bdb9e
commit f06b882139
7 changed files with 59 additions and 77 deletions

View File

@@ -16,8 +16,8 @@ struct EventProfileName: View {
@State var display_name: DisplayName?
@State var nip05: NIP05?
@State var donation: Int?
@State var purple_badge: DamusPurple.UserBadgeInfo?
@State var purple_account: DamusPurple.Account?
let size: EventViewKind
init(pubkey: Pubkey, damus: DamusState, size: EventViewKind = .normal) {
@@ -26,7 +26,7 @@ struct EventProfileName: View {
self.size = size
let donation = damus.ndb.lookup_profile(pubkey)?.map({ p in p?.profile?.damus_donation }).value
self._donation = State(wrappedValue: donation)
self.purple_badge = nil
self.purple_account = nil
}
var friend_type: FriendType? {
@@ -94,7 +94,7 @@ struct EventProfileName: View {
.frame(width: 14, height: 14)
}
SupporterBadge(percent: self.supporter_percentage(), purple_badge_info: self.purple_badge, style: .compact)
SupporterBadge(percent: self.supporter_percentage(), purple_account: self.purple_account, style: .compact)
}
.onReceive(handle_notify(.profile_updated)) { update in
if update.pubkey != pubkey {
@@ -119,13 +119,11 @@ struct EventProfileName: View {
donation = profile.damus_donation
}
}
.onAppear(perform: {
Task {
if damus_state.purple.enable_purple {
self.purple_badge = await damus_state.purple.profile_purple_badge_info(pubkey: pubkey)
}
.task {
if damus_state.purple.enable_purple {
self.purple_account = try? await damus_state.purple.get_maybe_cached_account(pubkey: pubkey)
}
})
}
}
}

View File

@@ -41,14 +41,14 @@ struct ProfileName: View {
@State var display_name: DisplayName?
@State var nip05: NIP05?
@State var donation: Int?
@State var purple_badge: DamusPurple.UserBadgeInfo?
@State var purple_account: DamusPurple.Account?
init(pubkey: Pubkey, prefix: String = "", damus: DamusState, show_nip5_domain: Bool = true) {
self.pubkey = pubkey
self.prefix = prefix
self.damus_state = damus
self.show_nip5_domain = show_nip5_domain
self.purple_badge = nil
self.purple_account = nil
}
var friend_type: FriendType? {
@@ -109,15 +109,15 @@ struct ProfileName: View {
.frame(width: 14, height: 14)
}
SupporterBadge(percent: supporter(profile: profile), purple_badge_info: self.purple_badge, style: .full)
SupporterBadge(percent: supporter(profile: profile), purple_account: self.purple_account, style: .compact)
}
.onAppear(perform: {
Task {
if damus_state.purple.enable_purple {
self.purple_badge = await damus_state.purple.profile_purple_badge_info(pubkey: pubkey)
}
.task {
if damus_state.purple.enable_purple {
self.purple_account = try? await damus_state.purple.get_maybe_cached_account(pubkey: pubkey)
}
})
}
.onReceive(handle_notify(.profile_updated)) { update in
if update.pubkey != pubkey {
return

View File

@@ -83,7 +83,7 @@ struct DamusPurpleAccountView: View {
SupporterBadge(
percent: nil,
purple_badge_info: DamusPurple.UserBadgeInfo.from(account: account),
purple_account: account,
style: .full
)
}

View File

@@ -32,7 +32,7 @@ struct DamusPurpleNewUserOnboardingView: View {
.ignoresSafeArea() // Necessary to avoid weird white edges
}
.task {
guard let account = try? await damus_state.purple.get_account(pubkey: damus_state.pubkey), account.active else {
guard let account = try? await damus_state.purple.fetch_account(pubkey: damus_state.pubkey), account.active else {
return
}
// Let's notify other views across SwiftUI to update our user's Purple status.

View File

@@ -117,7 +117,7 @@ struct DamusPurpleView: View {
func load_account() async {
do {
if let account = try await damus_state.purple.get_account(pubkey: damus_state.keypair.pubkey) {
if let account = try await damus_state.purple.fetch_account(pubkey: damus_state.keypair.pubkey) {
self.my_account_info_state = .loaded(account: account)
return
}
@@ -232,7 +232,8 @@ struct DamusPurpleView: View {
switch result {
case .success:
self.damus_state.purple.starred_profiles_cache[keypair.pubkey] = nil
// TODO (will): why do this here?
//self.damus_state.purple.starred_profiles_cache[keypair.pubkey] = nil
Task {
await self.damus_state.purple.send_receipt()
}