Skip to content

Instantly share code, notes, and snippets.

@JohnnyD1776
Last active October 8, 2025 08:46
Show Gist options
  • Select an option

  • Save JohnnyD1776/2657db13be68f68ead6840499907504c to your computer and use it in GitHub Desktop.

Select an option

Save JohnnyD1776/2657db13be68f68ead6840499907504c to your computer and use it in GitHub Desktop.
SwiftUI Demo for ContentUnavailable
//
/*
Filename: ContentUnavailableDemo.swift
Project: Demo Project
Created by John Durcan on 08/10/2025.
Copyright © 2025 Itch Studio Ltd.. All rights reserved.
Company No. 14729010. Registered Address: 128, City Road, London, EC1V 2NX
Licensed under the MIT License. You may obtain a copy of the License at
https:opensource.org/licenses/MIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
👋 Welcome to my SwiftUI demo showcasing advanced animation and layout techniques!
I'm John Durcan, a seasoned iOS and Mac App Developer passionate about creating intuitive and engaging apps.
🌐 Connect with me on LinkedIn: http:linkedin.com/in/john-durcan
🌟 Check out my portfolio at: https:itch.studio
📱 Explore my AI-powered poetry app: https:poeticai.info
💻 View more of my work on GitHub: https:github.com/JohnnyD1776
☕ Support my development journey: https:ko-fi.com/JohnnyD1776
File Description: Demonstrates ContentUnavailable. Documentation at https://developer.apple.com/documentation/swiftui/contentunavailableview
*/
import SwiftUI
struct Movie: Identifiable, Hashable {
let id = UUID()
let title: String
let year: Int
}
struct ContentUnavailableDemo: View {
@State private var searchText = ""
@State private var searchResults: [Movie] = []
@State private var isLoading = false
@State private var hasError = false
@State private var mockMovies = [
Movie(title: "Inception", year: 2010),
Movie(title: "The Matrix", year: 1999),
Movie(title: "Interstellar", year: 2014),
Movie(title: "Dune", year: 2021)
]
var body: some View {
NavigationStack {
List {
ForEach(searchResults) { movie in
HStack {
Image(systemName: "film")
.foregroundStyle(.blue)
VStack(alignment: .leading) {
Text(movie.title)
.font(.headline)
Text("(\(movie.year))")
.font(.subheadline)
.foregroundStyle(.secondary)
}
}
}
}
.navigationTitle("Movie Search")
.searchable(text: $searchText, prompt: "Search for a movie...")
.overlay {
if searchResults.isEmpty && searchText.isEmpty {
// Search prompt state
ContentUnavailableView {
Label("Search", systemImage: "magnifyingglass")
} description: {
Text("Type a movie title to search")
.foregroundStyle(.secondary)
}
} else if isLoading {
ContentUnavailableView {
ProgressView()
} description: {
Text("Loading…")
.foregroundStyle(.secondary)
}
} else if hasError {
ContentUnavailableView {
Label("Search Failed", systemImage: "exclamationmark.triangle.fill")
} description: {
Text("Something went wrong. Try again?")
} actions: {
Button("Retry") {
performSearch()
}
.buttonStyle(.borderedProminent)
}
} else if searchResults.isEmpty {
ContentUnavailableView.search(text: searchText)
}
}
.onSubmit(of: .search) {
performSearch()
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Error Mode") {
hasError = true
searchResults = []
}
.disabled(isLoading)
}
}
}
}
private func performSearch() {
isLoading = true
hasError = false
Task {
try? await Task.sleep(nanoseconds: 1_000_000_000) // Simulate network delay
await MainActor.run {
simulateSearch(for: searchText)
isLoading = false
}
}
}
private func simulateSearch(for query: String) {
searchResults = mockMovies.filter { $0.title.localizedCaseInsensitiveContains(query) }
if searchResults.isEmpty && !query.isEmpty {
// Force no results for demo
searchResults = []
}
hasError = query == "error"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment