A SwiftUI app that fetches and displays top-rated movies and movies by genre using the TMDB API. The app leverages Swift Concurrency (async/await
and MainActor
) and follows the MVVM architecture for clean code structure.
- Discover Top-Rated Movies: View a list of the highest-rated movies from TMDB.
- Browse by Genre: Explore movies categorized by genres like Action, Comedy, Drama, and more.
- Swift Concurrency: Smooth and efficient data fetching using
async/await
. - Responsive UI: Built with SwiftUI for seamless and adaptive user experience.
- Modern Architecture: Implements the MVVM pattern for scalability and maintainability.
- iOS 16.0+
- Xcode 14.0+
- TMDB API Key (Sign up at TMDB API to get your API key.)
-
Clone the Repository:
git clone https://github.com/yourusername/swiftui-movies-app.git cd swiftui-movies-app
-
Install Dependencies:
- Open the project in Xcode, and it will automatically fetch dependencies via Swift Package Manager.
-
Set up TMDB API Key:
- Add your API key to
Constants.swift
:struct Constants { static let apiKey = "YOUR_API_KEY_HERE" static let baseURL = "https://api.themoviedb.org/3" }
- Add your API key to
-
Run the App:
- Select your target device in Xcode and press
Run
(⌘R).
- Select your target device in Xcode and press
This app follows the MVVM (Model-View-ViewModel) architecture:
- Represents movie-related data structures (
Movie
,Genre
, etc.). - Handles decoding of JSON responses from the TMDB API.
- Acts as the intermediary between Views and Models.
- Manages state, processes data, and fetches information using
async/await
.
- SwiftUI-based components for rendering the UI.
- Reactive UI updates with
@State
and@Published
properties.
- APIManager.swift: Handles network requests to the TMDB API using
URLSession
andasync/await
. - MovieViewModel.swift: Business logic and state management for movies.
- GenreViewModel.swift: Fetches and filters movies based on genre.
- Constants.swift: Holds API keys and base URLs.
- Movie.swift: Model for movie data from TMDB.
- Genre.swift: Model for genres.
The app utilizes Swift Concurrency for efficient data fetching:
async/await
:- Used in API calls for cleaner, asynchronous code.
func fetchMovies() async throws -> [Movie] { let url = URL(string: "\(Constants.baseURL)/movie/top_rated?api_key=\(Constants.apiKey)")! let (data, _) = try await URLSession.shared.data(from: url) return try JSONDecoder().decode(MovieResponse.self, from: data).results }