Skip to content

Commit

Permalink
Allow locale to be injected in TRSettingsController
Browse files Browse the repository at this point in the history
  • Loading branch information
sharplet committed Apr 18, 2016
1 parent 9009566 commit befadb5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Tropos/Controllers/TRSettingsController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef NS_ENUM(NSInteger, TRUnitSystem) {

@property (nonatomic, readonly) RACSignal *unitSystemChanged;

- (instancetype)initWithLocale:(NSLocale *)locale NS_DESIGNATED_INITIALIZER;

- (void)registerSettings;

- (TRUnitSystem)unitSystem;
Expand Down
11 changes: 9 additions & 2 deletions Tropos/Controllers/TRSettingsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

@interface TRSettingsController ()

@property (nonatomic) NSLocale *locale;
@property (nonatomic) RACSignal *userDefaultsChanged;

@end
Expand All @@ -15,16 +16,22 @@ @implementation TRSettingsController

#pragma mark - Initializers

- (instancetype)init
- (instancetype)initWithLocale:(NSLocale *)locale
{
self = [super init];
if (!self) return nil;

self.locale = locale;
self.userDefaultsChanged = [[NSNotificationCenter defaultCenter] rac_addObserverForName:NSUserDefaultsDidChangeNotification object:nil];

return self;
}

- (instancetype)init
{
return [self initWithLocale:[NSLocale autoupdatingCurrentLocale]];
}

#pragma mark - Properties

- (RACSignal *)unitSystemChanged
Expand Down Expand Up @@ -60,7 +67,7 @@ - (void)setUnitSystem:(TRUnitSystem)unitSystem

- (void)registerUnitSystem
{
BOOL localeUsesMetric = [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];
BOOL localeUsesMetric = [[self.locale objectForKey:NSLocaleUsesMetricSystem] boolValue];
TRUnitSystem unitSystem = localeUsesMetric? TRUnitSystemMetric : TRUnitSystemImperial;
[[NSUserDefaults standardUserDefaults] registerDefaults:@{TRSettingsUnitSystemKey: @(unitSystem)}];
}
Expand Down

0 comments on commit befadb5

Please sign in to comment.