Skip to content

Commit

Permalink
fix: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanStepanok committed Aug 22, 2024
1 parent 2e43d42 commit b8b54cd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
43 changes: 28 additions & 15 deletions Core/Core/Extensions/DateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,53 @@ public extension Date {
self.init(timeInterval: 0, since: date)
}

func timeAgoDisplay() -> String {
func timeAgoDisplay(dueIn: Bool = false) -> String {
let currentDate = Date()
let calendar = Calendar.current

let dueString = dueIn ? CoreLocalization.Date.due : ""
let dueInString = dueIn ? CoreLocalization.Date.dueIn : ""

let startOfCurrentDate = calendar.startOfDay(for: currentDate)
let startOfSelfDate = calendar.startOfDay(for: self)

let daysRemaining = Calendar.current.dateComponents(
[.day],
from: startOfCurrentDate,
to: self
).day ?? 0

// Calculate date ranges
guard let sevenDaysAgo = calendar.date(byAdding: .day, value: -7, to: startOfCurrentDate),
let sevenDaysAhead = calendar.date(byAdding: .day, value: 7, to: startOfCurrentDate) else {
return self.dateToString(style: .mmddyy, useRelativeDates: false)
return dueInString + self.dateToString(style: .mmddyy, useRelativeDates: false)
}

let isCurrentYear = calendar.component(.year, from: self) == calendar.component(.year, from: currentDate)
let isCurrentYear = calendar.component(.year, from: self) == calendar.component(.year, from: startOfCurrentDate)

if calendar.isDateInToday(self) {
return CoreLocalization.Date.today
if calendar.isDateInToday(startOfSelfDate) {
return dueString + CoreLocalization.Date.today
}

if calendar.isDateInYesterday(self) {
return CoreLocalization.yesterday
if calendar.isDateInYesterday(startOfSelfDate) {
return dueString + CoreLocalization.yesterday
}

if calendar.isDateInTomorrow(self) {
return CoreLocalization.tomorrow
if calendar.isDateInTomorrow(startOfSelfDate) {
return dueString + CoreLocalization.tomorrow
}

if startOfSelfDate > startOfCurrentDate && startOfSelfDate <= sevenDaysAhead {
let weekdayFormatter = DateFormatter()
weekdayFormatter.dateFormat = "EEEE"
if startOfSelfDate == calendar.date(byAdding: .day, value: 1, to: startOfCurrentDate) {
return CoreLocalization.tomorrow
return dueInString + CoreLocalization.tomorrow
} else if startOfSelfDate == calendar.date(byAdding: .day, value: 7, to: startOfCurrentDate) {
return CoreLocalization.Date.next(weekdayFormatter.string(from: startOfSelfDate))
} else {
return weekdayFormatter.string(from: startOfSelfDate)
return dueIn ? (
CoreLocalization.Date.dueInDays(daysRemaining)
) : weekdayFormatter.string(from: startOfSelfDate)
}
}

Expand All @@ -81,7 +92,7 @@ public extension Date {

let specificFormatter = DateFormatter()
specificFormatter.dateFormat = isCurrentYear ? "MMMM d" : "MMMM d, yyyy"
return specificFormatter.string(from: self)
return dueInString + specificFormatter.string(from: self)
}

func isDateInNextWeek(date: Date, currentDate: Date) -> Bool {
Expand Down Expand Up @@ -148,13 +159,13 @@ public extension Date {
return totalSeconds
}

func dateToString(style: DateStringStyle, useRelativeDates: Bool) -> String {
func dateToString(style: DateStringStyle, useRelativeDates: Bool, dueIn: Bool = false) -> String {
let dateFormatter = DateFormatter()

dateFormatter.locale = .current

if useRelativeDates {
return timeAgoDisplay()
return timeAgoDisplay(dueIn: dueIn)
} else {
switch style {
case .courseStartsMonthDDYear:
Expand Down Expand Up @@ -213,7 +224,9 @@ public extension Date {
case .iso8601:
return date
case .shortWeekdayMonthDayYear:
return getShortWeekdayMonthDayYear(dateFormatterString: date)
return (
dueIn ? CoreLocalization.Date.dueIn : ""
) + getShortWeekdayMonthDayYear(dateFormatterString: date)
}
}

Expand Down
8 changes: 8 additions & 0 deletions Core/Core/SwiftGen/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ public enum CoreLocalization {
public static func daysAgo(_ p1: Any) -> String {
return CoreLocalization.tr("Localizable", "DATE.DAYS_AGO", String(describing: p1), fallback: "%@ Days Ago")
}
/// Due
public static let due = CoreLocalization.tr("Localizable", "DATE.DUE", fallback: "Due ")
/// Due in
public static let dueIn = CoreLocalization.tr("Localizable", "DATE.DUE_IN", fallback: "Due in ")
/// Due in %@ Days
public static func dueInDays(_ p1: Any) -> String {
return CoreLocalization.tr("Localizable", "DATE.DUE_IN_DAYS", String(describing: p1), fallback: "Due in %@ Days")
}
/// Ended
public static let ended = CoreLocalization.tr("Localizable", "DATE.ENDED", fallback: "Ended")
/// Just now
Expand Down
3 changes: 3 additions & 0 deletions Core/Core/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"DATE.TODAY" = "Today";
"DATE.NEXT" = "Next %@";
"DATE.DAYS_AGO" = "%@ Days Ago";
"DATE.DUE" = "Due ";
"DATE.DUE_IN" = "Due in ";
"DATE.DUE_IN_DAYS" = "Due in %@ Days";

"ALERT.ACCEPT" = "ACCEPT";
"ALERT.CANCEL" = "CANCEL";
Expand Down
20 changes: 15 additions & 5 deletions Dashboard/Dashboard/Presentation/Elements/PrimaryCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public struct PrimaryCardView: View {
).day ?? 0
courseButton(
title: futureAssignment.title,
description: DashboardLocalization.Learn.PrimaryCard.dueDays(
futureAssignment.type,
daysRemaining
description: futureAssignment.date.dateToString(
style: .shortWeekdayMonthDayYear,
useRelativeDates: useRelativeDates,
dueIn: true
),
icon: CoreAssets.chapter.swiftUIImage,
selected: false,
Expand Down Expand Up @@ -264,13 +265,22 @@ struct PrimaryCardView_Previews: PreviewProvider {
courseImage: "https://thumbs.dreamstime.com/b/logo-edx-samsung-tablet-edx-massive-open-online-course-mooc-provider-hosts-online-university-level-courses-wide-117763805.jpg",
courseStartDate: nil,
courseEndDate: Date(),
futureAssignments: [],
futureAssignments: [
Assignment(
type: "Lesson",
title: "HomeWork",
description: "Some description",
date: Date().addingTimeInterval(64000 * 3),
complete: false,
firstComponentBlockId: "123"
)
],
pastAssignments: [],
progressEarned: 10,
progressPossible: 45,
canResume: true,
resumeTitle: "Course Chapter 1",
useRelativeDates: true,
useRelativeDates: false,
assignmentAction: { _ in },
openCourseAction: {},
resumeAction: {}
Expand Down

0 comments on commit b8b54cd

Please sign in to comment.