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
extension List { | |
func emptyListPlaceholder(_ items: [Any], _ placeholder: AnyView) -> some View { | |
modifier(EmptyDataModifier(items: items, placeholder: placeholder)) | |
} | |
} | |
struct ContentView: View { | |
@State var countries: [Country] = [] // Data source |
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
struct ContentView: View { | |
@State var countries: [Country] = [] // Data source | |
var body: some View { | |
List(countries) { country in | |
Text(country.name) | |
.font(.title) | |
} | |
.modifier(EmptyDataModifier( |
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
struct EmptyDataModifier<Placeholder: View>: ViewModifier { | |
let items: [Any] | |
let placeholder: Placeholder | |
@ViewBuilder | |
func body(content: Content) -> some View { | |
if !items.isEmpty { | |
content | |
} else { |
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
struct ContentView: View { | |
@State var countries: [Country] = [] // Data source | |
var body: some View { | |
EmptyList(countries, // Data items | |
listRowView: { country in // List row view | |
Text(country.name) | |
.font(.title) | |
}, placeholderView: { |
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
struct EmptyList<Items: RandomAccessCollection, ListRowView: View, PlaceholderView: View>: View where Items.Element: Identifiable { | |
private let items: Items | |
private let listRowView: (Items.Element) -> ListRowView | |
private let placeholderView: () -> PlaceholderView | |
/// - Parameters: | |
/// - items: Source data for List. Item must implement Identifiable protocol | |
/// - listRowView: View displayed for each source Item | |
/// - placeholderView: Placeholder. View displayed when the items collection isEmpty |
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
struct ContentView: View { | |
@State var countries: [Country] = [] // Data source | |
var body: some View { | |
if countries.isEmpty { | |
Text("No Countries") // Placeholder | |
.font(.largeTitle) | |
} else { | |
List(countries) { country in // List countires |
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
struct Country: Identifiable { | |
let id = UUID() | |
let name: String | |
} |
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
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
var window: UIWindow? | |
func scene(_ scene: UIScene, | |
willConnectTo session: UISceneSession, | |
options connectionOptions: UIScene.ConnectionOptions) { | |
// Get UIWindowScene | |
guard let windowScene = (scene as? UIWindowScene) else { | |
return |
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
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
// Create a new window property | |
self.window = UIWindow(frame: UIScreen.main.bounds) | |