Bug fixes
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SimilaritySearchKit
|
||||
|
||||
struct MainView: View {
|
||||
@StateObject var viewModel = AppState()
|
||||
@@ -69,211 +70,3 @@ struct MainView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
/*struct MainView: View {
|
||||
@ObservedObject var viewModel: MainViewModel
|
||||
@StateObject var userSettings: UserSettings = UserSettings.shared
|
||||
|
||||
@State private var selectedCategory: Category? = nil
|
||||
@State private var showEditView: Bool = false
|
||||
@State private var showSearchView: Bool = false
|
||||
@State private var showSettingsView: Bool = false
|
||||
@State private var serverConnection: Bool = false
|
||||
@State private var showLoadingIndicator: Bool = false
|
||||
|
||||
|
||||
var columns: [GridItem] = [GridItem(.adaptive(minimum: 150), spacing: 0)]
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(selection: $selectedCategory) {
|
||||
// All recipes
|
||||
NavigationLink {
|
||||
RecipeSearchView(viewModel: viewModel)
|
||||
} label: {
|
||||
HStack(alignment: .center) {
|
||||
Image(systemName: "magnifyingglass")
|
||||
Text("Search")
|
||||
.font(.system(size: 20, weight: .medium, design: .default))
|
||||
}
|
||||
.padding(7)
|
||||
}
|
||||
|
||||
// Categories
|
||||
ForEach(viewModel.categories) { category in
|
||||
if category.recipe_count != 0 {
|
||||
NavigationLink(value: category) {
|
||||
HStack(alignment: .center) {
|
||||
if selectedCategory != nil && category.name == selectedCategory!.name {
|
||||
Image(systemName: "book")
|
||||
} else {
|
||||
Image(systemName: "book.closed.fill")
|
||||
}
|
||||
Text(category.name == "*" ? String(localized: "Other") : category.name)
|
||||
.font(.system(size: 20, weight: .medium, design: .default))
|
||||
Spacer()
|
||||
Text("\(category.recipe_count)")
|
||||
.font(.system(size: 15, weight: .bold, design: .default))
|
||||
.foregroundStyle(Color.background)
|
||||
.frame(width: 25, height: 25, alignment: .center)
|
||||
.minimumScaleFactor(0.5)
|
||||
.background {
|
||||
Circle()
|
||||
.foregroundStyle(Color.secondary)
|
||||
}
|
||||
}.padding(7)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Cookbooks")
|
||||
.navigationDestination(isPresented: $showSettingsView) {
|
||||
SettingsView(viewModel: viewModel)
|
||||
}
|
||||
.navigationDestination(isPresented: $showSearchView) {
|
||||
RecipeSearchView(viewModel: viewModel)
|
||||
}
|
||||
.toolbar {
|
||||
MainViewToolBar(
|
||||
viewModel: viewModel,
|
||||
showEditView: $showEditView,
|
||||
showSettingsView: $showSettingsView,
|
||||
serverConnection: $serverConnection,
|
||||
showLoadingIndicator: $showLoadingIndicator
|
||||
)
|
||||
}
|
||||
} detail: {
|
||||
NavigationStack {
|
||||
if let category = selectedCategory {
|
||||
CategoryDetailView(
|
||||
categoryName: category.name,
|
||||
viewModel: viewModel,
|
||||
showEditView: $showEditView
|
||||
)
|
||||
.id(category.id) // Workaround: This is needed to update the detail view when the selection changes
|
||||
}
|
||||
}
|
||||
}
|
||||
.tint(.nextcloudBlue)
|
||||
.sheet(isPresented: $showEditView) {
|
||||
RecipeEditView(
|
||||
viewModel:
|
||||
RecipeEditViewModel(
|
||||
mainViewModel: viewModel,
|
||||
uploadNew: true
|
||||
),
|
||||
isPresented: $showEditView
|
||||
)
|
||||
}
|
||||
.task {
|
||||
showLoadingIndicator = true
|
||||
self.serverConnection = await viewModel.checkServerConnection()
|
||||
await viewModel.getCategories()
|
||||
await viewModel.updateAllRecipeDetails()
|
||||
|
||||
// Open detail view for default category
|
||||
if userSettings.defaultCategory != "" {
|
||||
if let cat = viewModel.categories.first(where: { c in
|
||||
if c.name == userSettings.defaultCategory {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}) {
|
||||
self.selectedCategory = cat
|
||||
}
|
||||
}
|
||||
showLoadingIndicator = false
|
||||
}
|
||||
.refreshable {
|
||||
self.serverConnection = await viewModel.checkServerConnection()
|
||||
await viewModel.getCategories()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
struct MainViewToolBar: ToolbarContent {
|
||||
@ObservedObject var viewModel: MainViewModel
|
||||
@Binding var showEditView: Bool
|
||||
@Binding var showSettingsView: Bool
|
||||
@Binding var serverConnection: Bool
|
||||
@Binding var showLoadingIndicator: Bool
|
||||
@State private var presentPopover: Bool = false
|
||||
|
||||
var body: some ToolbarContent {
|
||||
// Top left menu toolbar item
|
||||
ToolbarItem(placement: .topBarLeading) {
|
||||
Menu {
|
||||
Button {
|
||||
self.showSettingsView = true
|
||||
} label: {
|
||||
Text("Settings")
|
||||
Image(systemName: "gearshape")
|
||||
}
|
||||
Button {
|
||||
Task {
|
||||
showLoadingIndicator = true
|
||||
UserSettings.shared.lastUpdate = Date.distantPast
|
||||
await viewModel.getCategories()
|
||||
for category in viewModel.categories {
|
||||
await viewModel.getCategory(named: category.name, fetchMode: .preferServer)
|
||||
}
|
||||
await viewModel.updateAllRecipeDetails()
|
||||
showLoadingIndicator = false
|
||||
}
|
||||
} label: {
|
||||
Text("Refresh all")
|
||||
Image(systemName: "icloud.and.arrow.down")
|
||||
}
|
||||
|
||||
} label: {
|
||||
Image(systemName: "ellipsis.circle")
|
||||
}
|
||||
}
|
||||
|
||||
// Server connection indicator
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button {
|
||||
print("Check server connection")
|
||||
presentPopover = true
|
||||
} label: {
|
||||
if showLoadingIndicator {
|
||||
ProgressView()
|
||||
} else if serverConnection {
|
||||
Image(systemName: "checkmark.icloud")
|
||||
} else {
|
||||
Image(systemName: "xmark.icloud")
|
||||
}
|
||||
}.popover(isPresented: $presentPopover) {
|
||||
VStack(alignment: .leading) {
|
||||
Text(serverConnection ? LocalizedStringKey("Connected to server.") : LocalizedStringKey("Unable to connect to server."))
|
||||
.bold()
|
||||
|
||||
Text("Last updated: \(DateFormatter.utcToString(date: UserSettings.shared.lastUpdate))")
|
||||
.font(.caption)
|
||||
.foregroundStyle(Color.secondary)
|
||||
}
|
||||
.padding()
|
||||
.presentationCompactAdaptation(.popover)
|
||||
}
|
||||
}
|
||||
|
||||
// Create new recipes
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button {
|
||||
print("Add new recipe")
|
||||
showEditView = true
|
||||
} label: {
|
||||
Image(systemName: "plus.circle.fill")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
@@ -87,44 +87,21 @@ struct TokenLoginView: View {
|
||||
showAlert = true
|
||||
return false
|
||||
}
|
||||
let headerFields = [
|
||||
HeaderField.ocsRequest(value: true),
|
||||
]
|
||||
let request = RequestWrapper.customRequest(
|
||||
method: .GET,
|
||||
path: .CATEGORIES,
|
||||
headerFields: headerFields,
|
||||
authenticate: true
|
||||
)
|
||||
|
||||
var (data, error): (Data?, Error?) = (nil, nil)
|
||||
do {
|
||||
let loginString = "\(userSettings.username):\(userSettings.token)"
|
||||
let loginData = loginString.data(using: String.Encoding.utf8)!
|
||||
let authString = loginData.base64EncodedString()
|
||||
DispatchQueue.main.async {
|
||||
userSettings.authString = authString
|
||||
}
|
||||
(data, error) = try await NetworkHandler.sendHTTPRequest(
|
||||
request,
|
||||
hostPath: "https://\(userSettings.serverAddress)/index.php/apps/cookbook/api/v1/",
|
||||
authString: authString
|
||||
)
|
||||
|
||||
} catch {
|
||||
print("Error: ", error)
|
||||
UserSettings.shared.setAuthString()
|
||||
let (data, error) = await cookbookApi.getCategories(auth: UserSettings.shared.authString)
|
||||
|
||||
if let error = error {
|
||||
alertMessage = "Login failed. Please check your inputs and internet connection."
|
||||
showAlert = true
|
||||
return false
|
||||
}
|
||||
|
||||
guard let data = data else {
|
||||
alertMessage = "Login failed. Please check your inputs."
|
||||
showAlert = true
|
||||
return false
|
||||
}
|
||||
if let testRequest: [Category] = JSONDecoder.safeDecode(data) {
|
||||
print("validationResponse: \(testRequest)")
|
||||
return true
|
||||
}
|
||||
alertMessage = "Login failed. Please check your inputs and internet connection."
|
||||
showAlert = true
|
||||
return false
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,7 @@ struct V2LoginView: View {
|
||||
|
||||
@State var loginStage: V2LoginStage = .login
|
||||
@State var loginRequest: LoginV2Request? = nil
|
||||
|
||||
@State var userSettings = UserSettings.shared
|
||||
|
||||
|
||||
// TextField handling
|
||||
enum Field {
|
||||
case server
|
||||
@@ -73,14 +71,18 @@ struct V2LoginView: View {
|
||||
|
||||
HStack {
|
||||
Button {
|
||||
if userSettings.serverAddress == "" {
|
||||
if UserSettings.shared.serverAddress == "" {
|
||||
alertMessage = "Please enter a valid server address."
|
||||
showAlert = true
|
||||
return
|
||||
}
|
||||
|
||||
Task {
|
||||
await sendLoginV2Request()
|
||||
let error = await sendLoginV2Request()
|
||||
if let error = error {
|
||||
alertMessage = "A network error occured (\(error.rawValue))."
|
||||
showAlert = true
|
||||
}
|
||||
if let loginRequest = loginRequest {
|
||||
await UIApplication.shared.open(URL(string: loginRequest.login)!)
|
||||
} else {
|
||||
@@ -107,20 +109,27 @@ struct V2LoginView: View {
|
||||
Button {
|
||||
// fetch login v2 response
|
||||
Task {
|
||||
guard let res = await fetchLoginV2Response() else {
|
||||
let (response, error) = await fetchLoginV2Response()
|
||||
|
||||
if let error = error {
|
||||
alertMessage = "Login failed. Please login via the browser and try again. (\(error.rawValue))"
|
||||
showAlert = true
|
||||
return
|
||||
}
|
||||
guard let response = response else {
|
||||
alertMessage = "Login failed. Please login via the browser and try again."
|
||||
showAlert = true
|
||||
return
|
||||
}
|
||||
print("Login successfull for user \(res.loginName)!")
|
||||
self.userSettings.username = res.loginName
|
||||
self.userSettings.token = res.appPassword
|
||||
let loginString = "\(userSettings.username):\(userSettings.token)"
|
||||
print("Login successful for user \(response.loginName)!")
|
||||
UserSettings.shared.username = response.loginName
|
||||
UserSettings.shared.token = response.appPassword
|
||||
let loginString = "\(UserSettings.shared.username):\(UserSettings.shared.token)"
|
||||
let loginData = loginString.data(using: String.Encoding.utf8)!
|
||||
DispatchQueue.main.async {
|
||||
userSettings.authString = loginData.base64EncodedString()
|
||||
UserSettings.shared.authString = loginData.base64EncodedString()
|
||||
}
|
||||
self.userSettings.onboarding = false
|
||||
UserSettings.shared.onboarding = false
|
||||
}
|
||||
} label: {
|
||||
Text("Validate")
|
||||
@@ -141,64 +150,14 @@ struct V2LoginView: View {
|
||||
}
|
||||
}
|
||||
|
||||
func sendLoginV2Request() async {
|
||||
let hostPath = "https://\(userSettings.serverAddress)"
|
||||
let headerFields: [HeaderField] = [
|
||||
//HeaderField.ocsRequest(value: true),
|
||||
//HeaderField.accept(value: .JSON)
|
||||
]
|
||||
let request = RequestWrapper.customRequest(
|
||||
method: .POST,
|
||||
path: .LOGINV2REQ,
|
||||
headerFields: headerFields
|
||||
)
|
||||
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 sendLoginV2Request() async -> NetworkError? {
|
||||
let (req, error) = await NextcloudApi.loginV2Request()
|
||||
self.loginRequest = req
|
||||
return error
|
||||
}
|
||||
|
||||
func fetchLoginV2Response() async -> LoginV2Response? {
|
||||
guard let loginRequest = loginRequest else { return nil }
|
||||
let headerFields = [
|
||||
HeaderField.ocsRequest(value: true),
|
||||
HeaderField.accept(value: .JSON),
|
||||
HeaderField.contentType(value: .FORM)
|
||||
]
|
||||
let request = RequestWrapper.customRequest(
|
||||
method: .POST,
|
||||
path: .NONE,
|
||||
headerFields: headerFields,
|
||||
body: "token=\(loginRequest.poll.token)".data(using: .utf8),
|
||||
authenticate: false
|
||||
)
|
||||
|
||||
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
|
||||
func fetchLoginV2Response() async -> (LoginV2Response?, NetworkError?) {
|
||||
guard let loginRequest = loginRequest else { return (nil, .parametersNil) }
|
||||
return await NextcloudApi.loginV2Response(req: loginRequest)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
import SimilaritySearchKit
|
||||
|
||||
struct SearchTabView: View {
|
||||
@EnvironmentObject var viewModel: SearchTabView.ViewModel
|
||||
@@ -17,6 +17,13 @@ struct SearchTabView: View {
|
||||
NavigationStack {
|
||||
VStack {
|
||||
ScrollView(showsIndicators: false) {
|
||||
/*
|
||||
Picker("Topping", selection: $viewModel.searchMode) {
|
||||
ForEach(ViewModel.SearchMode.allCases, id: \.self) { mode in
|
||||
Text(mode.rawValue)
|
||||
}
|
||||
}.pickerStyle(.segmented)
|
||||
*/
|
||||
LazyVStack {
|
||||
ForEach(viewModel.recipesFiltered(), id: \.recipe_id) { recipe in
|
||||
NavigationLink(value: recipe) {
|
||||
@@ -47,13 +54,26 @@ struct SearchTabView: View {
|
||||
class ViewModel: ObservableObject {
|
||||
@Published var allRecipes: [Recipe] = []
|
||||
@Published var searchText: String = ""
|
||||
@Published var searchMode: SearchMode = .name
|
||||
|
||||
var similarityIndex: SimilarityIndex? = nil
|
||||
var similaritySearchResults: [SearchResult] = []
|
||||
|
||||
enum SearchMode: String, CaseIterable {
|
||||
case name = "Name & Keywords", ingredient = "Ingredients"
|
||||
}
|
||||
|
||||
func recipesFiltered() -> [Recipe] {
|
||||
guard searchText != "" else { return allRecipes }
|
||||
return allRecipes.filter { recipe in
|
||||
recipe.name.lowercased().contains(searchText.lowercased()) || // check name for occurence of search term
|
||||
(recipe.keywords != nil && recipe.keywords!.lowercased().contains(searchText.lowercased())) // check keywords for search term
|
||||
if searchMode == .name {
|
||||
guard searchText != "" else { return allRecipes }
|
||||
return allRecipes.filter { recipe in
|
||||
recipe.name.lowercased().contains(searchText.lowercased()) || // check name for occurence of search term
|
||||
(recipe.keywords != nil && recipe.keywords!.lowercased().contains(searchText.lowercased())) // check keywords for search term
|
||||
}
|
||||
} else if searchMode == .ingredient {
|
||||
// TODO: Fuzzy ingredient search
|
||||
}
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user