TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Swift modelling help - am I on the right track?

Forums > iOS

I'm planning out a new app concept but was getting a bit caught on the logic from the model I had designed - but then I couldn't think of a cleaner way to achieve it.

I'm hoping some fresh eyes, and some more experienced people might be able to help me realise what is working or what is not.

Assume we have the concept - Personal Car Maintenance.

We have set schedules for things that should be looked at, and when. However not everything is a daily, or monthly check. Some are "as needed" or others are every n days.

class Car {
  var brand: String // for some context 
  var color: String // for some context 
  var checkUps: [CheckUp] // <-- the assigned checkups for the car
}

class CheckUp {
  var name: String
  var priority: Int
  var frequency: Frequency
  var status: Status // enum of .active or .inactive 

  enum Frequency {
    case asNeeded
    case regularIntervals(numberOfDays: Int, schedule: [Schedule], startDate: Date)
    case specificDaysOfWeek(daysOfWeek: [Int], schedule: [Schedule], startDate: Date)

    struct Schedule {
      var timeOfDay: Date
      var quantity: Int
      var name: String // This could be something like "Top up" or "Replacement"
    }
  }
}

Now the parts which I'm having issue visualising on how to implement are:

  • Upcoming check up screen, and;
  • Local notifications

On the "upcoming check up screen" I essentially wanted something similar to a TV show tracker:

Except, on this screen it would be all the stored Car and their checkUps in a date ordered list.

Today 1/1/2000
- No checkups

Tomorrow 2/2/2000
- Car 1 (Holden, Blue)
-- Change oil @ 9:00am

Friday
- Car 2 (Ford, Orange)
-- Change oil @ 10:00am

- Car 1 (Holden, Blue)
-- Clean mats @ 1:00pm

Now I assume the logic used to make the list I can use too to set local notifications - as in get today's event (eg. Car 1 - change oil @ 9:00am) then see the schedule frequency and set the next notification for then.

My logic behind the Frequency enum was I would have helper functions to fill the schedule array based on the selection. Something like:

func calculateSchedule(interval: Int, startFrom: Date) -> [Schedule] {
  let numberOfItems = 10
  var schedules = [Schedule]()
  for i in 0..<numberOfItems {
    let intervalInSeconds = TimeInterval(interval * 3600 * i)
    let scheduleTime = Calendar.current.date(byAdding: .second, value: Int(intervalInSeconds), to: startFrom)!
    let schedule = Schedule(time: scheduleTime)
    schedules.append(schedule)
  }
  return schedules
}

// then another function for days of the week:
// func calculateSchedule(daysOfWeek: [Int], startFrom: Date) -> [Schedule] { .. }

Except in this I'm now hard coding the number of schedules to create, but it does make it cleaner in being able to query them all and list them, or create notifications for them by getting the next in the array.

Is this logical or am I heading in the wrong direction? Aside from the other issues (like number of queued up local notifications allowed), in terms of the functionality would be good or is there another way I'm overlooking or not aware of?

In terms of creating a Checkup for the Car, we only have one Frequency (as needed, specific dates, or day intervals), however those frequencies can have multiple times.

As an example - we can have a Frequency set as .specificDaysOfWeek(..) being Monday, Wednesday and Friday as the days. And we can have a start date to be a week from creation into the system.

But for those M/W/F days we can have different times for the frequency. As in, 8am - 12pm - 6pm. And since the schedules is an array we could have it so it is:

8am - 20ml - Fill Up
12pm - 10ml - Top Up
6pm - 100ml - Full Flush

How can we handle all this, and in terms of the List view too (for SwiftUI) and then using them for the notifications..

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.