Elevating Fitness Experiences with a Reusable SwiftUI Video Gallery Component

Abdullah Bilgin
Swift Insights
Published in
4 min readAug 18, 2023

Building a Versatile Component for Fitness Apps to Showcase Exercise Videos

Introduction

In the dynamic landscape of app development, the power of creating reusable components cannot be overstated. Recently, I delved into the world of SwiftUI with a goal in mind: crafting a versatile component that fitness app developers could integrate seamlessly. This journey led me to design a SwiftUI component that showcases exercise videos and adds a new dimension to fitness apps. Join me as I share the process behind building this reusable component and its potential to enhance fitness experiences across various apps.

Designing for Reusability: The Video Gallery Component

Instead of building a standalone fitness app, I created a SwiftUI component that could be easily integrated into existing fitness apps. This component, let’s call it ExerciseVideoGallery, is designed to display exercise videos alongside their descriptions. Fitness app developers can plug this component into their projects, enhancing the overall user experience without reinventing the wheel.

The Core: Exercise Data Model

At the heart of the ExerciseVideoGallery component is the Exercise Data model. This model encapsulates crucial details about each exercise, from its name and category to its description and video URL. Leveraging Swift's Codable protocol, I ensured that integrating exercise data from external sources could be seamless.

import SwiftUI

// Defining a struct named Exercise that conforms to the Codable and Identifiable protocols
struct Exercise: Codable, Identifiable {

// Properties of the Exercise struct
let id: Int // Unique identifier for the exercise
let name: String // Name of the exercise
let category: String // Category to which the exercise belongs
let description: String // Description of the exercise
let video: String // URL or identifier for the exercise video

// Computed property to generate a thumbnail name based on the exercise name
var thumbnail: String {
"video-\(name)" // The thumbnail name is constructed as "video-exercisename"
}
}

Versatile Visuals: ExerciseListItemView Custom View

To make the component visually engaging, I designed a custom SwiftUI view called ExerciseListItemView. This view elegantly displays exercise thumbnails, names, and descriptions. Users can initiate video playback effortlessly with an embedded play button icon over the thumbnail. Using customizable fonts, colors, and layouts enhances the component's versatility, allowing it to blend seamlessly into various app designs.

import SwiftUI

// Defining a SwiftUI view for displaying an exercise item
struct ExerciseListItemView: View {
// MARK: - Properties
let exercise: Exercise // The exercise object to display

// MARK: - Body
var body: some View {
HStack(spacing: 10) {
ZStack {
// Displaying the exercise thumbnail image
Image(exercise.thumbnail)
.resizable()
.scaledToFit()
.frame(height: 60)
.cornerRadius(9)
// Displaying a play button icon over the thumbnail
Image(systemName: "play.circle")
.resizable()
.scaledToFit()
.frame(height: 28)
.shadow(radius: 4)
.foregroundColor(.white)
.opacity(0.6)
} //: ZStack

VStack(alignment: .leading, spacing: 10) {
// Displaying the exercise name
Text(exercise.name)
.font(.title3)
.fontWeight(.heavy)
.foregroundColor(.SuccessColor)

// Displaying the exercise description with a maximum of two lines
Text(exercise.description)
.font(.footnote)
.multilineTextAlignment(.leading)
.lineLimit(2)
} //: VStack
} //: HStack
}
}

Dynamic Playback: VideoPlayerView Integration

The star of the show, the VideoPlayerView, seamlessly integrates with AVKit to provide dynamic video playback. This view makes video playback smooth and ensures that users can interact with the video as needed. Incorporating this feature elevates the component from static design to interactive functionality, making it a valuable addition to fitness apps.

import SwiftUI
import AVKit

struct VideoPlayerView: View {
// MARK: - Properties
var videoSelected: String // The name of the selected video file
var videoTitle: String // The title of the video

// MARK: - Body
var body: some View {
// Using VideoPlayer view to display the video
VideoPlayer(player: playVideo(fileName: videoSelected, fileFormat: "mp4"))
}
}

Simple Integration: The Reusable ExerciseVideoGallery

The true power of this component lies in its ease of integration. App developers can incorporate the ExerciseVideoGallery into their projects with minimal effort. By simply passing in exercise data, developers can create an immersive video gallery interface that enriches their fitness app's offerings.

import SwiftUI

struct ContentView: View {

let components: [Components] = Bundle.main.decode("components.json")

var body: some View {
VideoGalleryListView()
}

}

Conclusion: A Tool for Fitness App Developers

As I reflect on this journey of creating a reusable SwiftUI component, I am excited about the potential it holds for fitness app developers. With the ExerciseVideoGallery, developers can elevate the fitness experiences of their users by offering a comprehensive video library of exercises. From design customization to seamless video playback, the component is a testament to how reusability and innovation can intersect in app development.

By incorporating the ExerciseVideoGallery developers can provide users with a dynamic and engaging fitness resource in their fitness apps. The component's reusability ensures that users across different apps can benefit from an enhanced fitness journey.

--

--

Abdullah Bilgin
Swift Insights

"iOS engineer & IT head, crafting code & innovation. Leading with tech prowess & strategic vision. Bridging iOS dev & IT realms. Lifelong learner. 📱💡"