From 9eeb55b446b0607ab3e0976678cad385b71ad093 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sat, 13 Aug 2022 16:12:45 +0200 Subject: [PATCH] Code style change --- Shared/Settings/MultiselectRow.swift | 47 ++++++++++++++++++++++ Shared/Settings/SponsorBlockSettings.swift | 44 +------------------- Yattee.xcodeproj/project.pbxproj | 7 ++++ iOS/SafeArea.swift | 14 ++++--- 4 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 Shared/Settings/MultiselectRow.swift diff --git a/Shared/Settings/MultiselectRow.swift b/Shared/Settings/MultiselectRow.swift new file mode 100644 index 00000000..59cf1002 --- /dev/null +++ b/Shared/Settings/MultiselectRow.swift @@ -0,0 +1,47 @@ +import SwiftUI + +struct MultiselectRow: View { + let title: String + let selected: Bool + var action: (Bool) -> Void + + @State private var toggleChecked = false + + var body: some View { + Button(action: { action(!selected) }) { + HStack { + #if os(macOS) + Toggle(isOn: $toggleChecked) { + Text(self.title) + Spacer() + } + .onAppear { + toggleChecked = selected + } + .onChange(of: toggleChecked) { new in + action(new) + } + #else + Text(self.title) + Spacer() + if selected { + Image(systemName: "checkmark") + #if os(iOS) + .foregroundColor(.accentColor) + #endif + } + #endif + } + .contentShape(Rectangle()) + } + #if !os(tvOS) + .buttonStyle(.plain) + #endif + } +} + +struct MultiselectRow_Previews: PreviewProvider { + static var previews: some View { + MultiselectRow(title: "Title", selected: false, action: { _ in }) + } +} diff --git a/Shared/Settings/SponsorBlockSettings.swift b/Shared/Settings/SponsorBlockSettings.swift index bff9e665..5c4c20da 100644 --- a/Shared/Settings/SponsorBlockSettings.swift +++ b/Shared/Settings/SponsorBlockSettings.swift @@ -42,7 +42,7 @@ struct SponsorBlockSettings: View { Section(header: SettingsHeader(text: "Categories to Skip"), footer: categoriesDetails) { #if os(macOS) let list = ForEach(SponsorBlockAPI.categories, id: \.self) { category in - SponsorBlockCategorySelectionRow( + MultiselectRow( title: SponsorBlockAPI.categoryDescription(category) ?? "Unknown", selected: sponsorBlockCategories.contains(category) ) { value in @@ -62,7 +62,7 @@ struct SponsorBlockSettings: View { Spacer() #else ForEach(SponsorBlockAPI.categories, id: \.self) { category in - SponsorBlockCategorySelectionRow( + MultiselectRow( title: SponsorBlockAPI.categoryDescription(category) ?? "Unknown", selected: sponsorBlockCategories.contains(category) ) { value in @@ -99,46 +99,6 @@ struct SponsorBlockSettings: View { sponsorBlockCategories.insert(category) } } - - struct SponsorBlockCategorySelectionRow: View { - let title: String - let selected: Bool - var action: (Bool) -> Void - - @State private var toggleChecked = false - - var body: some View { - Button(action: { action(!selected) }) { - HStack { - #if os(macOS) - Toggle(isOn: $toggleChecked) { - Text(self.title) - Spacer() - } - .onAppear { - toggleChecked = selected - } - .onChange(of: toggleChecked) { new in - action(new) - } - #else - Text(self.title) - Spacer() - if selected { - Image(systemName: "checkmark") - #if os(iOS) - .foregroundColor(.accentColor) - #endif - } - #endif - } - .contentShape(Rectangle()) - } - #if !os(tvOS) - .buttonStyle(.plain) - #endif - } - } } struct SponsorBlockSettings_Previews: PreviewProvider { diff --git a/Yattee.xcodeproj/project.pbxproj b/Yattee.xcodeproj/project.pbxproj index de5c223c..07a9fb7b 100644 --- a/Yattee.xcodeproj/project.pbxproj +++ b/Yattee.xcodeproj/project.pbxproj @@ -340,6 +340,9 @@ 375E45F627B1976B00BA7902 /* MPVOGLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375E45F427B1976B00BA7902 /* MPVOGLView.swift */; }; 375E45F827B1AC4700BA7902 /* PlayerControlsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375E45F727B1AC4700BA7902 /* PlayerControlsModel.swift */; }; 375E45F927B1AC4700BA7902 /* PlayerControlsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375E45F727B1AC4700BA7902 /* PlayerControlsModel.swift */; }; + 375EC972289F2ABF00751258 /* MultiselectRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375EC971289F2ABF00751258 /* MultiselectRow.swift */; }; + 375EC973289F2ABF00751258 /* MultiselectRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375EC971289F2ABF00751258 /* MultiselectRow.swift */; }; + 375EC974289F2ABF00751258 /* MultiselectRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375EC971289F2ABF00751258 /* MultiselectRow.swift */; }; 375F7410289DC35A00747050 /* PlayerBackendView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375F740F289DC35A00747050 /* PlayerBackendView.swift */; }; 375F7411289DC35A00747050 /* PlayerBackendView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375F740F289DC35A00747050 /* PlayerBackendView.swift */; }; 375F7412289DC35A00747050 /* PlayerBackendView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375F740F289DC35A00747050 /* PlayerBackendView.swift */; }; @@ -1040,6 +1043,7 @@ 375DFB5726F9DA010013F468 /* InstancesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstancesModel.swift; sourceTree = ""; }; 375E45F427B1976B00BA7902 /* MPVOGLView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPVOGLView.swift; sourceTree = ""; }; 375E45F727B1AC4700BA7902 /* PlayerControlsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerControlsModel.swift; sourceTree = ""; }; + 375EC971289F2ABF00751258 /* MultiselectRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiselectRow.swift; sourceTree = ""; }; 375F740F289DC35A00747050 /* PlayerBackendView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerBackendView.swift; sourceTree = ""; }; 3761ABFC26F0F8DE00AA496F /* EnvironmentValues.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EnvironmentValues.swift; sourceTree = ""; }; 3763495026DFF59D00B9A393 /* AppSidebarRecents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppSidebarRecents.swift; sourceTree = ""; }; @@ -1687,6 +1691,7 @@ 374C053427242D9F009BDDBE /* SponsorBlockSettings.swift */, 376BE50627347B57009AD608 /* SettingsHeader.swift */, 37B044B626F7AB9000E1419D /* SettingsView.swift */, + 375EC971289F2ABF00751258 /* MultiselectRow.swift */, ); path = Settings; sourceTree = ""; @@ -2873,6 +2878,7 @@ 37030FFB27B0398000ECDDAA /* MPVClient.swift in Sources */, 3756C2AA2861151C00E4B059 /* NetworkStateModel.swift in Sources */, 3758638A2721B0A9000CB14E /* ChannelCell.swift in Sources */, + 375EC972289F2ABF00751258 /* MultiselectRow.swift in Sources */, 37001563271B1F250049C794 /* AccountsModel.swift in Sources */, 3795593627B08538007FF8F4 /* StreamControl.swift in Sources */, 37CC3F50270D010D00608308 /* VideoBanner.swift in Sources */, @@ -2970,6 +2976,7 @@ 378AE93C274EDFB2006A4EE1 /* Backport.swift in Sources */, 37152EEB26EFEB95004FB96D /* LazyView.swift in Sources */, 37F4AD2028612DFD004D0F66 /* Buffering.swift in Sources */, + 375EC973289F2ABF00751258 /* MultiselectRow.swift in Sources */, 377FC7E2267A084A00A6BBAF /* VideoCell.swift in Sources */, 37CC3F51270D010D00608308 /* VideoBanner.swift in Sources */, 37F961A027BD90BB00058149 /* PlayerBackendType.swift in Sources */, diff --git a/iOS/SafeArea.swift b/iOS/SafeArea.swift index 98014f67..e04f9fa3 100644 --- a/iOS/SafeArea.swift +++ b/iOS/SafeArea.swift @@ -3,13 +3,15 @@ import UIKit struct SafeArea { static var insets: UIEdgeInsets { - let keyWindow = UIApplication.shared.connectedScenes - .filter { $0.activationState == .foregroundActive } - .compactMap { $0 as? UIWindowScene } - .first? - .windows - .first { $0.isKeyWindow } + let keyWindow = scene?.windows.first { $0.isKeyWindow } return keyWindow?.safeAreaInsets ?? .init() } + + static var scene: UIWindowScene? { + UIApplication.shared.connectedScenes + .filter { $0.activationState == .foregroundActive } + .compactMap { $0 as? UIWindowScene } + .first + } }