1

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?

  • I don't quite understand what you need? When you clicked on Cell you need expanded list with all data fields which have struct password? Or show only one field password which have on model? – sergio_veliz Apr 22 '22 at 14:48

1 Answers1

0

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)
                    
                }
            }
        }
    }
}
ChrisR
  • 9,523
  • 1
  • 8
  • 26