@@ -170,7 +170,7 @@ class CertificateFileManager {
170170struct CertificateView : View {
171171 @State private var customCertificates : [ CustomCertificate ] = [ ]
172172 @State private var showAddCertificateSheet = false
173- @State private var editingFolder : String ? = nil
173+ @State private var editingCertificate : CustomCertificate ? = nil
174174 @State private var selectedCert : String ? = nil
175175 @State private var showingDeleteAlert = false
176176 @State private var certToDelete : CustomCertificate ?
@@ -211,7 +211,7 @@ struct CertificateView: View {
211211
212212 HStack {
213213 Button ( action: {
214- editingFolder = cert. folderName
214+ editingCertificate = cert
215215 showAddCertificateSheet = true
216216 } ) {
217217 Image ( systemName: " pencil " )
@@ -252,7 +252,7 @@ struct CertificateView: View {
252252 . toolbar {
253253 ToolbarItem ( placement: . navigationBarTrailing) {
254254 Button ( action: {
255- editingFolder = nil
255+ editingCertificate = nil
256256 showAddCertificateSheet = true
257257 } ) {
258258 Image ( systemName: " plus " )
@@ -261,12 +261,17 @@ struct CertificateView: View {
261261 }
262262 . sheet ( isPresented: $showAddCertificateSheet, onDismiss: {
263263 customCertificates = CertificateFileManager . shared. loadCertificates ( )
264- editingFolder = nil
264+ editingCertificate = nil
265265 // Ensure at least one certificate is selected
266266 ensureSelection ( )
267267 } ) {
268- AddCertificateView ( editingFolder: editingFolder)
269- . presentationDetents ( [ . large] )
268+ if let editingCertificate = editingCertificate {
269+ AddCertificateView ( editingCertificate: editingCertificate)
270+ . presentationDetents ( [ . large] )
271+ } else {
272+ AddCertificateView ( )
273+ . presentationDetents ( [ . large] )
274+ }
270275 }
271276 . alert ( " Delete Certificate? " , isPresented: $showingDeleteAlert) {
272277 Button ( " Delete " , role: . destructive) {
@@ -320,17 +325,18 @@ struct CertificateView: View {
320325
321326struct AddCertificateView : View {
322327 @Environment ( \. dismiss) private var dismiss
323- let editingFolder : String ?
328+ let editingCertificate : CustomCertificate ?
324329
325330 @State private var p12File : CertificateFileItem ?
326331 @State private var provFile : CertificateFileItem ?
327332 @State private var password = " "
328333 @State private var activeSheet : CertificatePickerKind ?
329334 @State private var isChecking = false
330335 @State private var errorMessage = " "
336+ @State private var hasLoadedForEdit = false
331337
332- init ( editingFolder : String ? = nil ) {
333- self . editingFolder = editingFolder
338+ init ( editingCertificate : CustomCertificate ? = nil ) {
339+ self . editingCertificate = editingCertificate
334340 }
335341
336342 var body : some View {
@@ -382,7 +388,7 @@ struct AddCertificateView: View {
382388 . font ( . subheadline)
383389 }
384390 }
385- . navigationTitle ( editingFolder != nil ? " Edit Certificate " : " New Certificate " )
391+ . navigationTitle ( editingCertificate != nil ? " Edit Certificate " : " New Certificate " )
386392 . navigationBarTitleDisplayMode ( . inline)
387393 . toolbar {
388394 ToolbarItem ( placement: . navigationBarLeading) {
@@ -417,15 +423,17 @@ struct AddCertificateView: View {
417423 errorMessage = " "
418424 }
419425 . onAppear {
420- if let folder = editingFolder {
421- loadForEdit ( folder: folder)
426+ // Load data for editing only once and only if we haven't already loaded it
427+ if let cert = editingCertificate, !hasLoadedForEdit {
428+ loadForEdit ( cert: cert)
429+ hasLoadedForEdit = true
422430 }
423431 }
424432 }
425433 }
426434
427- private func loadForEdit( folder : String ) {
428- let certFolder = CertificateFileManager . shared. certificatesDirectory. appendingPathComponent ( folder )
435+ private func loadForEdit( cert : CustomCertificate ) {
436+ let certFolder = CertificateFileManager . shared. certificatesDirectory. appendingPathComponent ( cert . folderName )
429437 let p12URL = certFolder. appendingPathComponent ( " certificate.p12 " )
430438 let provURL = certFolder. appendingPathComponent ( " profile.mobileprovision " )
431439 let passwordURL = certFolder. appendingPathComponent ( " password.txt " )
@@ -448,7 +456,7 @@ struct AddCertificateView: View {
448456 do {
449457 var p12Data : Data
450458 var provData : Data
451- if editingFolder != nil {
459+ if editingCertificate != nil {
452460 // For edit, files are in app container, no security scope needed
453461 p12Data = try Data ( contentsOf: p12URL)
454462 provData = try Data ( contentsOf: provURL)
@@ -475,7 +483,7 @@ struct AddCertificateView: View {
475483 case . success( . success) :
476484 let displayName = CertificatesManager . getCertificateName ( p12Data: p12Data, password: password) ?? " Custom Certificate "
477485
478- if let folder = editingFolder {
486+ if let folder = editingCertificate ? . folderName {
479487 try CertificateFileManager . shared. updateCertificate ( folderName: folder, p12Data: p12Data, provData: provData, password: password, displayName: displayName)
480488 } else {
481489 _ = try CertificateFileManager . shared. saveCertificate ( p12Data: p12Data, provData: provData, password: password, displayName: displayName)
0 commit comments