Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- // MARK: - RoleSettingsView
- struct RoleSettingsView: View {
- @EnvironmentObject var siloData: SiloData
- @EnvironmentObject var userData: UserData
- // State variables
- @State private var isVotingRightsEnabled: Bool = true
- @State private var isMaxAmountEnabled: Bool = false
- @State private var maxAmount: Int = 0
- ///APPROVAL
- @State private var requiresApproval: Bool = true
- @State private var approvalPercentage: Double = 50.0
- @State private var approvalDate: Date = Date()
- @State private var selectedLocationApproval: String = "General Meeting"
- ///DEMOTION
- @State private var canBeDemoted: Bool = true
- @State private var demotionPercentage: Double = 80.0
- @State private var demotionDate: Date = Date()
- @State private var selectedLocationDemotion: String = "General Meeting"
- ///RENEWAL
- @State private var canBeRenewed: Bool = true
- @State private var renewalPercentage: Double = 50.0
- @State private var renewalDate: Date = Date()
- @State private var selectedLocationRenewal: String = "General Meeting"
- ///DOCS
- @State private var viewDocs: Bool = false
- @State private var uploadDocs: Bool = false
- @State private var downloadDocs: Bool = false
- ///FEED
- @State private var viewFeed: Bool = false
- @State private var allowComment: Bool = false
- @State private var allowLike: Bool = false
- ///SPENDING
- @State private var seeBalance: Bool = true
- @State private var seeTransactions: Bool = true
- @State private var spending: Bool = true
- var body: some View {
- NavigationStack {
- Form {
- generalSection()
- approvalSection()
- demotionSection()
- renewalSection()
- documentSection()
- spendingSection()
- feedSection()
- }
- .navigationTitle("Role Settings")
- .navigationBarTitleDisplayMode(.inline)
- }
- }
- // MARK: - General Section
- @ViewBuilder
- private func generalSection() -> some View {
- Section(header: Text("General")) {
- TextField("Role Name", text: .constant("Role Name"))
- .textFieldStyle(RoundedBorderTextFieldStyle())
- Toggle("Voting Rights", isOn: $isVotingRightsEnabled)
- Toggle("Max amount in this role?", isOn: $isMaxAmountEnabled)
- if isMaxAmountEnabled {
- Stepper("Max Amount: \(maxAmount)", value: $maxAmount, in: 0...1000)
- }
- }
- }
- // MARK: - Approval Section
- @ViewBuilder
- private func approvalSection() -> some View {
- Section(header: Text("Approval")) {
- Toggle("Requires Approval", isOn: $requiresApproval)
- if requiresApproval {
- SliderView(percentage: $approvalPercentage)
- Picker("Where", selection: $selectedLocationApproval) {
- Text("General Meeting").tag("General Meeting")
- Text("Any Meeting").tag("Any Meeting")
- Text("Specific Meeting").tag("Specific Meeting")
- }
- .pickerStyle(MenuPickerStyle())
- // Show DatePicker only if "Specific Meeting" is selected
- if selectedLocationApproval == "Specific Meeting" {
- DatePicker("Specific Meeting:", selection: $approvalDate, displayedComponents: .date)
- }
- }
- }
- }
- // MARK: - Demotion Section
- @ViewBuilder
- private func demotionSection() -> some View {
- Section(header: Text("Demotion")) {
- Toggle("Can be demoted?", isOn: $canBeDemoted)
- if canBeDemoted {
- SliderView(percentage: $demotionPercentage)
- Picker("Where", selection: $selectedLocationDemotion) {
- Text("General Meeting").tag("General Meeting")
- Text("Any Meeting").tag("Any Meeting")
- Text("Specific Meeting").tag("Specific Meeting")
- }
- .pickerStyle(MenuPickerStyle())
- // Show DatePicker only if "Specific Meeting" is selected
- if selectedLocationDemotion == "Specific Meeting" {
- DatePicker("Specific Meeting:", selection: $demotionDate, displayedComponents: .date)
- }
- }
- }
- }
- // MARK: - Renewal Section
- @ViewBuilder
- private func renewalSection() -> some View {
- Section(header: Text("Renewal")) {
- Toggle("Can be renewed?", isOn: $canBeRenewed)
- if canBeRenewed {
- SliderView(percentage: $renewalPercentage)
- Picker("Where", selection: $selectedLocationRenewal) {
- Text("General Meeting").tag("General Meeting")
- Text("Any Meeting").tag("Any Meeting")
- Text("Specific Meeting").tag("Specific Meeting")
- }
- .pickerStyle(MenuPickerStyle())
- if selectedLocationRenewal == "Specific Meeting" {
- DatePicker("Specific Meeting:", selection: $renewalDate, displayedComponents: .date)
- }
- }
- }
- }
- // MARK: - Document Section
- @ViewBuilder
- private func documentSection() -> some View {
- Section(header: Text("Documents")) {
- Toggle("View Documents", isOn: $viewDocs)
- if viewDocs{
- Toggle("Upload Documents", isOn: $uploadDocs)
- Toggle("Download Documents", isOn: $downloadDocs)
- }
- }
- }
- // MARK: - Feed Section
- @ViewBuilder
- private func feedSection() -> some View {
- Section(header: Text("Feed")) {
- Toggle("View Feed", isOn: $viewFeed)
- if viewFeed{
- Toggle("Allow Comment", isOn: $allowComment)
- Toggle("Allow Like", isOn: $allowLike)
- }
- }
- }
- // MARK: - Spending Section
- @ViewBuilder
- private func spendingSection() -> some View {
- Section(header: Text("Spending")) {
- Toggle("See Balance", isOn: $seeBalance)
- Toggle("See Transactions", isOn: $seeTransactions)
- Toggle("Allow Spending", isOn: $spending)
- // if allowSpending {
- // NavigationLink("Restricted Industries", destination: ListOfIndustriesView())
- // NavigationLink("Restricted Locations", destination: PermittedLocationsView())
- // NavigationLink("Trusted Vendors", destination: TrustedVendorsView())
- // }
- }
- }
- // MARK: - Delete Button
- private func deleteButton(for role: Role) -> some View {
- Button(action: {
- siloData.deleteRole(role: role)
- }) {
- Text("Delete Role")
- .foregroundColor(.white)
- .padding()
- .frame(maxWidth: .infinity)
- .background(Color.red)
- .cornerRadius(8)
- }
- }
- }
- // MARK: - SliderView Component
- struct SliderView: View {
- @Binding var percentage: Double
- var body: some View {
- Slider(value: $percentage, in: 0...100, step: 1)
- Text("Percentage of votes required: \(Int(percentage))%")
- }
- }
- // MARK: - Preview
- struct RoleSettingsView_Preview: PreviewProvider {
- static var previews: some View {
- // Create a sample role with a String ID
- let sampleRole = Role(id: UUID().uuidString, name: "Manager", isAdmin: true) // Convert UUID to String
- let sampleSiloData = SiloData()
- sampleSiloData.roles = [sampleRole] // Add sample role to roles
- sampleSiloData.selectedRoleId = sampleRole.id // Set selected role
- return RoleSettingsView()
- .environmentObject(sampleSiloData)
- .environmentObject(UserData()) // Pass UserData if needed
- .previewLayout(.sizeThatFits)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement