Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update app's theme when system theme is updated #3211

Closed
wants to merge 14 commits into from
5 changes: 5 additions & 0 deletions WMF Framework/BatchEditToolbarViewController.swift
Expand Up @@ -33,6 +33,11 @@ public final class BatchEditToolbarViewController: UIViewController {
}
apply(theme: theme)
}

override public func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}
}

extension BatchEditToolbarViewController: Themeable {
Expand Down
31 changes: 31 additions & 0 deletions WMF Framework/Theme.swift
Expand Up @@ -454,3 +454,34 @@ public protocol Themeable : class {
@objc(applyTheme:)
func apply(theme: Theme) //this might be better as a var theme: Theme { get set } - common VC superclasses could check for viewIfLoaded and call an update method in the setter. This would elminate the need for the viewIfLoaded logic in every applyTheme:
}

@objc public extension Theme {
@objc static let didChangeNotification = NSNotification.Name(rawValue: "WMFThemeDidChangeNotification")
}

private extension Themeable where Self: UITraitEnvironment {
@available(iOS 12.0, *)
func adjustTheme(_ previousTraitCollection: UITraitCollection?) {
let newUserInterfaceStyle = traitCollection.userInterfaceStyle
guard previousTraitCollection?.userInterfaceStyle != newUserInterfaceStyle else {
return
}
let newTheme: Theme = newUserInterfaceStyle == .dark ? .black : .standard
guard newTheme != UserDefaults.wmf.wmf_appTheme else {
return
}
NotificationCenter.default.post(name: Theme.didChangeNotification, object: newTheme)
}
}

@objc public extension UIViewController {
func adjustTheme(_ previousTraitCollection: UITraitCollection?) {
guard
#available(iOS 12.0, *),
let self = self as? (Themeable & UITraitEnvironment)
else {
return
}
self.adjustTheme(previousTraitCollection)
}
}
2 changes: 1 addition & 1 deletion WMF Framework/ThemeableTextField.swift
Expand Up @@ -85,7 +85,7 @@ open class ThemeableTextField: UITextField, Themeable {
layer.shadowOpacity = 0.0
layer.shadowRadius = 0.0
}

reloadInputViews()
}
}

Expand Down
5 changes: 5 additions & 0 deletions WMF Framework/WMFCaptchaViewController.swift
Expand Up @@ -162,6 +162,11 @@ class WMFCaptchaViewController: UIViewController, UITextFieldDelegate, Themeable

apply(theme: theme)
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

@objc func requestAnAccountTapped(_ recognizer: UITapGestureRecognizer) {
wmf_openExternalUrl(URL.init(string: "https://en.wikipedia.org/wiki/Wikipedia:Request_an_account"))
Expand Down
7 changes: 7 additions & 0 deletions Wikipedia/Code/AboutViewController.m
Expand Up @@ -147,6 +147,11 @@ - (void)viewDidLoad {
[self updateNavigationBar];
}

- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self adjustTheme:previousTraitCollection];
}

- (void)closeButtonPressed {
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
Expand Down Expand Up @@ -348,6 +353,8 @@ - (void)applyTheme:(WMFTheme *)theme {
return;
}
self.view.backgroundColor = theme.colors.paperBackground;
[self.webView wmf_setTextFontColor:self.theme];
[self.webView wmf_setLogoStyleWithTheme:self.theme];
}

@end
13 changes: 8 additions & 5 deletions Wikipedia/Code/AccountViewController.swift
Expand Up @@ -70,10 +70,14 @@ class AccountViewController: SubSettingsViewController {
let item = sections[safeIndex: indexPath.section]?.items[safeIndex: indexPath.row] else {
return UITableViewCell()
}


cell.apply(theme)

if theme.colors.icon == nil {
cell.iconColor = item.iconColor
cell.iconBackgroundColor = item.iconBackgroundColor
}
cell.iconName = item.iconName
cell.iconColor = item.iconColor
cell.iconBackgroundColor = item.iconBackgroundColor
cell.title = item.title

switch item.type {
Expand All @@ -92,8 +96,6 @@ class AccountViewController: SubSettingsViewController {
cell.disclosureSwitch.addTarget(self, action: #selector(autoSignTalkPageDiscussions(_:)), for: .valueChanged)
}

cell.apply(theme)

return cell
}

Expand Down Expand Up @@ -165,5 +167,6 @@ class AccountViewController: SubSettingsViewController {

view.backgroundColor = theme.colors.paperBackground
tableView.backgroundColor = theme.colors.baseBackground
tableView.reloadData()
}
}
Expand Up @@ -104,6 +104,7 @@ final class InsertMediaAdvancedSettingsViewController: ViewController {
view.backgroundColor = theme.colors.paperBackground
tableView.backgroundColor = view.backgroundColor
tableView.separatorColor = theme.colors.border
tableView.reloadData()
}
}

Expand Down
Expand Up @@ -102,6 +102,7 @@ final class InsertMediaImageSizeSettingsViewController: ViewController {
view.backgroundColor = theme.colors.paperBackground
tableView.backgroundColor = view.backgroundColor
tableView.separatorColor = theme.colors.border
tableView.reloadData()
}
}

Expand Down
Expand Up @@ -67,6 +67,7 @@ final class InsertMediaImageTypeSettingsViewController: ViewController {
let selectedBackgroundView = UIView()
selectedBackgroundView.backgroundColor = theme.colors.midBackground
cell.selectedBackgroundView = selectedBackgroundView
tableView.reloadData()
}

// MARK: - Themeable
Expand Down
3 changes: 1 addition & 2 deletions Wikipedia/Code/AppearanceSettingsViewController.swift
Expand Up @@ -189,8 +189,7 @@ final class AppearanceSettingsViewController: SubSettingsViewController {
}

func userDidSelect(theme: Theme) {
let userInfo = ["theme": theme]
NotificationCenter.default.post(name: Notification.Name(ReadingThemesControlsViewController.WMFUserDidSelectThemeNotification), object: nil, userInfo: userInfo)
NotificationCenter.default.post(name: Theme.didChangeNotification, object: theme)
}

@objc public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Expand Down
6 changes: 6 additions & 0 deletions Wikipedia/Code/BaseExploreFeedSettingsViewController.swift
Expand Up @@ -166,6 +166,11 @@ class BaseExploreFeedSettingsViewController: SubSettingsViewController {
NotificationCenter.default.addObserver(self, selector: #selector(newExploreFeedPreferencesWereRejected(_:)), name: NSNotification.Name.WMFNewExploreFeedPreferencesWereRejected, object: nil)
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

var preferredLanguages: [MWKLanguageLink] {
return MWKLanguageLinkController.sharedInstance().preferredLanguages
}
Expand Down Expand Up @@ -252,6 +257,7 @@ class BaseExploreFeedSettingsViewController: SubSettingsViewController {
return
}
tableView.backgroundColor = theme.colors.baseBackground
tableView.reloadData()
}

}
Expand Down
4 changes: 2 additions & 2 deletions Wikipedia/Code/CreateReadingListViewController.swift
Expand Up @@ -36,7 +36,7 @@ class CreateReadingListViewController: WMFScrollViewController, UITextFieldDeleg

createReadingListButton.isEnabled = false
}

override func viewWillDisappear(_ animated: Bool) {
view.endEditing(false)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ extension CreateReadingListViewController: Themeable {

view.backgroundColor = theme.colors.paperBackground
view.tintColor = theme.colors.link

readingListNameTextField.apply(theme: theme)
descriptionTextField.apply(theme: theme)

Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/DescriptionEditViewController.swift
Expand Up @@ -265,6 +265,7 @@ import UIKit
warningCharacterCountLabel.textColor = theme.colors.descriptionWarning
publishDescriptionButton.apply(theme: theme)
descriptionTextView.keyboardAppearance = theme.keyboardAppearance
descriptionTextView.reloadInputViews()
}
}

Expand Down
Expand Up @@ -31,6 +31,11 @@ class DescriptionWelcomeContainerViewController: UIViewController, Themeable {
hasAlreadyFadedInAndUp = true
}
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

private func shouldFadeInAndUp() -> Bool {
switch pageType {
Expand Down
5 changes: 5 additions & 0 deletions Wikipedia/Code/DescriptionWelcomeContentsViewController.swift
Expand Up @@ -19,6 +19,11 @@ class DescriptionWelcomeContentsViewController: UIViewController, Themeable {
apply(theme: theme)
view.wmf_configureSubviewsForDynamicType()
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

private func updateUIStrings(){
switch pageType {
Expand Down
5 changes: 5 additions & 0 deletions Wikipedia/Code/DescriptionWelcomeInitialViewController.swift
Expand Up @@ -26,6 +26,11 @@ class DescriptionWelcomeInitialViewController: UIViewController, Themeable {
super.viewDidLoad()
apply(theme: theme)
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

override var prefersStatusBarHidden: Bool {
return false
Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/DescriptionWelcomePageViewController.swift
Expand Up @@ -127,6 +127,7 @@ class DescriptionWelcomePageViewController: UIPageViewController, UIPageViewCont

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
skipButton.titleLabel?.font = UIFont.wmf_font(.semiboldFootnote, compatibleWithTraitCollection: traitCollection)
nextButton.titleLabel?.font = UIFont.wmf_font(.semiboldFootnote, compatibleWithTraitCollection: traitCollection)
}
Expand Down
5 changes: 5 additions & 0 deletions Wikipedia/Code/DescriptionWelcomePanelViewController.swift
Expand Up @@ -35,6 +35,11 @@ class DescriptionWelcomePanelViewController: UIViewController, Themeable {

view.wmf_configureSubviewsForDynamicType()
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

private func embedContainerControllerView() {
let containerController = DescriptionWelcomeContentsViewController.wmf_viewControllerFromDescriptionWelcomeStoryboard()
Expand Down
4 changes: 2 additions & 2 deletions Wikipedia/Code/ExploreFeedSettingsViewController.swift
Expand Up @@ -175,10 +175,10 @@ class ExploreFeedSettingsViewController: BaseExploreFeedSettingsViewController {
}

override func viewDidLoad() {
super.viewDidLoad()
title = CommonStrings.exploreFeedTitle
assert(!preferredLanguages.isEmpty)
displayType = preferredLanguages.count == 1 ? .singleLanguage : .multipleLanguages
super.viewDidLoad()
title = CommonStrings.exploreFeedTitle
}

@objc private func closeButtonPressed() {
Expand Down
Expand Up @@ -141,6 +141,7 @@ class InsertMediaSearchResultsCollectionViewController: UICollectionViewControll
}
view.backgroundColor = theme.colors.paperBackground
collectionView.backgroundColor = theme.colors.paperBackground
collectionView.reloadData()
}

// MARK: - Empty State
Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/InsertMediaSearchViewController.swift
Expand Up @@ -138,5 +138,6 @@ extension InsertMediaSearchViewController: Themeable {
view.backgroundColor = theme.colors.paperBackground
searchBar.apply(theme: theme)
searchBar.tintColor = theme.colors.link
searchBar.reloadInputViews()
}
}
1 change: 1 addition & 0 deletions Wikipedia/Code/InsertMediaSelectedImageView.swift
Expand Up @@ -61,5 +61,6 @@ extension InsertMediaSelectedImageView: Themeable {
backgroundColor = theme.colors.baseBackground
imageInfoContainerView.backgroundColor = backgroundColor
imageView.backgroundColor = .clear
imageInfoView.apply(theme: theme)
}
}
2 changes: 2 additions & 0 deletions Wikipedia/Code/InsertMediaSelectedImageViewController.swift
Expand Up @@ -92,5 +92,7 @@ extension InsertMediaSelectedImageViewController: Themeable {
}
view.backgroundColor = theme.colors.baseBackground
activityIndicator.style = theme.isDark ? .white : .gray
wmf_applyTheme(toEmptyView: theme)
selectedView.apply(theme: theme)
}
}
2 changes: 2 additions & 0 deletions Wikipedia/Code/InsertMediaSettingsViewController.swift
Expand Up @@ -247,6 +247,8 @@ final class InsertMediaSettingsViewController: ViewController {
tableView.backgroundColor = view.backgroundColor
imageView.apply(theme: theme)
buttonView.apply(theme: theme)
tableView.reloadData()
tableView.reloadInputViews()
}
}

Expand Down
5 changes: 5 additions & 0 deletions Wikipedia/Code/LibrariesUsed.swift
Expand Up @@ -72,6 +72,11 @@ class LibrariesUsedViewController: UIViewController, UITableViewDelegate, UITabl
}
libraries = librariesUsed(from: plistPath)
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

private func librariesUsed(from plistPath: String) -> [LibraryUsed] {
guard
Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/NewsViewController.swift
Expand Up @@ -75,6 +75,7 @@ class NewsViewController: ColumnarCollectionViewController, DetailPresentingFrom
}
view.backgroundColor = theme.colors.paperBackground
collectionView.backgroundColor = theme.colors.paperBackground
collectionView.reloadData()
}

// MARK: - UIViewControllerPreviewingDelegate
Expand Down
Expand Up @@ -44,6 +44,7 @@ class ReadingListDetailUnderBarViewController: UIViewController {

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
articleCountLabel.font = UIFont.wmf_font(.semiboldFootnote, compatibleWithTraitCollection: traitCollection)
titleTextField.font = UIFont.wmf_font(.boldTitle1, compatibleWithTraitCollection: traitCollection)
descriptionTextField.font = UIFont.wmf_font(.footnote, compatibleWithTraitCollection: traitCollection)
Expand Down
5 changes: 1 addition & 4 deletions Wikipedia/Code/ReadingThemesControlsViewController.swift
Expand Up @@ -9,8 +9,6 @@ protocol WMFReadingThemesControlsViewControllerDelegate: class {
@objc(WMFReadingThemesControlsViewController)
class ReadingThemesControlsViewController: UIViewController {

@objc static let WMFUserDidSelectThemeNotification = "WMFUserDidSelectThemeNotification"
@objc static let WMFUserDidSelectThemeNotificationThemeKey = "theme"
@objc static let nibName = "ReadingThemesControlsViewController"

var theme = Theme.standard
Expand Down Expand Up @@ -195,8 +193,7 @@ class ReadingThemesControlsViewController: UIViewController {
}

func userDidSelect(theme: Theme) {
let userInfo = ["theme": theme]
NotificationCenter.default.post(name: Notification.Name(ReadingThemesControlsViewController.WMFUserDidSelectThemeNotification), object: nil, userInfo: userInfo)
NotificationCenter.default.post(name: Theme.didChangeNotification, object: theme)
}

@IBAction func sepiaThemeButtonPressed(_ sender: Any) {
Expand Down
5 changes: 5 additions & 0 deletions Wikipedia/Code/SavedProgressViewController.swift
Expand Up @@ -67,6 +67,11 @@ class SavedProgressViewController: UIViewController, Themeable {
self?.view.isHidden = isHidden
})
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

func apply(theme: Theme) {
self.theme = theme
Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/SearchBarExtendedViewController.swift
Expand Up @@ -61,6 +61,7 @@ class SearchBarExtendedViewController: UIViewController {

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
if let textStyle = dataSource?.textStyle(for: button) {
button.titleLabel?.font = UIFont.wmf_font(textStyle, compatibleWithTraitCollection: traitCollection)
}
Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/SearchSettingsViewController.swift
Expand Up @@ -48,6 +48,7 @@ final class SearchSettingsViewController: SubSettingsViewController {
}
view.backgroundColor = theme.colors.baseBackground
tableView.backgroundColor = theme.colors.baseBackground
tableView.reloadData()
}
}

Expand Down
5 changes: 5 additions & 0 deletions Wikipedia/Code/ShareViewController.swift
Expand Up @@ -120,6 +120,11 @@ class ShareViewController: UIViewController, Themeable {
})
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}

@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
Expand Down