-
-
Save devmauvsantos/c43d8980bdfb424eba7bc0478e32c124 to your computer and use it in GitHub Desktop.
SwiftUI onAppear bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// OnAppearCheck | |
// | |
// Created by VAndrJ on 21.12.2023. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@State var isLoggedIn: Bool = false | |
var body: some View { | |
if isLoggedIn { | |
TabView { | |
TabViewView(title: "Store", tabIcon: "trash", isLoggedIn: $isLoggedIn) | |
TabViewView(title: "New", tabIcon: "newspaper", isLoggedIn: $isLoggedIn) | |
TabViewView(title: "Gallery", tabIcon: "photo", isLoggedIn: $isLoggedIn) | |
TabViewView(title: "Discover", tabIcon: "magnifyingglass", isLoggedIn: $isLoggedIn) | |
TabViewView(title: "Profile", tabIcon: "person", isLoggedIn: $isLoggedIn) | |
} | |
} else { | |
LoginView { | |
isLoggedIn = true | |
} | |
} | |
} | |
} | |
struct TabViewView: View { | |
let title: String | |
let tabIcon: String | |
@Binding var isLoggedIn: Bool | |
var body: some View { | |
NavigationStack { | |
Text(title) | |
.navigationBarTitle(title) | |
.task { | |
print("ποΈ \(title) some task") | |
} | |
.onAppear { | |
print("π \(title) onAppear") | |
} | |
.onDisappear { | |
print("πΆβπ«οΈ \(title) onDisappear") | |
} | |
.toolbar { | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Button("Logout") { | |
isLoggedIn = false | |
} | |
} | |
} | |
} | |
.task { | |
print("ποΈ \(title) NavigationStack some task") | |
} | |
.onAppear { | |
print("π \(title) NavigationStack onAppear") | |
} | |
.onDisappear { | |
print("πΆβπ«οΈ \(title) NavigationStack onDisappear") | |
} | |
.tabItem { | |
Image(systemName: tabIcon) | |
Text(title) | |
} | |
} | |
} | |
struct LoginView: View { | |
let onLogin: () -> Void | |
var body: some View { | |
Button("Login", action: onLogin) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// OnAppearCheck | |
// | |
// Created by VAndrJ on 21.12.2023. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@State var isLoggedIn: Bool = false | |
var body: some View { | |
if isLoggedIn { | |
TabView { | |
TabViewView(title: "Store", tabIcon: "trash") | |
TabViewView(title: "New", tabIcon: "newspaper") | |
TabViewView(title: "Gallery", tabIcon: "photo") | |
TabViewView(title: "Discover", tabIcon: "magnifyingglass") | |
TabViewView(title: "Profile", tabIcon: "person") | |
} | |
.environment(\.logout, LogoutAction(action: logout)) | |
} else { | |
LoginView { | |
isLoggedIn = true | |
} | |
} | |
} | |
func logout() { | |
isLoggedIn = false | |
} | |
} | |
struct LogoutAction { | |
let action: () -> Void | |
func callAsFunction() { | |
action() | |
} | |
} | |
struct LogoutActionKey: EnvironmentKey { | |
static var defaultValue: LogoutAction? = nil | |
} | |
extension EnvironmentValues { | |
var logout: LogoutAction? { | |
get { self[LogoutActionKey.self] } | |
set { self[LogoutActionKey.self] = newValue } | |
} | |
} | |
struct TabViewView: View { | |
let title: String | |
let tabIcon: String | |
@Environment(\.logout) var logout | |
var body: some View { | |
NavigationStack { | |
Text(title) | |
.navigationBarTitle(title) | |
.task { | |
print("ποΈ \(title) some task") | |
} | |
.onAppear { | |
print("π \(title) onAppear") | |
} | |
.onDisappear { | |
print("πΆβπ«οΈ \(title) onDisappear") | |
} | |
.toolbar { | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Button("Logout") { | |
logout?() | |
} | |
} | |
} | |
} | |
.task { | |
print("ποΈ \(title) NavigationStack some task") | |
} | |
.onAppear { | |
print("π \(title) NavigationStack onAppear") | |
} | |
.onDisappear { | |
print("πΆβπ«οΈ \(title) NavigationStack onDisappear") | |
} | |
.tabItem { | |
Image(systemName: tabIcon) | |
Text(title) | |
} | |
} | |
} | |
struct LoginView: View { | |
let onLogin: () -> Void | |
var body: some View { | |
Button("Login", action: onLogin) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment