Home > Enterprise >  Weekly activity summary help? SWIFTUI
Weekly activity summary help? SWIFTUI

Time:03-02

I'm tryin' to obtain a list of activities ("dd/mm/YY: goal achieved/missed goal") which has to be setted every week. The problem is that I obtain a list of activities with the same date and the same result of the previous one. For example:

28/02/2022: goal achieved
28/02/2022: goal achieved
28/02/2022: goal achieved

and the next day:

01/03/2022: missed goal
01/03/2022: missed goal
01/03/2022: missed goal
01/03/2022: missed goal

I want to obtain, instead, a list like:

28/02/2022: goal achieved
01/03/2022: missed goal
02/03/2022: goal achieved...

These are useful structs:

struct Persistent {
 @AppStorage("goalachieved") static var goalachieved : Bool = false
 @AppStorage("activitylist") static var activitylist : [String] = []
}

struct obj {
static var currentDate = Date()
static var stringDate = ""
static var activity = Activity(date:Persistent.lastUpdatedDate)
}

This is the ActivityListView:

import SwiftUI

func activitystring(activity:Activity) -> String{
    var output = ""
    output = "\(obj.activity.date): \(activity.reachedobj(goal achieved: Persistent.goalachieved))"
    return output
    
}

struct Activity: Identifiable{
    
    let id = UUID()
    let date: String
    
    func reachedobj(goalachieved: Bool) -> String {
         var output = ""
        if Persistent.goalachieved == false { output = "Missed goal." }
        if Persistent.goalachieved == true { output = "Goal achieved!"}
         
         return output
         
     }
    
}

struct ActivityRow: View{
    
    var activity: Activity
    
    
    var body: some View{
        Text(activitystring(activity:obj.activity))
        Divider()
    }
}



struct ActivityListView: View {
    
    
    var body: some View {
        
        ScrollView{
            
            Text("Week summary").font(.system(size: 15)).foregroundColor(Color.green)
            
            Text("")
            
        ForEach(Persistent.activitylist, id: \.self) { activity in 
           
            ActivityRow(activity: obj.activity)
            
            }
        
        }
    }
    
}

struct ActivityListView_Previews: PreviewProvider {
    static var previews: some View {
        ActivityListView()
    }
}

Finally this is the useful code in the ApplicationApp file (main) where I update activity list:

MenuView().onAppear(){
                    
       if Persistent.activitylist.count>7{
          Persistent.activitylist.removeAll()
       }
                    
      obj.currentDate = Date()
      let formatter = DateFormatter()
      formatter.dateFormat = "dd/MM/YY"
      obj.stringDate = formatter.string(from:obj.currentDate)

      if Persistent.lastUpdatedDate != obj.stringDate{

      Persistent.goalachieved = false
      let activity = Activity(date: Persistent.lastUpdatedDate)
      Persistent.activitylist.append(activitystring(activity: activity))
      Persistent.lastUpdatedDate = obj.stringDate

      }                  
 }
    

What's wrong on this?

CodePudding user response:

In your Persistent object you have an array of many activities, called activitylist , but one single boolean that tells if the goal is achieved - goalachieved indeed.

Your view is iterating through the array of Persistent.activitylist, so you will have many lines for one single result - achieved or not achieved. You might actually want to iterate over an array of Persistent objects - meaning that somewhere you should probably store [Persistent] in some variable. In this way, you will see one line only for each result.

If I also may suggest: use the conventions for naming variables, Swift uses "camelCaseConventionForVariables", easier to read than "thewholevariableislowercase"

CodePudding user response:

You're calling obj.activity in your ForEach and ActivityRow, that's why it repeats that same static property all over the place.

You better just drop your struct obj and try again without it

  • Related