Hi, I am trying to replicate a similar project as the learning app project that is taught in module 5. I am building the app as I go through the lessons, its just my way of learning so that I remember what is being taught.
Currently I am at Lesson 5 of the module and just like LessonsListView on the application thats being built on module 5, my application has a SurahListView. This basically has a ScrollView that contains a list of Navigation Links for Surahs (just the same as lessons on the learning app), on clicking the navigation link the destination is pointed to SurahDetailView() and it contains an onAppear modifier, and this modifier performs a function that basically records the Surah the user has started reading just the same way the learning application records the lesson user started, so that upon loading SurahDetailView its content can be dynamically loaded. However when a Surah Navigation Link is clicked the onAppear does not get triggered. I have tried putting breaking points within the function and it never gets hit thus I have a bad feeling that onAppear is not at all called, I am a bit clueless of what to fix to have the onAppear called.
Source for SurahListView:
import SwiftUI
struct SurahListView: View {
@EnvironmentObject var AppViewModel: AppViewModel
var body: some View {
ScrollView {
ForEach(AppViewModel.translationData.records){surah in
NavigationLink {
SurahDetailView()
.onAppear {
AppViewModel.beginReadingSurah(SurahIndex: surah.id)
}
} label: {
ZStack {
GeometryReader { geo in
Rectangle()
.foregroundColor(surah.id % 2 == 0 ? .white: AppViewModel.bgOddCardColor)
.frame(height: geo.size.height+10)
.shadow(radius: 0.5)
}
LazyVStack (alignment: .leading) {
HStack{
ZStack{
Image(systemName: "circle.fill")
.resizable()
.frame(width: 42, height: 45)
Text(String(surah.id))
.foregroundColor(.white)
.font(Font.custom("Optima Bold", size: 20))
}
.padding(.horizontal, 20)
VStack (alignment: .leading){
Text("\(surah.name)")
.font(Font.custom("DecoType Naskh Regular", size: 20))
Text("(\(surah.translation))")
.font(Font.custom("Optima Bold", size: 18))
Text("Number of Verses: \(String(surah.total_verses))")
.font(Font.custom("Optima Regular", size: 12))
}
}.padding(.horizontal)
}.padding()
}
}
.accentColor(.black)
}
}
.navigationTitle("Select \(AppViewModel.translationData.module_name)")
}
}
Source for SurahDetailView: This is the destination view when once a NavLink on SurahListView is clicked.
import SwiftUI
struct SurahDetailView: View {
@EnvironmentObject var AppViewModel: AppViewModel
var body: some View {
if AppViewModel.cSelectSurah != nil {
ScrollView{
ForEach(0..<AppViewModel.cSelectSurah!.verses.count) { index in
ZStack {
GeometryReader { geo in
// rgb(207, 216, 220)
Rectangle()
.foregroundColor(index % 2 == 0 ? .white: AppViewModel.bgOddCardColor)
.frame(height: geo.size.height+10)
.shadow(radius: 0.5)
}
LazyVStack(alignment:.trailing, spacing: 10) {
HStack{
VStack(alignment:.trailing, spacing: 10){
Text(AppViewModel.cSelectSurah!.verses[index].text)
.font(Font.custom("DecoType Naskh Regular", size: 15))
.multilineTextAlignment(.trailing)
Text(AppViewModel.cSelectSurah!.verses[index].translation)
.font(Font.custom("Optima Regular", size: 15))
.multilineTextAlignment(.leading)
Text(AppViewModel.cSelectSurah!.verses[index].dv_translation)
.font(Font.system(size: 15))
.multilineTextAlignment(.trailing)
}
ZStack{
Image(systemName: "circle.fill")
.resizable()
.frame(width: 42, height: 45)
Text(String(AppViewModel.cSelectSurah!.verses[index].id))
.foregroundColor(.white)
.font(Font.custom("Optima Bold", size: 20))
}
.padding(.horizontal, 20)
}
}
.padding()
}
.padding()
}
}
.navigationTitle(AppViewModel.cSelectSurah!.name)
}
}
}
View Model that contains the function that is being performed within onAppear:
import SwiftUI
import Foundation
class AppViewModel: ObservableObject {
// MARK: - Globaly Shared Application Data
var app_data = [AppData]()
@Published var duasData:Duas
@Published var translationData:Translation
// MARK: - Common Element Properties
@Published var bgOddCardColor = Color(.sRGB, red: 207/255, green: 216/255, blue: 220/255, opacity: 1)
// MARK: - Current Selected Surah
@Published var cSelectSurah:Surah?
var scIndex:Int = 0
init() {
self.app_data = DataService.getData()
self.duasData = self.app_data[0].duas
self.translationData = self.app_data[0].translation
}
// MARK: - Surah Navigation Controls
func beginReadingSurah(SurahIndex sIndex:Int){
// Find the module ID of the current Selected Surah
for index in 0..<translationData.records.count{
if sIndex == translationData.records[index].id {
self.scIndex = sIndex
break
}
}
// Set the current Selected Surah
cSelectSurah = translationData.records[scIndex]
}
}
Here is the GitRepo in case you would like to go through the whole project:
Any assistance in resolving the issue would be really appreciated.