mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
fix(healthchat): refresh the account status whenever settings appears
The banner kept telling the user to complete a profile they had just completed, and nothing was fetched until the screen was actually shown.
This commit is contained in:
@@ -9,27 +9,37 @@ import Foundation
|
||||
import Observation
|
||||
|
||||
/// Loads the own account's status for the banners that advertise it.
|
||||
/// Nothing is fetched until ``load()`` is called, so the settings screen decides when.
|
||||
@Observable
|
||||
final class HealthChatAccountStatusLoader {
|
||||
private(set) var status: HealthChatAccountStatus = .unknown
|
||||
|
||||
/// The load kicked off on creation — await this in tests before asserting.
|
||||
@ObservationIgnored private(set) var loadingTask: Task<Void, Never>?
|
||||
@ObservationIgnored private let apiService: HealthChatAPIServiceProtocol?
|
||||
|
||||
/// The load in flight — await this in tests before asserting.
|
||||
@ObservationIgnored @CancellableTask private(set) var loadingTask: Task<Void, Never>?
|
||||
|
||||
init(apiService: HealthChatAPIServiceProtocol) {
|
||||
loadingTask = Task { await load(from: apiService) }
|
||||
self.apiService = apiService
|
||||
}
|
||||
|
||||
/// Creates a loader with a fixed status, for previews.
|
||||
init(status: HealthChatAccountStatus) {
|
||||
self.status = status
|
||||
apiService = nil
|
||||
}
|
||||
|
||||
/// Fetches the status again, replacing a load still in flight.
|
||||
func load() {
|
||||
guard let apiService else { return }
|
||||
loadingTask = Task { await load(from: apiService) }
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func load(from apiService: HealthChatAPIServiceProtocol) async {
|
||||
// Every failure below leaves the status unknown: a banner must not report an error.
|
||||
guard case let .success(profile) = await apiService.ownProfile() else {
|
||||
// Every failure below leaves the status untouched: a banner must not report an error.
|
||||
guard case let .success(profile) = await apiService.ownProfile(), !Task.isCancelled else {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -38,7 +48,7 @@ final class HealthChatAccountStatusLoader {
|
||||
return
|
||||
}
|
||||
|
||||
guard case let .success(completeness) = await apiService.profileCompleteness() else {
|
||||
guard case let .success(completeness) = await apiService.profileCompleteness(), !Task.isCancelled else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user