Loading vehicle from DB in new report screen

This commit is contained in:
Selim Mustafaev 2024-12-01 21:53:55 +03:00
parent 46a70ef611
commit 22e5ca764e
8 changed files with 56 additions and 87 deletions

View File

@ -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 = "<group>"; };
7AB587332C42D3FA00FA7B66 /* StorageService+Notes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StorageService+Notes.swift"; sourceTree = "<group>"; };
7AB587362C42E3EC00FA7B66 /* StorageServiceStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageServiceStub.swift; sourceTree = "<group>"; };
7AB587402C42FFE200FA7B66 /* ApiServiceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiServiceProtocol.swift; sourceTree = "<group>"; };
7AB67E8B2435C38700258F61 /* CustomTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextField.swift; sourceTree = "<group>"; };
7AB67E8D2435D1A000258F61 /* CustomButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomButton.swift; sourceTree = "<group>"; };
@ -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 */,

View File

@ -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()
}
}
}

View File

@ -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 { }
}

View File

@ -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() {

View File

@ -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
}
}
}

View File

@ -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
}

View File

@ -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)
}
}
}

View File

@ -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 { }
}