So i am pretty new to Xcode, but i have been using Python and other language for some while. But I am quite new to the game of view and view control. So it may be that i have over complicated this a bit - and it may be that I have some wrong understanding of the dependencies and appcontroller (that i thought would be a good idea). So here we have a main file we call it app.swift, we have a startupmanager.swift, a appcoordinator and a dependeciescontainer. But it may be that this is either a overkill - or that I am doing it wrong.
So my thought was that i had a dependeciecontainer, a appcoordinator for the views and a startupmanager that controll the initialized fetching. I have controlled the memory when i run it - checking if it is higher, lower eg - but it was first when i did my 2 days profile i saw a lot of new errors, like this: Fikser(7291,0x204e516c0) malloc: xzm: failed to initialize deferred reclamation buffer (46). and i also get macro errors, probably from the @Query in my feedview.
So my thought was that a depencecie manager and a startupmanager was a good idea together with a app coordinator. But maybe I am wrong - maybe this is not a good idea? Or maybe I am doing some things twice? I have added a lot of prints and debugs for checking. But it seems that it starts off to heavy?
import SwiftUI
import Combine
@MainActor
class AppCoordinator: ObservableObject {
@Published var isLoggedIn: Bool = false
private var authManager: AuthenticationManager = .shared
private var cancellables = Set<AnyCancellable>()
private let startupManager: StartupManager
private let container: DependencyContainer
@Published var path = NavigationPath()
enum Screen: Hashable, Identifiable {
case profile
case activeJobs
case offers
case message
var id: Self { self }
}
init(container: DependencyContainer) {
self.container = container
self.startupManager = container.makeStartupManager()
setupObserving()
startupManager.start()
print("AppCoordinator initialized!")
}
private func setupObserving() {
authManager.$isAuthenticated
.receive(on: RunLoop.main)
.sink { [weak self] isAuthenticated in
self?.isLoggedIn = isAuthenticated
}
.store(in: &cancellables)
}
func userDidLogout() {
authManager.logout()
path.removeLast(path.count)
}
func showProfile() {
path.append(Screen.profile)
}
func showActiveJobs() {
path.append(Screen.activeJobs)
}
func showOffers() {
path.append(Screen.offers)
}
func showMessage() {
path.append(Screen.message)
}
@ViewBuilder
func viewForDestination(_ destination: Screen) -> some View {
switch destination {
case .profile:
ProfileView()
case .activeJobs:
ActiveJobsView()
case .offers:
OffersView()
case .message:
ChatView()
}
}
@ViewBuilder
func viewForJob(_ job: Job) -> some View {
PostDetailView(
job: job,
jobUserDetailsRepository: container.makeJobUserDetailsRepository()
)
}
@ViewBuilder
func viewForProfileSubview(_ destination: ProfileView.ProfileSubviews) -> some View {
switch destination{
case .personalSettings:
PersonalSettingView()
case .historicData:
HistoricDataView()
case .transactions:
TransactionView()
case .helpCenter:
HelpcenterView()
case .helpContract:
HelpContractView()
}
}
enum HomeBarDestinations: Hashable, Identifiable {
case postJob
case jobPosting
var id: Self { self }
}
@ViewBuilder
func viewForHomeBar(_ destination: HomeBarView.HomeBarDestinations) -> some View {
switch destination {
case .postJob:
PostJobView()
}
}
}
import Apollo
import FikserAPI
import SwiftData
class DependencyContainer {
static var shared: DependencyContainer!
private let modelContainer: ModelContainer
static func initialize(with modelContainer: ModelContainer) {
shared = DependencyContainer(modelContainer: modelContainer)
}
private init(modelContainer: ModelContainer) {
self.modelContainer = modelContainer
print("DependencyContainer being initialized at ")
}
@MainActor
private lazy var userData: UserData = {
return UserData(apollo: Network.shared.apollo)
}()
@MainActor
private lazy var userDetailsRepository: UserDetailsRepository = {
return UserDetailsRepository(userData: makeUserData())
}()
@MainActor
private lazy var jobData: JobData = {
return JobData(apollo: Network.shared.apollo)
}()
@MainActor
private lazy var jobRepository: JobRepository = {
return JobRepository(jobData: makeJobData(), modelContainer: modelContainer)
}()
@MainActor
func makeUserData() -> UserData {
return userData
}
@MainActor
func makeUserDetailsRepository() -> UserDetailsRepository {
return userDetailsRepository
}
@MainActor
func makeStartupManager() -> StartupManager {
return StartupManager(
userDetailsRepository: makeUserDetailsRepository(),
jobRepository: makeJobRepository(),
authManager: AuthenticationManager.shared,
lastUpdateRepository: makeLastUpdateRepository()
)
}
@MainActor
func makeJobData() -> JobData {
return jobData
}
@MainActor
func makeJobRepository() -> any JobRepositoryProtocol {
return jobRepository
}
@MainActor
private lazy var jobUserData: JobUserData = {
return JobUserData(apollo: Network.shared.apollo)
}()
@MainActor
private lazy var jobUserDetailsRepository: JobUserDetailsRepository = {
return JobUserDetailsRepository(jobUserData: makeJobUserData())
}()
@MainActor
func makeJobUserData() -> JobUserData {
return jobUserData
}
@MainActor
func makeJobUserDetailsRepository() -> JobUserDetailsRepository {
return jobUserDetailsRepository
}
@MainActor
private lazy var lastUpdateData: LastUpdateData = {
return LastUpdateData(apollo: Network.shared.apollo)
}()
@MainActor
private lazy var lastUpdateRepository: LastUpdateRepository = {
return LastUpdateRepository(lastUpdateData: makeLastUpdateData())
}()
@MainActor
func makeLastUpdateData() -> LastUpdateData {
return lastUpdateData
}
@MainActor
func makeLastUpdateRepository() -> LastUpdateRepository {
return lastUpdateRepository
}
}```