A SwiftUI application that fetches random jokes from a remote API, displays them to the user, and allows saving favorites. This project demonstrates basic data fetching, SwiftUI layout, audio playback, and local data persistence using UserDefaults
.
The Joke Assembly allows users to:
- Fetch a random joke from joke.deno.dev based on a selected Joke Type (e.g., general, dad, knock-knock).
- Toggle whether or not the punchline is shown.
- Play a short audio clip (if enabled) when revealing the punchline.
- Save jokes to a Favorites list using
UserDefaults
. - View and delete saved jokes from the Favorites list.
The UI is built in SwiftUI, with a simple TabView
to switch between Home and Favorites screens.
-
Purpose:
- Fetches jokes from the remote API using
URLSession
andasync/await
. - Decodes the JSON into a
Joke
model. - Maintains a list of favorite jokes, which are stored in
UserDefaults
.
- Fetches jokes from the remote API using
-
Key Methods:
getData()
: Makes a network call to fetch jokes.addToFavorites(_ joke: Joke)
: Saves a selected joke to thefavoriteJokes
array.loadFavorites()
&saveFavorites()
: Handle persistence of favorites inUserDefaults
.
-
Purpose:
- Acts as the main screen (and root of the
TabView
). - Contains Home UI elements:
- Get Joke button: Fetch a new joke on demand.
- Punchline button: Reveals the punchline, optionally playing a sound effect.
- Star Icon (blue): Adds the current joke to Favorites.
- Showcases an enum of joke types (e.g.
.dad
,.food
,.anime
) which can be selected from a Picker to filter jokes.
- Acts as the main screen (and root of the
-
Additional UI Details:
- A
Toggle
to enable or disable sound effects. - A
Spacer()
to organize layout. - Animations and transitions for showing/hiding the punchline.
- A
- Purpose:
- Displays the user’s favorite jokes in a
List
. - Uses SwiftUI’s
.onDelete
to allow swipe-to-delete functionality. - Provides a scrollable list to handle multiple favorite jokes.
- Shows the setup in black and the punchline in cyan.
- Displays the user’s favorite jokes in a
- Platform: iOS (SwiftUI-based).
- Xcode: Version 14 or later is recommended.
- Swift version: 5.6 or later.
- iOS Deployment Target: iOS 15+ (adjust as needed in your Xcode project settings).
- Clone or download the repository.
- Open the
.xcodeproj
or.xcworkspace
file in Xcode. - Select an iOS simulator (e.g., iPhone 14).
- Press the Run button (\▶) to build and launch the app.
You should see two tabs at the bottom: Home and Favorites. Fetch jokes on demand using the Get Joke button, toggle punchlines, and save jokes to favorites.
We welcome contributions from the community. Here are the steps to contribute:
-
Fork this repository.
- Click the “Fork” button in the top-right corner of the GitHub page.
-
Create a New Branch for your feature or bug fix.
-
git checkout -b feature/amazing_feature
-
-
Make Changes & Commit
- Implement your feature or fix the bug.
- Test thoroughly.
- Commit changes with clear, concise messages:
git commit -m "Add amazing feature"
-
Push to Your Fork
-
git push origin feature/amazing_feature
-
-
Open a Pull Request
- In your fork on GitHub, click the Compare & pull request button.
- Provide a detailed description of your changes, including any relevant issue references.
-
Code Review & Merging
- The maintainers will review your pull request.
- Discuss any suggested changes.
- Once approved, your contribution will be merged.
Please follow the code style, naming conventions, and best practices established in the project to keep the codebase consistent.