diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 2d7ba9a..756a647 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -156,7 +156,6 @@ 7AB5871D2C42C1CF00FA7B66 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 7AB5871C2C42C1CF00FA7B66 /* RealmSwift */; }; 7AB587322C42D38E00FA7B66 /* StorageServiceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB587312C42D38E00FA7B66 /* StorageServiceProtocol.swift */; }; 7AB587342C42D3FA00FA7B66 /* StorageService+Notes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB587332C42D3FA00FA7B66 /* StorageService+Notes.swift */; }; - 7AB587372C42E3EC00FA7B66 /* StorageServiceStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB587362C42E3EC00FA7B66 /* StorageServiceStub.swift */; }; 7AB587412C42FFE200FA7B66 /* ApiServiceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB587402C42FFE200FA7B66 /* ApiServiceProtocol.swift */; }; 7AB67E8C2435C38700258F61 /* CustomTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB67E8B2435C38700258F61 /* CustomTextField.swift */; }; 7AB67E8E2435D1A000258F61 /* CustomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB67E8D2435D1A000258F61 /* CustomButton.swift */; }; @@ -430,7 +429,6 @@ 7AB587222C42D27F00FA7B66 /* AutoCatTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AutoCatTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 7AB587312C42D38E00FA7B66 /* StorageServiceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageServiceProtocol.swift; sourceTree = ""; }; 7AB587332C42D3FA00FA7B66 /* StorageService+Notes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StorageService+Notes.swift"; sourceTree = ""; }; - 7AB587362C42E3EC00FA7B66 /* StorageServiceStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageServiceStub.swift; sourceTree = ""; }; 7AB587402C42FFE200FA7B66 /* ApiServiceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiServiceProtocol.swift; sourceTree = ""; }; 7AB67E8B2435C38700258F61 /* CustomTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextField.swift; sourceTree = ""; }; 7AB67E8D2435D1A000258F61 /* CustomButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomButton.swift; sourceTree = ""; }; @@ -981,7 +979,6 @@ isa = PBXGroup; children = ( 7A1E78FB2CE91A590004B740 /* Data */, - 7AB587362C42E3EC00FA7B66 /* StorageServiceStub.swift */, 7A176DB12C43071A00999D6B /* ApiServiceStub.swift */, 7AB0EF882C5D307600291EE6 /* LocationServiceStub.swift */, ); @@ -1416,7 +1413,6 @@ 7A27ADC7249D43210035F39E /* RegionsController.swift in Sources */, 7AFBE8CC2C3085C6003C491D /* ACProgressView.swift in Sources */, 7ADF6C93250B954900F237B2 /* Navigation.swift in Sources */, - 7AB587372C42E3EC00FA7B66 /* StorageServiceStub.swift in Sources */, 7A64AE752469DFB600ABE48E /* MediaBrowserViewController.swift in Sources */, 7A64AE732469DFB600ABE48E /* DismissAnimationController.swift in Sources */, 7ADF6C97250F41B000F237B2 /* PNKeyboard.swift in Sources */, diff --git a/AutoCat/Controllers/CheckController.swift b/AutoCat/Controllers/CheckController.swift index 78600e9..de0cd92 100644 --- a/AutoCat/Controllers/CheckController.swift +++ b/AutoCat/Controllers/CheckController.swift @@ -252,25 +252,25 @@ class CheckController: UIViewController, UITableViewDelegate, UISearchResultsUpd func updateDetailController(with vehicle: VehicleDto) { if let splitViewController = self.view.window?.rootViewController as? UISplitViewController { - var detail: UINavigationController? - if splitViewController.viewControllers.count == 2 { - detail = splitViewController.viewControllers.last as? UINavigationController - } else { - let storyboard = UIStoryboard(name: "Main", bundle: nil) - detail = storyboard.instantiateViewController(identifier: "ReportNavController") - } - - if let detail = detail { - detail.popToRootViewController(animated: true) - let report = detail.viewControllers.first as? ReportController - report?.number = vehicle.getNumber() - splitViewController.showDetailViewController(detail, sender: self) - } - -// Task { -// let coordinator = ReportCoordinator(splitController: splitViewController, vehicle: vehicle) -// try? await coordinator.start() +// var detail: UINavigationController? +// if splitViewController.viewControllers.count == 2 { +// detail = splitViewController.viewControllers.last as? UINavigationController +// } else { +// let storyboard = UIStoryboard(name: "Main", bundle: nil) +// detail = storyboard.instantiateViewController(identifier: "ReportNavController") // } +// +// if let detail = detail { +// detail.popToRootViewController(animated: true) +// let report = detail.viewControllers.first as? ReportController +// report?.number = vehicle.getNumber() +// splitViewController.showDetailViewController(detail, sender: self) +// } + + Task { + let coordinator = ReportCoordinator(splitController: splitViewController, vehicle: vehicle) + try? await coordinator.start() + } } } diff --git a/AutoCat/Preview/StorageServiceStub.swift b/AutoCat/Preview/StorageServiceStub.swift deleted file mode 100644 index 5f3113b..0000000 --- a/AutoCat/Preview/StorageServiceStub.swift +++ /dev/null @@ -1,28 +0,0 @@ -// -// StorageServiceStub.swift -// AutoCat -// -// Created by Selim Mustafaev on 13.07.2024. -// Copyright © 2024 Selim Mustafaev. All rights reserved. -// - -import AutoCatCore - -actor StorageServiceStub: StorageServiceProtocol { - - let vehicle = VehicleDto() - - func addNote(text: String, to number: String) async throws -> AutoCatCore.VehicleDto { - vehicle - } - - func deleteNote(id: String, for number: String) async throws -> AutoCatCore.VehicleDto { - vehicle - } - - func editNote(id: String, text: String, for number: String) async throws -> AutoCatCore.VehicleDto { - vehicle - } - - func updateVehicleIfExists(dto: AutoCatCore.VehicleDto) async throws { } -} diff --git a/AutoCat/Screens/ReportScreen/ReportViewModel.swift b/AutoCat/Screens/ReportScreen/ReportViewModel.swift index 038ca4f..4a17b71 100644 --- a/AutoCat/Screens/ReportScreen/ReportViewModel.swift +++ b/AutoCat/Screens/ReportScreen/ReportViewModel.swift @@ -11,7 +11,7 @@ import SwiftUI @MainActor @Observable -class ReportViewModel { +class ReportViewModel: ACHudContainer { @ObservationIgnored @Service var api: ApiServiceProtocol @ObservationIgnored @Service var storageService: StorageServiceProtocol @@ -43,6 +43,18 @@ class ReportViewModel { init(vehicle: VehicleDto) { self.vehicle = vehicle + self.loadVehicle() + } + + func loadVehicle() { + + Task { + do { + vehicle = try await storageService.loadVehicle(number: vehicle.getNumber()) + } catch { + hud = .error(error) + } + } } func openEvents() { diff --git a/AutoCatCore/Services/StorageService/StorageService.swift b/AutoCatCore/Services/StorageService/StorageService.swift index b14430e..c5d7b67 100644 --- a/AutoCatCore/Services/StorageService/StorageService.swift +++ b/AutoCatCore/Services/StorageService/StorageService.swift @@ -41,4 +41,13 @@ public actor StorageService: StorageServiceProtocol { realm.add(Vehicle(dto: dto), update: .all) } } + + public func loadVehicle(number: String) async throws -> VehicleDto { + + if let vehicle = realm.object(ofType: Vehicle.self, forPrimaryKey: number) { + return vehicle.dto + } else { + throw StorageError.vehicleNotFound + } + } } diff --git a/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift b/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift index 83d2084..b602bcd 100644 --- a/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift +++ b/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift @@ -15,4 +15,5 @@ public protocol StorageServiceProtocol: Sendable { func deleteNote(id: String, for number: String) async throws -> VehicleDto func editNote(id: String, text: String, for number: String) async throws -> VehicleDto func updateVehicleIfExists(dto: VehicleDto) async throws + func loadVehicle(number: String) async throws -> VehicleDto } diff --git a/AutoCatCoreTests/StorageServiceTests.swift b/AutoCatCoreTests/StorageServiceTests.swift index 939f834..80c1a65 100644 --- a/AutoCatCoreTests/StorageServiceTests.swift +++ b/AutoCatCoreTests/StorageServiceTests.swift @@ -112,4 +112,19 @@ struct StorageServiceTests { _ = try await storageService.deleteNote(id: "", for: existingVehicleNumber) } } + + @Test("Load existing vehicle") + func loadExistingVehicle() async throws { + + let vehicle = try await storageService.loadVehicle(number: existingVehicleNumber) + #expect(vehicle.number == existingVehicleNumber) + } + + @Test("Load non-existent vehicle") + func loadNonExistentVehicle() async throws { + + await #expect(throws: StorageError.vehicleNotFound) { + _ = try await storageService.loadVehicle(number: nonExistingVehicleNumber) + } + } } diff --git a/AutoCatTests/Mocks/StorageServiceMock.swift b/AutoCatTests/Mocks/StorageServiceMock.swift deleted file mode 100644 index 30355ec..0000000 --- a/AutoCatTests/Mocks/StorageServiceMock.swift +++ /dev/null @@ -1,36 +0,0 @@ -// -// StorageServiceMock.swift -// AutoCatTests -// -// Created by Selim Mustafaev on 13.07.2024. -// Copyright © 2024 Selim Mustafaev. All rights reserved. -// - -import Foundation -import AutoCatCore - -actor StorageServiceMock { - - var vehicle = VehicleDto() - - func setVehicle(_ vehicle: VehicleDto) async { - self.vehicle = vehicle - } -} - -extension StorageServiceMock: StorageServiceProtocol { - - func addNote(text: String, to number: String) async throws -> AutoCatCore.VehicleDto { - vehicle - } - - func deleteNote(id: String, for number: String) async throws -> AutoCatCore.VehicleDto { - vehicle - } - - func editNote(id: String, text: String, for number: String) async throws -> AutoCatCore.VehicleDto { - vehicle - } - - func updateVehicleIfExists(dto: AutoCatCore.VehicleDto) async throws { } -}