fixing tag loading on full refresh, smart skipping

This commit is contained in:
Andy Pack 2023-01-08 22:22:57 +00:00
parent d25a9d8b88
commit 509aea0b42
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
3 changed files with 164 additions and 128 deletions

View File

@ -47,9 +47,9 @@ class LiveUser: ObservableObject {
Logger.sys.debug("failed to get APNS token") Logger.sys.debug("failed to get APNS token")
} }
@Published var isInitiallyRefreshingUser = true @Published var isFullRefreshingUser = true
@Published var isInitiallyRefreshingPlaylists = true @Published var isFullRefreshingPlaylists = true
@Published var isInitiallyRefreshingTags = true @Published var isFullRefreshingTags = true
@Published var isRefreshingUser = false @Published var isRefreshingUser = false
@Published var isRefreshingPlaylists = false @Published var isRefreshingPlaylists = false
@ -71,8 +71,10 @@ class LiveUser: ObservableObject {
self.user = user self.user = user
} }
func lastfm_connected() -> Bool { var lastfm_connected: Bool {
return username.count > 0 get {
return user.lastfm_username.count > 0
}
} }
func logout() { func logout() {
@ -109,6 +111,8 @@ class LiveUser: ObservableObject {
} }
func refresh_user(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) { func refresh_user(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) {
if !isRefreshingUser {
self.isRefreshingUser = true self.isRefreshingUser = true
Logger.sys.info("refreshing user") Logger.sys.info("refreshing user")
@ -132,7 +136,7 @@ class LiveUser: ObservableObject {
self.user = UserApi.fromJSON(user: json) self.user = UserApi.fromJSON(user: json)
self.isRefreshingUser = false self.isRefreshingUser = false
self.isInitiallyRefreshingUser = false self.isFullRefreshingUser = false
if let success = onSuccess { if let success = onSuccess {
Logger.sys.debug("successfully refreshed user") Logger.sys.debug("successfully refreshed user")
@ -148,8 +152,15 @@ class LiveUser: ObservableObject {
} }
} }
} }
else
{
Logger.sys.info("skipping refreshing user, already refreshing")
}
}
func refresh_playlists(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) { func refresh_playlists(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) {
if !isRefreshingPlaylists {
self.isRefreshingPlaylists = true self.isRefreshingPlaylists = true
Logger.sys.info("refreshing playlists") Logger.sys.info("refreshing playlists")
@ -175,7 +186,7 @@ class LiveUser: ObservableObject {
self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
self.isRefreshingPlaylists = false self.isRefreshingPlaylists = false
self.isInitiallyRefreshingPlaylists = false self.isFullRefreshingPlaylists = false
if let success = onSuccess { if let success = onSuccess {
Logger.sys.debug("successfully refreshed playlists") Logger.sys.debug("successfully refreshed playlists")
@ -198,8 +209,15 @@ class LiveUser: ObservableObject {
} }
} }
} }
else
{
Logger.sys.info("skipping refreshing playlists, already refreshing")
}
}
func refresh_tags(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) { func refresh_tags(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) {
if !isRefreshingTags {
self.isRefreshingTags = true self.isRefreshingTags = true
Logger.sys.info("refreshing tags") Logger.sys.info("refreshing tags")
@ -225,7 +243,7 @@ class LiveUser: ObservableObject {
self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
self.isRefreshingTags = false self.isRefreshingTags = false
self.isInitiallyRefreshingTags = false self.isFullRefreshingTags = false
if let success = onSuccess { if let success = onSuccess {
Logger.sys.debug("successfully refreshed tags") Logger.sys.debug("successfully refreshed tags")
@ -248,16 +266,34 @@ class LiveUser: ObservableObject {
} }
} }
} }
else
{
Logger.sys.info("skipping refreshing tags, already refreshing")
}
}
var isFullRefreshing: Bool {
get {
return isFullRefreshingTags || isFullRefreshingPlaylists || isFullRefreshingUser
}
}
func full_refresh() { func full_refresh() {
self.isInitiallyRefreshingUser = true
self.isInitiallyRefreshingPlaylists = true if !isFullRefreshing {
self.isInitiallyRefreshingTags = true self.isFullRefreshingUser = true
self.isFullRefreshingPlaylists = true
self.isFullRefreshingTags = true
self.refresh_user() self.refresh_user()
self.refresh_playlists() self.refresh_playlists()
self.refresh_tags() self.refresh_tags()
} }
else
{
Logger.sys.info("skipping full refresh, already refreshing")
}
}
func check_network_response(response: AFDataResponse<Any>) -> Bool { func check_network_response(response: AFDataResponse<Any>) -> Bool {
return NetworkHelper.check_network_response(response: response, onTokenFail: { return NetworkHelper.check_network_response(response: response, onTokenFail: {

View File

@ -18,9 +18,9 @@ struct AppSkeleton: View {
var body: some View { var body: some View {
if self.liveUser.isInitiallyRefreshingUser if self.liveUser.isFullRefreshingUser
|| self.liveUser.isInitiallyRefreshingPlaylists || self.liveUser.isFullRefreshingPlaylists
|| self.liveUser.isInitiallyRefreshingTags || self.liveUser.isFullRefreshingTags
{ {
LoadingScreen() LoadingScreen()
.onAppear { .onAppear {
@ -39,7 +39,7 @@ struct AppSkeleton: View {
} }
.tag(0) .tag(0)
if liveUser.lastfm_connected() { if liveUser.lastfm_connected {
TagList() TagList()
.tabItem { .tabItem {

View File

@ -27,7 +27,7 @@ struct PlaylistView: View {
var body: some View { var body: some View {
Form { Form {
if liveUser.lastfm_connected() { if liveUser.lastfm_connected {
PlaylistStatsSection(playlist: $playlist, showingToast: $showingToast, toastText: $toastText, toastSuccess: $toastSuccess) PlaylistStatsSection(playlist: $playlist, showingToast: $showingToast, toastText: $toastText, toastSuccess: $toastSuccess)
} }