Nextcloud login flow v2, Network code rewrite

This commit is contained in:
Vicnet
2023-09-20 13:25:25 +02:00
parent 0f16b164d6
commit 26dd5c34ff
16 changed files with 504 additions and 364 deletions

View File

@@ -59,7 +59,7 @@ struct RecipeBookView: View {
for recipe in recipes {
Task {
let _ = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id)
let _ = await viewModel.loadImage(recipeId: recipe.recipe_id, full: true)
let _ = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false)
}
}
}

View File

@@ -8,9 +8,17 @@
import SwiftUI
struct MainView: View {
@StateObject var viewModel = MainViewModel()
@StateObject var userSettings: UserSettings
@ObservedObject var viewModel: MainViewModel
@ObservedObject var userSettings: UserSettings
var columns: [GridItem] = [GridItem(.adaptive(minimum: 150), spacing: 0)]
init(userSettings: UserSettings, viewModel: MainViewModel) {
self.userSettings = userSettings
self.viewModel = viewModel
self.viewModel.apiInterface = APIInterface(userSettings: userSettings)
}
var body: some View {
NavigationView {
ScrollView(.vertical, showsIndicators: false) {
@@ -60,11 +68,6 @@ struct MainView: View {
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView(userSettings: UserSettings())
}
}

View File

@@ -141,7 +141,7 @@ struct LoginTab: View {
}
}
}
Text("Submitting will open a web browser. Please follow the login instructions provided there.\nAfter a successfull login, return to this application and press 'Done'.")
Text("Submitting will open a web browser. Please follow the login instructions provided there.\nAfter a successfull login, return to this application and press 'Validate'.")
.font(.subheadline)
.padding(.bottom)
.tint(.white)
@@ -152,10 +152,13 @@ struct LoginTab: View {
// fetch login v2 response
Task {
guard let res = await fetchLoginV2Response() else { return }
print(res.loginName)
print("Login successfull for user \(res.loginName)!")
userSettings.username = res.loginName
userSettings.token = res.appPassword
userSettings.onboarding = false
}
} label: {
Text("Done")
Text("Validate")
.foregroundColor(.white)
.font(.headline)
.padding()
@@ -188,23 +191,64 @@ struct LoginTab: View {
}
func sendLoginV2Request() async {
let request = RequestWrapper(
let hostPath = "https://\(userSettings.serverAddress)"
let headerFields: [HeaderField] = [
//HeaderField.ocsRequest(value: true),
//HeaderField.accept(value: .JSON)
]
let request = RequestWrapper.customRequest(
method: .POST,
path: "https://\(userSettings.serverAddress)/index.php/login/v2"
path: .LOGINV2REQ,
headerFields: headerFields
)
let (loginReq, _): (LoginV2Request?, Error?) = await NetworkHandler.sendDataRequest(request)
self.loginRequest = loginReq
do {
let (data, _): (Data?, Error?) = try await NetworkHandler.sendHTTPRequest(
request,
hostPath: hostPath,
authString: nil
)
guard let data = data else { return }
print("Data: \(data)")
let loginReq: LoginV2Request? = JSONDecoder.safeDecode(data)
self.loginRequest = loginReq
} catch {
print("Could not establish communication with the server.")
}
}
func fetchLoginV2Response() async -> LoginV2Response? {
guard let loginRequest = loginRequest else { return nil }
let request = RequestWrapper(
let headerFields = [
HeaderField.ocsRequest(value: true),
HeaderField.accept(value: .JSON),
HeaderField.contentType(value: .FORM)
]
let request = RequestWrapper.customRequest(
method: .POST,
path: loginRequest.poll.endpoint,
body: "token=\(loginRequest.poll.token)"
path: .NONE,
headerFields: headerFields,
body: "token=\(loginRequest.poll.token)".data(using: .utf8),
authenticate: false
)
let (loginRes, _): (LoginV2Response?, Error?) = await NetworkHandler.sendDataRequest(request)
return loginRes
var (data, error): (Data?, Error?) = (nil, nil)
do {
(data, error) = try await NetworkHandler.sendHTTPRequest(
request,
hostPath: loginRequest.poll.endpoint,
authString: nil
)
} catch {
print("Error: ", error)
}
guard let data = data else { return nil }
if let loginRes: LoginV2Response = JSONDecoder.safeDecode(data) {
return loginRes
}
print("Could not decode.")
return nil
}
}

View File

@@ -36,10 +36,10 @@ struct RecipeCardView: View {
.clipShape(RoundedRectangle(cornerRadius: 10))
.padding(.horizontal)
.task {
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, full: false)
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: true)
}
.refreshable {
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, full: false, needsUpdate: true)
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: true, needsUpdate: true)
}
}
}

View File

@@ -65,11 +65,11 @@ struct RecipeDetailView: View {
.navigationTitle(showTitle ? recipe.name : "")
.task {
recipeDetail = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id)
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, full: true)
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false)
}
.refreshable {
recipeDetail = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id, needsUpdate: true)
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, full: true, needsUpdate: true)
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false, needsUpdate: true)
}
}