Habit Tracker

Habit Tracker

by dockui

Clean and intuitive habit tracker UI template, designed to help users visually track their daily habits

Platform
SwiftUI
Code
Native Swift
Format
Screen
Access
Free

Code Snippet

import SwiftUI
import Charts

struct Habit: Identifiable {
    let id = UUID()
    let name: String
    let icon: String
    let streak: Int
    let completionRate: Double
    let weeklyData: [(day: String, completed: Int)]
}

struct HabitTrackingView: View {
    // Sample data
    let habits = [
        Habit(
            name: "Meditation",
            icon: "brain.head.profile",
            streak: 7,
            completionRate: 0.85,
            weeklyData: [
                ("Mon", 1), ("Tue", 1), ("Wed", 0),
                ("Thu", 1), ("Fri", 1), ("Sat", 1), ("Sun", 1)
            ]
        ),
        Habit(
            name: "Exercise",
            icon: "figure.run",
            streak: 4,
            completionRate: 0.71,
            weeklyData: [
                ("Mon", 1), ("Tue", 1), ("Wed", 1),
                ("Thu", 0), ("Fri", 1), ("Sat", 0), ("Sun", 1)
            ]
        )
    ]
    
    var body: some View {
        NavigationStack {
            ScrollView {
                VStack(spacing: 20) {
                    // Stats Overview
                    statsOverview
                    
                    // Weekly Progress Chart
                    weeklyProgressChart
                    
                    // Habits List
                    habitsList
                }
                .padding()
            }
            .navigationTitle("Habits")
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button(action: {}) {
                        Image(systemName: "plus.circle.fill")
                            .foregroundStyle(.blue)
                    }
                }
            }
        }
    }
    
    private var statsOverview: some View {
        HStack {
            StatCard(title: "Total Habits", value: "\(habits.count)", icon: "list.bullet", color: .blue)
            StatCard(title: "Completion Rate", value: "78%", icon: "chart.line.uptrend.xyaxis", color: .green)
        }
    }
    
    private var weeklyProgressChart: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Weekly Progress")
                .font(.title3)
                .fontWeight(.semibold)
            
            Chart {
                ForEach(habits) { habit in
                    ForEach(habit.weeklyData, id: \.day) { data in
                        BarMark(
                            x: .value("Day", data.day),
                            y: .value("Completed", data.completed)
                        )
                        .foregroundStyle(by: .value("Habit", habit.name))
                    }
                }
            }
            .frame(height: 200)
            .chartForegroundStyleScale([
                "Meditation": Color.blue,
                "Exercise": Color.green
            ])
        }
        .padding()
        .background(Color(.systemBackground))
        .clipShape(RoundedRectangle(cornerRadius: 15))
        .shadow(color: .black.opacity(0.1), radius: 5)
    }
    
    private var habitsList: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Your Habits")
                .font(.title3)
                .fontWeight(.semibold)
            
            ForEach(habits) { habit in
                HabitRow(habit: habit)
            }
        }
    }
}

struct StatCard: View {
    let title: String
    let value: String
    let icon: String
    let color: Color
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack {
                Image(systemName: icon)
                    .foregroundStyle(color)
                Text(title)
                    .foregroundStyle(.secondary)
            }
            .font(.subheadline)
            
            Text(value)
                .font(.title2)
                .fontWeight(.bold)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding()
        .background(Color(.systemBackground))
        .clipShape(RoundedRectangle(cornerRadius: 15))
        .shadow(color: .black.opacity(0.1), radius: 5)
    }
}

struct HabitRow: View {
    let habit: Habit
    
    var body: some View {
        HStack {
            Image(systemName: habit.icon)
                .font(.title2)
                .frame(width: 40, height: 40)
                .background(Color.blue.opacity(0.1))
                .clipShape(Circle())
                .foregroundStyle(.blue)
            
            VStack(alignment: .leading, spacing: 4) {
                Text(habit.name)
                    .fontWeight(.semibold)
                
                Text("\(habit.streak) day streak")
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
            }
            
            Spacer()
            
            CircularProgressView(progress: habit.completionRate)
                .frame(width: 40, height: 40)
        }
        .padding()
        .background(Color(.systemBackground))
        .clipShape(RoundedRectangle(cornerRadius: 15))
        .shadow(color: .black.opacity(0.1), radius: 5)
    }
}

struct CircularProgressView: View {
    let progress: Double
    
    var body: some View {
        ZStack {
            Circle()
                .stroke(Color.gray.opacity(0.2), lineWidth: 4)
            
            Circle()
                .trim(from: 0, to: progress)
                .stroke(Color.blue, style: StrokeStyle(lineWidth: 4, lineCap: .round))
                .rotationEffect(.degrees(-90))
            
            Text("\(Int(progress * 100))%")
                .font(.caption2)
                .fontWeight(.medium)
        }
    }
}

Similar Blogs

View All Articles
How to Translate Xcode String Catalogs with AI

How to Translate Xcode String Catalogs with AI

Learn how to efficiently translate Xcode string catalogs using AI fast, streamlining localization for your iOS and SwiftUI applications.

by dockui
May 4
How to optimize your SwiftUI app in 2026

How to optimize your SwiftUI app in 2026

Discover top strategies to optimize your SwiftUI app in 2026, enhancing performance, responsiveness, and maintainability with modern concurrency.

by dockui
Apr 29
SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

SwiftUI iOS confirmation alert with Confirm and Cancel buttons. Beginners guide to building safe actions, destructive deletes, and commit dialogs.

by dockui
Nov 11
SwiftUI: IOS Haptic Feedback Guide 2026

SwiftUI: IOS Haptic Feedback Guide 2026

Add click to vibrate haptic feedback in SwiftUI. Full guide to UIImpactFeedbackgenerator, success, error, selection, and tap vibration patterns with code.

by dockui
Nov 11

Code copied to clipboard!