-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Date Time Characteristic View Controller #2
Added Date Time Characteristic View Controller #2
Conversation
dateComponents.year = Int(GATTDateTime.Year.max.rawValue) | ||
dateComponents.timeZone = TimeZone(identifier: "UTC") | ||
|
||
return Calendar.current.date(from: dateComponents) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Calendar.gregorian
.
|
||
var valueDidChange: ((GATTDateTime) -> ())? | ||
|
||
var minimumDate: Date? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this not be optional? Also can you make it a private lazy var
so it is calculated only once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make it not optional if I use force unwrapping xD
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, use guard with fatalError
like in you are doing in viewDidLoad
.
|
||
@IBAction func datePickerChanged(_ sender: Any) { | ||
|
||
let strDate = CustomDateFormatter.default.string(from: datePicker.date) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be dateString
.
let strDate = CustomDateFormatter.default.string(from: datePicker.date) | ||
dateTimeLabel.text = strDate | ||
|
||
let dateComponents = Calendar.current.dateComponents(in: TimeZone(identifier: "UTC")!, from: datePicker.date) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the TimeZone(identifier: "UTC")!
also be a private let
or private lazy var
on the view controller to avoid unnecessary allocation.
|
||
let dateComponents = Calendar.current.dateComponents(in: TimeZone(identifier: "UTC")!, from: datePicker.date) | ||
|
||
let datetime = GATTDateTime(dateComponents: dateComponents) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GATTDateTime
Can be initialized from Date
, not sure why we are using DateComponents
(which has a fallible initializer.
|
||
func updateDatePicker() { | ||
|
||
if let date = value.dateComponents.date { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer guard
|
||
let storyboard = UIStoryboard(name: "DateTimeCharacteristic", bundle: .main) | ||
|
||
let viewController = storyboard.instantiateInitialViewController() as! DateTimeCharacteristicViewController |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am open to any suggestions for improvement or code reusability for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like to use this approach
protocol InstantiableController {
static func instantiate<T>() -> T
}
extension InstantiableController where Self: UIViewController {
static func instantiate<T>() -> T {
guard let storyboardName = String(describing: self).text(before: "ViewController") else {
fatalError("The controller name is not standard.")
}
let storyboard = UIStoryboard(name: storyboardName, bundle: Bundle(for: self))
let identifier = String(describing: T.self)
guard let viewController = storyboard.instantiateViewController(withIdentifier: identifier) as? T else {
fatalError("The storyboard identifier does not exist.")
}
return viewController
}
}
For example:
Storyboard
: DateTimeCharacteristic
ViewController
: DateTimeCharacteristicViewController
where the name of the controller must be the same as the storyboard plus ViewController
To avoid mistakes on storyboard and controller naming, I use unit tests.
In that way I just need to add InstantiableController
to any controller and call:
let dateTimeController: DateTimeCharacteristicViewController = .instantiate()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use that approach then, make it a separate protocol.
|
||
struct CustomDateFormatter { | ||
|
||
static let `default`: DateFormatter = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
Issue #1
File:
DateTimeCharacteristicViewController.swift
It's already tested using LightBlue app