|
| 1 | +// |
| 2 | +// SCTimeSlider.m |
| 3 | +// SelfControl |
| 4 | +// |
| 5 | +// Created by Charlie Stigler on 4/17/21. |
| 6 | +// |
| 7 | + |
| 8 | +#import "SCDurationSlider.h" |
| 9 | +#import "SCTimeIntervalFormatter.h" |
| 10 | +#import <TransformerKit/NSValueTransformer+TransformerKit.h> |
| 11 | + |
| 12 | +#define kValueTransformerName @"BlockDurationSliderTransformer" |
| 13 | + |
| 14 | +@implementation SCDurationSlider |
| 15 | + |
| 16 | +- (void)drawRect:(NSRect)dirtyRect { |
| 17 | + [super drawRect:dirtyRect]; |
| 18 | + |
| 19 | + // Drawing code here. |
| 20 | +} |
| 21 | + |
| 22 | +- (instancetype)initWithCoder:(NSCoder *)coder { |
| 23 | + if (self = [super initWithCoder: coder]) { |
| 24 | + [self initializeDurationProperties]; |
| 25 | + } |
| 26 | + return self; |
| 27 | +} |
| 28 | +- (instancetype)init { |
| 29 | + if (self = [super init]) { |
| 30 | + [self initializeDurationProperties]; |
| 31 | + } |
| 32 | + return self; |
| 33 | +} |
| 34 | + |
| 35 | +- (void)initializeDurationProperties { |
| 36 | + // default: 1 day max with ticks every 15 minutes |
| 37 | + _maxDuration = 1440; |
| 38 | + _durationTickInterval = 15; |
| 39 | + |
| 40 | + // register an NSValueTransformer |
| 41 | + [self registerMinutesValueTransformer]; |
| 42 | +} |
| 43 | + |
| 44 | +- (void)setMaxDuration:(NSInteger)maxDuration { |
| 45 | + _maxDuration = maxDuration; |
| 46 | + [self recalculateSliderIntervals]; |
| 47 | +} |
| 48 | +- (void)setDurationTickInterval:(NSInteger)durationTickInterval { |
| 49 | + _durationTickInterval = durationTickInterval; |
| 50 | + [self recalculateSliderIntervals]; |
| 51 | +} |
| 52 | + |
| 53 | +- (void)recalculateSliderIntervals { |
| 54 | + // no dividing by 0 for us! |
| 55 | + if (self.durationTickInterval == 0) { |
| 56 | + _durationTickInterval = 1; |
| 57 | + } |
| 58 | + |
| 59 | + long numTickMarks = (self.maxDuration / self.durationTickInterval) + 1; |
| 60 | + [self setMaxValue: self.maxDuration]; |
| 61 | + [self setNumberOfTickMarks: numTickMarks]; |
| 62 | +} |
| 63 | + |
| 64 | +- (void)registerMinutesValueTransformer { |
| 65 | + [NSValueTransformer registerValueTransformerWithName: kValueTransformerName |
| 66 | + transformedValueClass: [NSNumber class] |
| 67 | + returningTransformedValueWithBlock:^id _Nonnull(id _Nonnull value) { |
| 68 | + // if it's not a number or convertable to one, IDK man |
| 69 | + if (![value respondsToSelector: @selector(intValue)]) return @0; |
| 70 | + |
| 71 | + NSInteger minutesValue= [self sliderValueToMinutes: [value intValue]]; |
| 72 | + |
| 73 | + return @(minutesValue); |
| 74 | + }]; |
| 75 | +} |
| 76 | + |
| 77 | +- (NSInteger)sliderValueToMinutes:(NSInteger)value { |
| 78 | + // instead of having 0 as the first option (who would ever want to start a 0-minute block?) |
| 79 | + // we make it 1 minute, which is super handy for testing blocklists |
| 80 | + // (of course, if the next tick mark is 1 minute anyway, we can skip that) |
| 81 | + if (value == 0 && self.durationTickInterval != 1) { |
| 82 | + return 1; |
| 83 | + } |
| 84 | + |
| 85 | + return value; |
| 86 | +} |
| 87 | +- (NSInteger)durationValueMinutes { |
| 88 | + return [self sliderValueToMinutes: self.intValue]; |
| 89 | +} |
| 90 | + |
| 91 | +- (void)bindDurationToObject:(id)obj keyPath:(NSString*)keyPath { |
| 92 | + [self bind: @"value" |
| 93 | + toObject: obj |
| 94 | + withKeyPath: keyPath |
| 95 | + options: @{ |
| 96 | + NSContinuouslyUpdatesValueBindingOption: @YES, |
| 97 | + NSValueTransformerNameBindingOption: kValueTransformerName |
| 98 | + }]; |
| 99 | +} |
| 100 | + |
| 101 | +- (NSString*)durationDescription { |
| 102 | + return [SCDurationSlider timeSliderDisplayStringFromNumberOfMinutes: self.durationValueMinutes]; |
| 103 | +} |
| 104 | + |
| 105 | +// String conversion utility methods |
| 106 | + |
| 107 | ++ (NSString *)timeSliderDisplayStringFromTimeInterval:(NSTimeInterval)numberOfSeconds { |
| 108 | + static SCTimeIntervalFormatter* formatter = nil; |
| 109 | + if (formatter == nil) { |
| 110 | + formatter = [[SCTimeIntervalFormatter alloc] init]; |
| 111 | + } |
| 112 | + |
| 113 | + NSString* formatted = [formatter stringForObjectValue:@(numberOfSeconds)]; |
| 114 | + return formatted; |
| 115 | +} |
| 116 | + |
| 117 | ++ (NSString *)timeSliderDisplayStringFromNumberOfMinutes:(NSInteger)numberOfMinutes { |
| 118 | + if (numberOfMinutes < 0) return @"Invalid duration"; |
| 119 | + |
| 120 | + static NSCalendar* gregorian = nil; |
| 121 | + if (gregorian == nil) { |
| 122 | + gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; |
| 123 | + } |
| 124 | + |
| 125 | + NSRange secondsRangePerMinute = [gregorian |
| 126 | + rangeOfUnit:NSCalendarUnitSecond |
| 127 | + inUnit:NSCalendarUnitMinute |
| 128 | + forDate:[NSDate date]]; |
| 129 | + NSInteger numberOfSecondsPerMinute = (NSInteger)NSMaxRange(secondsRangePerMinute); |
| 130 | + |
| 131 | + NSTimeInterval numberOfSecondsSelected = (NSTimeInterval)(numberOfSecondsPerMinute * numberOfMinutes); |
| 132 | + |
| 133 | + NSString* displayString = [SCDurationSlider timeSliderDisplayStringFromTimeInterval:numberOfSecondsSelected]; |
| 134 | + return displayString; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +@end |
0 commit comments