iOS 14/macOS Big Sur Support

This commit is contained in:
Arkadiusz Fal
2021-11-28 15:37:55 +01:00
parent 696751e07c
commit 5ef89ac9f4
57 changed files with 1147 additions and 813 deletions

View File

@@ -83,7 +83,7 @@ struct ChannelPlaylistView: View {
.navigationTitle(playlist.title)
#else
.background(.thickMaterial)
.background(Color.tertiaryBackground)
#endif
}

View File

@@ -9,7 +9,7 @@ struct ChannelVideosView: View {
@StateObject private var store = Store<Channel>()
@Environment(\.dismiss) private var dismiss
@Environment(\.presentationMode) private var presentationMode
@Environment(\.inNavigationView) private var inNavigationView
#if os(iOS)
@@ -43,7 +43,7 @@ struct ChannelVideosView: View {
}
var content: some View {
VStack {
let content = VStack {
#if os(tvOS)
HStack {
Text(navigationTitle)
@@ -65,40 +65,43 @@ struct ChannelVideosView: View {
.frame(maxWidth: .infinity)
#endif
VerticalCells(items: videos)
#if !os(iOS)
.prefersDefaultFocus(in: focusNamespace)
#if os(iOS)
VerticalCells(items: videos)
#else
if #available(macOS 12.0, *) {
VerticalCells(items: videos)
.prefersDefaultFocus(in: focusNamespace)
} else {
VerticalCells(items: videos)
}
#endif
}
.environment(\.inChannelView, true)
#if !os(iOS)
.focusScope(focusNamespace)
#endif
#if !os(tvOS)
.toolbar {
ToolbarItem(placement: .navigation) {
ShareButton(
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
)
}
.toolbar {
ToolbarItem(placement: .navigation) {
ShareButton(
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
)
}
ToolbarItem {
HStack {
Text("**\(store.item?.subscriptionsString ?? "loading")** subscribers")
.foregroundColor(.secondary)
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
ToolbarItem {
HStack {
Text("**\(store.item?.subscriptionsString ?? "loading")** subscribers")
.foregroundColor(.secondary)
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
subscriptionToggleButton
subscriptionToggleButton
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
}
}
}
}
#else
.background(.thickMaterial)
.background(Color.tertiaryBackground)
#endif
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
@@ -107,7 +110,6 @@ struct ChannelVideosView: View {
}
}
#endif
.modifier(UnsubscribeAlertModifier())
.onAppear {
if store.item.isNil {
resource.addObserver(store)
@@ -115,6 +117,17 @@ struct ChannelVideosView: View {
}
}
.navigationTitle(navigationTitle)
return Group {
if #available(macOS 12.0, *) {
content
#if !os(iOS)
.focusScope(focusNamespace)
#endif
} else {
content
}
}
}
private var resource: Resource {

View File

@@ -26,8 +26,13 @@ struct DetailBadge: View {
struct DefaultStyleModifier: ViewModifier {
func body(content: Content) -> some View {
content
.background(.thinMaterial)
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
content
.background(.thinMaterial)
} else {
content
.background(Color.background)
}
}
}

View File

@@ -1,15 +1,15 @@
import SwiftUI
struct OpenSettingsButton: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.presentationMode) private var presentationMode
#if !os(macOS)
@EnvironmentObject<NavigationModel> private var navigation
#endif
var body: some View {
Button {
dismiss()
let button = Button {
presentationMode.wrappedValue.dismiss()
#if os(macOS)
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
@@ -19,7 +19,13 @@ struct OpenSettingsButton: View {
} label: {
Label("Open Settings", systemImage: "gearshape.2")
}
.buttonStyle(.borderedProminent)
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
button
.buttonStyle(.borderedProminent)
} else {
button
}
}
}

View File

@@ -25,7 +25,7 @@ struct PlayerControlsView<Content: View>: View {
}
private var controls: some View {
HStack {
let controls = HStack {
Button(action: {
model.presentingPlayer.toggle()
}) {
@@ -92,14 +92,23 @@ struct PlayerControlsView<Content: View>: View {
.padding(.horizontal)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 55)
.padding(.vertical, 0)
.background(.ultraThinMaterial)
.borderTop(height: 0.4, color: Color("PlayerControlsBorderColor"))
.borderBottom(height: navigationStyle == .sidebar ? 0 : 0.4, color: Color("PlayerControlsBorderColor"))
.borderTop(height: 0.4, color: Color("ControlsBorderColor"))
.borderBottom(height: navigationStyle == .sidebar ? 0 : 0.4, color: Color("ControlsBorderColor"))
#if !os(tvOS)
.onSwipeGesture(up: {
model.presentingPlayer = true
})
#endif
return Group {
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
controls
.background(Material.ultraThinMaterial)
} else {
controls
.background(Color.tertiaryBackground)
}
}
}
private var appVersion: String {

View File

@@ -1,414 +0,0 @@
import Defaults
import Siesta
import SwiftUI
struct SearchView: View {
private var query: SearchQuery?
@State private var searchSortOrder = SearchQuery.SortOrder.relevance
@State private var searchDate = SearchQuery.Date.any
@State private var searchDuration = SearchQuery.Duration.any
@State private var presentingClearConfirmation = false
@State private var recentsChanged = false
#if os(tvOS)
@State private var searchDebounce = Debounce()
@State private var recentsDebounce = Debounce()
#endif
@State private var favoriteItem: FavoriteItem?
@Environment(\.navigationStyle) private var navigationStyle
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var state
private var videos = [Video]()
var items: [ContentItem] {
state.store.collection.sorted { $0 < $1 }
}
init(_ query: SearchQuery? = nil, videos: [Video] = [Video]()) {
self.query = query
self.videos = videos
}
var body: some View {
PlayerControlsView {
VStack {
if showRecentQueries {
recentQueries
} else {
#if os(tvOS)
ScrollView(.vertical, showsIndicators: false) {
HStack(spacing: 0) {
if accounts.app.supportsSearchFilters {
filtersHorizontalStack
}
if let favoriteItem = favoriteItem {
FavoriteButton(item: favoriteItem)
.id(favoriteItem.id)
.labelStyle(.iconOnly)
.font(.system(size: 25))
}
}
HorizontalCells(items: items)
}
.edgesIgnoringSafeArea(.horizontal)
#else
VerticalCells(items: items)
#endif
if noResults {
Text("No results")
if searchFiltersActive {
Button("Reset search filters", action: resetFilters)
}
Spacer()
}
}
}
}
.toolbar {
#if !os(tvOS)
ToolbarItemGroup(placement: toolbarPlacement) {
#if os(macOS)
if let favoriteItem = favoriteItem {
FavoriteButton(item: favoriteItem)
.id(favoriteItem.id)
}
#endif
if accounts.app.supportsSearchFilters {
Section {
#if os(macOS)
HStack {
Text("Sort:")
.foregroundColor(.secondary)
searchSortOrderPicker
}
#else
Menu("Sort: \(searchSortOrder.name)") {
searchSortOrderPicker
}
#endif
}
.transaction { t in t.animation = .none }
}
#if os(iOS)
Spacer()
if let favoriteItem = favoriteItem {
FavoriteButton(item: favoriteItem)
.id(favoriteItem.id)
}
Spacer()
#endif
if accounts.app.supportsSearchFilters {
filtersMenu
}
}
#endif
}
.onAppear {
if query != nil {
state.queryText = query!.query
state.resetQuery(query!)
updateFavoriteItem()
}
if !videos.isEmpty {
state.store.replace(ContentItem.array(of: videos))
}
}
.searchable(text: $state.queryText, placement: searchFieldPlacement) {
ForEach(state.querySuggestions.collection, id: \.self) { suggestion in
Text(suggestion)
.searchCompletion(suggestion)
}
}
.onChange(of: state.queryText) { newQuery in
if newQuery.isEmpty {
state.resetQuery()
}
state.loadSuggestions(newQuery)
#if os(tvOS)
searchDebounce.invalidate()
recentsDebounce.invalidate()
searchDebounce.debouncing(2) {
state.changeQuery { query in
query.query = newQuery
updateFavoriteItem()
}
}
recentsDebounce.debouncing(10) {
recents.addQuery(newQuery)
}
#endif
}
.onSubmit(of: .search) {
state.changeQuery { query in query.query = state.queryText }
recents.addQuery(state.queryText)
updateFavoriteItem()
}
.onChange(of: searchSortOrder) { order in
state.changeQuery { query in
query.sortBy = order
updateFavoriteItem()
}
}
.onChange(of: searchDate) { date in
state.changeQuery { query in
query.date = date
updateFavoriteItem()
}
}
.onChange(of: searchDuration) { duration in
state.changeQuery { query in
query.duration = duration
updateFavoriteItem()
}
}
#if !os(tvOS)
.navigationTitle("Search")
#endif
}
var searchFieldPlacement: SearchFieldPlacement {
#if os(iOS)
.navigationBarDrawer(displayMode: .always)
#else
.automatic
#endif
}
var toolbarPlacement: ToolbarItemPlacement {
#if os(iOS)
.bottomBar
#else
.automatic
#endif
}
fileprivate var showRecentQueries: Bool {
navigationStyle == .tab && state.queryText.isEmpty
}
fileprivate var filtersActive: Bool {
searchDuration != .any || searchDate != .any
}
fileprivate func resetFilters() {
searchSortOrder = .relevance
searchDate = .any
searchDuration = .any
}
fileprivate var noResults: Bool {
items.isEmpty && !state.isLoading && !state.query.isEmpty
}
var recentQueries: some View {
VStack {
List {
Section(header: Text("Recents")) {
if recentItems.isEmpty {
Text("Search history is empty")
.foregroundColor(.secondary)
}
ForEach(recentItems) { item in
Button(item.title) {
state.queryText = item.title
state.changeQuery { query in query.query = item.title }
updateFavoriteItem()
}
#if os(iOS)
.swipeActions(edge: .trailing) {
deleteButton(item)
}
#elseif os(tvOS)
.contextMenu {
deleteButton(item)
}
#endif
}
}
.redrawOn(change: recentsChanged)
if !recentItems.isEmpty {
clearAllButton
}
}
}
#if os(iOS)
.listStyle(.insetGrouped)
#endif
}
#if !os(macOS)
func deleteButton(_ item: RecentItem) -> some View {
Button(role: .destructive) {
recents.close(item)
recentsChanged.toggle()
} label: {
Label("Delete", systemImage: "trash")
}
}
#endif
var clearAllButton: some View {
Button("Clear All", role: .destructive) {
presentingClearConfirmation = true
}
.confirmationDialog("Clear All", isPresented: $presentingClearConfirmation) {
Button("Clear All", role: .destructive) {
recents.clearQueries()
}
}
}
var searchFiltersActive: Bool {
searchDate != .any || searchDuration != .any
}
var recentItems: [RecentItem] {
Defaults[.recentlyOpened].filter { $0.type == .query }.reversed()
}
var searchSortOrderPicker: some View {
Picker("Sort", selection: $searchSortOrder) {
ForEach(SearchQuery.SortOrder.allCases) { sortOrder in
Text(sortOrder.name).tag(sortOrder)
}
}
}
#if os(tvOS)
var searchSortOrderButton: some View {
Button(action: { self.searchSortOrder = self.searchSortOrder.next() }) { Text(self.searchSortOrder.name)
.font(.system(size: 30))
.padding(.horizontal)
.padding(.vertical, 2)
}
.buttonStyle(.card)
.contextMenu {
ForEach(SearchQuery.SortOrder.allCases) { sortOrder in
Button(sortOrder.name) {
self.searchSortOrder = sortOrder
}
}
}
}
var searchDateButton: some View {
Button(action: { self.searchDate = self.searchDate.next() }) {
Text(self.searchDate.name)
.font(.system(size: 30))
.padding(.horizontal)
.padding(.vertical, 2)
}
.buttonStyle(.card)
.contextMenu {
ForEach(SearchQuery.Date.allCases) { searchDate in
Button(searchDate.name) {
self.searchDate = searchDate
}
}
}
}
var searchDurationButton: some View {
Button(action: { self.searchDuration = self.searchDuration.next() }) {
Text(self.searchDuration.name)
.font(.system(size: 30))
.padding(.horizontal)
.padding(.vertical, 2)
}
.buttonStyle(.card)
.contextMenu {
ForEach(SearchQuery.Duration.allCases) { searchDuration in
Button(searchDuration.name) {
self.searchDuration = searchDuration
}
}
}
}
var filtersHorizontalStack: some View {
HStack {
HStack(spacing: 30) {
Text("Sort")
.foregroundColor(.secondary)
searchSortOrderButton
}
.frame(maxWidth: 300, alignment: .trailing)
HStack(spacing: 30) {
Text("Duration")
.foregroundColor(.secondary)
searchDurationButton
}
.frame(maxWidth: 300)
HStack(spacing: 30) {
Text("Date")
.foregroundColor(.secondary)
searchDateButton
}
.frame(maxWidth: 300, alignment: .leading)
}
.font(.system(size: 30))
}
#else
var filtersMenu: some View {
Menu(filtersActive ? "Filter: active" : "Filter") {
Picker(selection: $searchDuration, label: Text("Duration")) {
ForEach(SearchQuery.Duration.allCases) { duration in
Text(duration.name).tag(duration)
}
}
Picker("Upload date", selection: $searchDate) {
ForEach(SearchQuery.Date.allCases) { date in
Text(date.name).tag(date)
}
}
}
.foregroundColor(filtersActive ? .accentColor : .secondary)
.transaction { t in t.animation = .none }
}
#endif
private func updateFavoriteItem() {
favoriteItem = FavoriteItem(section: .searchQuery(
state.query.query,
state.query.date?.rawValue ?? "",
state.query.duration?.rawValue ?? "",
state.query.sortBy.rawValue
))
}
}
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
SearchView(SearchQuery(query: "Is Google Evil"), videos: Video.fixtures(30))
.injectFixtureEnvironmentObjects()
}
}
}

View File

@@ -31,9 +31,6 @@ struct SubscriptionsView: View {
FavoriteButton(item: FavoriteItem(section: .subscriptions))
}
}
.refreshable {
loadResources(force: true)
}
}
fileprivate func loadResources(force: Bool = false) {

View File

@@ -113,7 +113,7 @@ struct VideoContextMenuView: View {
private var subscriptionButton: some View {
Group {
if subscriptions.isSubscribing(video.channel.id) {
Button(role: .destructive) {
Button {
#if os(tvOS)
subscriptions.unsubscribe(video.channel.id)
#else
@@ -143,7 +143,7 @@ struct VideoContextMenuView: View {
}
func removeFromPlaylistButton(playlistID: String) -> some View {
Button(role: .destructive) {
Button {
playlists.removeVideo(videoIndexID: video.indexID!, playlistID: playlistID)
} label: {
Label("Remove from playlist", systemImage: "text.badge.minus")

View File

@@ -2,14 +2,14 @@ import Defaults
import SwiftUI
struct WelcomeScreen: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.presentationMode) private var presentationMode
@EnvironmentObject<AccountsModel> private var accounts
@Default(.accounts) private var allAccounts
var body: some View {
VStack {
let welcomeScreen = VStack {
Spacer()
Text("Welcome")
@@ -26,7 +26,7 @@ struct WelcomeScreen: View {
AccountSelectionView(showHeader: false)
Button {
dismiss()
presentationMode.wrappedValue.dismiss()
} label: {
Text("Start")
}
@@ -36,7 +36,7 @@ struct WelcomeScreen: View {
#else
AccountsMenuView()
.onChange(of: accounts.current) { _ in
dismiss()
presentationMode.wrappedValue.dismiss()
}
#if os(macOS)
.frame(maxWidth: 280)
@@ -50,10 +50,16 @@ struct WelcomeScreen: View {
Spacer()
}
.interactiveDismissDisabled()
#if os(macOS)
.frame(minWidth: 400, minHeight: 400)
.frame(minWidth: 400, minHeight: 400)
#endif
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
welcomeScreen
.interactiveDismissDisabled()
} else {
welcomeScreen
}
}
}