From dffcfc8b0639f60351bb70171aa2feb2778e3bb5 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 4 Sep 2022 17:24:07 +0200 Subject: [PATCH] Pull to refresh fixes, added alert for playlist error --- Model/PlaylistsModel.swift | 3 ++- Shared/Playlists/PlaylistsView.swift | 28 +++++++++++++++++++++++++--- Shared/Trending/TrendingView.swift | 6 ++++-- Shared/Views/PopularView.swift | 4 +++- Shared/Views/SubscriptionsView.swift | 2 +- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Model/PlaylistsModel.swift b/Model/PlaylistsModel.swift index d222cf01..90f82f4d 100644 --- a/Model/PlaylistsModel.swift +++ b/Model/PlaylistsModel.swift @@ -53,8 +53,9 @@ final class PlaylistsModel: ObservableObject { onSuccess() } } - .onFailure { _ in + .onFailure { error in self.playlists = [] + NavigationModel.shared.presentAlert(title: "Could not refresh Playlists", message: error.userMessage) } } diff --git a/Shared/Playlists/PlaylistsView.swift b/Shared/Playlists/PlaylistsView.swift index 6177cf6b..c549c2ce 100644 --- a/Shared/Playlists/PlaylistsView.swift +++ b/Shared/Playlists/PlaylistsView.swift @@ -129,6 +129,21 @@ struct PlaylistsView: View { .onChange(of: model.reloadPlaylists) { _ in resource?.load() } + #if os(iOS) + .refreshControl { refreshControl in + model.load(force: true) { + model.reloadPlaylists.toggle() + refreshControl.endRefreshing() + } + } + .backport + .refreshable { + DispatchQueue.main.async { + model.load(force: true) { model.reloadPlaylists.toggle() } + } + } + .navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode) + #endif #if os(tvOS) .fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) { PlaylistFormView(playlist: $createdPlaylist) @@ -155,15 +170,22 @@ struct PlaylistsView: View { } ) #endif - #if os(iOS) - .navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode) - #endif + #if !os(macOS) .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in model.load() resource?.loadIfNeeded() } #endif + #if !os(tvOS) + .background( + Button("Refresh") { + resource?.load() + } + .keyboardShortcut("r") + .opacity(0) + ) + #endif } #if os(tvOS) diff --git a/Shared/Trending/TrendingView.swift b/Shared/Trending/TrendingView.swift index 8314de7e..c1a660cc 100644 --- a/Shared/Trending/TrendingView.swift +++ b/Shared/Trending/TrendingView.swift @@ -140,8 +140,10 @@ struct TrendingView: View { } .backport .refreshable { - resource.load().onFailure { error in - NavigationModel.shared.presentAlert(title: "Could not refresh Trending", message: error.userMessage) + DispatchQueue.main.async { + resource.load().onFailure { error in + NavigationModel.shared.presentAlert(title: "Could not refresh Trending", message: error.userMessage) + } } } .navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode) diff --git a/Shared/Views/PopularView.swift b/Shared/Views/PopularView.swift index 0a5faa18..3bcb4285 100644 --- a/Shared/Views/PopularView.swift +++ b/Shared/Views/PopularView.swift @@ -50,7 +50,9 @@ struct PopularView: View { } .backport .refreshable { - resource?.load() + DispatchQueue.main.async { + resource?.load() + } } .navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode) #endif diff --git a/Shared/Views/SubscriptionsView.swift b/Shared/Views/SubscriptionsView.swift index ac80b5f1..e3fde703 100644 --- a/Shared/Views/SubscriptionsView.swift +++ b/Shared/Views/SubscriptionsView.swift @@ -32,7 +32,7 @@ struct SubscriptionsView: View { } .backport .refreshable { - loadResources(force: true) + await loadResources(force: true) } #endif }