Skip to content

Commit

Permalink
Improve schedule parsing accuracy & efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
jevonmao committed Oct 17, 2022
1 parent 32a8f69 commit 783d50a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Sources/Shared/Models/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ extension Constants {
struct Schedule {
static let startTimePattern: _Regex = #"((0?[1-9]|1[0-2]):[0-5][0-9]-)"#.r!
static let endTimePattern: _Regex = #"-(((0?[1-9]|1[0-2]):[0-5][0-9])|noon)"#.r!
static let periodPattern = try! _Regex(pattern: #"(per|period) \d+"#, options: [.caseInsensitive])
static let periodPattern = try! _Regex(pattern: #"(per|period)\s?\d+"#, options: [.caseInsensitive])
static let officeHourPattern = try! _Regex(pattern: #"(academic *per\w*)|(office *hours?)"#,
options: [.caseInsensitive])
static let lunchPattern = try! _Regex(pattern: #"^.*(lunch|nutrition).*$"#, options: [.caseInsensitive])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ struct ScheduleDetailView: View {
}

//1st or 2nd lunch revolving periods, 2nd out of 3 UI sections
var lunchPeriods: [ClassPeriod] {
var lunchPeriods: LunchBlock? {
let periods = scheduleDay?.periods ?? []
let firstIndex = scheduleDay?.periods?.firstIndex{$0.periodCategory.isLunchRevolving}
let lastIndex = scheduleDay?.periods?.lastIndex{$0.periodCategory.isLunchRevolving} //Last instance 1st/2nd nutrition block
if let firstIndex = firstIndex,
let lastIndex = lastIndex {
return Array(periods[firstIndex...lastIndex])
let lunchPeriods = Array(periods[firstIndex...lastIndex])
return LunchBlock(from: lunchPeriods)
}
return []
return nil
}

//Periods after lunch, 3rd out of 3 UI sections
Expand All @@ -74,7 +75,7 @@ struct ScheduleDetailView: View {
}
return []
}

var period8: ClassPeriod? {
scheduleDay?.periods?.filter {$0.periodNumber == 8}.first
}
Expand Down Expand Up @@ -104,8 +105,7 @@ struct ScheduleDetailView: View {
if shouldFallback {
ScheduleViewTextLines(scheduleLines: scheduleDay?.scheduleText.lines)
.padding(.top, 30)
.vibrancyEffectStyle(.label)
.vibrancyEffect()
.foregroundColor(.platformBackground)

}
else {
Expand Down Expand Up @@ -166,34 +166,34 @@ struct ScheduleDetailView: View {
PeriodBlockItem(block: period, isBlurred: showBackgroundImage)
}

if let firstLunch = lunchPeriods.first{$0.periodCategory == .firstLunch},
let firstLunchPeriod = lunchPeriods.first{$0.periodCategory == .firstLunchPeriod},
let secondLunch = lunchPeriods.first{$0.periodCategory == .secondLunch},
let secondLunchPeriod = lunchPeriods.first{$0.periodCategory == .secondLunchPeriod} {
HStack {
VStack {
makeLunchTitle(content: "1st Lunch Times")

PeriodBlockItem(block: firstLunch,
twoLine: true,
isBlurred: showBackgroundImage)
PeriodBlockItem(block: firstLunchPeriod,
twoLine: true,
isBlurred: showBackgroundImage)
}
.padding(.trailing, 5)
VStack {
makeLunchTitle(content: "2nd Lunch Times")

PeriodBlockItem(block: secondLunchPeriod,
twoLine: true,
isBlurred: showBackgroundImage)
PeriodBlockItem(block: secondLunch,
twoLine: true,
isBlurred: showBackgroundImage)
}
if let lunchPeriods = lunchPeriods {
HStack {
VStack {
// Force unwrap - shouldFallBack should catch nil
let firstLunch = lunchPeriods.firstLunch
makeLunchTitle(content: "1st Lunch Times")
PeriodBlockItem(block: firstLunch.lunchPeriod,
twoLine: true,
isBlurred: showBackgroundImage)
PeriodBlockItem(block: firstLunch.revolvingPeriod,
twoLine: true,
isBlurred: showBackgroundImage)
}
.padding(.trailing, 5)
VStack {
let secondLunch = lunchPeriods.secondLunch
makeLunchTitle(content: "2nd Lunch Times")
PeriodBlockItem(block: secondLunch.lunchPeriod,
twoLine: true,
isBlurred: showBackgroundImage)
PeriodBlockItem(block: secondLunch.revolvingPeriod,
twoLine: true,
isBlurred: showBackgroundImage)
}
}
}



ForEach(postLunchPeriods, id: \.self){period in
PeriodBlockItem(block: period,
Expand Down Expand Up @@ -319,6 +319,30 @@ struct ScheduleDetailView: View {
.lineLimit(1)
.minimumScaleFactor(0.5)
}

struct LunchBlock {
struct Lunch {
var revolvingPeriod: ClassPeriod
var lunchPeriod: ClassPeriod
}
var firstLunch: Lunch
var secondLunch: Lunch

init?(from lunchPeriods: [ClassPeriod]) {
if let firstLunch = lunchPeriods.first(where: {$0.periodCategory == .firstLunch}),
let firstLunchPeriod = lunchPeriods.first(where: {$0.periodCategory == .firstLunchPeriod}),
let secondLunch = lunchPeriods.first(where: {$0.periodCategory == .secondLunch}),
let secondLunchPeriod = lunchPeriods.first(where: {$0.periodCategory == .secondLunchPeriod}) {
self.firstLunch = Lunch(revolvingPeriod: firstLunchPeriod,
lunchPeriod: firstLunch)
self.secondLunch = Lunch(revolvingPeriod: secondLunchPeriod,
lunchPeriod: secondLunch)
}
else {
return nil
}
}
}
}

struct ScheduleDetailView_Previews: PreviewProvider {
Expand Down Expand Up @@ -346,6 +370,10 @@ struct ScheduleDetailView_Previews: PreviewProvider {
showBackgroundImage: false)
.environmentObject(configureSettings(legacySchedule: true))

ScheduleDetailView(scheduleDay: .sampleScheduleDay,
showBackgroundImage: true)
.environmentObject(configureSettings(legacySchedule: true))

ScheduleDetailView(scheduleDay: .sampleScheduleDay, showBackgroundImage: false)
.environmentObject(configureSettings())
}
Expand Down
18 changes: 7 additions & 11 deletions Sources/Shared/Views/ScheduleView/Model/ScheduleDay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ struct ScheduleDay: Hashable, Identifiable, Codable {
self.date = date
self.scheduleText = scheduleText
self.mockDate = mockDate
self.periods = parseClassPeriods() ?? nil
if let periods = self.periods {
self.periods = appendOptionalPeriod8(periods: periods)
}
}

internal init(date: Date, scheduleText: String, dayTitle: String) {
self.date = date
self.scheduleText = scheduleText
self.init(date: date, scheduleText: scheduleText)
self.dayTitle = dayTitle
}

internal init(date: Date, scheduleText: String) {
self.date = date
self.scheduleText = scheduleText
self.init(date: date, scheduleText: scheduleText, mockDate: nil)
}

var mockDate: Date? //Mock representation of current date, for testing
Expand All @@ -37,13 +39,7 @@ struct ScheduleDay: Hashable, Identifiable, Codable {
var dayTitle: String?

var customPeriods = [ClassPeriod]() //Future feature, no use for now
var periods: [ClassPeriod]? {
guard let periods = parseClassPeriods()
else {
return nil
}
return appendOptionalPeriod8(periods: periods)
}
var periods: [ClassPeriod]?

var atheleticsInfo: String {
guard dayOfTheWeek != 6 && dayOfTheWeek != 0
Expand Down

0 comments on commit 783d50a

Please sign in to comment.