TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Day 47

Forums > 100 Days of SwiftUI

Hi, Searching for some help again.
I'm busy with day 47 for some days now. On purpose. Since I notice I do follow most of the things that are told, but feel like I black out the moment I need to do something myself. So I'm ussing this project to go back to the others, watch/read back on the course. And hope that will help me to not blackout.

I think that I make things more complicated then needed. But I'm stuck on: "for a tough challenge – see the hints below! – make that detail screen contain how many times they have completed it, plus a button incrementing their completion count.".

My idea was to make an list of all the times the user completed the task. And also a total of times. I struggled around with dates and got it working. My problem is now that I can't seem to add new dates to the array inside the struct.
I found some topics here on the Forum from before in wich for example @Obelix explains that it is not possible https://www.hackingwithswift.com/forums/swiftui/what-is-meant-by-self-is-immutable-in-this-example/15188https://www.hackingwithswift.com/forums/swiftui/what-is-meant-by-self-is-immutable-in-this-example/15188

There is also on solution given, but I don't know how to get that working in my situation. Since the data also needs to be saved so that when the user closes the app and opens them again, the data is still there. Can somebody give me some hints on how I can do this?

2      

From what I understand, it should be possible? My struct:

struct HabitName: Identifiable, Codable {
    var id = UUID()
    var habitName: String
    var habitDiscription: String
    var habTrack: [String]

    mutating func addTrack(add: String) {
        habTrack.append(add)
    }

}

From what I am saying, the mutating function should make it possible to add somthing to habTrack.

somewhere alse I have in my code:

                        habit.addTrack(add: curDate)

The error i'm (still) getting is :

"Cannot use mutating member on immutable value: 'self' is immutable"

2      

@suzanne struck the wrong note on her struct.

Cannot use mutating member on immutable value: 'self' is immutable

We don't see the code where you defined the variable habit.

For example:

// (1) Define habit as a constant
let habit: HabitName // <-- Hey compiler I have a struct of type HabitName. 
// Make one for me, but make it constant!

// or

// (2) Define habit as a variable
var habit: HabitName // <-- Hey compiler I have a struct of type HabitName. 
// Make one for me, but note I may change it from time to time.

Because you explicitly tell the compiler that (1) is a constant (LET) the compiler will never, ever let you change it.

Because you explicitly tell the compiler that (2) is a variable (VAR) the compiler will let you change what is stored by the variable.

Keep coding

Please let us know how you solved this!

2      

I will add more of the code.. Tbh, I am a bit afraid of posting it since of the naming of functions, variables and others. And maybe it not all logical to others.

The Struct is created as mentiond above.

I'm creating an class

class Habits: ObservableObject, Identifiable {
    @Published var habitNames = [HabitName]() {
        didSet {
            if var encoded = try? JSONEncoder().encode(habitNames) {
                UserDefaults.standard.set(encoded, forKey: "HabitNames")
            }
        }
    }

    init() {
        if var savedItems = UserDefaults.standard.data(forKey: "HabitNames") {
            if var decodedItems = try? JSONDecoder().decode([HabitName].self, from: savedItems) {
                habitNames = decodedItems
                return
            }
        }
        habitNames = []
    }

}

The three times var her where original let. I changed them to see if it makes a difference.

My function to get the currentDate and formatted to how I want it:

func getDate() -> String {
    let currentDate = Date.now
    let formatter = DateFormatter()
    formatter.dateFormat = "E d MMM y, HH:mm"

    let dateFormatted = formatter.string(from: currentDate)
    return(dateFormatted)
}

One of the Views, This one works as an sheet

struct addHabit: View {
    @Environment(\.dismiss) var dismiss
    @ObservedObject var habits: Habits

    var curDate = getDate()

    @State private var habName = ""
    @State private var habDiscription = ""

    var body: some View {
        NavigationView {
            Form {
                TextField("Habit name", text: $habName)
                TextField("Discription", text: $habDiscription)
            }
            .navigationTitle("Add new Habit")
            .toolbar {
                Button("Save") {
                    var habit = HabitName(habitName: habName, habitDiscription: habDiscription, habTrack: ["added on \(curDate)"])
                    habits.habitNames.append(habit)
                    dismiss()
                }
            }
        }
    }
}

Where you see 'var habit = ' I had let before. Changing it to var doesn't seem to make an difference.

My ContentView

struct ContentView: View {
    @State private var showAddHabit = false

    @StateObject var habits = Habits()

    var body: some View {
        let columns = [
            GridItem()
        ]

        NavigationView {
            ScrollView {
                LazyVGrid(columns: columns) {
                    ForEach(habits.habitNames) { habit in
                        NavigationLink {
                            discriptionView(habits: habits, habit: habit)
                        } label: {
                            VStack {
                                VStack {
                                    Text(habit.habitName)
                                }
                                .padding(.vertical)
                                .frame(maxWidth: .infinity)
                            }
                            .clipShape(RoundedRectangle(cornerRadius: 10))
                            .overlay(
                                RoundedRectangle(cornerRadius: 10)
                                    .stroke(.blue)
                            )
                        }
                    }
                    .onDelete(perform: removeHabit)
                }
            }
            .navigationTitle("TrackYourHabit")
            .padding()
            .font(.subheadline)
            .toolbar {
                Button {
                    showAddHabit = true
                } label: {
                    Image(systemName: "plus")
                }
                .sheet(isPresented: $showAddHabit) {
                    addHabit(habits: habits)
                }
            }
        }

    }
}

And the last View

struct discriptionView: View {
    @ObservedObject var habits: Habits
    @State private var tracking = false
    @State private var curDate = getDate()

    var habit: HabitName

    var body: some View {
        Section {
            VStack {
                Text(habit.habitDiscription)
                    .CustomFontStyle()
            }
            .padding()

            VStack {
                Button("Track Habbit") {
                    tracking = true
                }
                .padding()
                .background(.green)
                .foregroundColor(.white)
                .clipShape(RoundedRectangle(cornerRadius: 25, style: .circular))
                .font(.title)
                .alert("Did you finish \(habit.habitName)?", isPresented: $tracking) {
                    Button("Cancel") {}
                    Button("Save") {
                        habit.addTrack(add: curDate)

                    }
                } message: {
                    Text("Do you want to track \(habit.habitName) now?")
                }
            }

            VStack {

                ForEach(habit.habTrack, id: \.self) { track in
                    Text(track)
                }
            }

            Spacer()
            Spacer()
            Spacer()

        }
        .navigationTitle(habit.habitName)
        .navigationBarTitleDisplayMode(.large)
    }
}

In this view where there is an Button and I try "habit.addTrack(add: curDate)" I get the error

2      

I think I fixed it. But not sure if it is realy correct.

In my discriptionView I changed

    var habit: HabitName

to

    @State var habit: HabitName

Now it does work like I want to. Only not sure if it is really logic.

2      

Hi @suzanne! This is the way it should work. As you change it within view, i has to be marked as @State. Remember that when you change any property in the struct, the whole struct is created again. With @State wrapper the value is "stored" outside the struct so it will be the same upon other changes you implement and when view refreshes. E.g. if you view is refreshed and habit is not stored in @State it's gone for goods... So that is why compiler barks at you and won't let you go without it, because upon next view refresh it just crashes.

2      

@ygeras I don't completly follow what you are saying.. I notice that it doesn't work like I want and I do think that that is wat you are telling me.

In my discriptionView I made the change mentioned in my last comment. I also did some Styling changes. Since I thought it was working so I wil give my discriptionView again:

struct discriptionView: View {
    @ObservedObject var habits: Habits
    @State private var tracking = false

    @State var habit: HabitName

    var body: some View {
        VStack {
            Section {
                VStack {
                    Text(habit.habitDiscription)
                        .CustomFontStyle()
                }
                .padding()

                VStack {
                    Button("Track Habbit") {
                        tracking = true
                    }
                    .modifier(ButtonLayout())
                    .alert("Did you finish \(habit.habitName)?", isPresented: $tracking) {
                        Button("Cancel") {}
                        Button("Save") {
                            habit.addTrack(add: "completed on: \(getDate())")

                        }
                    } message: {
                        Text("Do you want to track \(habit.habitName) now?")
                    }
                }
                VStack {
                    ForEach(habit.habTrack, id: \.self) { track in
                        Text(track)
                    }
                }
                Spacer()
                Spacer()
                Spacer()

            }
            .navigationTitle(habit.habitName)
            .navigationBarTitleDisplayMode(.large)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .modifier(BackGroundColor())

    }
}

If I click the Button, click safe, the data is saved. I can see it in the Foreach from this view. But When I go back to the previous view and then back to this one, the tracked data is gone :(.
So I guess I did manage to get one step further, but it still doesn't work.. I was conviced it worked earlier today. But I don't think I can have broke it with style changes.

2      

Hi @suzanne! I just made some small changes in your data model. You can adjust to your needs as well. As you can see the main change is in the observable object. Hope all is clear there. If not, post a question what is not clear.

struct ContentView: View {
    @State private var showAddHabit = false
    @StateObject var habits = Habits()

    var body: some View {
        let columns = [
            GridItem()
        ]

        NavigationView {
            ScrollView {
                LazyVGrid(columns: columns) {
                    ForEach(habits.habitNames) { habit in
                        NavigationLink {
                            discriptionView(habits: habits, habit: habit)
                        } label: {
                            VStack {
                                VStack {
                                    Text(habit.habitName)
                                }
                                .padding(.vertical)
                                .frame(maxWidth: .infinity)
                            }
                            .clipShape(RoundedRectangle(cornerRadius: 10))
                            .overlay(
                                RoundedRectangle(cornerRadius: 10)
                                    .stroke(.blue)
                            )
                        }
                    }
                }
            }
            .navigationTitle("TrackYourHabit")
            .padding()
            .font(.subheadline)
            .toolbar {
                Button {
                    showAddHabit = true
                } label: {
                    Image(systemName: "plus")
                }
                .sheet(isPresented: $showAddHabit) {
                    addHabit(habits: habits)
                }
            }
        }

    }
}

struct HabitName: Identifiable, Codable {
    var id = UUID()
    var habitName: String
    var habitDiscription: String
    var habTrack: [Date]
}

class Habits: ObservableObject, Identifiable {
    @Published var habitNames = [HabitName]() {
        didSet {
            if let encoded = try? JSONEncoder().encode(habitNames) {
                UserDefaults.standard.set(encoded, forKey: "HabitNames")
            }
        }
    }

    init() {
        if let savedItems = UserDefaults.standard.data(forKey: "HabitNames") {
            if let decodedItems = try? JSONDecoder().decode([HabitName].self, from: savedItems) {
                habitNames = decodedItems
                return
            }
        }
        habitNames = []
    }

    func addTracker(habit: HabitName, date: Date) {
        if let index = habitNames.firstIndex(where: { $0.id == habit.id }) {
            habitNames[index].habTrack.append(date)
        }
    }

}

struct addHabit: View {
    @Environment(\.dismiss) var dismiss
    @ObservedObject var habits: Habits

    @State private var habName = ""
    @State private var habDiscription = ""

    var body: some View {
        NavigationView {
            Form {
                TextField("Habit name", text: $habName)
                TextField("Discription", text: $habDiscription)
            }
            .navigationTitle("Add new Habit")
            .toolbar {
                Button("Save") {
                    let habit = HabitName(habitName: habName, habitDiscription: habDiscription, habTrack: [Date()])
                    habits.habitNames.append(habit)
                    dismiss()
                }
            }
        }
    }
}

struct discriptionView: View {
    @ObservedObject var habits: Habits
    @State private var tracking = false

    var habit: HabitName

    var body: some View {
        Section {
            VStack {
                Text(habit.habitDiscription)
            }
            .padding()

            VStack {
                Button("Track Habbit") {
                    tracking = true
                }
                .padding()
                .background(.green)
                .foregroundColor(.white)
                .clipShape(RoundedRectangle(cornerRadius: 25, style: .circular))
                .font(.title)
                .alert("Did you finish \(habit.habitName)?", isPresented: $tracking) {
                    Button("Cancel") {}
                    Button("Save") {
                        habits.addTracker(habit: habit, date: Date())

                    }
                } message: {
                    Text("Do you want to track \(habit.habitName) now?")
                }
            }

            VStack {

                ForEach(habit.habTrack, id: \.self) { track in
                    Text(track.formatted())
                }
            }

            Spacer()
            Spacer()
            Spacer()

        }
        .navigationTitle(habit.habitName)
        .navigationBarTitleDisplayMode(.large)
    }
}

2      

Thankyou soo much!
I think I was close in my attempts today. In the challange it said:

Use firstIndex(of:) to find where the previous activity was in the class’s array, then change it to be your new activity – something like data.activities[index] = newActivity will work. (This requires the Equatable conformance from step 1!)

I did try things with Index but did not get it working. I played around with your code and understand what it is doing and how. So thankyou! I think this will also help me with my next task: let user delete things from the habit list.

I did make some small changes to it:

func addTracker(habit: HabitName) {
            if let index = habitNames.firstIndex(where: { $0.id == habit.id }) {
                habitNames[index].habTrack.insert(getDate(), at: 0)
            }
        }

So the new entry will nou be added at the top.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Reply to this topic…

You need to create an account or log in to reply.

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.