Skip to content

Commit a49ce10

Browse files
authored
Merge pull request #257 from woocommerce/feature/138-section-dates
Order List: Date grouping and sections
2 parents 5971fc4 + 975ab32 commit a49ce10

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

WooCommerce/Classes/Model/Order+Woo.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,37 @@ extension Order {
1414

1515
return NSLocale(localeIdentifier: identifier).currencySymbol
1616
}
17+
18+
/// Translates a Section Identifier into a Human-Readable String.
19+
///
20+
static func descriptionForSectionIdentifier(_ identifier: String) -> String {
21+
guard let age = Age(rawValue: identifier) else {
22+
return String()
23+
}
24+
25+
return age.description
26+
}
27+
}
28+
29+
enum Age: String {
30+
case months = "0"
31+
case weeks = "2"
32+
case days = "4"
33+
case yesterday = "5"
34+
case today = "6"
35+
36+
var description: String {
37+
switch self {
38+
case .months:
39+
return NSLocalizedString("Older than a Month", comment: "Notifications Months Section Header")
40+
case .weeks:
41+
return NSLocalizedString("Older than a Week", comment: "Notifications Weeks Section Header")
42+
case .days:
43+
return NSLocalizedString("Older than 2 days", comment: "Notifications +2 Days Section Header")
44+
case .yesterday:
45+
return NSLocalizedString("Yesterday", comment: "Notifications Yesterday Section Header")
46+
case .today:
47+
return NSLocalizedString("Today", comment: "Notifications Today Section Header")
48+
}
49+
}
1750
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
import WordPressShared
3+
import Yosemite
4+
5+
extension StorageOrder {
6+
/// Returns a Section Identifier that can be sorted. Note that this string is not human readable, and
7+
/// you should use the *descriptionForSectionIdentifier* method as well!.
8+
///
9+
@objc func normalizedAgeAsString() -> String {
10+
// Normalize Dates: Time must not be considered. Just the raw dates
11+
guard let fromDate = dateCreated?.normalizedDate() else {
12+
return ""
13+
}
14+
15+
let toDate = Date().normalizedDate()
16+
17+
// Analyze the Delta-Components
18+
let calendar = Calendar.current
19+
let components = [.day, .weekOfYear, .month] as Set<Calendar.Component>
20+
let dateComponents = calendar.dateComponents(components, from: fromDate, to: toDate)
21+
let identifier: Age
22+
23+
// Months
24+
if let month = dateComponents.month, month >= 1 {
25+
identifier = .months
26+
// Weeks
27+
} else if let week = dateComponents.weekOfYear, week >= 1 {
28+
identifier = .weeks
29+
// Days
30+
} else if let day = dateComponents.day, day > 1 {
31+
identifier = .days
32+
} else if let day = dateComponents.day, day == 1 {
33+
identifier = .yesterday
34+
} else {
35+
identifier = .today
36+
}
37+
38+
return identifier.rawValue
39+
}
40+
}

WooCommerce/Classes/ViewRelated/Orders/OrdersViewController.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class OrdersViewController: UIViewController {
2626
let storageManager = AppDelegate.shared.storageManager
2727
let descriptor = NSSortDescriptor(keyPath: \StorageOrder.dateCreated, ascending: false)
2828

29-
return ResultsController<StorageOrder>(storageManager: storageManager, sortedBy: [descriptor])
29+
return ResultsController<StorageOrder>(storageManager: storageManager, sectionNameKeyPath: "normalizedAgeAsString", sortedBy: [descriptor])
3030
}()
3131

3232
/// Indicates if there are any Objects matching the criteria (or not).
@@ -236,9 +236,7 @@ extension OrdersViewController: UITableViewDataSource {
236236
}
237237

238238
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
239-
// FIXME: this is hard-coded data. Will fix when WordPressShared date helpers are available to make fuzzy dates.
240-
// return NSLocalizedString("Today", comment: "Title for header section")
241-
return nil
239+
return Order.descriptionForSectionIdentifier(resultsController.sections[section].name)
242240
}
243241
}
244242

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
CE21B3E020FFC59700A259D5 /* ProductDetailsTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE21B3DE20FFC59700A259D5 /* ProductDetailsTableViewCell.swift */; };
131131
CE21B3E120FFC59700A259D5 /* ProductDetailsTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE21B3DF20FFC59700A259D5 /* ProductDetailsTableViewCell.xib */; };
132132
CE22571B20E16FBC0037F478 /* LeftImageTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE1EC8EB20B8A3FF009762BF /* LeftImageTableViewCell.xib */; };
133+
CE24BCD8212F25D4001CD12E /* StorageOrder+Woo.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE24BCD7212F25D4001CD12E /* StorageOrder+Woo.swift */; };
133134
CE263DE6206ACD220015A693 /* NotificationsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE263DE5206ACD220015A693 /* NotificationsViewController.swift */; };
134135
CE263DE8206ACE3E0015A693 /* MainTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE263DE7206ACE3E0015A693 /* MainTabBarController.swift */; };
135136
CE32B10B20BEDE05006FBCF4 /* TwoColumnSectionHeaderView.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE32B10A20BEDE05006FBCF4 /* TwoColumnSectionHeaderView.xib */; };
@@ -316,6 +317,7 @@
316317
CE21B3DC20FF9BC200A259D5 /* ProductListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductListViewController.swift; sourceTree = "<group>"; };
317318
CE21B3DE20FFC59700A259D5 /* ProductDetailsTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductDetailsTableViewCell.swift; sourceTree = "<group>"; };
318319
CE21B3DF20FFC59700A259D5 /* ProductDetailsTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ProductDetailsTableViewCell.xib; sourceTree = "<group>"; };
320+
CE24BCD7212F25D4001CD12E /* StorageOrder+Woo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StorageOrder+Woo.swift"; sourceTree = "<group>"; };
319321
CE263DE5206ACD220015A693 /* NotificationsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsViewController.swift; sourceTree = "<group>"; };
320322
CE263DE7206ACE3E0015A693 /* MainTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabBarController.swift; sourceTree = "<group>"; };
321323
CE32B10A20BEDE05006FBCF4 /* TwoColumnSectionHeaderView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TwoColumnSectionHeaderView.xib; sourceTree = "<group>"; };
@@ -569,6 +571,7 @@
569571
B57B678B2107638C00AF8905 /* Order+Woo.swift */,
570572
748C777F211E18A600814F2C /* OrderStats+Woo.swift */,
571573
B5E96B3721137AA100DF68D0 /* OrderStatus+Woo.swift */,
574+
CE24BCD7212F25D4001CD12E /* StorageOrder+Woo.swift */,
572575
);
573576
path = Model;
574577
sourceTree = "<group>";
@@ -1157,6 +1160,7 @@
11571160
7421344A210A323C00C13890 /* WooAnalyticsStat.swift in Sources */,
11581161
CE1EC8D120B6FE39009762BF /* FootnoteView.swift in Sources */,
11591162
B58B4AB82108F14700076FDD /* NoticeNotificationInfo.swift in Sources */,
1163+
CE24BCD8212F25D4001CD12E /* StorageOrder+Woo.swift in Sources */,
11601164
B557DA1520979904005962F4 /* CustomerNoteTableViewCell.swift in Sources */,
11611165
CE855366209BA6A700938BDC /* CustomerInfoTableViewCell.swift in Sources */,
11621166
B5A8532220BDBFAF00FAAB4D /* CircularImageView.swift in Sources */,

0 commit comments

Comments
 (0)