@@ -14,7 +14,7 @@ struct SessionEditor: View {
14
14
@Query private var items : [ DjangoFilesSession ]
15
15
16
16
@State private var showLoginSheet : Bool = false
17
- @State private var newSession : DjangoFilesSession ?
17
+ @State private var tempSession : DjangoFilesSession ?
18
18
19
19
let session : DjangoFilesSession ?
20
20
var onSessionCreated : ( ( DjangoFilesSession ) -> Void ) ?
@@ -25,32 +25,50 @@ struct SessionEditor: View {
25
25
26
26
@State private var showDuplicateAlert = false
27
27
28
- private func checkURLAuthAndSave( ) {
29
- if let session {
30
- session. url = url? . absoluteString ?? " "
31
- session. token = token
32
- session. auth = false
33
- } else {
34
- if items. contains ( where: { $0. url == url? . absoluteString } ) {
35
- showDuplicateAlert = true
36
- return
37
- }
38
- newSession = DjangoFilesSession ( )
39
- newSession!. url = url? . absoluteString ?? " "
40
- newSession!. token = token
41
- newSession!. auth = false
42
- modelContext. insert ( newSession!)
43
- showLoginSheet = true
44
- }
45
- }
46
-
47
28
@State private var url : URL ? = nil
48
29
@State private var token : String = " "
49
- @State private var badURL = false
50
- @State private var insecureURL = false
30
+ @State private var badURL : Bool = false
31
+ @State private var insecureURL : Bool = false
32
+ @State private var isCheckingServer : Bool = false
33
+ @State private var serverError : String ? = nil
51
34
52
35
@FocusState private var isURLFieldFocused : Bool
53
36
37
+ private func checkURLAuthAndSave( ) {
38
+ Task {
39
+ isCheckingServer = true
40
+ serverError = nil
41
+
42
+ // Create a temporary DFAPI instance to check auth methods
43
+ let api = DFAPI ( url: url!, token: " " )
44
+ if let authResponse = await api. getAuthMethods ( ) {
45
+ isCheckingServer = false
46
+
47
+ // Server is valid, proceed with login
48
+ if let session {
49
+ // For editing, update the URL and clear auth
50
+ session. url = url? . absoluteString ?? " "
51
+ session. token = token
52
+ session. auth = false
53
+ showLoginSheet = true
54
+ } else {
55
+ if items. contains ( where: { $0. url == url? . absoluteString } ) {
56
+ showDuplicateAlert = true
57
+ return
58
+ }
59
+ // Create temporary session but don't save it yet
60
+ tempSession = DjangoFilesSession ( )
61
+ tempSession!. url = url? . absoluteString ?? " "
62
+ tempSession!. token = token
63
+ tempSession!. auth = false
64
+ showLoginSheet = true
65
+ }
66
+ } else {
67
+ isCheckingServer = false
68
+ serverError = " Could not connect to server or server is not a Django Files instance "
69
+ }
70
+ }
71
+ }
54
72
55
73
var body : some View {
56
74
NavigationStack {
@@ -68,6 +86,7 @@ struct SessionEditor: View {
68
86
if temp? . scheme != nil && temp? . scheme != " " {
69
87
url = temp
70
88
insecureURL = ( url? . scheme? . lowercased ( ) ) == ( " http " )
89
+ serverError = nil
71
90
}
72
91
}
73
92
) , prompt: Text ( verbatim: " https://df.example.com " ) )
@@ -86,11 +105,23 @@ struct SessionEditor: View {
86
105
let warningMessage = " ⚠️ HTTPS strongly recommend. "
87
106
TextField ( " " , text: Binding (
88
107
get: { warningMessage } ,
89
- set: { _ in } // Prevents user from modifying the text
108
+ set: { _ in }
90
109
) )
91
- . disabled ( true ) // Prevents user input
110
+ . disabled ( true )
111
+ . foregroundColor ( . red)
112
+ }
113
+ if let error = serverError {
114
+ Text ( " ❌ " + error)
115
+ . disabled ( true )
92
116
. foregroundColor ( . red)
93
117
}
118
+ if isCheckingServer {
119
+ HStack {
120
+ ProgressView ( )
121
+ . progressViewStyle ( . circular)
122
+ Text ( " Checking server... " )
123
+ }
124
+ }
94
125
}
95
126
. padding ( . top, - 40 )
96
127
. toolbar {
@@ -117,6 +148,7 @@ struct SessionEditor: View {
117
148
Text ( " Save " )
118
149
}
119
150
. accessibilityIdentifier ( " serverSubmitButton " )
151
+ . disabled ( isCheckingServer)
120
152
. alert ( isPresented: $badURL) {
121
153
Alert ( title: Text ( " Invalid URL " ) , message: Text ( " Invalid URL format or scheme (http or https). \n Example: https://df.myserver.com " ) )
122
154
}
@@ -131,7 +163,6 @@ struct SessionEditor: View {
131
163
}
132
164
. onAppear {
133
165
if let session {
134
- // Edit the incoming animal.
135
166
url = URL ( string: session. url)
136
167
}
137
168
}
@@ -143,24 +174,26 @@ struct SessionEditor: View {
143
174
)
144
175
}
145
176
. sheet ( isPresented: $showLoginSheet, onDismiss: {
146
- if let newSession = newSession, newSession. auth {
147
- do {
148
- try modelContext. save ( )
149
- onSessionCreated ? ( newSession)
177
+ if let session = session {
178
+ if session. auth {
179
+ try ? modelContext. save ( )
150
180
dismiss ( )
151
- } catch {
152
- print ( " Error saving session: \( error) " )
153
181
}
182
+ } else if let tempSession = tempSession, tempSession. auth {
183
+ modelContext. insert ( tempSession)
184
+ try ? modelContext. save ( )
185
+ onSessionCreated ? ( tempSession)
186
+ dismiss ( )
154
187
}
155
188
} ) {
156
- if let newSession = newSession {
157
- LoginView ( selectedServer: newSession, onLoginSuccess: {
158
- newSession. auth = true
159
- } )
160
- } else if let session = session {
189
+ if let session = session {
161
190
LoginView ( selectedServer: session, onLoginSuccess: {
162
191
session. auth = true
163
192
} )
193
+ } else if let tempSession = tempSession {
194
+ LoginView ( selectedServer: tempSession, onLoginSuccess: {
195
+ tempSession. auth = true
196
+ } )
164
197
}
165
198
}
166
199
}
0 commit comments