Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DockhandAPI/Sources/DockhandAPI/ClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public enum DockhandAPIClientFactory {
public static func makeClient(
baseURL: URL,
token: String?,
customHeaders: [DockhandCustomHeader] = []
customHeaders: [DockhandCustomHeader] = [],
session: URLSession? = nil
) -> Client {
let normalizedToken = token?.trimmingCharacters(in: .whitespacesAndNewlines)
let transport = URLSessionTransport(
configuration: URLSessionTransport.Configuration(session: DockhandHTTPSession.api)
configuration: URLSessionTransport.Configuration(session: session ?? DockhandHTTPSession.api)
)

let trimmedPath = baseURL.path.hasSuffix("/") ? String(baseURL.path.dropLast()) : baseURL.path
Expand Down
8 changes: 4 additions & 4 deletions DockhandMobile.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@
"ASSETCATALOG_COMPILER_APPICON_NAME" = "dockhand_glass";
"ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS" = NO;
"CODE_SIGN_IDENTITY" = "iPhone Developer";
"CURRENT_PROJECT_VERSION" = 17;
"CURRENT_PROJECT_VERSION" = 18;
"DEVELOPMENT_TEAM" = 8DA225EN79;
"GENERATE_INFOPLIST_FILE" = NO;
"INFOPLIST_FILE" = "DockhandMobile/Info.plist";
Expand All @@ -523,7 +523,7 @@
"@executable_path/Frameworks",
);
"LOCALIZED_STRING_SWIFTUI_SUPPORT" = YES;
"MARKETING_VERSION" = "1.6.0";
"MARKETING_VERSION" = "1.7.0";
"PRODUCT_BUNDLE_IDENTIFIER" = "garanda.dockhand.mobile";
SDKROOT = iphoneos;
"SWIFT_EMIT_LOC_STRINGS" = YES;
Expand All @@ -538,7 +538,7 @@
"ASSETCATALOG_COMPILER_APPICON_NAME" = "dockhand_glass";
"ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS" = NO;
"CODE_SIGN_IDENTITY" = "iPhone Developer";
"CURRENT_PROJECT_VERSION" = 17;
"CURRENT_PROJECT_VERSION" = 18;
"DEVELOPMENT_TEAM" = 8DA225EN79;
"GENERATE_INFOPLIST_FILE" = NO;
"INFOPLIST_FILE" = "DockhandMobile/Info.plist";
Expand All @@ -547,7 +547,7 @@
"@executable_path/Frameworks",
);
"LOCALIZED_STRING_SWIFTUI_SUPPORT" = YES;
"MARKETING_VERSION" = "1.6.0";
"MARKETING_VERSION" = "1.7.0";
"PRODUCT_BUNDLE_IDENTIFIER" = "garanda.dockhand.mobile";
SDKROOT = iphoneos;
"SWIFT_EMIT_LOC_STRINGS" = YES;
Expand Down
16 changes: 14 additions & 2 deletions DockhandMobile/App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class AppModel {
var environments: [Components.Schemas.Environment] = []
var selectedEnvironmentID: Int?
var isLoadingEnvironments = false
private var environmentsLoadToken = UUID()
var environmentError: String?
var lastHealthStatus: String?
private(set) var dashboardRefreshRevision = 0
Expand Down Expand Up @@ -105,6 +106,12 @@ final class AppModel {
"\(selectedProfileID ?? "none"):\(selectedEnvironmentID ?? -1)"
}

/// A remembered environment ID alone is not enough: list tasks must restart
/// when the new server's environment actually becomes available.
var connectionLoadID: String {
"\(connectionScopeID):\(selectedEnvironment?.id ?? -1)"
}

var connectionScope: DockhandConnectionScope {
DockhandConnectionScope(profileID: selectedProfileID, environmentID: selectedEnvironmentID)
}
Expand Down Expand Up @@ -196,7 +203,10 @@ final class AppModel {
}

func refreshEnvironments(forceEnvironmentReset: Bool = false) async {
let loadToken = UUID()
environmentsLoadToken = loadToken
guard selectedProfile != nil else {
isLoadingEnvironments = false
environmentError = nil
environments = []
lastHealthStatus = nil
Expand All @@ -205,6 +215,7 @@ final class AppModel {

guard let baseURL = normalizedBaseURL,
let profileID = selectedProfile?.id else {
isLoadingEnvironments = false
environmentError = String(localized: "Invalid Dockhand URL")
environments = []
lastHealthStatus = nil
Expand All @@ -215,7 +226,7 @@ final class AppModel {
environmentError = nil

defer {
isLoadingEnvironments = false
if loadToken == environmentsLoadToken { isLoadingEnvironments = false }
}

do {
Expand All @@ -235,6 +246,7 @@ final class AppModel {
throw DockhandConnectionStageError(stage: .environments, underlying: error)
}
}
guard loadToken == environmentsLoadToken, selectedProfile?.id == profileID, !Task.isCancelled else { return }
lastHealthStatus = loaded.0
let loadedEnvironments = loaded.1
environments = loadedEnvironments.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }
Expand All @@ -254,7 +266,7 @@ final class AppModel {

PreferencesStore.setSelectedEnvironmentID(selectedEnvironmentID, for: profileID)
} catch {
guard !error.isDockhandCancellation else { return }
guard loadToken == environmentsLoadToken, selectedProfile?.id == profileID, !error.isDockhandCancellation else { return }
// Keep the last known environments so screens recover on their own
// once the server is reachable again.
environmentError = error.dockhandUserFacingMessage
Expand Down
49 changes: 29 additions & 20 deletions DockhandMobile/Features/Containers/ContainersView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,47 @@ import SwiftUI
@Observable
final class ContainersStore {
var containers: [Components.Schemas.Container] = []
private var listLoadToken = UUID()
private var loadedListScopeID: String?
var isLoading = false
var error: String?
var actionMessage: String?
var activeActionID: String?
var pendingUpdates: [String: PendingContainerUpdate] = [:]
var updatingContainerID: String?

func load(appModel: AppModel) async {
func load(appModel: AppModel, service: DockhandService? = nil) async {
let scope = appModel.connectionLoadID
let token = UUID()
listLoadToken = token
isLoading = false
if loadedListScopeID != scope {
containers = []
pendingUpdates = [:]
loadedListScopeID = scope
}
error = nil
guard let baseURL = appModel.normalizedBaseURL,
let environmentID = appModel.selectedEnvironment?.id else {
containers = []
pendingUpdates = [:]
return
}

isLoading = true
error = nil
defer { isLoading = false }

defer { if listLoadToken == token { isLoading = false } }
do {
let service = appModel.service(baseURL: baseURL)
// Update badges are optional; a failed pending-updates request must not hide the list.
let service = service ?? appModel.service(baseURL: baseURL)
async let loadedUpdates = try? service.fetchPendingContainerUpdates(environmentID: environmentID)
containers = try await service.fetchContainers(environmentID: environmentID)
.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }
let updates = PendingContainerUpdate.actionable(
await loadedUpdates ?? [],
liveContainerIDs: Set(containers.map(\.id))
)
let loaded = try await service.fetchContainers(environmentID: environmentID)
guard listLoadToken == token, scope == appModel.connectionLoadID, !Task.isCancelled else { return }
containers = loaded.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }
// Optional update badges must not delay the main list.
let updates = PendingContainerUpdate.actionable(await loadedUpdates ?? [], liveContainerIDs: Set(loaded.map(\.id)))
guard listLoadToken == token, scope == appModel.connectionLoadID, !Task.isCancelled else { return }
pendingUpdates = Dictionary(updates.map { ($0.containerID, $0) }, uniquingKeysWith: { first, _ in first })
} catch {
guard !error.isDockhandCancellation else { return }
guard listLoadToken == token, scope == appModel.connectionLoadID, !error.isDockhandCancellation else { return }
self.error = error.dockhandUserFacingMessage
}
}
Expand Down Expand Up @@ -169,7 +179,11 @@ struct ContainersView: View {
}

Section("Containers") {
if filteredContainers.isEmpty {
if appModel.isLoadingEnvironments && appModel.selectedEnvironment == nil {
ProgressView("Loading environments…")
} else if store.isLoading && store.containers.isEmpty {
ProgressView("Loading…")
} else if filteredContainers.isEmpty {
Text(
!searchText.trimmed.isEmpty
? String(format: String(localized: "No results for “%@”"), locale: Locale.current, searchText.trimmed)
Expand Down Expand Up @@ -230,12 +244,7 @@ struct ContainersView: View {
}
}
}
.overlay {
if store.isLoading && store.containers.isEmpty {
ProgressView()
}
}
.task(id: "\(appModel.connectionScopeID):\(appModel.dashboardRefreshRevision)") {
.task(id: "\(appModel.connectionLoadID):\(appModel.dashboardRefreshRevision)") {
await store.load(appModel: appModel)
}
.onChange(of: filterRequestRevision, initial: true) {
Expand Down
Loading
Loading