Skip to content

Instantly share code, notes, and snippets.

View hungtruong's full-sized avatar

Hung Truong hungtruong

View GitHub Profile
@hungtruong
hungtruong / GetHomeCards.proto
Last active May 11, 2020 22:58
Proto definition for Quibi's GetHomeCards endpoint
syntax = "proto2";
message HomeCardResponse {
repeated CardContainer container = 1;
}
message CardContainer {
required Card card = 1;
}
func updateProgress(_ progress: Float) {
if self.progressView == nil {
let progressView = UIView()
progressView.backgroundColor = .black
progressView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(progressView)
let trailingConstraint =
progressView.leadingAnchor.constraint(equalTo: self.leadingAnchor)
NSLayoutConstraint.activate([
progressView.topAnchor.constraint(equalTo: self.topAnchor),
func setupWorkout(_ workout: Workout) {
stackView.arrangedSubviews.forEach { view in
view.removeFromSuperview()
stackView.removeArrangedSubview(view)
}
workout.workoutSegments.forEach { workoutSegment in
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
let barView = UIView()
func color() -> UIColor {
switch self.highPower() {
case let p where p <= 0.6 :
return .gray
case let p where p < 0.76:
return .blue
case let p where p < 0.90:
return .green
case let p where p < 1.05:
return .yellow
if let _ = message["sendReminder"] {
[1, 2, 3, 4, 5].forEach { count in
DispatchQueue.main.asyncAfter(deadline: .now() + count) {
self.playNotification()
}
}
}
if let calories = message["calories"] as? Int,
let distance = message["distance"] as? Int,
let start = message["start"] as? Date,
let end = message["end"] as? Date
{
let calorieQuantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
let calorieQuantity = HKQuantity(unit: .kilocalorie(), doubleValue: Double(calories))
let caloriesBurned = HKQuantitySample(type: calorieQuantityType, quantity: calorieQuantity, start: start, end: end)
let distanceQuantityType = HKQuantityType.quantityType(forIdentifier: .distanceCycling)!
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
for type in collectedTypes {
guard let quantityType = type as? HKQuantityType else {
return
}
switch quantityType {
case HKQuantityType.quantityType(forIdentifier: .heartRate):
guard let statistics = workoutBuilder.statistics(for: quantityType),
let value = statistics.mostRecentQuantity()?.doubleValue(for: heartRateUnit)
self.workoutSession = session
self.workoutBuilder = workoutSession.associatedWorkoutBuilder()
self.workoutBuilder.dataSource =
HKLiveWorkoutDataSource(healthStore: healthStore,
workoutConfiguration: workoutConfiguration)
self.workoutSession.delegate = self
self.workoutBuilder.delegate = self
self.workoutBuilder.beginCollection(withStart: Date()) { (_, _) in }
session.startActivity(with: Date())
func wattage(for ftp: Int, interval: TimeInterval) -> Int {
switch self {
case .warmup(duration: let duration, powerLow: let powerLow, powerHigh: let powerHigh):
return Int((((interval / duration) * (powerHigh - powerLow)) + powerLow) * Double(ftp))
case .steady(duration: _, power: let power):
return Int(power * Double(ftp))
case .intervals(reps: _, onDuration: _, offDuration: _, onPower: _, offPower: _):
return 0
case .cooldown(duration: let duration, powerLow: let powerLow, powerHigh: let powerHigh):
return Int(((((duration - interval) / duration) * (powerHigh - powerLow)) + powerLow) * Double(ftp))
enum WorkoutSegment {
case warmup(duration: TimeInterval, powerLow: Double, powerHigh: Double)
case intervals(reps: Int, onDuration: TimeInterval, offDuration: TimeInterval, onPower: Double,
offPower: Double)
case steady(duration: TimeInterval, power: Double)
case cooldown(duration: TimeInterval, powerLow: Double, powerHigh: Double)
}