Skip to content

Commit ec7b646

Browse files
committed
Update deprecated UN initializer.
1 parent 62c0746 commit ec7b646

File tree

2 files changed

+44
-53
lines changed

2 files changed

+44
-53
lines changed

Shared/Samples/Run valve isolation trace/RunValveIsolationTraceView.Model.swift

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ extension RunValveIsolationTraceView {
2020
/// The view model for this sample.
2121
@MainActor
2222
class Model: ObservableObject {
23-
/// A map with a 'night time' street map style.
24-
let map = Map(basemapStyle: .arcGISStreetsNight)
23+
/// A web map with a utility network used to run the isolation trace.
24+
let map = Map(item: PortalItem.napervilleGasNetwork())
2525

2626
/// The utility network for this sample.
27-
private let utilityNetwork = UtilityNetwork(url: .featureServiceURL)
28-
29-
/// The service geodatabase used to create the feature layer.
30-
private let serviceGeodatabase = ServiceGeodatabase(url: .featureServiceURL)
27+
private var utilityNetwork: UtilityNetwork {
28+
map.utilityNetworks.first!
29+
}
3130

32-
/// The service geodatabase feature layers.
33-
private var layers: [FeatureLayer] = []
31+
/// The feature layers of the web map.
32+
private var layers: [FeatureLayer] {
33+
map.operationalLayers.compactMap { $0 as? FeatureLayer }
34+
}
3435

3536
/// The last element that was added to the list of filter barriers.
3637
///
@@ -40,7 +41,7 @@ extension RunValveIsolationTraceView {
4041
@Published private(set) var lastAddedElement: UtilityElement?
4142

4243
/// The current tracing related activity.
43-
@Published private(set) var tracingActivity: TracingActivity? = .loadingServiceGeodatabase
44+
@Published private(set) var tracingActivity: TracingActivity? = .loadingNetwork
4445

4546
/// The base trace parameters.
4647
private let traceParameters = UtilityTraceParameters(traceType: .isolation, startingLocations: [])
@@ -67,7 +68,7 @@ extension RunValveIsolationTraceView {
6768
@Published var terminalSelectorIsOpen = false
6869

6970
/// The status text to display to the user.
70-
@Published var statusText: String = "Loading Utility Network…"
71+
@Published private(set) var statusText = "Loading Utility Network…"
7172

7273
/// The filter barrier identifier.
7374
private static let filterBarrierIdentifier = "filter barrier"
@@ -94,40 +95,27 @@ extension RunValveIsolationTraceView {
9495
}()
9596

9697
init() {
97-
map.addUtilityNetwork(utilityNetwork)
98+
// Updates the URL session challenge handler to use the
99+
// specified credentials and tokens for any challenges.
100+
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = ChallengeHandler()
98101
}
99102

100103
deinit {
101-
ArcGISEnvironment.authenticationManager.arcGISCredentialStore.removeAll()
104+
// Resets the URL session challenge handler to use default handling.
105+
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = nil
102106
}
103107

104108
/// Performs important tasks including adding credentials, loading and adding operational layers.
105109
func setup() async {
106110
do {
107-
try await ArcGISEnvironment.authenticationManager.arcGISCredentialStore.add(.publicSample)
108-
try await loadServiceGeodatabase()
111+
// Load the map to get the utility network.
112+
try await map.load()
109113
try await loadUtilityNetwork()
110114
} catch {
111115
statusText = error.localizedDescription
112116
}
113117
}
114118

115-
/// Loads the service geodatabase and initialize the layers.
116-
private func loadServiceGeodatabase() async throws {
117-
tracingActivity = .loadingServiceGeodatabase
118-
defer { tracingActivity = nil }
119-
try await serviceGeodatabase.load()
120-
121-
// The gas device layer and gas line layer are created from the service geodatabase.
122-
if let gasDeviceLayerTable = serviceGeodatabase.table(withLayerID: 0),
123-
let gasLineLayerTable = serviceGeodatabase.table(withLayerID: 3) {
124-
let layers = [gasLineLayerTable, gasDeviceLayerTable].map(FeatureLayer.init)
125-
// Add the utility network feature layers to the map for display.
126-
map.addOperationalLayers(layers)
127-
self.layers = layers
128-
}
129-
}
130-
131119
/// Loads the utility network.
132120
private func loadUtilityNetwork() async throws {
133121
tracingActivity = .loadingNetwork
@@ -246,9 +234,9 @@ extension RunValveIsolationTraceView {
246234
}
247235
do {
248236
for (networkName, elements) in groups {
249-
guard let layer = map.operationalLayers.first(
250-
where: { ($0 as? FeatureLayer)?.featureTable?.tableName == networkName }
251-
) as? FeatureLayer else { continue }
237+
guard let layer = layers.first(
238+
where: { $0.featureTable?.tableName == networkName }
239+
) else { continue }
252240
let features = try await utilityNetwork.features(for: elements)
253241
layer.selectFeatures(features)
254242
}
@@ -361,31 +349,36 @@ extension RunValveIsolationTraceView {
361349
extension RunValveIsolationTraceView.Model {
362350
/// The different states of a utility network trace.
363351
enum TracingActivity: CaseIterable {
364-
case loadingServiceGeodatabase,
365-
loadingNetwork,
352+
case loadingNetwork,
366353
startingLocation,
367354
runningTrace
368355
}
369356
}
370357

371-
private extension ArcGISCredential {
372-
/// The public credentials for the data in this sample.
373-
/// - Note: Never hardcode login information in a production application. This is done solely
374-
/// for the sake of the sample.
375-
static var publicSample: ArcGISCredential {
376-
get async throws {
377-
try await TokenCredential.credential(
378-
for: .featureServiceURL,
379-
username: "viewer01",
380-
password: "I68VGU^nMurF"
381-
)
382-
}
358+
/// The authentication model used to handle challenges and credentials.
359+
private struct ChallengeHandler: ArcGISAuthenticationChallengeHandler {
360+
func handleArcGISAuthenticationChallenge(
361+
_ challenge: ArcGISAuthenticationChallenge
362+
) async throws -> ArcGISAuthenticationChallenge.Disposition {
363+
// NOTE: Never hardcode login information in a production application.
364+
// This is done solely for the sake of the sample.
365+
return .continueWithCredential(
366+
// Credentials for sample server 7 services.
367+
try await TokenCredential.credential(for: challenge, username: "viewer01", password: "I68VGU^nMurF")
368+
)
383369
}
384370
}
385371

386-
private extension URL {
387-
/// The URL to the feature service for running the isolation trace.
388-
static var featureServiceURL: URL {
389-
URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleGas/FeatureServer")!
372+
private extension PortalItem {
373+
/// A web map portal item for the Naperville Gas Device and Line layers.
374+
static func napervilleGasNetwork() -> PortalItem {
375+
PortalItem(
376+
// Sample server 7 authentication required.
377+
portal: Portal(
378+
url: URL(string: "https://sampleserver7.arcgisonline.com/portal")!,
379+
connection: .authenticated
380+
),
381+
id: .init("f439b4724bb54ac088a2c21eaf70da7b")!
382+
)
390383
}
391384
}

Shared/Samples/Run valve isolation trace/RunValveIsolationTraceView.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ struct RunValveIsolationTraceView: View {
6868
isConfigurationPresented.toggle()
6969
}
7070
.disabled(model.tracingActivity == .runningTrace ||
71-
model.tracingActivity == .loadingServiceGeodatabase ||
7271
model.tracingActivity == .loadingNetwork)
7372
Spacer()
7473
Button("Trace") {
@@ -177,7 +176,6 @@ private extension RunValveIsolationTraceView.Model.TracingActivity {
177176
/// A human-readable label for the tracing activity.
178177
var label: String {
179178
switch self {
180-
case .loadingServiceGeodatabase: return "Loading service geodatabase…"
181179
case .loadingNetwork: return "Loading utility network…"
182180
case .startingLocation: return "Getting starting location feature…"
183181
case .runningTrace: return "Running isolation trace…"

0 commit comments

Comments
 (0)