Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct ManageRolesView: View {
- @EnvironmentObject var siloData: SiloData
- @State private var showInfoSheetRoles: Bool = false
- @State private var roleName: String = ""
- @State private var selectedRoleId: String? // Bind this to navigation
- @State private var showNextView = false // A flag to trigger the navigation
- @State private var selectedRole: Role? // Track the selected role explicitly
- // Bind this to navigation
- @Environment(\.dismiss) var dismiss
- @EnvironmentObject var userData: UserData
- var body: some View {
- NavigationStack {
- VStack(alignment: .leading) {
- addRoleSection()
- Divider()
- roleList()
- Divider()
- totalRolesInfo()
- saveButton()
- }
- .navigationTitle("Manage Roles")
- .toolbar {
- ToolbarItem(placement: .navigationBarTrailing) {
- infoButton()
- }
- }
- .sheet(isPresented: $showInfoSheetRoles) {
- InfoSheetRolesView()
- .presentationDetents([.fraction(0.9)])
- }
- }
- .padding()
- .onAppear {
- // Ensure the roles are loaded when the view appears
- loadRoles()
- }
- }
- @ViewBuilder
- private func addRoleSection() -> some View {
- HStack {
- TextField("Enter role name", text: $roleName)
- .padding()
- .background(Color(.systemGray6))
- .cornerRadius(8)
- Button(action: addNewRole) {
- HStack {
- Image(systemName: "plus.circle")
- .foregroundColor(.white)
- .font(.system(size: 20))
- Text("Add")
- .fontWeight(.semibold)
- .foregroundColor(.white)
- }
- .padding()
- .background(roleName.isEmpty ? Color.gray : Color.blue)
- .cornerRadius(8)
- }
- .disabled(roleName.isEmpty)
- }
- .padding(.vertical)
- }
- // MARK: - Add roles
- private func addNewRole() {
- guard let selectedSiloID = siloData.selectedSilo?.id,
- let siloIndex = siloData.silos.firstIndex(where: { $0.id == selectedSiloID }) else {
- print("No silo selected.")
- return
- }
- let newRole = Role(id: UUID().uuidString, name: roleName, isAdmin: false)
- // Add the new role to the specific silo
- siloData.silos[siloIndex].roles.append(newRole)
- // Refresh roles
- siloData.roles = siloData.silos[siloIndex].roles
- // Sync the selectedSilo object
- siloData.selectedSilo = siloData.silos[siloIndex]
- // Reset input
- roleName = ""
- // Trigger UI update
- siloData.objectWillChange.send()
- debugPrintSilos()
- }
- func debugPrintSilos() {
- for silo in siloData.silos {
- print("Silo: \(silo.siloName), Roles: \(silo.roles.map { $0.name })")
- }
- }
- private func loadRoles() {
- if let selectedSiloID = siloData.currentSiloID,
- let selectedSilo = siloData.silos.first(where: { $0.id == selectedSiloID }) {
- siloData.roles = selectedSilo.roles
- print("Loaded roles for silo '\(selectedSilo.siloName)': \(siloData.roles.map { $0.name })")
- } else {
- siloData.roles = []
- print("No silo selected. Cleared roles.")
- }
- siloData.objectWillChange.send()
- }
- @ViewBuilder
- private func roleList() -> some View {
- ScrollView {
- VStack(spacing: 8) {
- if siloData.roles.isEmpty {
- Text("No roles available for this silo.")
- .foregroundColor(.gray)
- } else {
- ForEach(siloData.roles, id: \.id) { role in
- NavigationLink(
- destination: RoleSettingsView(),
- tag: role.id,
- selection: $selectedRoleId
- ) {
- roleRow(for: role)
- }
- .buttonStyle(PlainButtonStyle())
- }
- }
- }
- .padding()
- }
- }
- //MARK: - What is this?
- private func roleRow(for role: Role) -> some View {
- HStack {
- Text(role.name.isEmpty ? "Unnamed Role" : role.name)
- .fontWeight(.semibold)
- Spacer()
- }
- .padding()
- .background(Color(.systemGray6))
- .cornerRadius(8)
- }
- //MARK: - Good
- @ViewBuilder
- private func totalRolesInfo() -> some View {
- HStack {
- Image(systemName: "person.3.fill")
- // .foregroundColor(.black)
- Text("Total Roles")
- Spacer()
- let totalRoles = siloData.roles.count
- Text("\(totalRoles)")
- .foregroundColor(.gray)
- }
- .padding()
- }
- private func saveButton() -> some View {
- Button(action: { dismiss() }) {
- Text("Save")
- .fontWeight(.bold)
- .foregroundColor(.white)
- .padding()
- .frame(maxWidth: .infinity)
- .background(Color.green)
- .cornerRadius(8)
- }
- }
- @ViewBuilder
- private func infoButton() -> some View {
- Button(action: { showInfoSheetRoles.toggle() }) {
- Image(systemName: "info.circle")
- .font(.title)
- }
- }
- }
- // MARK: - Preview
- struct ManageRolesView_Preview: PreviewProvider {
- static var previews: some View {
- // Create a sample role
- let sampleRole = Role(id: UUID().uuidString, name: "Manager", isAdmin: true)
- // Create a sample silo with the sample role
- let sampleSilo = Silo(id: "1", siloName: "Sample Silo", roles: [sampleRole])
- // Create a SiloData object and populate it with the sample silo
- let sampleSiloData = SiloData()
- sampleSiloData.silos = [sampleSilo] // Add the sample silo to the silos array
- sampleSiloData.currentSiloID = sampleSilo.id // Set the currentSiloID to the sample silo's ID
- sampleSiloData.roles = sampleSilo.roles // Initialize roles with the sample silo's roles
- sampleSiloData.selectedSilo = sampleSilo // Set the selected silo
- // Return the ManageRolesView preview with the SiloData environment
- return ManageRolesView()
- .environmentObject(sampleSiloData)
- // .previewLayout(.sizeThatFits)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement