From 50e149199075c82239f400b076de9464ec44ed50 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Tue, 23 May 2023 18:54:53 +0200 Subject: [PATCH] Refactor hide shorts --- Shared/Channels/ChannelPlaylistView.swift | 5 ++--- Shared/Channels/ChannelVideosView.swift | 6 ++---- Shared/EnvironmentValues.swift | 9 --------- Shared/Playlists/PlaylistsView.swift | 8 +++----- Shared/Search/SearchView.swift | 8 +++----- Shared/Subscriptions/FeedView.swift | 3 +-- Shared/Subscriptions/SubscriptionsView.swift | 7 ++----- Shared/Trending/TrendingView.swift | 8 +++----- Shared/Views/ContentItemView.swift | 2 +- Shared/Views/HideShortsButtons.swift | 9 +++++---- Shared/Views/PopularView.swift | 7 +++---- 11 files changed, 25 insertions(+), 47 deletions(-) diff --git a/Shared/Channels/ChannelPlaylistView.swift b/Shared/Channels/ChannelPlaylistView.swift index f1cd7f6c..2c989e7c 100644 --- a/Shared/Channels/ChannelPlaylistView.swift +++ b/Shared/Channels/ChannelPlaylistView.swift @@ -10,7 +10,6 @@ struct ChannelPlaylistView: View { @Environment(\.colorScheme) private var colorScheme @Default(.channelPlaylistListingStyle) private var channelPlaylistListingStyle - @Default(.hideShorts) private var hideShorts @ObservedObject private var accounts = AccountsModel.shared var player = PlayerModel.shared @@ -103,7 +102,7 @@ struct ChannelPlaylistView: View { HStack { ListingStyleButtons(listingStyle: $channelPlaylistListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() ShareButton(contentItem: contentItem) favoriteButton @@ -133,7 +132,7 @@ struct ChannelPlaylistView: View { Section { HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } Section { diff --git a/Shared/Channels/ChannelVideosView.swift b/Shared/Channels/ChannelVideosView.swift index fa2d1db1..fca43ac9 100644 --- a/Shared/Channels/ChannelVideosView.swift +++ b/Shared/Channels/ChannelVideosView.swift @@ -30,7 +30,6 @@ struct ChannelVideosView: View { @Default(.channelPlaylistListingStyle) private var channelPlaylistListingStyle @Default(.expandChannelDescription) private var expandChannelDescription - @Default(.hideShorts) private var hideShorts var presentedChannel: Channel? { store.item?.channel ?? channel ?? recents.presentedChannel @@ -98,7 +97,6 @@ struct ChannelVideosView: View { .environment(\.loadMoreContentHandler) { loadNextPage() } .environment(\.inChannelView, true) .environment(\.listingStyle, channelPlaylistListingStyle) - .environment(\.hideShorts, hideShorts) #if os(tvOS) .prefersDefaultFocus(in: focusNamespace) #endif @@ -138,7 +136,7 @@ struct ChannelVideosView: View { ListingStyleButtons(listingStyle: $channelPlaylistListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() contentTypePicker } @@ -283,7 +281,7 @@ struct ChannelVideosView: View { Section { HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } } } label: { diff --git a/Shared/EnvironmentValues.swift b/Shared/EnvironmentValues.swift index 04d231db..7509ef07 100644 --- a/Shared/EnvironmentValues.swift +++ b/Shared/EnvironmentValues.swift @@ -66,10 +66,6 @@ private struct ScrollViewBottomPaddingKey: EnvironmentKey { static let defaultValue: Double = 30 } -private struct HideShortsKey: EnvironmentKey { - static let defaultValue = false -} - extension EnvironmentValues { var inChannelView: Bool { get { self[InChannelViewKey.self] } @@ -125,9 +121,4 @@ extension EnvironmentValues { get { self[NoListingDividersKey.self] } set { self[NoListingDividersKey.self] = newValue } } - - var hideShorts: Bool { - get { self[HideShortsKey.self] } - set { self[HideShortsKey.self] = newValue } - } } diff --git a/Shared/Playlists/PlaylistsView.swift b/Shared/Playlists/PlaylistsView.swift index beaa66db..d71e484d 100644 --- a/Shared/Playlists/PlaylistsView.swift +++ b/Shared/Playlists/PlaylistsView.swift @@ -24,7 +24,6 @@ struct PlaylistsView: View { @Default(.playlistListingStyle) private var playlistListingStyle @Default(.showCacheStatus) private var showCacheStatus - @Default(.hideShorts) private var hideShorts var items: [ContentItem] { var videos = currentPlaylist?.videos ?? [] @@ -68,7 +67,6 @@ struct PlaylistsView: View { .environment(\.scrollViewBottomPadding, 70) .environment(\.currentPlaylistID, currentPlaylist?.id) .environment(\.listingStyle, playlistListingStyle) - .environment(\.hideShorts, hideShorts) if currentPlaylist != nil, items.isEmpty { hintText("Playlist is empty\n\nTap and hold on a video and then \n\"Add to Playlist\"".localized()) @@ -149,7 +147,7 @@ struct PlaylistsView: View { HideWatchedButtons() } ToolbarItem { - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } } #else @@ -220,7 +218,7 @@ struct PlaylistsView: View { Section { HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } Section { @@ -391,7 +389,7 @@ struct PlaylistsView: View { ListingStyleButtons(listingStyle: $playlistListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() #else Spacer() #endif diff --git a/Shared/Search/SearchView.swift b/Shared/Search/SearchView.swift index 24a96f7e..87d89cdf 100644 --- a/Shared/Search/SearchView.swift +++ b/Shared/Search/SearchView.swift @@ -30,7 +30,6 @@ struct SearchView: View { @Default(.saveRecents) private var saveRecents @Default(.showHome) private var showHome @Default(.searchListingStyle) private var searchListingStyle - @Default(.hideShorts) private var hideShorts private var videos = [Video]() @@ -71,13 +70,12 @@ struct SearchView: View { #endif } .environment(\.listingStyle, searchListingStyle) - .environment(\.hideShorts, hideShorts) .toolbar { #if os(macOS) ToolbarItemGroup(placement: toolbarPlacement) { ListingStyleButtons(listingStyle: $searchListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() FavoriteButton(item: favoriteItem) .id(favoriteItem?.id) @@ -216,7 +214,7 @@ struct SearchView: View { Section { HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } Section { @@ -574,7 +572,7 @@ struct SearchView: View { Spacer() ListingStyleButtons(listingStyle: $searchListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } .labelStyle(.iconOnly) .padding(.leading, 30) diff --git a/Shared/Subscriptions/FeedView.swift b/Shared/Subscriptions/FeedView.swift index 86da7a5f..cb21a124 100644 --- a/Shared/Subscriptions/FeedView.swift +++ b/Shared/Subscriptions/FeedView.swift @@ -9,7 +9,6 @@ struct FeedView: View { #if os(tvOS) @Default(.subscriptionsListingStyle) private var subscriptionsListingStyle - @Default(.hideShorts) private var hideShorts #endif var videos: [ContentItem] { @@ -55,7 +54,7 @@ struct FeedView: View { SubscriptionsPageButton() ListingStyleButtons(listingStyle: $subscriptionsListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() #endif if showCacheStatus { diff --git a/Shared/Subscriptions/SubscriptionsView.swift b/Shared/Subscriptions/SubscriptionsView.swift index 110b8f68..68123910 100644 --- a/Shared/Subscriptions/SubscriptionsView.swift +++ b/Shared/Subscriptions/SubscriptionsView.swift @@ -10,7 +10,6 @@ struct SubscriptionsView: View { @Default(.subscriptionsViewPage) private var subscriptionsViewPage @Default(.subscriptionsListingStyle) private var subscriptionsListingStyle - @Default(.hideShorts) private var hideShorts @ObservedObject private var feed = FeedModel.shared @ObservedObject private var subscriptions = SubscribedChannelsModel.shared @@ -28,8 +27,6 @@ struct SubscriptionsView: View { } } .environment(\.listingStyle, subscriptionsListingStyle) - .environment(\.hideShorts, hideShorts) - #if os(iOS) .navigationBarTitleDisplayMode(.inline) .toolbar { @@ -47,7 +44,7 @@ struct SubscriptionsView: View { ToolbarItemGroup { ListingStyleButtons(listingStyle: $subscriptionsListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() toggleWatchedButton .id(feed.watchedId) playUnwatchedButton @@ -75,7 +72,7 @@ struct SubscriptionsView: View { Section { HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } playUnwatchedButton diff --git a/Shared/Trending/TrendingView.swift b/Shared/Trending/TrendingView.swift index 9244d8fb..73d15135 100644 --- a/Shared/Trending/TrendingView.swift +++ b/Shared/Trending/TrendingView.swift @@ -10,7 +10,6 @@ struct TrendingView: View { @Default(.trendingCountry) private var country @Default(.trendingListingStyle) private var trendingListingStyle - @Default(.hideShorts) private var hideShorts @State private var presentingCountrySelection = false @@ -41,7 +40,6 @@ struct TrendingView: View { Section { VerticalCells(items: trending) { if shouldDisplayHeader { header } } .environment(\.listingStyle, trendingListingStyle) - .environment(\.hideShorts, hideShorts) } .toolbar { @@ -128,7 +126,7 @@ struct TrendingView: View { } ToolbarItem { - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } } #else @@ -179,7 +177,7 @@ struct TrendingView: View { Section { HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } Section { @@ -253,7 +251,7 @@ struct TrendingView: View { Spacer() ListingStyleButtons(listingStyle: $trendingListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() Button { resource.load() diff --git a/Shared/Views/ContentItemView.swift b/Shared/Views/ContentItemView.swift index a6fa9c77..de86498b 100644 --- a/Shared/Views/ContentItemView.swift +++ b/Shared/Views/ContentItemView.swift @@ -6,7 +6,7 @@ struct ContentItemView: View { let item: ContentItem @Environment(\.listingStyle) private var listingStyle @Environment(\.noListingDividers) private var noListingDividers - @Environment(\.hideShorts) private var hideShorts + @Default(.hideShorts) private var hideShorts @Default(.hideWatched) private var hideWatched @FetchRequest private var watchRequest: FetchedResults diff --git a/Shared/Views/HideShortsButtons.swift b/Shared/Views/HideShortsButtons.swift index b24ba98b..b68281af 100644 --- a/Shared/Views/HideShortsButtons.swift +++ b/Shared/Views/HideShortsButtons.swift @@ -1,14 +1,15 @@ +import Defaults import SwiftUI struct HideShortsButtons: View { - @Binding var hide: Bool + @Default(.hideShorts) private var hideShorts var body: some View { Button { - hide.toggle() + hideShorts.toggle() } label: { Group { - if hide { + if hideShorts { Label("Short videos: hidden", systemImage: "bolt.slash.fill") .help("Short videos: hidden") } else { @@ -27,7 +28,7 @@ struct HideShortsButtons: View { struct HideShortsButtons_Previews: PreviewProvider { static var previews: some View { VStack { - HideShortsButtons(hide: .constant(true)) + HideShortsButtons() } } } diff --git a/Shared/Views/PopularView.swift b/Shared/Views/PopularView.swift index 6540a232..8ca31a6b 100644 --- a/Shared/Views/PopularView.swift +++ b/Shared/Views/PopularView.swift @@ -10,7 +10,6 @@ struct PopularView: View { @State private var error: RequestError? @Default(.popularListingStyle) private var popularListingStyle - @Default(.hideShorts) private var hideShorts var resource: Resource? { accounts.api.popular @@ -76,7 +75,7 @@ struct PopularView: View { } ToolbarItem { - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } } #else @@ -95,7 +94,7 @@ struct PopularView: View { Section { HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() } Section { @@ -135,7 +134,7 @@ struct PopularView: View { Spacer() ListingStyleButtons(listingStyle: $popularListingStyle) HideWatchedButtons() - HideShortsButtons(hide: $hideShorts) + HideShortsButtons() Button { resource?.load()