Dont instantiate TabView with zero items

Seems this prevents the crash when the TabView tries to set
its index to 0/0.
This commit is contained in:
Jackson Harper 2022-10-13 14:02:15 +08:00
parent 14c87a1240
commit 2e9bda7835
2 changed files with 23 additions and 21 deletions

View file

@ -153,27 +153,29 @@
var audioCards: some View {
ZStack {
let textItems = self.audioController.textItems ?? []
TabView(selection: $tabIndex) {
ForEach(0 ..< textItems.count, id: \.self) { id in
SpeechCard(id: id)
.tag(id)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.onChange(of: tabIndex, perform: { index in
if index != audioController.currentAudioIndex, index < (audioController.textItems?.count ?? 0) {
audioController.seek(toUtterance: index)
}
})
.onChange(of: audioController.currentAudioIndex, perform: { index in
if index >= textItems.count {
return
if textItems.count > 0 {
TabView(selection: $tabIndex) {
ForEach(0 ..< textItems.count, id: \.self) { id in
SpeechCard(id: id)
.tag(id)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.onChange(of: tabIndex, perform: { index in
if index != audioController.currentAudioIndex, index < (audioController.textItems?.count ?? 0) {
audioController.seek(toUtterance: index)
}
})
.onChange(of: audioController.currentAudioIndex, perform: { index in
if index >= textItems.count {
return
}
if self.audioController.state != .reachedEnd {
tabIndex = index
}
})
if self.audioController.state != .reachedEnd {
tabIndex = index
}
})
}
if audioController.state == .reachedEnd {
// If we have reached the end display a replay button with an overlay behind

View file

@ -207,6 +207,8 @@
@Published public var durationString: String?
@Published public var voiceList: [(name: String, key: String, category: VoiceCategory, selected: Bool)]?
@Published public var textItems: [String]?
let dataService: DataService
var timer: Timer?
@ -480,8 +482,6 @@
let body: String
}
public var textItems: [String]?
func setTextItems() {
if let document = self.document {
textItems = document.utterances.map { utterance in