Skip to content

Commit

Permalink
Support compact mode (a.k.a 꽉찬모드) as a PoC (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
shp7724 authored Jan 12, 2023
1 parent 4c5eb8d commit 6fbff29
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 16 deletions.
4 changes: 2 additions & 2 deletions SNUTT-2022/SNUTT/Models/Lecture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ extension Lecture {
id = dto._id
title = dto.course_title
instructor = dto.instructor
timePlaces = dto.class_time_json.map { .init(from: $0, isCustom: dto.course_number == nil) }
timePlaces = dto.class_time_json.map { .init(from: $0, isCustom: dto.isCustom) }
timeMasks = dto.class_time_mask ?? []
isCustom = (dto.course_number == nil || dto.course_number == "")
isCustom = dto.isCustom
courseNumber = dto.course_number ?? ""
lectureNumber = dto.lecture_number ?? ""
credit = dto.credit
Expand Down
27 changes: 24 additions & 3 deletions SNUTT-2022/SNUTT/Models/TimePlace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,37 @@ struct TimePlace: Identifiable {
}

var endTimeDouble: Double {
return TimeUtils.getTimeInDouble(from: endTime)
endTimeDouble(compactMode: false)
}

var duration: Double {
return endTimeDouble - startTimeDouble
func duration(compactMode: Bool) -> Double {
return endTimeDouble(compactMode: compactMode) - startTimeDouble
}

var preciseTimeString: String {
return "\(day.veryShortSymbol)(\(startTime)~\(endTime))"
}

private func endTimeDouble(compactMode: Bool) -> Double {
if compactMode && !isCustom {
return TimeUtils.getTimeInDouble(from: endTime.roundUpForCompactMode())
} else {
return TimeUtils.getTimeInDouble(from: endTime)
}
}
}

private extension String {
func roundUpForCompactMode() -> String {
var time = TimeUtils.getTime(from: self)
if time.minute > 0 && time.minute < 30 {
time.minute = 30
} else if time.minute > 30 {
time.hour += 1
time.minute = 0
}
return time.toString()
}
}

extension TimePlace {
Expand Down
1 change: 1 addition & 0 deletions SNUTT-2022/SNUTT/Models/TimetableConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct TimetableConfiguration: Codable {
var minHour: Int = 9
var maxHour: Int = 18
var autoFit: Bool = true
var compactMode: Bool = false

var visibleWeeks: [Weekday] = [.mon, .tue, .wed, .thu, .fri]

Expand Down
4 changes: 4 additions & 0 deletions SNUTT-2022/SNUTT/Repositories/Dto/TimetableDto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ struct LectureDto: Codable {
let updated_at: String?
let color: LectureColorDto?
let colorIndex: Int?

var isCustom: Bool {
course_number == nil || course_number == ""
}
}

struct LectureColorDto: Codable {
Expand Down
21 changes: 15 additions & 6 deletions SNUTT-2022/SNUTT/Utils/TimeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import Foundation

struct TimeUtils {
struct Time {
let hour: Int
let minute: Int
var hour: Int
var minute: Int

func toString() -> String {
return "\(String(format: "%02d", hour)):\(String(format: "%02d", minute))"
}
}

/// 월요일 7교시인 경우 `월7`을 반환한다.
Expand All @@ -27,7 +31,7 @@ struct TimeUtils {

static func getTimeInString(from time: Double) -> String {
let preciseTime = getPreciseHourMinute(from: time)
return "\(String(format: "%02d", preciseTime.hour)):\(String(format: "%02d", preciseTime.minute))"
return preciseTime.toString()
}

static func getTimeInString(from date: Date) -> String {
Expand Down Expand Up @@ -57,10 +61,15 @@ struct TimeUtils {
return .init(hour: hour, minute: minute)
}

static func getTimeInDouble(from time: String) -> Double {
static func getTime(from time: String) -> Time {
let splitted = time.components(separatedBy: ":")
guard let hour = Int(splitted[0]), let minute = Int(splitted[1]) else { return 0 }
return getTimeInDouble(from: .init(hour: hour, minute: minute))
guard let hour = Int(splitted[0]), let minute = Int(splitted[1]) else { return .init(hour: 0, minute: 0) }
return .init(hour: hour, minute: minute)
}

static func getTimeInDouble(from time: String) -> Double {
let time = getTime(from: time)
return getTimeInDouble(from: time)
}

static func getTimeInDouble(from time: Time) -> Double {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ struct LectureBlocks: View {
#endif
}
.frame(width: Painter.getWeekWidth(in: reader.size, weekCount: Painter.getWeekCount(current: current, config: config)),
height: Painter.getHeight(of: timePlace, in: reader.size, hourCount: Painter.getHourCount(current: current, config: config)),
height: Painter.getHeight(of: timePlace, in: reader.size, hourCount: Painter.getHourCount(current: current, config: config), config: config),
alignment: .top)
.offset(x: offsetPoint.x, y: offsetPoint.y)
.animation(.customSpring, value: config.compactMode)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ struct TimetablePainter {
return nil
}

let weekCount = getWeekCount(current: current, config: config)
let x = hourWidth + CGFloat(weekdayIndex) * getWeekWidth(in: containerSize, weekCount: weekCount)
let x = hourWidth + CGFloat(weekdayIndex) * getWeekWidth(in: containerSize, weekCount: getWeekCount(current: current, config: config))
let y = weekdayHeight + CGFloat(hourIndex) * getHourHeight(in: containerSize, hourCount: getHourCount(current: current, config: config))

return CGPoint(x: x, y: y)
}

/// 주어진 `TimePlace`블록의 높이를 구한다.
static func getHeight(of timePlace: TimePlace, in containerSize: CGSize, hourCount: Int) -> CGFloat {
return timePlace.duration * getHourHeight(in: containerSize, hourCount: hourCount)
static func getHeight(of timePlace: TimePlace, in containerSize: CGSize, hourCount: Int, config: TimetableConfiguration) -> CGFloat {
return timePlace.duration(compactMode: config.compactMode) * getHourHeight(in: containerSize, hourCount: hourCount)
}

// MARK: Auto Fit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ struct TimetableSettingScene: View {
Section {
Toggle("자동 맞춤", isOn: $viewModel.timetableConfig.autoFit)
.animation(.easeInOut, value: viewModel.timetableConfig.autoFit)

Toggle("꽉 찬 시간표", isOn: $viewModel.timetableConfig.compactMode)
.animation(.easeInOut, value: viewModel.timetableConfig.compactMode)
} footer: {
if viewModel.timetableConfig.compactMode {
Text("꽉 찬 시간표를 표시합니다. 직접 추가한 강의와 일부 겹치는 현상이 발생할 수 있습니다.")
}
}

if !viewModel.timetableConfig.autoFit {
Expand Down

0 comments on commit 6fbff29

Please sign in to comment.