element-ios/Riot/Managers/Theme/ThemeService.m

120 lines
3.2 KiB
Mathematica
Raw Normal View History

/*
Copyright 2016 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
2019-01-11 10:45:27 +00:00
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "ThemeService.h"
#ifdef IS_SHARE_EXTENSION
#import "RiotShareExtension-Swift.h"
#else
#import "Riot-Swift.h"
#endif
NSString *const kThemeServiceDidChangeThemeNotification = @"kThemeServiceDidChangeThemeNotification";
@implementation ThemeService
2019-01-14 09:53:43 +00:00
@synthesize themeId;
2019-01-11 10:45:27 +00:00
+ (ThemeService *)shared
{
static ThemeService *sharedOnceInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
2019-01-11 10:45:27 +00:00
sharedOnceInstance = [ThemeService new];
});
return sharedOnceInstance;
}
2019-01-14 09:53:43 +00:00
- (void)setThemeId:(NSString *)theThemeId
2019-01-11 10:45:27 +00:00
{
2019-01-14 09:53:43 +00:00
// Update the current theme
themeId = theThemeId;
self.theme = [self themeWithThemeId:self.themeId];
}
2019-01-11 10:45:27 +00:00
2019-01-14 09:53:43 +00:00
- (void)setTheme:(id<Theme> _Nonnull)theme
{
_theme = theme;
2019-01-11 10:45:27 +00:00
2019-01-14 09:53:43 +00:00
[UIScrollView appearance].indicatorStyle = self.theme.scrollBarStyle;
[[NSNotificationCenter defaultCenter] postNotificationName:kThemeServiceDidChangeThemeNotification object:nil];
2019-01-11 10:45:27 +00:00
}
- (id<Theme>)themeWithThemeId:(NSString*)themeId
{
2019-01-11 17:29:32 +00:00
id<Theme> theme;
2019-01-11 10:45:27 +00:00
2019-01-14 09:53:43 +00:00
if ([themeId isEqualToString:@"auto"])
{
// Translate "auto" into a theme
themeId = UIAccessibilityIsInvertColorsEnabled() ? @"dark" : @"light";
}
2019-01-11 10:45:27 +00:00
if ([themeId isEqualToString:@"dark"])
{
2019-01-11 17:29:32 +00:00
theme = [DarkTheme new];
2019-01-11 10:45:27 +00:00
}
else if ([themeId isEqualToString:@"black"])
{
2019-01-11 17:29:32 +00:00
// TODO: Use dark theme for the moment
theme = [DarkTheme new];
}
else
{
// Use light theme by default
theme = [DefaultTheme new];
2019-01-11 10:45:27 +00:00
}
return theme;
}
#pragma mark - Private methods
2019-01-11 10:57:02 +00:00
- (instancetype)init
{
2019-01-11 10:57:02 +00:00
self = [super init];
if (self)
{
// Riot Colors not yet themeable
_riotColorPinkRed = [[UIColor alloc] initWithRgb:0xFF0064];
_riotColorRed = [[UIColor alloc] initWithRgb:0xFF4444];
_riotColorBlue = [[UIColor alloc] initWithRgb:0x81BDDB];
_riotColorCuriousBlue = [[UIColor alloc] initWithRgb:0x2A9EDB];
_riotColorIndigo = [[UIColor alloc] initWithRgb:0xBD79CC];
_riotColorOrange = [[UIColor alloc] initWithRgb:0xF8A15F];
2019-01-11 10:57:02 +00:00
// Observe "Invert Colours" settings changes (available since iOS 11)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessibilityInvertColorsStatusDidChange) name:UIAccessibilityInvertColorsStatusDidChangeNotification object:nil];
}
return self;
}
- (void)accessibilityInvertColorsStatusDidChange
{
// Refresh the theme only for "auto"
2019-01-14 09:53:43 +00:00
if ([self.themeId isEqualToString:@"auto"])
{
2019-01-14 09:53:43 +00:00
self.theme = [self themeWithThemeId:self.themeId];
}
}
@end