UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Challenge project on consolidation 7-9

Forums > 100 Days of SwiftUI

Hey there,

I just completed the challenge set in consolidation part 7-9, which was:

  • At the very least, this means there should be a list of all activities they want to track, plus a form to add new activities – a title and description should be enough.
  • For a bigger challenge, tapping one of the activities should show a detail screen with the description, how many times they have completed it, plus a button incrementing their completion count.
  • For an even bigger challenge, use Codable and UserDefaults to load and save all your data.

It works fine, but still I am wondering how did you completed it, especially point 2 of incrementing number of executions?

As I designed following

struct Activity: Codable, Identifable {
  var id = UUID()
  var name: String
  var description: String
  var performed: Int = 0
}

and

class Activities: @ObservableObject {
  @Published items = [Activity]()
}

it's clear I cannot simply increase activity.performed in activity detail view when passed from a list of activities. So I used @EnvironmentObject wrapper and it is working fine. But still wondering if this can be done any other way, as the property wrapper I used hasn't been mentioned in projects done so far?

Best, Kris.

2      

My ContentView show a list of activities, each activity is shown in an ActivityView. The list has a + button in the right of the navigation bar that show a AddActivityView.

A NavigationLink is associated with each item of the list. The link go to an ActivityCompletionView.

The following variables are declared in the ActivityCompletionView struct

    @State var activity: Activity
    @ObservedObject var activities: Activities

In the view I have the following section.

                    Section(header: Text("Completion")) {
                        HStack {
                        Text("\(self.activity.completionCount, specifier: "%d") times")
                        Spacer()
                        Button(action: {
                             self.augmentCompletionCount()
                         }) {
                             Image(systemName: "plus")
                         }
                        }
                    }

And the ActivityCompletionView struct has the augmentCompletionCount().

    func augmentCompletionCount() {
        let index = activities.activityIndex(of: activity.id)!
        activities.items[index].completionCount += 1
        activity.completionCount += 1
    }

I should have avoided keeping two things synchonized here. And also avoided a UUID to index conversion "activityIndex(of: activity.id)" but I was in a hurry to go to the next session :-)

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.