Reputation: 109
My WatchOS app running on Core Data. There are 2 Views there:
I also added List
as I want to use .onDelete
in my App.
I am trying to limit the user in creation infinite number of Goals. I'd like to give an opportunity of adding maximum 10 Goals, then the AddGoal button becomes disabled. Do you have any suggestions how I can do that?
First View Code:
struct FirstView: View {
@Environment(\.managedObjectContext) var context
@FetchRequest (
entity:NewGoal.entity(),
sortDescriptors:[NSSortDescriptor(keyPath: \NewGoal.dateAdded, ascending: false)],
animation: .easeInOut )
var results:FetchedResults<NewGoal>
var body: some View {
VStack{
VStack(alignment: .leading){
NavigationLink(
destination: AddGoalView(),
label: {
Text("Set Money Goal")})
Text("10/10")
VStack(alignment: .leading){
List{
ForEach(results){ item in
NavigationLink(
destination: TABmodule(goalItem: item, GTitle: item.goalTitle ?? "", Sum: item.neededSum, summarize: item.yourSum),
label: {
HStack{
HStack(alignment: .center, spacing:0){
Text(String(item.indDatenum))
Text(item.pickedValueDateS ?? "")
}
VStack(alignment: .leading){
Text(item.goalTitle ?? "")
HStack(spacing: 0){
Text("$\(item.yourSum, specifier: "%.f")")
Text("/ $\(item.neededSum, specifier: "%.f")")
}
}
}
}).clipShape(RoundedRectangle(cornerRadius: 9))
} .onDelete(perform: deleteGoal2)
}.listStyle(PlainListStyle())
}
}
}
}
func deleteGoal2(at offsets: IndexSet) {
for index in offsets {
let item = results[index]
context.delete(item)
}
do{
try context.save()
}catch let err{
print(err.localizedDescription)
}
}
}
AddGoal func on the AddGoalView:
private func addGoal(){
if index == 0 {
pickedValueDateS = "W"
pickedValueDateN = 7 * indexnum
pickedValueDateN1 = 7 * indexnum
} else if index == 1{
pickedValueDateS = "M"
pickedValueDateN = 30 * indexnum
pickedValueDateN1 = 30 * indexnum
} else if index == 2{
pickedValueDateS = "Y"
pickedValueDateN = 365 * indexnum
pickedValueDateN1 = 365 * indexnum
} else {
pickedValueDateS = "N/A"
pickedValueDateN = 1
}
let goal = NewGoal(context: context)
goal.goalTitle = goalTitle
goal.dateAdded = Date()
goal.neededSum = neededSum
goal.pickedValueDateS = pickedValueDateS
goal.allNewSum = allNewSum
goal.pickedValueDateN = Int64(pickedValueDateN)////ALL DAYS
goal.pickedValueDateN1 = Int64(pickedValueDateN1)////MINUS DAYS
goal.dayChange = Int64(daychange)
goal.indDate = Int64(index)
goal.indDatenum = Int64(indexnum)
do{
try context.save()
presentationMode.wrappedValue.dismiss()
}catch let err{
print(err.localizedDescription)
}
}
Upvotes: 1
Views: 579
Reputation: 29301
Add this to any View
you want disabled like the NavigationLink
so the user can't go to that View
.disabled(results.count >= 10)
Upvotes: 1