-
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
Merged
colemancda
merged 2 commits into
feature/gatt-characteristics-screens
from
feature/add-datetime-characteristic-support
Jun 28, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...xplorer/Controller/GATT Characteristic Editors/DateTimeCharacteristicViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// DateTimeCharacteristicViewController.swift | ||
// BluetoothExplorer | ||
// | ||
// Created by Carlos Duclos on 6/25/18. | ||
// Copyright © 2018 PureSwift. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import CoreData | ||
import Bluetooth | ||
import GATT | ||
|
||
final class DateTimeCharacteristicViewController: UIViewController, CharacteristicViewController, InstantiableViewController { | ||
|
||
// MARK: - IB Outlets | ||
|
||
@IBOutlet private(set) weak var dateTimeLabel: UILabel! | ||
|
||
@IBOutlet private(set) weak var datePicker: UIDatePicker! | ||
|
||
// MARK: - Properties | ||
|
||
var value = GATTDateTime(date: Date()) | ||
|
||
var valueDidChange: ((GATTDateTime) -> ())? | ||
|
||
private lazy var minimumDate: Date = { | ||
|
||
var dateComponents = DateComponents() | ||
dateComponents.year = Int(GATTDateTime.Year.min.rawValue) | ||
dateComponents.timeZone = TimeZone(identifier: "UTC") | ||
|
||
guard let minimumDate = calendar.date(from: dateComponents) | ||
else { fatalError("Couldn't create minimumDate") } | ||
|
||
return minimumDate | ||
}() | ||
|
||
private lazy var maximumDate: Date = { | ||
|
||
var dateComponents = DateComponents() | ||
dateComponents.year = Int(GATTDateTime.Year.max.rawValue) | ||
dateComponents.timeZone = TimeZone(identifier: "UTC") | ||
|
||
guard let maximumDate = calendar.date(from: dateComponents) | ||
else { fatalError("Couldn't create maximumDate") } | ||
|
||
return maximumDate | ||
}() | ||
|
||
private lazy var calendar: Calendar = { | ||
|
||
return Calendar(identifier: .gregorian) | ||
}() | ||
|
||
private lazy var timeZone: TimeZone = { | ||
|
||
guard let timezone = TimeZone(identifier: "UTC") | ||
else { fatalError("Couldn't create timezone") } | ||
|
||
return timezone | ||
}() | ||
|
||
// MARK: - Loading | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
datePicker.minimumDate = minimumDate | ||
datePicker.maximumDate = maximumDate | ||
|
||
configureView() | ||
} | ||
|
||
// MARK: - Actions | ||
|
||
@IBAction func datePickerChanged(_ sender: Any) { | ||
|
||
let dateString = CustomDateFormatter.default.string(from: datePicker.date) | ||
dateTimeLabel.text = dateString | ||
|
||
self.value = GATTDateTime(date: datePicker.date) | ||
valueDidChange?(value) | ||
} | ||
|
||
// MARK: - Methods | ||
|
||
func configureView() { | ||
|
||
guard isViewLoaded else { return } | ||
|
||
updateDatePicker() | ||
} | ||
|
||
func updateDatePicker() { | ||
|
||
guard let date = value.dateComponents.date | ||
else { return } | ||
|
||
dateTimeLabel.text = CustomDateFormatter.default.string(from: date) | ||
datePicker.date = date | ||
} | ||
|
||
} | ||
|
||
extension DateTimeCharacteristicViewController { | ||
|
||
struct CustomDateFormatter { | ||
|
||
static let `default`: DateFormatter = { | ||
|
||
let formatter = DateFormatter() | ||
formatter.dateFormat = "MM/dd/yyyy HH:mm:ss" | ||
return formatter | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
BluetoothExplorer/Controller/Protocols/InstantiableViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// InstantiableViewController.swift | ||
// BluetoothExplorer | ||
// | ||
// Created by Carlos Duclos on 6/26/18. | ||
// Copyright © 2018 PureSwift. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol InstantiableViewController { | ||
|
||
static func instantiate<T>() -> T | ||
} | ||
|
||
extension InstantiableViewController 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 | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍🏻