2017-03-08 15:14:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2019-01-11 10:45:27 +00:00
|
|
|
Copyright 2019 New Vector Ltd
|
2017-03-08 15:14:41 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-01-11 09:32:56 +00:00
|
|
|
#import "ThemeService.h"
|
2018-07-02 16:01:33 +00:00
|
|
|
|
|
|
|
#ifdef IS_SHARE_EXTENSION
|
|
|
|
#import "RiotShareExtension-Swift.h"
|
|
|
|
#else
|
2018-07-02 12:51:47 +00:00
|
|
|
#import "Riot-Swift.h"
|
2018-07-02 16:01:33 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-11 09:32:56 +00:00
|
|
|
NSString *const kThemeServiceDidChangeThemeNotification = @"kThemeServiceDidChangeThemeNotification";
|
2017-07-21 09:28:17 +00:00
|
|
|
|
2019-01-11 09:32:56 +00:00
|
|
|
@implementation ThemeService
|
2019-01-14 12:57:53 +00:00
|
|
|
@synthesize themeId = _themeId;
|
2017-03-08 15:14:41 +00:00
|
|
|
|
2019-01-11 10:45:27 +00:00
|
|
|
+ (ThemeService *)shared
|
2017-07-21 09:28:17 +00:00
|
|
|
{
|
2019-01-11 09:32:56 +00:00
|
|
|
static ThemeService *sharedOnceInstance;
|
2017-07-21 09:28:17 +00:00
|
|
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2019-01-11 10:45:27 +00:00
|
|
|
sharedOnceInstance = [ThemeService new];
|
2017-07-21 09:28:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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 12:57:53 +00:00
|
|
|
_themeId = theThemeId;
|
2020-06-04 13:16:42 +00:00
|
|
|
[self reEvaluateTheme];
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:58:00 +00:00
|
|
|
- (void)setTheme:(id<Theme> _Nonnull)theme
|
2020-06-04 13:16:42 +00:00
|
|
|
{
|
2020-06-05 07:58:00 +00:00
|
|
|
// update only if really changed
|
|
|
|
if (![_theme.identifier isEqualToString:theme.identifier])
|
2020-06-04 13:16:42 +00:00
|
|
|
{
|
2020-06-05 07:58:00 +00:00
|
|
|
_theme = theme;
|
|
|
|
|
|
|
|
[self updateAppearance];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kThemeServiceDidChangeThemeNotification object:nil];
|
2020-06-04 13:16:42 +00:00
|
|
|
}
|
2019-01-14 09:53:43 +00:00
|
|
|
}
|
2019-01-11 10:45:27 +00:00
|
|
|
|
2020-06-05 07:58:00 +00:00
|
|
|
- (id<Theme>)themeWithThemeId:(NSString*)themeId
|
2019-01-14 09:53:43 +00:00
|
|
|
{
|
2020-06-05 07:58:00 +00:00
|
|
|
id<Theme> theme;
|
2020-07-10 14:59:43 +00:00
|
|
|
|
|
|
|
if (themeId == nil || [themeId isEqualToString:@"auto"])
|
2019-01-14 09:53:43 +00:00
|
|
|
{
|
2020-06-04 12:29:15 +00:00
|
|
|
if (@available(iOS 13, *))
|
|
|
|
{
|
2020-06-04 12:33:02 +00:00
|
|
|
// Translate "auto" into a theme with UITraitCollection
|
2020-06-05 07:58:00 +00:00
|
|
|
themeId = ([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark) ? @"dark" : @"light";
|
2020-06-04 12:29:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Translate "auto" into a theme
|
2020-06-05 07:58:00 +00:00
|
|
|
themeId = UIAccessibilityIsInvertColorsEnabled() ? @"dark" : @"light";
|
2020-06-04 12:29:15 +00:00
|
|
|
}
|
2019-01-14 09:53:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 10:45:27 +00:00
|
|
|
if ([themeId isEqualToString:@"dark"])
|
|
|
|
{
|
2020-06-05 07:58:00 +00:00
|
|
|
theme = [DarkTheme new];
|
2019-01-11 10:45:27 +00:00
|
|
|
}
|
|
|
|
else if ([themeId isEqualToString:@"black"])
|
|
|
|
{
|
2020-06-05 07:58:00 +00:00
|
|
|
theme = [BlackTheme new];
|
2019-01-11 17:29:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-05 07:58:00 +00:00
|
|
|
// Use light theme by default
|
|
|
|
theme = [DefaultTheme new];
|
2019-01-11 10:45:27 +00:00
|
|
|
}
|
2020-06-05 07:58:00 +00:00
|
|
|
|
|
|
|
return theme;
|
2019-01-11 10:45:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 15:41:48 +00:00
|
|
|
- (BOOL)isCurrentThemeDark
|
|
|
|
{
|
|
|
|
if ([self.theme.identifier isEqualToString:@"dark"] || [self.theme.identifier isEqualToString:@"black"])
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2019-01-11 10:45:27 +00:00
|
|
|
#pragma mark - Private methods
|
|
|
|
|
2019-01-11 10:57:02 +00:00
|
|
|
- (instancetype)init
|
2017-03-08 15:14:41 +00:00
|
|
|
{
|
2019-01-11 10:57:02 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
// Riot Colors not yet themeable
|
2020-07-10 13:37:32 +00:00
|
|
|
_riotColorCuriousBlue = [[UIColor alloc] initWithRgb:0x2A9EDB];
|
2019-01-11 10:57:02 +00:00
|
|
|
|
2020-06-04 12:33:02 +00:00
|
|
|
if (@available(iOS 13, *))
|
|
|
|
{
|
|
|
|
// Observe application did become active for iOS appearance setting changes
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Observe "Invert Colours" settings changes (available since iOS 11)
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessibilityInvertColorsStatusDidChange) name:UIAccessibilityInvertColorsStatusDidChangeNotification object:nil];
|
|
|
|
}
|
2020-09-07 07:56:44 +00:00
|
|
|
|
|
|
|
[self reEvaluateTheme];
|
2019-01-11 10:57:02 +00:00
|
|
|
}
|
|
|
|
return self;
|
2017-09-21 13:29:46 +00:00
|
|
|
}
|
2017-07-21 09:28:17 +00:00
|
|
|
|
2020-06-04 13:16:42 +00:00
|
|
|
- (void)reEvaluateTheme
|
2017-09-21 13:29:46 +00:00
|
|
|
{
|
2020-06-05 07:58:00 +00:00
|
|
|
self.theme = [self themeWithThemeId:self.themeId];
|
2017-09-21 13:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:33:02 +00:00
|
|
|
- (void)accessibilityInvertColorsStatusDidChange
|
|
|
|
{
|
2020-06-04 13:16:42 +00:00
|
|
|
[self reEvaluateTheme];
|
2020-06-04 12:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidBecomeActive
|
|
|
|
{
|
2020-06-04 13:16:42 +00:00
|
|
|
[self reEvaluateTheme];
|
2020-06-04 12:33:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 08:39:40 +00:00
|
|
|
- (void)updateAppearance
|
|
|
|
{
|
|
|
|
[UIScrollView appearance].indicatorStyle = self.theme.scrollBarStyle;
|
|
|
|
|
|
|
|
// Define the navigation bar text color
|
|
|
|
[[UINavigationBar appearance] setTintColor:self.theme.tintColor];
|
|
|
|
|
|
|
|
// Define the UISearchBar cancel button color
|
2020-05-28 16:24:03 +00:00
|
|
|
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{ NSForegroundColorAttributeName : self.theme.tintColor } forState: UIControlStateNormal];
|
2021-04-28 23:19:40 +00:00
|
|
|
|
|
|
|
[[UIStackView appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setSpacing:-7];
|
|
|
|
[[UIStackView appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setDistribution:UIStackViewDistributionEqualCentering];
|
2019-02-19 08:39:40 +00:00
|
|
|
}
|
2017-03-08 15:14:41 +00:00
|
|
|
|
|
|
|
@end
|