fixing tag loading on full refresh, smart skipping
This commit is contained in:
parent
d25a9d8b88
commit
509aea0b42
@ -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,154 +111,188 @@ class LiveUser: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func refresh_user(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) {
|
func refresh_user(onSuccess: (() -> Void)? = nil, onFailure: (() -> Void)? = nil) {
|
||||||
self.isRefreshingUser = true
|
|
||||||
|
|
||||||
Logger.sys.info("refreshing user")
|
if !isRefreshingUser {
|
||||||
|
self.isRefreshingUser = true
|
||||||
let api = UserApi.getUser
|
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
||||||
|
|
||||||
if self.check_network_response(response: response) {
|
Logger.sys.info("refreshing user")
|
||||||
|
|
||||||
|
let api = UserApi.getUser
|
||||||
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
guard let data = response.data else {
|
if self.check_network_response(response: response) {
|
||||||
Logger.net.error("error getting user")
|
|
||||||
return
|
guard let data = response.data else {
|
||||||
}
|
Logger.net.error("error getting user")
|
||||||
|
return
|
||||||
guard let json = try? JSON(data: data) else {
|
}
|
||||||
Logger.parse.error("error parsing user")
|
|
||||||
return
|
guard let json = try? JSON(data: data) else {
|
||||||
}
|
Logger.parse.error("error parsing user")
|
||||||
|
return
|
||||||
// update state
|
}
|
||||||
self.user = UserApi.fromJSON(user: json)
|
|
||||||
|
// update state
|
||||||
self.isRefreshingUser = false
|
self.user = UserApi.fromJSON(user: json)
|
||||||
self.isInitiallyRefreshingUser = false
|
|
||||||
|
self.isRefreshingUser = false
|
||||||
if let success = onSuccess {
|
self.isFullRefreshingUser = false
|
||||||
Logger.sys.debug("successfully refreshed user")
|
|
||||||
success()
|
if let success = onSuccess {
|
||||||
}
|
Logger.sys.debug("successfully refreshed user")
|
||||||
|
success()
|
||||||
} else {
|
}
|
||||||
|
|
||||||
if let failure = onFailure {
|
} else {
|
||||||
Logger.net.error("failed to refresh user")
|
|
||||||
failure()
|
if let failure = onFailure {
|
||||||
|
Logger.net.error("failed to refresh user")
|
||||||
|
failure()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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) {
|
||||||
self.isRefreshingPlaylists = true
|
|
||||||
|
|
||||||
Logger.sys.info("refreshing playlists")
|
if !isRefreshingPlaylists {
|
||||||
|
self.isRefreshingPlaylists = true
|
||||||
let api = PlaylistApi.getPlaylists
|
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
||||||
|
|
||||||
if self.check_network_response(response: response) {
|
Logger.sys.info("refreshing playlists")
|
||||||
|
|
||||||
|
let api = PlaylistApi.getPlaylists
|
||||||
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
guard let data = response.data else {
|
if self.check_network_response(response: response) {
|
||||||
Logger.net.error("error getting playlists from net request")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let json = try? JSON(data: data) else {
|
|
||||||
Logger.parse.error("error parsing playlists reponse")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let playlists = json["playlists"].arrayValue
|
guard let data = response.data else {
|
||||||
|
Logger.net.error("error getting playlists from net request")
|
||||||
// update state
|
return
|
||||||
self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
}
|
||||||
|
|
||||||
self.isRefreshingPlaylists = false
|
guard let json = try? JSON(data: data) else {
|
||||||
self.isInitiallyRefreshingPlaylists = false
|
Logger.parse.error("error parsing playlists reponse")
|
||||||
|
return
|
||||||
if let success = onSuccess {
|
}
|
||||||
Logger.sys.debug("successfully refreshed playlists")
|
|
||||||
success()
|
let playlists = json["playlists"].arrayValue
|
||||||
}
|
|
||||||
|
// update state
|
||||||
let encoder = JSONEncoder()
|
self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
||||||
do {
|
|
||||||
UserDefaults.standard.set(String(data: try encoder.encode(playlists), encoding: .utf8), forKey: "playlists")
|
self.isRefreshingPlaylists = false
|
||||||
} catch {
|
self.isFullRefreshingPlaylists = false
|
||||||
Logger.parse.error("error encoding playlists: \(error)")
|
|
||||||
}
|
if let success = onSuccess {
|
||||||
|
Logger.sys.debug("successfully refreshed playlists")
|
||||||
} else {
|
success()
|
||||||
|
}
|
||||||
if let failure = onFailure {
|
|
||||||
Logger.net.error("failed to refresh playlists")
|
let encoder = JSONEncoder()
|
||||||
failure()
|
do {
|
||||||
|
UserDefaults.standard.set(String(data: try encoder.encode(playlists), encoding: .utf8), forKey: "playlists")
|
||||||
|
} catch {
|
||||||
|
Logger.parse.error("error encoding playlists: \(error)")
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if let failure = onFailure {
|
||||||
|
Logger.net.error("failed to refresh playlists")
|
||||||
|
failure()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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) {
|
||||||
self.isRefreshingTags = true
|
|
||||||
|
|
||||||
Logger.sys.info("refreshing tags")
|
|
||||||
|
|
||||||
let api = TagApi.getTags
|
if !isRefreshingTags {
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
self.isRefreshingTags = true
|
||||||
|
|
||||||
if self.check_network_response(response: response) {
|
Logger.sys.info("refreshing tags")
|
||||||
|
|
||||||
|
let api = TagApi.getTags
|
||||||
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
guard let data = response.data else {
|
if self.check_network_response(response: response) {
|
||||||
Logger.net.error("error getting tags")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let json = try? JSON(data: data) else {
|
|
||||||
Logger.parse.error("error parsing tags response")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let tags = json["tags"].arrayValue
|
guard let data = response.data else {
|
||||||
|
Logger.net.error("error getting tags")
|
||||||
// update state
|
return
|
||||||
self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
}
|
||||||
|
|
||||||
self.isRefreshingTags = false
|
guard let json = try? JSON(data: data) else {
|
||||||
self.isInitiallyRefreshingTags = false
|
Logger.parse.error("error parsing tags response")
|
||||||
|
return
|
||||||
if let success = onSuccess {
|
}
|
||||||
Logger.sys.debug("successfully refreshed tags")
|
|
||||||
success()
|
let tags = json["tags"].arrayValue
|
||||||
}
|
|
||||||
|
// update state
|
||||||
let encoder = JSONEncoder()
|
self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
||||||
do {
|
|
||||||
UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags")
|
self.isRefreshingTags = false
|
||||||
} catch {
|
self.isFullRefreshingTags = false
|
||||||
Logger.parse.error("error encoding tags: \(error)")
|
|
||||||
}
|
if let success = onSuccess {
|
||||||
|
Logger.sys.debug("successfully refreshed tags")
|
||||||
} else {
|
success()
|
||||||
|
}
|
||||||
if let failure = onFailure {
|
|
||||||
Logger.net.error("failed to refresh tags")
|
let encoder = JSONEncoder()
|
||||||
failure()
|
do {
|
||||||
|
UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags")
|
||||||
|
} catch {
|
||||||
|
Logger.parse.error("error encoding tags: \(error)")
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if let failure = onFailure {
|
||||||
|
Logger.net.error("failed to refresh tags")
|
||||||
|
failure()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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
|
|
||||||
self.isInitiallyRefreshingTags = true
|
|
||||||
|
|
||||||
self.refresh_user()
|
if !isFullRefreshing {
|
||||||
self.refresh_playlists()
|
self.isFullRefreshingUser = true
|
||||||
self.refresh_tags()
|
self.isFullRefreshingPlaylists = true
|
||||||
|
self.isFullRefreshingTags = true
|
||||||
|
|
||||||
|
self.refresh_user()
|
||||||
|
self.refresh_playlists()
|
||||||
|
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 {
|
||||||
|
@ -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 {
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user