Advertisement
randomPenguin12345

ManageRolesView

Jan 9th, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.92 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct ManageRolesView: View {
  4.     @EnvironmentObject var siloData: SiloData
  5.     @State private var showInfoSheetRoles: Bool = false
  6.     @State private var roleName: String = ""
  7.     @State private var selectedRoleId: String? // Bind this to navigation
  8.     @State private var showNextView = false // A flag to trigger the navigation
  9.     @State private var selectedRole: Role? // Track the selected role explicitly
  10.  
  11.  // Bind this to navigation
  12.     @Environment(\.dismiss) var dismiss
  13.    
  14.     @EnvironmentObject var userData: UserData
  15.  
  16.     var body: some View {
  17.         NavigationStack {
  18.             VStack(alignment: .leading) {
  19.                 addRoleSection()
  20.                 Divider()
  21.                 roleList()
  22.                 Divider()
  23.                 totalRolesInfo()
  24.                 saveButton()
  25.             }
  26.             .navigationTitle("Manage Roles")
  27.             .toolbar {
  28.                 ToolbarItem(placement: .navigationBarTrailing) {
  29.                     infoButton()
  30.                 }
  31.             }
  32.             .sheet(isPresented: $showInfoSheetRoles) {
  33.                 InfoSheetRolesView()
  34.                     .presentationDetents([.fraction(0.9)])
  35.             }
  36.         }
  37.         .padding()
  38.         .onAppear {
  39.             // Ensure the roles are loaded when the view appears
  40.             loadRoles()
  41.         }
  42.     }
  43.  
  44.     @ViewBuilder
  45.     private func addRoleSection() -> some View {
  46.         HStack {
  47.             TextField("Enter role name", text: $roleName)
  48.                 .padding()
  49.                 .background(Color(.systemGray6))
  50.                 .cornerRadius(8)
  51.            
  52.             Button(action: addNewRole) {
  53.                 HStack {
  54.                     Image(systemName: "plus.circle")
  55.                         .foregroundColor(.white)
  56.                         .font(.system(size: 20))
  57.                     Text("Add")
  58.                         .fontWeight(.semibold)
  59.                         .foregroundColor(.white)
  60.                 }
  61.                 .padding()
  62.                 .background(roleName.isEmpty ? Color.gray : Color.blue)
  63.                 .cornerRadius(8)
  64.             }
  65.             .disabled(roleName.isEmpty)
  66.         }
  67.         .padding(.vertical)
  68.     }
  69.  
  70.     // MARK: - Add roles
  71.    
  72.     private func addNewRole() {
  73.         guard let selectedSiloID = siloData.selectedSilo?.id,
  74.               let siloIndex = siloData.silos.firstIndex(where: { $0.id == selectedSiloID }) else {
  75.             print("No silo selected.")
  76.             return
  77.         }
  78.  
  79.         let newRole = Role(id: UUID().uuidString, name: roleName, isAdmin: false)
  80.  
  81.         // Add the new role to the specific silo
  82.         siloData.silos[siloIndex].roles.append(newRole)
  83.  
  84.         // Refresh roles
  85.         siloData.roles = siloData.silos[siloIndex].roles
  86.  
  87.         // Sync the selectedSilo object
  88.         siloData.selectedSilo = siloData.silos[siloIndex]
  89.  
  90.         // Reset input
  91.         roleName = ""
  92.  
  93.         // Trigger UI update
  94.         siloData.objectWillChange.send()
  95.  
  96.         debugPrintSilos()
  97.     }
  98.  
  99.  
  100.  
  101.     func debugPrintSilos() {
  102.         for silo in siloData.silos {
  103.             print("Silo: \(silo.siloName), Roles: \(silo.roles.map { $0.name })")
  104.         }
  105.     }
  106.  
  107.  
  108.     private func loadRoles() {
  109.         if let selectedSiloID = siloData.currentSiloID,
  110.            let selectedSilo = siloData.silos.first(where: { $0.id == selectedSiloID }) {
  111.             siloData.roles = selectedSilo.roles
  112.             print("Loaded roles for silo '\(selectedSilo.siloName)': \(siloData.roles.map { $0.name })")
  113.         } else {
  114.             siloData.roles = []
  115.             print("No silo selected. Cleared roles.")
  116.         }
  117.        
  118.         siloData.objectWillChange.send()
  119.     }
  120.  
  121.  
  122.    
  123.  
  124.     @ViewBuilder
  125.     private func roleList() -> some View {
  126.         ScrollView {
  127.             VStack(spacing: 8) {
  128.                 if siloData.roles.isEmpty {
  129.                     Text("No roles available for this silo.")
  130.                         .foregroundColor(.gray)
  131.                 } else {
  132.                     ForEach(siloData.roles, id: \.id) { role in
  133.                         NavigationLink(
  134.                             destination: RoleSettingsView(),
  135.                             tag: role.id,
  136.                             selection: $selectedRoleId
  137.                         ) {
  138.                             roleRow(for: role)
  139.                         }
  140.                         .buttonStyle(PlainButtonStyle())
  141.                     }
  142.                 }
  143.             }
  144.             .padding()
  145.         }
  146.     }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. //MARK: - What is this?
  157.  
  158.  
  159.     private func roleRow(for role: Role) -> some View {
  160.         HStack {
  161.             Text(role.name.isEmpty ? "Unnamed Role" : role.name)
  162.                 .fontWeight(.semibold)
  163.             Spacer()
  164.         }
  165.         .padding()
  166.         .background(Color(.systemGray6))
  167.         .cornerRadius(8)
  168.     }
  169.  
  170.     //MARK: - Good
  171.    
  172.     @ViewBuilder
  173.     private func totalRolesInfo() -> some View {
  174.         HStack {
  175.             Image(systemName: "person.3.fill")
  176.            //     .foregroundColor(.black)
  177.             Text("Total Roles")
  178.             Spacer()
  179.             let totalRoles = siloData.roles.count
  180.             Text("\(totalRoles)")
  181.                 .foregroundColor(.gray)
  182.         }
  183.         .padding()
  184.     }
  185.  
  186.  
  187.     private func saveButton() -> some View {
  188.         Button(action: { dismiss() }) {
  189.             Text("Save")
  190.                 .fontWeight(.bold)
  191.                 .foregroundColor(.white)
  192.                 .padding()
  193.                 .frame(maxWidth: .infinity)
  194.                 .background(Color.green)
  195.                 .cornerRadius(8)
  196.         }
  197.     }
  198.  
  199.     @ViewBuilder
  200.     private func infoButton() -> some View {
  201.         Button(action: { showInfoSheetRoles.toggle() }) {
  202.             Image(systemName: "info.circle")
  203.                 .font(.title)
  204.         }
  205.     }
  206. }
  207.  
  208.  
  209. // MARK: - Preview
  210. struct ManageRolesView_Preview: PreviewProvider {
  211.     static var previews: some View {
  212.         // Create a sample role
  213.         let sampleRole = Role(id: UUID().uuidString, name: "Manager", isAdmin: true)
  214.  
  215.         // Create a sample silo with the sample role
  216.         let sampleSilo = Silo(id: "1", siloName: "Sample Silo", roles: [sampleRole])
  217.  
  218.         // Create a SiloData object and populate it with the sample silo
  219.         let sampleSiloData = SiloData()
  220.         sampleSiloData.silos = [sampleSilo] // Add the sample silo to the silos array
  221.         sampleSiloData.currentSiloID = sampleSilo.id // Set the currentSiloID to the sample silo's ID
  222.         sampleSiloData.roles = sampleSilo.roles // Initialize roles with the sample silo's roles
  223.         sampleSiloData.selectedSilo = sampleSilo // Set the selected silo
  224.  
  225.         // Return the ManageRolesView preview with the SiloData environment
  226.         return ManageRolesView()
  227.             .environmentObject(sampleSiloData)
  228.         //    .previewLayout(.sizeThatFits)
  229.     }
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement