0

This is a continuation of the question here

I have a class like this:

class Waterz {

var waterzEntitle: String
var waterzArtitle: String
var waterzIsFavorited: Bool?
var waterzDate: [Date]?
var waterzFrequency: [String]?
var waterzSubMenus: [Waterz]? = []
var waterzContent: String?
var waterzLocation: String?
var waterzAudio: String?

init(entitle: String, artitle: String, isFavorited: Bool?, date: [Date]?, frequency: [EKRecurrenceDayOfWeek]?, subMenus: [Waterz]?, content: String?, location: String?, audio: String?) {
    waterzEntitle = entitle
    waterzArtitle = artitle
    waterzIsFavorited = isFavorited
    waterzDate = date
    waterzFrequency = frequency
    waterzSubMenus = subMenus
    waterzContent = content
    waterzLocation = location
    waterzAudio = audio
    }

}

and another class that is my databank:

class WaterzBank {

// main categories
    var mainCategories: Waterz

// individual categories
    var duaKumayl: Waterz
    var duaFaraj: Waterz
    var duas: Waterz
    var salatGhufayla: Waterz
    var salatLaylImportance: Waterz
    var salatLaylMethod: Waterz
    var salatLayl: Waterz
    var prayers: Waterz
    var amal: Waterz

// more variables here

    var favorites: Waterz

    init() {

    // MARK: - Duas
        duaKumayl = Waterz(entitle: "Dua Kumayl", artitle: "دعاء كميل", isFavorited: true, date: nil, frequency: nil, subMenus: nil, content: "kumayl", location: nil, audio: nil)
        duaFaraj = Waterz(entitle: "Dua Al-Faraj", artitle: "دعاء الفرج", isFavorited: false, date: ["10-21", "2-15"], frequency: ["friday"], subMenus: nil, content: "faraj", location: nil, audio: nil)
        duas = Waterz(entitle: "Duas", artitle: "الأدعية", isFavorited: nil, date: nil, frequency: nil, subMenus: [duaKumayl, duaMakarem, duaNudba, duaSabah, duaSamat, duaAtharat, duaAhd, duaTawassul, duaFaraj], content: nil, location: nil, audio: nil)


    // MARK: - Amal

        salatGhufayla = Waterz(entitle: "Salat Al-Ghufayla", artitle: "صلاة الغفيلة", isFavorited: false, date: nil, frequency: nil, subMenus: nil, content: "salatGhufayla", location: nil, audio: nil)
        salatLaylMethod = Waterz(entitle: "Method", artitle: "كيفيّة صلاة الليل", isFavorited: false, date: nil, frequency: nil, subMenus: nil, content: "salatLaylMethod", location: nil, audio: nil)
        salatLaylImportance = Waterz(entitle: "Importance of Salat Al-Layl", artitle: "فضل صلاة الليل", isFavorited: false, date: nil, frequency: nil, subMenus: nil, content: "salatLaylImportance", location: nil, audio: nil)
        salatLayl = Waterz(entitle: "Salat Al-Layl", artitle: "صلاة الليل", isFavorited: false, date: nil, frequency: nil, subMenus: [salatLaylMethod, salatLaylImportance], content: "salatLayl", location: nil, audio: nil)
        prayers = Waterz(entitle: "Prayers", artitle: "الصلوات", isFavorited: nil, date: nil, frequency: nil, subMenus: [salatGhufayla, salatLayl], content: nil, location: nil, audio: nil)
        amal = Waterz(entitle: "A'mal", artitle: "الأعمال", isFavorited: nil, date: nil, frequency: nil, subMenus: [prayers], content: nil, location: nil, audio: nil)


    // more variables initialized here


    // MARK: - Favorites

        favorites = Waterz(entitle: "Favorites", artitle: "المفضلة", isFavorited: nil, date: nil, frequency: nil, subMenus: nil, content: nil, location: nil, audio: nil)

        mainCategories = Waterz(entitle: "Categories", artitle: "الأقسام", isFavorited: nil, date: nil, frequency: nil, subMenus: [duas, amal, ziyarat, munajat, taqeebat, others, favorites], content: nil, location: nil, audio: nil)

    }

}

My first question is, how do I loop through all the variables inside the databank to see if isFavorited = true, then I would append that item to the 'subMenus' of 'favorites'? Is there a way to do this or should I make favorites a separate category outside of the rest? Next, since I'm assigning many SubCategories (and SubSubCategories, and so on...) certain days of week and dates, how would I pull out the variable on that certain day? For example, if today is Tuesday and there are a total of 5 variables who are assigned the day "Tuesday", then they should all print on the console.

Final question: I'm kind of lost because I'm not sure if I'm doing my databank correctly and there's no one to ask. I just want to see if what I'm doing is actually correct. What I did was state the variable and then in the init() I gave it it's properties. Is that correct? I have to input like 2000 more variables.

This is how my app looks like: app demo

May Rest in Peace
  • 2,070
  • 2
  • 20
  • 34
enigrify
  • 281
  • 3
  • 17
  • 2
    @May s answer is really clear but if I can add something you shouldnt put the name of your class in your variables: `waterzEntitle`, `waterzArtitle` should be `entitle` and `artitle` etc... As @May suggested this is about design so any advice could help you – Olympiloutre Aug 20 '19 at 00:32
  • 1
    Thank you for your advice. But I didn't really understand what you meant. Should I just remove the 'waterz' of each variable in the 'Waterz' class? – enigrify Aug 20 '19 at 13:48
  • 2
    @enigrify Yes. Your class `Waterz` shouldn't prefix each member with waterz. So the members can be `entitle`, `artitle`, `isFavorited` etc. – May Rest in Peace Aug 20 '19 at 15:47

1 Answers1

3

Firstly create allWaterz like this in your WaterzBank class :

lazy var allWaterz: [Waterz] = {
    return [duaKumayl,
        duaFaraj,
        duas,
        salatGhufayla,
        salatLaylImportance,
        salatLaylMethod,
        salatLayl,
        prayers,
        amal]
}()

Create an instance of WaterzBank

let databank = WaterzBank()

how do I loop through all the variables inside the databank to see if isFavorited = true, then I would append that item to the 'subMenus' of 'favorites'?

Use the filter function to check if isFavorite true and append to submenus like this :

databank.favorites.waterzSubMenus = (databank.favorites.waterzSubMenus ?? []) + databank.allWaterz.filter {
    $0.waterzIsFavorited == true
}

if today is Tuesday and there are a total of 5 variables who are assigned the day "Tuesday", then they should all print on the console.

Firstly use this extension for getting the day from date

extension Date {
    func dayOfWeek() -> String? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.string(from: self).capitalized
    }
}

Now you can use forEach and contains to check for the day and print it in your console :

databank.allWaterz.forEach {
    let doesContainDay = $0.waterzDate?.contains {
        $0.dayOfWeek() == "Friday"
    }

    if doesContainDay == true {
        print($0)
    }
}

Is that correct? I have to input like 2000 more variables.

Sorry, but I don't think your approach is correct. You might consider creating a JSON file of all your data and parsing it at runtime to create your data. But that is still a very crude way. You should use mobile DBs in this scenario. You might want to look into CoreData provided by Apple directly, although in my opinion it has a very steep learning curve. Or if you want to choose something easier but still very fast, you can go with Realm.

May Rest in Peace
  • 2,070
  • 2
  • 20
  • 34
  • 1
    Thank you so much for your answer. So If I were to use Realm, how would I do this differently to have a pre-populated database? Do I still use a similar system or do I pre-populate it in a completely different way? – enigrify Aug 20 '19 at 13:46
  • You should create a JSON with all your data. Put the JSON file in your app bundle. Now you can write a logic that when the app opens for the first time, you parse the JSON to fill your Realm db. – May Rest in Peace Aug 20 '19 at 15:44