Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions Shared/Model/GrocyAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,41 @@ public class GrocyApi: GrocyAPI {
}
}


private func executeUrlRequestSync<T: Codable>(
_ urlRequest: URLRequest,
_ type: T.Type,
completion: @escaping (T?, Error?) -> Void
) throws {
let dataTask = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
guard let safeData = data else {
completion(nil, APIError.invalidResponse)
return
}

if let httpResponse = response as? HTTPURLResponse {
if (200...299).contains(httpResponse.statusCode) {
do {
let responseDataDecoded = try JSONDecoder().decode(T.self, from: safeData)
completion(responseDataDecoded, nil)
} catch {
completion(nil, APIError.decodingError(error: error))
}
} else {
do {
let responseErrorDecoded = try JSONDecoder().decode(ErrorMessage.self, from: safeData)
completion(nil, APIError.errorString(description: responseErrorDecoded.errorMessage))
} catch {
completion(nil, APIError.decodingError(error: error))
}
}
}
}

dataTask.resume()
}


private func callAPI<T: Codable>(
_ endPoint: Endpoint,
method: Method,
Expand All @@ -273,25 +308,25 @@ public class GrocyApi: GrocyAPI {
query: query,
hassIngressToken: hassIngressToken
)
let result = try await URLSession.shared.data(for: urlRequest)
if let httpResponse = result.1 as? HTTPURLResponse {
if (200...299).contains(httpResponse.statusCode) {
do {
let responseDataDecoded = try JSONDecoder().decode(T.self, from: result.0)
return responseDataDecoded
} catch {
throw APIError.decodingError(error: error)
}
} else {
do {
let responseErrorDecoded = try JSONDecoder().decode(ErrorMessage.self, from: result.0)
throw APIError.errorString(description: responseErrorDecoded.errorMessage)
} catch {
throw APIError.decodingError(error: error)

return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<T, Error>) in
do {
try executeUrlRequestSync(urlRequest, T.self) { result, error in
if result == nil {
guard let safeError = error else {
continuation.resume(throwing: APIError.internalError)
return
}

continuation.resume(throwing: safeError)
} else {
continuation.resume(returning: result!)
}
}
} catch {
continuation.resume(throwing: error)
}
}
throw APIError.internalError
}

private func request(
Expand Down
2 changes: 1 addition & 1 deletion Shared/Model/OpenFoodFacts/OpenFoodFactsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import Combine

class OpenFoodFactsViewModel: ObservableObject {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared
@Published var offData: OpenFoodFactsResult?
@Published var errorMessage: String? = nil

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/Additional/ServerProblemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
struct ServerProblemView: View {
var isCompact: Bool = false

@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@AppStorage("devMode") private var devMode: Bool = false

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/Admin/UserManagementView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct UserManagementView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var searchString: String = ""

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/Admin/UserRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct UserRowActionsView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

var user: GrocyUser
var isCurrentUser: Bool
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/Components/AmountSelectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct AmountSelectionView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Binding var productID: Int?
@Binding var amount: Double
Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/Components/MyDoubleStepper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MyDoubleStepper: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared
@Binding var amount: Double

var description: String
Expand Down Expand Up @@ -90,7 +90,7 @@ struct MyDoubleStepper: View {


struct MyDoubleStepperOptional: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared
@Binding var amount: Double?

var description: String
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/Components/ProductField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct ProductField: View {
#endif


@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Binding var productID: Int?
var description: String
Expand Down
8 changes: 4 additions & 4 deletions Shared/Views/Components/ServerSettingsItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct ServerSettingsToggle: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var isOn: Bool = false

Expand Down Expand Up @@ -60,7 +60,7 @@ struct ServerSettingsToggle: View {
}

struct ServerSettingsIntStepper: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var value: Int = 0

Expand Down Expand Up @@ -108,7 +108,7 @@ struct ServerSettingsIntStepper: View {
}

struct ServerSettingsDoubleStepper: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var value: Double = 0

Expand Down Expand Up @@ -156,7 +156,7 @@ struct ServerSettingsDoubleStepper: View {
}

struct ServerSettingsObjectPicker: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var objectID: Int? = nil

Expand Down
6 changes: 3 additions & 3 deletions Shared/Views/MasterData/MDBarcodesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDBarcodeRowView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

var barcode: MDProductBarcode

Expand Down Expand Up @@ -36,7 +36,7 @@ struct MDBarcodeRowView: View {
}

struct MDBarcodesView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

var productID: Int

Expand Down Expand Up @@ -230,7 +230,7 @@ struct MDBarcodesView: View {
}

struct MDBarcodesView_Previews: PreviewProvider {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared
static var previews: some View {
NavigationView{
MDBarcodesView(productID: 27, toastType: Binding.constant(nil))
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDBatteriesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDBatteriesView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

var body: some View {
VStack{
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDForms/MDBarcodeFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDBarcodeFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDForms/MDLocationFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDLocationFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/MasterData/MDForms/MDProductFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDProductFormOFFView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared
@StateObject var offVM: OpenFoodFactsViewModel

@Binding var name: String
Expand Down Expand Up @@ -96,7 +96,7 @@ struct MDProductFormOFFView: View {
}

struct MDProductFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDProductGroupFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDProductPictureFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

var product: MDProduct?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDQuantityUnitConversionFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDQuantityUnitFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDStoreFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDTaskCategoryFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDForms/MDUserEntityFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDUserEntityFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDForms/MDUserFieldFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDUserFieldFormView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/MasterData/MDLocationsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDLocationRowView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

var location: MDLocation

Expand All @@ -32,7 +32,7 @@ struct MDLocationRowView: View {
}

struct MDLocationsView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var searchString: String = ""

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDProductGroupsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct MDProductGroupRowView: View {
}

struct MDProductGroupsView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/MasterData/MDProductsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct MDProductRowView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

var product: MDProduct

Expand Down Expand Up @@ -67,7 +67,7 @@ struct MDProductRowView: View {
}

struct MDProductsView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var searchString: String = ""

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDQuantityUnitsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct MDQuantityUnitRowView: View {
}

struct MDQuantityUnitsView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@Environment(\.dismiss) var dismiss

Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/MasterData/MDShoppingLocationsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct MDStoreRowView: View {
}

struct MDStoresView: View {
@StateObject var grocyVM: GrocyViewModel = .shared
@ObservedObject var grocyVM: GrocyViewModel = .shared

@State private var searchString: String = ""

Expand Down
Loading