This repository was archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Add clock entry using Measurement #10
Merged
aranasaurus
merged 2 commits into
iosdevelopershq:master
from
felix-dumit:felixdumit-clock
Dec 28, 2016
Merged
Changes from 1 commit
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 hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
CodeChallenge/Challenges/Clock/Entries/felixdumitClockEntry.swift
This file contains hidden or 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,81 @@ | ||
// | ||
// felixdumitClockEntry.swift | ||
// CodeChallenge | ||
// | ||
// Created by Felix Dumit on 12/22/16. | ||
// Copyright © 2016 iosdevelopers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
private var dateFormatter: DateFormatter = { | ||
let df = DateFormatter() | ||
df.dateFormat = "HH:mm:ss" | ||
return df | ||
}() | ||
|
||
let felixdumitClockEntry = CodeChallengeEntry<ClockChallenge>(name: "felix-dumit") { input in | ||
if #available(iOS 10.0, *) { | ||
|
||
guard let date = dateFormatter.date(from: input) else { | ||
fatalError("bad input") | ||
} | ||
|
||
func duration(for component: Calendar.Component) -> Measurement<UnitDuration> { | ||
let cmp = NSCalendar.current.component(component, from: date) | ||
return Measurement<UnitDuration>(value: Double(cmp), unit: component.durationUnit!) | ||
} | ||
|
||
let totalDuration = duration(for: .hour) + duration(for: .minute) + duration(for: .second) | ||
|
||
func clockAngle(unit: UnitDuration) -> Int { | ||
let percent = (totalDuration % unit.durationForClockFullTurn) / unit.durationForClockFullTurn | ||
return Int(360 * percent) | ||
} | ||
|
||
return (clockAngle(unit: .hours), clockAngle(unit: .minutes), clockAngle(unit: .seconds)) | ||
|
||
} else { | ||
return brandonShegaClockEntry.block(input) | ||
} | ||
} | ||
|
||
@available(iOS 10.0, *) | ||
private func /<D:Dimension>(lhs: Measurement<D>, rhs: Measurement<D>) -> Double { | ||
return lhs.converted(to: D.baseUnit()).value / rhs.converted(to: D.baseUnit()).value | ||
} | ||
|
||
@available(iOS 10.0, *) | ||
private func %<D:Dimension>(lhs: Measurement<D>, rhs: Measurement<D>) -> Measurement<D> { | ||
let value = lhs.converted(to: D.baseUnit()).value | ||
.truncatingRemainder(dividingBy: rhs.converted(to: D.baseUnit()).value) | ||
return Measurement(value: value, unit: D.baseUnit()) | ||
} | ||
|
||
@available(iOS 10.0, *) | ||
private extension UnitDuration { | ||
var durationForClockFullTurn: Measurement<UnitDuration> { | ||
switch self { | ||
case UnitDuration.hours: | ||
return Measurement<UnitDuration>(value:12, unit: .hours) | ||
case UnitDuration.minutes: | ||
return Measurement<UnitDuration>(value:1, unit: .hours) | ||
case UnitDuration.seconds: | ||
return Measurement<UnitDuration>(value: 1, unit: .minutes) | ||
default: //should not happen | ||
return Measurement<UnitDuration>(value: 0, unit: .seconds) | ||
} | ||
} | ||
} | ||
|
||
@available(iOS 10.0, *) | ||
private extension Calendar.Component { | ||
var durationUnit: UnitDuration? { | ||
switch self { | ||
case .hour: return .hours | ||
case .minute: return .minutes | ||
case .second: return .seconds | ||
default: return nil | ||
} | ||
} | ||
} |
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.
mmm... Maybe a
fatalError
here instead of returning someone else's solution?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.
☝️
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.
Ok just updated it ;)