Skip to content

Commit ca81b2d

Browse files
Merge pull request #427 from woocommerce/issue/nuking-note-viewmodel
Deprecating OrderNoteViewModel
2 parents bacb2a2 + bfac434 commit ca81b2d

File tree

6 files changed

+136
-103
lines changed

6 files changed

+136
-103
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Foundation
2+
3+
4+
// MARK: - Date Extensions
5+
//
6+
extension Date {
7+
8+
/// Returns the String Representation of the receiver, with the specified Date + Time Styles applied.
9+
///
10+
func toString(dateStyle: DateFormatter.Style, timeStyle: DateFormatter.Style) -> String {
11+
let formatter = DateFormatter()
12+
formatter.dateStyle = dateStyle
13+
formatter.timeStyle = timeStyle
14+
15+
return formatter.string(from: self)
16+
}
17+
}

WooCommerce/Classes/Extensions/Date+Helpers.swift renamed to WooCommerce/Classes/Extensions/DateFormatter+Helpers.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,4 @@ extension DateFormatter {
4848
return formatter
4949
}()
5050
}
51-
5251
}

WooCommerce/Classes/ViewModels/OrderNoteViewModel.swift

Lines changed: 0 additions & 35 deletions
This file was deleted.

WooCommerce/Classes/ViewRelated/Orders/OrderDetails/OrderDetailsViewController.swift

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,53 @@ import CocoaLumberjack
88

99
class OrderDetailsViewController: UIViewController {
1010

11-
// MARK: - Properties
12-
11+
/// Main TableView.
12+
///
1313
@IBOutlet weak var tableView: UITableView!
14-
var viewModel: OrderDetailsViewModel! {
15-
didSet {
16-
reloadSections()
17-
reloadTableViewIfPossible()
18-
}
19-
}
2014

15+
/// Pull To Refresh Support.
16+
///
2117
private lazy var refreshControl: UIRefreshControl = {
2218
let refreshControl = UIRefreshControl()
2319
refreshControl.addTarget(self, action: #selector(pullToRefresh), for: .valueChanged)
2420
return refreshControl
2521
}()
2622

27-
private var orderNotes: [OrderNoteViewModel] = [] {
28-
didSet {
29-
reloadSections()
30-
reloadTableViewIfPossible()
31-
}
32-
}
33-
23+
/// Indicates if the Billing details should be rendered.
24+
///
3425
private var displaysBillingDetails = false {
3526
didSet {
3627
reloadSections()
3728
}
3829
}
39-
private var sections = [Section]()
4030

4131
/// EntityListener: Update / Deletion Notifications.
4232
///
4333
private lazy var entityListener: EntityListener<Order> = {
4434
return EntityListener(storageManager: AppDelegate.shared.storageManager, readOnlyEntity: viewModel.order)
4535
}()
4636

37+
/// Sections to be rendered
38+
///
39+
private var sections = [Section]()
40+
41+
/// Order to be rendered!
42+
///
43+
var viewModel: OrderDetailsViewModel! {
44+
didSet {
45+
reloadSections()
46+
reloadTableViewIfPossible()
47+
}
48+
}
49+
50+
/// Order Notes
51+
///
52+
private var orderNotes: [OrderNote] = [] {
53+
didSet {
54+
reloadSections()
55+
reloadTableViewIfPossible()
56+
}
57+
}
4758

4859

4960
// MARK: - View Lifecycle
@@ -61,11 +72,6 @@ class OrderDetailsViewController: UIViewController {
6172
super.viewWillAppear(animated)
6273
syncNotes()
6374
}
64-
65-
override func viewWillDisappear(_ animated: Bool) {
66-
super.viewWillDisappear(animated)
67-
self.navigationController?.isNavigationBarHidden = false
68-
}
6975
}
7076

7177

@@ -101,6 +107,7 @@ private extension OrderDetailsViewController {
101107
guard let `self` = self else {
102108
return
103109
}
110+
104111
self.viewModel = OrderDetailsViewModel(order: order)
105112
}
106113

@@ -360,11 +367,9 @@ private extension OrderDetailsViewController {
360367
return
361368
}
362369

363-
cell.iconButton.setImage(note.iconImage, for: .normal)
364-
cell.iconButton.backgroundColor = note.iconColor
365-
cell.dateCreated = note.formattedDateCreated
366-
cell.statusText = note.statusText
367-
cell.contents = note.contents
370+
cell.isCustomerNote = note.isCustomerNote
371+
cell.dateCreated = note.dateCreated.toString(dateStyle: .medium, timeStyle: .short)
372+
cell.contents = note.note
368373
}
369374

370375
func configurePayment(cell: PaymentTableViewCell) {
@@ -434,7 +439,7 @@ private extension OrderDetailsViewController {
434439

435440
// MARK: - Get order note
436441
//
437-
func note(at indexPath: IndexPath) -> OrderNoteViewModel? {
442+
func note(at indexPath: IndexPath) -> OrderNote? {
438443
// We need to subtract 1 here because the first order note row is the "Add Order" cell
439444
let noteIndex = indexPath.row - 1
440445
guard orderNotes.indices.contains(noteIndex) else {
@@ -474,7 +479,7 @@ private extension OrderDetailsViewController {
474479
return
475480
}
476481

477-
self?.orderNotes = orderNotes.map { OrderNoteViewModel(with: $0) }
482+
self?.orderNotes = orderNotes
478483
WooAnalytics.shared.track(.orderNotesLoaded, withProperties: ["id": self?.viewModel.order.orderID ?? 0])
479484
onCompletion?(nil)
480485
}
@@ -526,7 +531,7 @@ extension OrderDetailsViewController: UITableViewDataSource {
526531
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
527532
if sections[section].title == nil {
528533
// iOS 11 table bug. Must return a tiny value to collapse `nil` or `empty` section headers.
529-
return CGFloat.leastNonzeroMagnitude
534+
return .leastNonzeroMagnitude
530535
}
531536

532537
return UITableView.automaticDimension
@@ -548,18 +553,13 @@ extension OrderDetailsViewController: UITableViewDataSource {
548553
}
549554

550555
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
551-
// Give the table some breathing room
552-
let lastSection = sections.count - 1
553-
if section == lastSection {
554-
return UITableView.automaticDimension
555-
}
556+
let lastSectionIndex = sections.count - 1
556557

557-
guard let _ = sections[section].footer else {
558-
// iOS 11 table bug. Must return a tiny value to collapse `nil` or `empty` section footers.
559-
return CGFloat.leastNonzeroMagnitude
558+
if sections[section].footer != nil || section == lastSectionIndex {
559+
return UITableView.automaticDimension
560560
}
561561

562-
return UITableView.automaticDimension
562+
return .leastNonzeroMagnitude
563563
}
564564

565565
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
import UIKit
2+
import Gridicons
23

3-
class OrderNoteTableViewCell: UITableViewCell {
4-
override func awakeFromNib() {
5-
super.awakeFromNib()
64

7-
dateLabel.applyBodyStyle()
8-
statusLabel.applyBodyStyle()
9-
noteLabel.applyBodyStyle()
10-
11-
statusLabel.textColor = StyleManager.wooGreyMid
12-
iconButton.layer.cornerRadius = iconButton.frame.width / 2
13-
iconButton.tintColor = .white
14-
}
5+
// MARK: - OrderNoteTableViewCell
6+
//
7+
class OrderNoteTableViewCell: UITableViewCell {
158

16-
@IBOutlet public var iconButton: UIButton!
9+
/// Top Left Icon
10+
///
11+
@IBOutlet private var iconButton: UIButton!
1712

13+
/// Top Right Label
14+
///
1815
@IBOutlet private var dateLabel: UILabel!
16+
17+
/// Secondary Top Label
18+
///
1919
@IBOutlet private var statusLabel: UILabel!
20+
21+
/// Bottom Label
22+
///
2023
@IBOutlet private var noteLabel: UILabel!
2124

22-
public var dateCreated: String? {
25+
26+
// MARK: - View Lifecycle
27+
28+
override func awakeFromNib() {
29+
super.awakeFromNib()
30+
31+
configureLabels()
32+
configureIconButton()
33+
}
34+
35+
36+
/// Date of Creation: To be displayed at the top of the cell
37+
///
38+
var dateCreated: String? {
2339
get {
2440
return dateLabel.text
2541
}
@@ -28,21 +44,57 @@ class OrderNoteTableViewCell: UITableViewCell {
2844
}
2945
}
3046

31-
public var statusText: String? {
47+
/// Note's Payload
48+
///
49+
var contents: String? {
3250
get {
33-
return statusLabel.text
51+
return noteLabel.text
3452
}
3553
set {
36-
statusLabel.text = newValue
54+
noteLabel.text = newValue
3755
}
3856
}
3957

40-
public var contents: String? {
41-
get {
42-
return noteLabel.text
58+
/// Indicates if the note is visible to the Customer (or it's set to private!)
59+
///
60+
var isCustomerNote: Bool = false {
61+
didSet {
62+
privacyWasUpdated()
4363
}
44-
set {
45-
noteLabel.text = newValue
64+
}
65+
}
66+
67+
68+
// MARK: - Private Methods
69+
//
70+
private extension OrderNoteTableViewCell {
71+
72+
/// Updates the Status Label + Icon's Color
73+
///
74+
func privacyWasUpdated() {
75+
if isCustomerNote {
76+
iconButton.backgroundColor = StyleManager.statusPrimaryBoldColor
77+
statusLabel.text = NSLocalizedString("Note to customer", comment: "Labels an order note to let user know it's visible to the customer")
78+
} else {
79+
iconButton.backgroundColor = StyleManager.wooGreyMid
80+
statusLabel.text = NSLocalizedString("Private note", comment: "Labels an order note to let the user know it's private and not seen by the customer")
4681
}
4782
}
83+
84+
/// Setup: Labels
85+
///
86+
func configureLabels() {
87+
dateLabel.applyBodyStyle()
88+
statusLabel.applyBodyStyle()
89+
noteLabel.applyBodyStyle()
90+
}
91+
92+
/// Setup: Icon Button
93+
///
94+
func configureIconButton() {
95+
let image = Gridicon.iconOfType(.aside)
96+
iconButton.setImage(image, for: .normal)
97+
iconButton.layer.cornerRadius = iconButton.frame.width / 2
98+
iconButton.tintColor = .white
99+
}
48100
}

0 commit comments

Comments
 (0)