mirror of
https://github.com/ish-app/ish.git
synced 2026-01-18 13:57:29 +00:00
157 lines
4.7 KiB
Objective-C
157 lines
4.7 KiB
Objective-C
//
|
|
// UserPreferences.m
|
|
// iSH
|
|
//
|
|
// Created by Charlie Melbye on 11/12/18.
|
|
//
|
|
|
|
#import <UIKit/UIKit.h>
|
|
#import "UserPreferences.h"
|
|
|
|
static NSString *const kPreferenceCapsLockMappingKey = @"Caps Lock Mapping";
|
|
static NSString *const kPreferenceFontSizeKey = @"Font Size";
|
|
static NSString *const kPreferenceThemeKey = @"Theme";
|
|
static NSString *const kPreferenceBackgroundKey = @"Background Color";
|
|
|
|
@implementation UserPreferences {
|
|
NSUserDefaults *_defaults;
|
|
}
|
|
|
|
+ (instancetype)shared {
|
|
static UserPreferences *shared = nil;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
shared = [[self alloc] init];
|
|
});
|
|
return shared;
|
|
}
|
|
|
|
- (instancetype)init {
|
|
self = [super init];
|
|
if (self) {
|
|
_defaults = [NSUserDefaults standardUserDefaults];
|
|
Theme *defaultTheme = [Theme presetThemeNamed:@"Light"];
|
|
[_defaults registerDefaults:@{kPreferenceFontSizeKey: @(12),
|
|
kPreferenceThemeKey: defaultTheme.properties,
|
|
kPreferenceCapsLockMappingKey: @(CapsLockMapControl),
|
|
}];
|
|
_theme = [[Theme alloc] initWithProperties:[_defaults objectForKey:kPreferenceThemeKey]];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (CapsLockMapping)capsLockMapping {
|
|
return [_defaults integerForKey:kPreferenceCapsLockMappingKey];
|
|
}
|
|
|
|
- (void)setCapsLockMapping:(CapsLockMapping)capsLockMapping {
|
|
[_defaults setInteger:capsLockMapping forKey:kPreferenceCapsLockMappingKey];
|
|
}
|
|
|
|
- (NSNumber *)fontSize {
|
|
return [_defaults objectForKey:kPreferenceFontSizeKey];
|
|
}
|
|
|
|
- (void)setFontSize:(NSNumber *)fontSize {
|
|
[_defaults setObject:fontSize forKey:kPreferenceFontSizeKey];
|
|
}
|
|
|
|
- (UIColor *)foregroundColor {
|
|
return self.theme.foregroundColor;
|
|
}
|
|
|
|
- (UIColor *)backgroundColor {
|
|
return self.theme.backgroundColor;
|
|
}
|
|
|
|
- (void)setTheme:(Theme *)theme {
|
|
_theme = theme;
|
|
[_defaults setObject:theme.properties forKey:kPreferenceThemeKey];
|
|
}
|
|
|
|
@end
|
|
|
|
static id ArchiveColor(UIColor *color) {
|
|
CGFloat r, g, b;
|
|
[color getRed:&r green:&g blue:&b alpha:nil];
|
|
return [NSString stringWithFormat:@"%f %f %f", r, g, b];
|
|
}
|
|
static UIColor *UnarchiveColor(id data) {
|
|
NSArray<NSString *> *components = [data componentsSeparatedByString:@" "];
|
|
CGFloat r = components[0].doubleValue;
|
|
CGFloat g = components[1].doubleValue;
|
|
CGFloat b = components[2].doubleValue;
|
|
return [UIColor colorWithRed:r green:g blue:b alpha:1];
|
|
}
|
|
|
|
@implementation Theme
|
|
|
|
- (instancetype)initWithProperties:(NSDictionary<NSString *,id> *)props {
|
|
if (self = [super init]) {
|
|
_foregroundColor = UnarchiveColor(props[kThemeForegroundColor]);
|
|
_backgroundColor = UnarchiveColor(props[kThemeBackgroundColor]);
|
|
}
|
|
return self;
|
|
}
|
|
|
|
+ (instancetype)_themeWithForegroundColor:(UIColor *)foreground backgroundColor:(UIColor *)background {
|
|
return [[self alloc] initWithProperties:@{kThemeForegroundColor: ArchiveColor(foreground),
|
|
kThemeBackgroundColor: ArchiveColor(background)}];
|
|
}
|
|
|
|
- (UIStatusBarStyle)statusBarStyle {
|
|
CGFloat lightness;
|
|
[self.backgroundColor getWhite:&lightness alpha:nil];
|
|
if (lightness > 0.5)
|
|
return UIStatusBarStyleDefault;
|
|
else
|
|
return UIStatusBarStyleLightContent;
|
|
}
|
|
|
|
- (UIKeyboardAppearance)keyboardAppearance {
|
|
CGFloat lightness;
|
|
[self.backgroundColor getWhite:&lightness alpha:nil];
|
|
if (lightness > 0.5)
|
|
return UIKeyboardAppearanceLight;
|
|
else
|
|
return UIKeyboardAppearanceDark;
|
|
}
|
|
|
|
- (NSDictionary<NSString *,id> *)properties {
|
|
return @{kThemeForegroundColor: ArchiveColor(self.foregroundColor),
|
|
kThemeBackgroundColor: ArchiveColor(self.backgroundColor)};
|
|
}
|
|
|
|
- (BOOL)isEqual:(id)object {
|
|
if ([self class] != [object class])
|
|
return NO;
|
|
return [self.properties isEqualToDictionary:[object properties]];
|
|
}
|
|
|
|
NSDictionary<NSString *, Theme *> *presetThemes;
|
|
+ (void)initialize {
|
|
presetThemes = @{@"Light": [self _themeWithForegroundColor:UIColor.blackColor
|
|
backgroundColor:UIColor.whiteColor],
|
|
@"Dark": [self _themeWithForegroundColor:UIColor.whiteColor
|
|
backgroundColor:UIColor.blackColor]};
|
|
}
|
|
|
|
+ (NSArray<NSString *> *)presetNames {
|
|
return @[@"Light", @"Dark"];
|
|
}
|
|
+ (instancetype)presetThemeNamed:(NSString *)name {
|
|
return presetThemes[name];
|
|
}
|
|
- (NSString *)presetName {
|
|
for (NSString *name in presetThemes) {
|
|
if ([self isEqual:presetThemes[name]])
|
|
return name;
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
@end
|
|
|
|
NSString *const kThemeForegroundColor = @"ForegroundColor";
|
|
NSString *const kThemeBackgroundColor = @"BackgroundColor";
|