I am developing an app using SwiftUI and Firestore.
On the home page, I'm showing the list of items from Firestore that are updating properly when any updations from the backend. Now on the Top plus button, I am navigating to another screen to add a new item. On adding the new item, the list on the home page gets updated, that pushing the AddNewItemView again in the stack.
In this case, I want to detect if the Listing Screen is not visible right now and stop listing to events.
import SwiftUI
struct HomeView: View {
private let useCase: AuthLogoutProvider = AuthService.shared
@ObservedObject var viewModel: HomeViewModel
init(_ viewModel: HomeViewModel) {
self.viewModel = viewModel
}
private var addTeam: some View {
return HStack {
Text("Add Team +")
.font(.system(size: 20))
.fontWeight(.regular)
.foregroundColor(Color.secondary)
.padding(.vertical)
}
.frame(width: 150, height: 54)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.secondary, lineWidth: 1)
)
}
var body: some View {
NavigationView {
VStack {
HStack {
NavigationLink(destination: AddTeamInputView(), label: {
addTeam
})
Spacer()
}
.padding()
List(viewModel.teamsInfoModels, id: \.id) { teamInfoModel in
VStack {
NavigationLink(destination: TeamDetailView(teamInfoModel), label: {
EmptyView()
})
TeamInfoCell(teamInfoModel)
}
}
Spacer()
}
.navigationTitle( Text("Countries"))
.navigationBarItems(
trailing: Button(action: {
useCase.signOut()
self.viewModel.logoutPerformed()
}) {
Text("Log out")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.secondary)
}
)
}
.onAppear {
print("HomeView onAppear")
}
.onDisappear {
print("HomeView onDisappear")
}
}
}
This is my Home View having a list of team and Add Team Button
on clicking Add new button navigating to another screen to add the Team on Firestore
AddTeamInputView
struct AddTeamInputView: View {
@ObservedObject private var viewModel: AddTeamInputViewModel
init(_ viewModel: AddTeamInputViewModel = AddTeamInputViewModel()) {
self.viewModel = viewModel
print("AddTeamInputView init called")
}
var body: some View {
VStack {
HStack {
TextField("Enter Team name", text: $viewModel.teamName)
.font(.title2)
.foregroundColor(.secondary)
.frame(width: UIScreen.main.bounds.width - 40)
}
.padding(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.secondary, lineWidth: 1)
)
if viewModel.move {
NavigationLink(destination: CountrySelectionView(viewModel: CountrySelectionViewModel(viewModel.createdTeam())), isActive: $viewModel.move) {
EmptyView()
}
}
AppButton(action: {
self.viewModel.addTeam()
}, title: "Save", width: 200)
.padding(.vertical, 40)
}
.navigationTitle("Name Your Team!")
}
}
AddTeamViewModel
class AddTeamInputViewModel: ObservableObject {
init() {
print("AddTeamInputViewModel init")
}
@Published var teamName: String = ""
@Published var move: Bool = false
private var team: Team? = nil
func addTeam() {
TeamDataProvider.shared.createTeam(teamName) { (error, teamId) in
if let teamId = teamId {
if let team = TeamDataProvider.shared.team(forId: teamId) {
DispatchQueue.main.async {
self.team = team
print("Move to select country for team == \(self.team?.teamName ?? "") == \(self.move)")
self.move.toggle()
print("Move Value after toggle == \(self.move)")
}
} else {
print("Team issue")
}
} else {
print("Team issue")
}
}
}
func createdTeam() -> Team {
return team!
}
deinit {
print("AddTeamInputViewModel deinit called")
}
}