Swiftmas – Day 05 – Decorations

Posted by

·

It’s time to decorate. Are you a lights all over the house kind of decorator or a simple small tree kind of decorator?

For the Fifth prompt, we have Decorations. What will you create?

I’m looking forward to seeing what you come up with today.

Planning your next DIY decor project? How about creating an app to help you manage and track your tasks? In this tutorial, you’ll build a DIY Decor Planner in SwiftUI. This app will allow you to:

  • Add tasks to your project list.
  • Mark tasks as completed or pending.
  • Delete tasks you no longer need.

By the end of this tutorial, you’ll have a functional app and ideas for expanding it further.

Getting Started


1. Set Up Your Project

You can find the sample project in the GitHub repository here

Create a new SwiftUI project in Xcode:

  1. Open Xcode and select Create a new project.
  2. Choose the App template, click Next.
  3. Enter your project name (e.g., “DIY Decor Planner”), set the interface to SwiftUI, and click Create.
  4. Download your bg image here.

Step 1: Define the Task Model

First, create a simple model to represent a task:

Explanation:

  • The Task struct contains:
    • id: A unique identifier for each task.
    • name: The name of the task.
    • isCompleted: A boolean to track whether the task is completed.
  • The Identifiable protocol lets SwiftUI handle lists of tasks.

Step 2: Set Up State Properties

In the ContentView, add properties to manage tasks and input:

Explanation:

  • tasks: Holds all the tasks in your planner.
  • newTaskName: Binds to the TextField for entering new tasks.

Step 3: Create the Main Layout

Wrap the interface in a NavigationView and add two main sections:

  1. Input Section: A TextField for adding new tasks.
  2. Task List: A List to display tasks.

Key Features in the Layout:

  • Task Input:
    • TextField allows users to enter the task name.
    • A button calls the addTask function to add tasks to the list.
  • Task List:
    • List dynamically displays tasks from the tasks array.
    • Buttons toggle the task’s completion status using toggleTaskCompletion.
    • Tasks can be deleted using SwiftUI’s onDelete.

Step 4: Add Functionality

Add Tasks

The addTask function appends a new task to the tasks array:

Toggle Task Completion

This function toggles the isCompleted status of a task:

Delete Tasks

The deleteTask function removes tasks by their index:


Step 5: Run Your App

  1. Run the app on the iOS Simulator or your device.
  2. Test the features:
  • Add tasks to your planner.
  • Mark tasks as completed.
  • Delete tasks from the list.

Complete Code

Here’s the full code for the DIY Decor Planner:


What’s Next?

Take your DIY Decor Planner to the next level! Here are some ideas:

  1. Add Due Dates: Let users assign deadlines to tasks and display tasks in order of urgency.
  2. Task Categories: Group tasks by category (e.g., painting, crafting, shopping).
  3. Notifications: Send reminders for incomplete tasks.
  4. Cloud Sync: Use CloudKit or Firebase to sync tasks across devices.
  5. Custom Themes: Allow users to choose from different color schemes and background images.

🎉 Congratulations! You’ve successfully built a DIY Decor Planner in SwiftUI. Now you can organize your projects and manage tasks with ease. Happy coding, see you tomorrow!


Discover more from

Subscribe to get the latest posts sent to your email.

thecodingsprite avatar

About the author

Hi! My name is Billie, my friends call me Billie Boo. I am a self taught iOS developer with a background in computer science, animation, graphic design & web design. I love sharing my knowledge & projects with the world & that is my mission for this blog. It’s never too late or too hard to follow your dreams.