Skip to content
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

Conversation

carlos21
Copy link
Member

@carlos21 carlos21 commented Jun 26, 2018

Issue #1

File:
DateTimeCharacteristicViewController.swift

It's already tested using LightBlue app

@carlos21 carlos21 self-assigned this Jun 26, 2018
@carlos21 carlos21 requested a review from colemancda June 26, 2018 02:09
dateComponents.year = Int(GATTDateTime.Year.max.rawValue)
dateComponents.timeZone = TimeZone(identifier: "UTC")

return Calendar.current.date(from: dateComponents)
Copy link
Member

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? {
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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 {
Copy link
Member

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
Copy link
Member

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.

Copy link
Member Author

@carlos21 carlos21 Jun 26, 2018

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()

Copy link
Member

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 = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

@colemancda colemancda merged commit eb1c308 into feature/gatt-characteristics-screens Jun 28, 2018
@colemancda colemancda deleted the feature/add-datetime-characteristic-support branch June 28, 2018 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants