I have the following code
var body: some View
{
@State private var repeatFrequency = 1
@State private var period = Period.months
Form
{
// --- several Views (HStack, etc.)
Toggle("Only Once", isOn: $onlyOnce)
if !onlyOnce
{
HStack
{
Text("Repeat every")
Text("\(repeatFrequency)")
Stepper("", value: $repeatFrequency, in: 1...1000)
.fixedSize()
}
Picker("", selection: $period)
{
ForEach(periods, id: \.self)
{ period in
let tagValue = periodCodes[periods.firstIndex(of: period)!]
Text("\(period)\(repeatFrequency > 1 ? "s" : "")").tag(tagValue)
}
}
.pickerStyle(.segmented)
}
HStack
{
Text(onlyOnce ? "On: " : "Starting: ")
DatePicker("",
selection: $date,
in: Date.tomorrow()...,
displayedComponents: .date)
.fixedSize()
}
}
The code inside the ‘if’ is duplicated a couple of times in my project. I would like to code this only once, as a View, and be able to include this wherever I need it.
Something like this:
if !onlyOnce
{
setRecurringView(frequency: repeatFrequency, period: period)
}
I don’t want to go to the view. I want it incorporated into the View where it is.
I hope I’m explaining this clearly.
Thanks in advance.