[ios, osx] Split MGLDirectionFormatter in two

Split MGLDirectionFormatter into MGLClockDirectionFormatter, which specializes in clock positions, and MGLCompassDirectionFormatter, which boxes the compass. This neatly avoids any confusion that might arise if the developer fails to read the documentation and thinks the original unified formatter can convert between the two types of directions.
This commit is contained in:
Minh Nguyễn
2016-04-22 11:12:27 -07:00
committed by Minh Nguyễn
parent e5c6cfde4b
commit 2a1a5f0885
12 changed files with 278 additions and 205 deletions

View File

@@ -0,0 +1,64 @@
#import "MGLClockDirectionFormatter.h"
#define wrap(value, min, max) \
(fmod((fmod((value - min), (max - min)) + (max - min)), (max - min)) + min)
@implementation MGLClockDirectionFormatter {
NSNumberFormatter *_numberFormatter;
}
- (instancetype)init {
if (self = [super init]) {
_unitStyle = NSFormattingUnitStyleMedium;
_numberFormatter = [[NSNumberFormatter alloc] init];
}
return self;
}
- (NSLocale *)locale {
return _numberFormatter.locale;
}
- (void)setLocale:(NSLocale *)locale {
_numberFormatter.locale = locale;
}
- (NSString *)stringFromDirection:(CLLocationDirection)direction {
NSInteger hour = round(-wrap(-direction, -360, 0) / 360 * 12);
NSString *format;
NSNumberFormatterStyle style = NSNumberFormatterDecimalStyle;
switch (self.unitStyle) {
case NSFormattingUnitStyleShort:
format = NSLocalizedString(@"%@:00", @"Clock position format, short style");
break;
case NSFormattingUnitStyleMedium:
format = NSLocalizedString(@"%@ o’clock", @"Clock position format, medium style");
break;
case NSFormattingUnitStyleLong:
format = NSLocalizedString(@"%@ o’clock", @"Clock position format, long style");
style = NSNumberFormatterSpellOutStyle;
break;
default:
break;
}
_numberFormatter.numberStyle = style;
return [NSString stringWithFormat:format, [_numberFormatter stringFromNumber:@(hour)]];
}
- (nullable NSString *)stringForObjectValue:(id)obj {
if (![obj isKindOfClass:[NSValue class]]) {
return nil;
}
return [self stringFromDirection:[obj doubleValue]];
}
- (BOOL)getObjectValue:(out id __nullable * __nullable)obj forString:(NSString *)string errorDescription:(out NSString * __nullable * __nullable)error {
NSAssert(NO, @"-getObjectValue:forString:errorDescription: has not been implemented");
return NO;
}
@end