Aditya Mangalampalli
Aditya Mangalampalli

Reputation: 21

SwiftUI Expandable List with Different Properties

so I have been trying to create an expandable list to present different types of information.

I have a generic struct as such for all the different instances:

struct Password: Identifiable {

var id = UUID()

var password: String?

var containsSymbols: Bool?

var containsUppercase: Bool?

var entropy: Double?

var possibleCombinations: Double?

var strengthColor: Color?

var content: [Password]?

}

And I instantiate various Password objects as such:

let entropyPassword = Password(entropy: entropy)

let combinationPassword = Password(possibleCombinations: possibleCombinaisons)

let password = Password(password: newPassword, containsSymbols: containsSymbols, containsUppercase: containsUppercase, strengthColor: color, content: [entropyPassword, combinationPassword])

However, in the main ContentView, I do something like this to achieve a nested expandable list.

List(vm.passwords) { password in

HStack {

Text("\(password.password ?? "Default")")

.padding()

.textSelection(.enabled)

Spacer()

Image(systemName: "lock.fill")

.foregroundColor(password.strengthColor)

}

}

However, I keep seeing the word "Default" when clicking on the initial password. Is there a way to click on the initial password to then see the entropy and the possible combinations, preferably in an expandable list?

Upvotes: 1

Views: 776

Answers (1)

ChrisR
ChrisR

Reputation: 12115

You can use DisclosureGroup or OutlineGroup for an expandable list. Below is an example of what you might want to achieve.

struct Password: Identifiable {
    var id = UUID()
    var password: String?
    var containsSymbols: Bool?
    var containsUppercase: Bool?
    var entropy: Double?
    var possibleCombinations: Double?
    var strengthColor: Color?
    var content: [Password]?
}

let entropyPassword = Password(entropy: 12)
let combinationPassword = Password(possibleCombinations: 256)
let passwords = [
    Password(password: "123_xfgtH$", containsSymbols: true, containsUppercase: true, strengthColor: .red, content: [entropyPassword, combinationPassword]),
    Password(password: "test", containsSymbols: true, containsUppercase: true, strengthColor: .red, content: [entropyPassword, combinationPassword])
    ]



struct ContentView: View {
    var body: some View {
        
        List(passwords) { password in
            
            DisclosureGroup {
                if password.content != nil {
                    ForEach(password.content!) { item in
                        Text("\(item.entropy ?? 0)")
                    }
                }
            } label: {
                HStack {
                    Text("\(password.password ?? "Default")")
                        .padding()
                        .textSelection(.enabled)
                    Spacer()
                    Image(systemName: "lock.fill")
                        .foregroundColor(password.strengthColor)
                    
                }
            }
        }
    }
}

Upvotes: 0

Related Questions