Sorry I am not sure why my contentView didn't post. Here is all of my code that I used from contentView to try and save the selection in the list using the files I already posted -
import SwiftUI
import DeviceActivity
import FamilyControls
import ManagedSettings
struct ScaleButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? 0.95 : 1.0)
.opacity(configuration.isPressed ? 0.8 : 1.0)
}
}
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
// @FetchRequest(sortDescriptors: []) var apps: FetchedResults<AppToken>
@State private var isSelected = false
@State var isPresented = false
@EnvironmentObject var model: DataModel
@EnvironmentObject var dataController: DataController
@State private var authorizationStatus = AuthorizationCenter.shared.authorizationStatus
var body: some View {
ZStack{
Color(red: 33 / 255, green: 33 / 255, blue: 33 / 255)
.ignoresSafeArea()
VStack{
Spacer()
if dataController.savedSelection.isEmpty {
Text("No Apps Selected")
.foregroundColor(.gray)
} else {
List(dataController.savedSelection, id: \.self) { app in
Text(app.bundleIdentifier ?? "Unknown App")
}
.scrollContentBackground(.hidden)
}
let appArray = dataController.savedSelection
if !appArray.isEmpty{
Button("Delete App") {
let app = dataController.savedSelection[0]
moc.delete(app)
do {
try moc.save()
// Update savedSelection to reflect the changes
dataController.fetchApps(context: moc)
} catch {
print("Error saving after deletion: \(error)")
}
}
.padding(.top, 15)
}
Spacer()
VStack{
Button(action: {
print("Blocking Button Clicked")
isPresented = true
})
{
Text("Select Apps to Block")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 300)
.background(
LinearGradient(
colors: [.blue, .purple],
startPoint: .leading,
endPoint: .trailing
)
)
.cornerRadius(20)
.shadow(color: Color.blue.opacity(0.6), radius: 10, x: 0, y: 2)
}
.familyActivityPicker(isPresented: $isPresented, selection: $model.selectionToDiscourage)
.buttonStyle(ScaleButtonStyle())
}
.onChange(of: model.selectionToDiscourage)
{
print("APPS SELECTED : \(model.selectionToDiscourage.applications.count)")
for i in model.selectionToDiscourage.applications {
print(i)
dataController.addApp(name:i.localizedDisplayName ?? "Temp", context: moc)
}
model.setShieldRestrictions()
print($model.selectionToDiscourage.applicationTokens)
}
.padding(.bottom, 15)
.padding(.top, 15)
}
.onAppear()
{
print("Screen Time Authorization Status \(authorizationStatus)")
ScheduleModel.setSchedule()
dataController.fetchApps(context: moc)
}
}
}
}