Advertisement
randomPenguin12345

RoleSettingsView

Jan 9th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 8.13 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. // MARK: - RoleSettingsView
  4. struct RoleSettingsView: View {
  5.     @EnvironmentObject var siloData: SiloData
  6.     @EnvironmentObject var userData: UserData
  7.    
  8.  
  9.     // State variables
  10.     @State private var isVotingRightsEnabled: Bool = true
  11.     @State private var isMaxAmountEnabled: Bool = false
  12.     @State private var maxAmount: Int = 0
  13.    
  14.     ///APPROVAL
  15.     @State private var requiresApproval: Bool = true
  16.     @State private var approvalPercentage: Double = 50.0
  17.     @State private var approvalDate: Date = Date()
  18.     @State private var selectedLocationApproval: String = "General Meeting"
  19.    
  20.     ///DEMOTION
  21.     @State private var canBeDemoted: Bool = true
  22.     @State private var demotionPercentage: Double = 80.0
  23.     @State private var demotionDate: Date = Date()
  24.     @State private var selectedLocationDemotion: String = "General Meeting"
  25.    
  26.     ///RENEWAL
  27.     @State private var canBeRenewed: Bool = true
  28.     @State private var renewalPercentage: Double = 50.0
  29.     @State private var renewalDate: Date = Date()
  30.     @State private var selectedLocationRenewal: String = "General Meeting"
  31.  
  32.     ///DOCS
  33.     @State private var viewDocs: Bool = false
  34.     @State private var uploadDocs: Bool = false
  35.     @State private var downloadDocs: Bool = false
  36.    
  37.     ///FEED
  38.     @State private var viewFeed: Bool = false
  39.     @State private var allowComment: Bool = false
  40.     @State private var allowLike: Bool = false
  41.  
  42.     ///SPENDING
  43.     @State private var seeBalance: Bool = true
  44.     @State private var seeTransactions: Bool = true
  45.     @State private var spending: Bool = true
  46.    
  47.    
  48.  
  49.     var body: some View {
  50.         NavigationStack {
  51.                 Form {
  52.                     generalSection()
  53.                     approvalSection()
  54.                     demotionSection()
  55.                     renewalSection()
  56.                     documentSection()
  57.                     spendingSection()
  58.                     feedSection()
  59.  
  60.                 }
  61.                 .navigationTitle("Role Settings")
  62.                 .navigationBarTitleDisplayMode(.inline)
  63.         }
  64.     }
  65.  
  66.     // MARK: - General Section
  67.     @ViewBuilder
  68.     private func generalSection() -> some View {
  69.         Section(header: Text("General")) {
  70.             TextField("Role Name", text: .constant("Role Name"))
  71.                 .textFieldStyle(RoundedBorderTextFieldStyle())
  72.             Toggle("Voting Rights", isOn: $isVotingRightsEnabled)
  73.             Toggle("Max amount in this role?", isOn: $isMaxAmountEnabled)
  74.             if isMaxAmountEnabled {
  75.                 Stepper("Max Amount: \(maxAmount)", value: $maxAmount, in: 0...1000)
  76.             }
  77.         }
  78.     }
  79.  
  80.     // MARK: - Approval Section
  81.     @ViewBuilder
  82.     private func approvalSection() -> some View {
  83.         Section(header: Text("Approval")) {
  84.             Toggle("Requires Approval", isOn: $requiresApproval)
  85.             if requiresApproval {
  86.                 SliderView(percentage: $approvalPercentage)
  87.                 Picker("Where", selection: $selectedLocationApproval) {
  88.                     Text("General Meeting").tag("General Meeting")
  89.                     Text("Any Meeting").tag("Any Meeting")
  90.                     Text("Specific Meeting").tag("Specific Meeting")
  91.                 }
  92.                 .pickerStyle(MenuPickerStyle())
  93.                
  94.                 // Show DatePicker only if "Specific Meeting" is selected
  95.                 if selectedLocationApproval == "Specific Meeting" {
  96.                     DatePicker("Specific Meeting:", selection: $approvalDate, displayedComponents: .date)
  97.                 }
  98.             }
  99.         }
  100.     }
  101.  
  102.  
  103.     // MARK: - Demotion Section
  104.     @ViewBuilder
  105.     private func demotionSection() -> some View {
  106.         Section(header: Text("Demotion")) {
  107.             Toggle("Can be demoted?", isOn: $canBeDemoted)
  108.             if canBeDemoted {
  109.                 SliderView(percentage: $demotionPercentage)
  110.                 Picker("Where", selection: $selectedLocationDemotion) {
  111.                     Text("General Meeting").tag("General Meeting")
  112.                     Text("Any Meeting").tag("Any Meeting")
  113.                     Text("Specific Meeting").tag("Specific Meeting")
  114.                 }
  115.                 .pickerStyle(MenuPickerStyle())
  116.                 // Show DatePicker only if "Specific Meeting" is selected
  117.                 if selectedLocationDemotion == "Specific Meeting" {
  118.                     DatePicker("Specific Meeting:", selection: $demotionDate, displayedComponents: .date)
  119.                 }
  120.             }
  121.         }
  122.     }
  123.  
  124.     // MARK: - Renewal Section
  125.     @ViewBuilder
  126.     private func renewalSection() -> some View {
  127.         Section(header: Text("Renewal")) {
  128.             Toggle("Can be renewed?", isOn: $canBeRenewed)
  129.             if canBeRenewed {
  130.                 SliderView(percentage: $renewalPercentage)
  131.                 Picker("Where", selection: $selectedLocationRenewal) {
  132.                     Text("General Meeting").tag("General Meeting")
  133.                     Text("Any Meeting").tag("Any Meeting")
  134.                     Text("Specific Meeting").tag("Specific Meeting")
  135.                 }
  136.                 .pickerStyle(MenuPickerStyle())
  137.                 if selectedLocationRenewal == "Specific Meeting" {
  138.                    
  139.                     DatePicker("Specific Meeting:", selection: $renewalDate, displayedComponents: .date)
  140.                 }
  141.             }
  142.         }
  143.     }
  144.  
  145.     // MARK: - Document Section
  146.     @ViewBuilder
  147.     private func documentSection() -> some View {
  148.         Section(header: Text("Documents")) {
  149.             Toggle("View Documents", isOn: $viewDocs)
  150.             if viewDocs{
  151.                 Toggle("Upload Documents", isOn: $uploadDocs)
  152.                 Toggle("Download Documents", isOn: $downloadDocs)
  153.             }
  154.         }
  155.     }
  156.  
  157.     // MARK: - Feed Section
  158.     @ViewBuilder
  159.     private func feedSection() -> some View {
  160.         Section(header: Text("Feed")) {
  161.             Toggle("View Feed", isOn: $viewFeed)
  162.             if viewFeed{
  163.                 Toggle("Allow Comment", isOn: $allowComment)
  164.                 Toggle("Allow Like", isOn: $allowLike)
  165.             }
  166.         }
  167.     }
  168.    
  169.     // MARK: - Spending Section
  170.     @ViewBuilder
  171.     private func spendingSection() -> some View {
  172.         Section(header: Text("Spending")) {
  173.             Toggle("See Balance", isOn: $seeBalance)
  174.             Toggle("See Transactions", isOn: $seeTransactions)
  175.             Toggle("Allow Spending", isOn: $spending)
  176.            
  177.       //      if allowSpending {
  178.       //          NavigationLink("Restricted Industries", destination: ListOfIndustriesView())
  179.       //          NavigationLink("Restricted Locations", destination: PermittedLocationsView())
  180.       //          NavigationLink("Trusted Vendors", destination: TrustedVendorsView())
  181.       //      }
  182.         }
  183.     }
  184.  
  185.     // MARK: - Delete Button
  186.     private func deleteButton(for role: Role) -> some View {
  187.         Button(action: {
  188.             siloData.deleteRole(role: role)
  189.         }) {
  190.             Text("Delete Role")
  191.                 .foregroundColor(.white)
  192.                 .padding()
  193.                 .frame(maxWidth: .infinity)
  194.                 .background(Color.red)
  195.                 .cornerRadius(8)
  196.         }
  197.     }
  198. }
  199.  
  200. // MARK: - SliderView Component
  201. struct SliderView: View {
  202.     @Binding var percentage: Double
  203.  
  204.     var body: some View {
  205.         Slider(value: $percentage, in: 0...100, step: 1)
  206.         Text("Percentage of votes required: \(Int(percentage))%")
  207.     }
  208. }
  209.  
  210. // MARK: - Preview
  211. struct RoleSettingsView_Preview: PreviewProvider {
  212.     static var previews: some View {
  213.         // Create a sample role with a String ID
  214.         let sampleRole = Role(id: UUID().uuidString, name: "Manager", isAdmin: true) // Convert UUID to String
  215.         let sampleSiloData = SiloData()
  216.         sampleSiloData.roles = [sampleRole] // Add sample role to roles
  217.         sampleSiloData.selectedRoleId = sampleRole.id // Set selected role
  218.  
  219.         return RoleSettingsView()
  220.             .environmentObject(sampleSiloData)
  221.             .environmentObject(UserData()) // Pass UserData if needed
  222.             .previewLayout(.sizeThatFits)
  223.     }
  224. }
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement