From 9f6fff82709ec55176138d04d3393cf03d8309a9 Mon Sep 17 00:00:00 2001 From: Arnfried Griesert Date: Fri, 1 Mar 2024 03:27:14 +0100 Subject: [PATCH] Added BWIThemeService helper class --- .../AllChats/AllChatsViewController.swift | 2 +- .../DeveloperSettingsView.swift | 22 +++++---- .../IntroduceFederationScreen1.swift | 10 ++-- .../IntroduceFederationScreen2.swift | 10 ++-- .../IntroduceFederationScreen3.swift | 5 +- .../IntroduceFederationView.swift | 11 +++-- bwi/Tools/BWIThemeService.swift | 48 +++++++++++++++++++ 7 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 bwi/Tools/BWIThemeService.swift diff --git a/Riot/Modules/Home/AllChats/AllChatsViewController.swift b/Riot/Modules/Home/AllChats/AllChatsViewController.swift index 631884469..2c0028ed0 100644 --- a/Riot/Modules/Home/AllChats/AllChatsViewController.swift +++ b/Riot/Modules/Home/AllChats/AllChatsViewController.swift @@ -338,7 +338,7 @@ class AllChatsViewController: HomeViewController { return } - let viewController = UIHostingController(rootView: IntroduceFederationView()) + let viewController = UIHostingController(rootView: IntroduceFederationView().environmentObject(BWIThemeService.shared)) viewController.isModalInPresentation = true present(viewController, animated: true) { notificationService.setShowFederationIntroductionFlag(false) diff --git a/bwi/DeveloperSettings/DeveloperSettingsView.swift b/bwi/DeveloperSettings/DeveloperSettingsView.swift index 7cbcd003e..b5932983b 100644 --- a/bwi/DeveloperSettings/DeveloperSettingsView.swift +++ b/bwi/DeveloperSettings/DeveloperSettingsView.swift @@ -23,7 +23,7 @@ import SwiftUI @available(iOS 14.0, *) class func makeViewController(session: MXSession) -> UIViewController { - return UIHostingController(rootView: DeveloperSettingsView(session: session)) + return UIHostingController(rootView: DeveloperSettingsView(session: session).environmentObject(BWIThemeService.shared)) } } @@ -32,6 +32,7 @@ import SwiftUI struct DeveloperSettingsView: View { let session: MXSession? + @EnvironmentObject var themeService: BWIThemeService @State private var showAlert = false @State private var showAlertBirthdayCampaign = false @State private var permalinkPrefix: String? = UserDefaults.standard.string(forKey: "bwi_permalink_prefix") @@ -42,7 +43,7 @@ struct DeveloperSettingsView: View { SwiftUI.Section { Button(action: { showAlert = createNewPersonalNotesRoom(mxSession: session) }) { Text(BWIL10n.bwiSettingsDeveloperCreateNewPersonalNotesRoom) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } .alert(isPresented: $showAlert) { @@ -51,7 +52,7 @@ struct DeveloperSettingsView: View { Button(action: { showAlert = resetMatomoInfoScreen(mxSession: session) }) { Text(BWIL10n.bwiSettingsDeveloperResetMatomoInfo) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } .alert(isPresented: $showAlert) { @@ -59,7 +60,7 @@ struct DeveloperSettingsView: View { } Button(action: { showAlertBirthdayCampaign = resetBirthdayCampaignScreen(mxSession: session) }) { Text(BWIL10n.bwiSettingsDeveloperSettingsResetBirthdayBanner) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } .alert(isPresented: $showAlertBirthdayCampaign) { @@ -67,34 +68,35 @@ struct DeveloperSettingsView: View { } Button(action: { _ = restrictUser(mxSession: session) }) { Text(BWIL10n.bwiSettingsDeveloperRestrictUser) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } Button(action: { _ = unrestrictUser(mxSession: session) }) { Text(BWIL10n.bwiSettingsDeveloperUnrestrictUser) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } Button(action: { _ = unmarkBannerVersion(mxSession: session) }) { Text(BWIL10n.bwiSettingsDeveloperUnmarkBanner) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } } SwiftUI.Section(header: Text(BWIL10n.bwiSettingsDeveloperIntroduceFederation)) { Button(action: { showIntroduceFederation = true }) { Text(BWIL10n.bwiSettingsDeveloperIntroduceFederationPreview) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } .sheet(isPresented: $showIntroduceFederation) { IntroduceFederationView() .interactiveDismissDisabled() + .environmentObject(BWIThemeService.shared) } if let session = session { Button(action: { BWIAccountNotificationService(mxSession: session).setShowFederationIntroductionFlag(true) }) { Text(BWIL10n.bwiSettingsDeveloperIntroduceFederationReset) - .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .foregroundColor(Color(themeService.theme.tintColor)) .font(.system(size: 17)) } } @@ -172,7 +174,7 @@ fileprivate func resetMatomoInfoScreen(mxSession: MXSession?) -> Bool { guard let mxSession = mxSession else { return false } - BWIAnalyticsAccountDataService(mxSession: mxSession) + _ = BWIAnalyticsAccountDataService(mxSession: mxSession) return true } diff --git a/bwi/IntroduceFederation/IntroduceFederationScreen1.swift b/bwi/IntroduceFederation/IntroduceFederationScreen1.swift index 3630ce876..103bf36ff 100644 --- a/bwi/IntroduceFederation/IntroduceFederationScreen1.swift +++ b/bwi/IntroduceFederation/IntroduceFederationScreen1.swift @@ -18,9 +18,11 @@ import SwiftUI struct IntroduceFederationScreen1: View { + @EnvironmentObject var themeService: BWIThemeService + var body: some View { ZStack(alignment: .top) { - Color(ThemeService.shared().theme.backgroundColor) + Color(themeService.theme.backgroundColor) VStack(spacing: 26) { ZStack { @@ -36,18 +38,18 @@ struct IntroduceFederationScreen1: View { .background(Color(hex: 0xE3E8F0)) .clipShape(Circle()) .padding(5) // outer border size - .background(Color(ThemeService.shared().theme.backgroundColor)) + .background(Color(themeService.theme.backgroundColor)) .clipShape(Circle()) .frame(width: IntroduceFederationView.imageSize + 12, height: IntroduceFederationView.imageSize + 12) .padding(.trailing, 50) } .padding(.bottom, 42) Text(BWIL10n.introduceFederationScreen1Title) - .foregroundColor(Color(ThemeService.shared().theme.textPrimaryColor)) + .foregroundColor(Color(themeService.theme.textPrimaryColor)) .font(.system(size: 28, weight: .semibold)) .multilineTextAlignment(.center) Text(BWIL10n.introduceFederationScreen1Description) - .foregroundColor(Color(ThemeService.shared().theme.textPrimaryColor)) + .foregroundColor(Color(themeService.theme.textPrimaryColor)) .font(.system(size: 17)) .multilineTextAlignment(.center) Spacer() diff --git a/bwi/IntroduceFederation/IntroduceFederationScreen2.swift b/bwi/IntroduceFederation/IntroduceFederationScreen2.swift index 8594f79a0..1243d5f99 100644 --- a/bwi/IntroduceFederation/IntroduceFederationScreen2.swift +++ b/bwi/IntroduceFederation/IntroduceFederationScreen2.swift @@ -18,22 +18,24 @@ import SwiftUI struct IntroduceFederationScreen2: View { + @EnvironmentObject var themeService: BWIThemeService + var body: some View { ZStack(alignment: .top) { - Color(ThemeService.shared().theme.backgroundColor) + Color(themeService.theme.backgroundColor) VStack(spacing: 26) { - Image(uiImage: getImage(darkmode: ThemeService.shared().isCurrentThemeDark())) + Image(uiImage: getImage(darkmode: themeService.isCurrentThemeDark)) .resizable() .aspectRatio(contentMode: .fit) .frame(height: IntroduceFederationView.imageSize + 12) .padding(.bottom, 42) Text(BWIL10n.introduceFederationScreen2Title) - .foregroundColor(Color(ThemeService.shared().theme.textPrimaryColor)) + .foregroundColor(Color(themeService.theme.textPrimaryColor)) .font(.system(size: 28, weight: .semibold)) .multilineTextAlignment(.center) Text(BWIL10n.introduceFederationScreen2Description) - .foregroundColor(Color(ThemeService.shared().theme.textPrimaryColor)) + .foregroundColor(Color(themeService.theme.textPrimaryColor)) .font(.system(size: 17)) .multilineTextAlignment(.center) Spacer() diff --git a/bwi/IntroduceFederation/IntroduceFederationScreen3.swift b/bwi/IntroduceFederation/IntroduceFederationScreen3.swift index 003ed6e85..dbd20f05f 100644 --- a/bwi/IntroduceFederation/IntroduceFederationScreen3.swift +++ b/bwi/IntroduceFederation/IntroduceFederationScreen3.swift @@ -18,6 +18,7 @@ import SwiftUI struct IntroduceFederationScreen3: View { + @EnvironmentObject var themeService: BWIThemeService @Environment(\.dismiss) var dismiss var body: some View { @@ -28,11 +29,11 @@ struct IntroduceFederationScreen3: View { .frame(height: IntroduceFederationView.topConstraintTitle) .padding(.bottom, 42) Text(BWIL10n.introduceFederationScreen3Title) - .foregroundColor(Color(ThemeService.shared().theme.textPrimaryColor)) + .foregroundColor(Color(themeService.theme.textPrimaryColor)) .font(.system(size: 28, weight: .semibold)) .multilineTextAlignment(.center) Text(BWIL10n.introduceFederationScreen3Description) - .foregroundColor(Color(ThemeService.shared().theme.textPrimaryColor)) + .foregroundColor(Color(themeService.theme.textPrimaryColor)) .font(.system(size: 17)) .multilineTextAlignment(.center) Spacer() diff --git a/bwi/IntroduceFederation/IntroduceFederationView.swift b/bwi/IntroduceFederation/IntroduceFederationView.swift index af56cee54..3e716006b 100644 --- a/bwi/IntroduceFederation/IntroduceFederationView.swift +++ b/bwi/IntroduceFederation/IntroduceFederationView.swift @@ -26,6 +26,7 @@ enum VisibleScreen { struct IntroduceFederationView: View { static let imageSize: CGFloat = 108 static let topConstraintTitle: CGFloat = 390 + @EnvironmentObject var themeService: BWIThemeService @State var visibleScreen = VisibleScreen.screen1 var body: some View { @@ -36,7 +37,7 @@ struct IntroduceFederationView: View { } .tabViewStyle(.page(indexDisplayMode: .always)) .background { - Color(ThemeService.shared().theme.backgroundColor) + Color(themeService.theme.backgroundColor) .edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/) } .overlay(alignment: .bottom) { @@ -44,7 +45,7 @@ struct IntroduceFederationView: View { if visibleScreen != .screen1 { Button(action: presentPreviousScreen) { Image(systemName: "arrow.left") - .foregroundColor(Color(ThemeService.shared().theme.textSecondaryColor)) + .foregroundColor(Color(themeService.theme.textSecondaryColor)) .padding() } } @@ -52,7 +53,7 @@ struct IntroduceFederationView: View { if visibleScreen != .screen3 { Button(action: presentNextScreen) { Image(systemName: "arrow.right") - .foregroundColor(Color(ThemeService.shared().theme.textSecondaryColor)) + .foregroundColor(Color(themeService.theme.textSecondaryColor)) .padding() } } @@ -65,8 +66,8 @@ struct IntroduceFederationView: View { } private func setupIndicatorColors() { - UIPageControl.appearance().currentPageIndicatorTintColor = ThemeService.shared().theme.textPrimaryColor - UIPageControl.appearance().pageIndicatorTintColor = ThemeService.shared().theme.textSecondaryColor + UIPageControl.appearance().currentPageIndicatorTintColor = themeService.theme.textPrimaryColor + UIPageControl.appearance().pageIndicatorTintColor = themeService.theme.textSecondaryColor } private func presentNextScreen() { diff --git a/bwi/Tools/BWIThemeService.swift b/bwi/Tools/BWIThemeService.swift new file mode 100644 index 000000000..f83199099 --- /dev/null +++ b/bwi/Tools/BWIThemeService.swift @@ -0,0 +1,48 @@ +// +/* + * Copyright (c) 2022 BWI GmbH + * + * 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 Foundation +import Combine + +class BWIThemeService: ObservableObject { + static let shared = BWIThemeService() + @Published var themeId: String? + @Published var theme: Theme + @Published var isCurrentThemeDark: Bool + var cancellable: Cancellable? + + private init() { + let service = ThemeService.shared() + themeId = service.themeId + theme = service.theme + isCurrentThemeDark = service.isCurrentThemeDark() + + cancellable = NotificationCenter.default + .publisher(for: .themeServiceDidChangeTheme) + .receive(on: DispatchQueue.main) + .sink() { _ in + self.themeDidChange() + } + } + + private func themeDidChange() { + let service = ThemeService.shared() + themeId = service.themeId + theme = service.theme + isCurrentThemeDark = service.isCurrentThemeDark() + } +}