2020-02-19 23:00:23 +00:00
|
|
|
//
|
|
|
|
// LiveUser.swift
|
2022-08-07 13:31:15 +01:00
|
|
|
// Mixonomer
|
2020-02-19 23:00:23 +00:00
|
|
|
//
|
|
|
|
// Created by Andy Pack on 19/02/2020.
|
|
|
|
// Copyright © 2020 Sarsoo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-03-06 23:36:51 +00:00
|
|
|
import Alamofire
|
|
|
|
import SwiftyJSON
|
2020-02-19 23:00:23 +00:00
|
|
|
|
|
|
|
class LiveUser: ObservableObject {
|
2020-02-22 13:28:15 +00:00
|
|
|
|
|
|
|
@Published var playlists: [Playlist]
|
|
|
|
@Published var tags: [Tag]
|
2020-03-03 00:04:20 +00:00
|
|
|
@Published var username: String
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2020-04-25 15:44:54 +01:00
|
|
|
@Published var loggedIn: Bool {
|
|
|
|
didSet {
|
|
|
|
UserDefaults.standard.set(loggedIn, forKey: "loggedIn")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 18:51:52 +00:00
|
|
|
@Published var isRefreshingPlaylists = false
|
|
|
|
@Published var isRefreshingTags = false
|
|
|
|
|
2020-04-25 15:44:54 +01:00
|
|
|
init(playlists: [Playlist], tags: [Tag], username: String, loggedIn: Bool) {
|
2020-02-19 23:00:23 +00:00
|
|
|
self.playlists = playlists
|
2020-02-20 18:25:28 +00:00
|
|
|
self.tags = tags
|
2020-03-03 00:04:20 +00:00
|
|
|
self.username = username
|
2020-04-25 15:44:54 +01:00
|
|
|
self.loggedIn = loggedIn
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-02-22 13:28:15 +00:00
|
|
|
|
|
|
|
func updatePlaylist(playlistIn: Playlist) {
|
|
|
|
guard let index = self.playlists.firstIndex(of: playlistIn) else {
|
|
|
|
fatalError("\(playlistIn) not found")
|
|
|
|
}
|
|
|
|
self.playlists[index] = playlistIn
|
|
|
|
}
|
2020-03-06 23:36:51 +00:00
|
|
|
|
|
|
|
func refreshPlaylists() {
|
2020-03-07 18:51:52 +00:00
|
|
|
self.isRefreshingPlaylists = true
|
|
|
|
|
2020-03-06 23:36:51 +00:00
|
|
|
let api = PlaylistApi.getPlaylists
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
|
|
|
|
guard let data = response.data else {
|
|
|
|
fatalError("error getting playlists")
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let json = try? JSON(data: data) else {
|
|
|
|
fatalError("error parsing reponse")
|
|
|
|
}
|
|
|
|
|
|
|
|
let playlists = json["playlists"].arrayValue
|
|
|
|
|
|
|
|
// update state
|
|
|
|
self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
|
2020-03-07 18:51:52 +00:00
|
|
|
self.isRefreshingPlaylists = false
|
|
|
|
|
2020-03-06 23:36:51 +00:00
|
|
|
let encoder = JSONEncoder()
|
|
|
|
do {
|
2020-03-20 00:08:18 +00:00
|
|
|
UserDefaults.standard.set(String(data: try encoder.encode(playlists), encoding: .utf8), forKey: "playlists")
|
2020-03-06 23:36:51 +00:00
|
|
|
} catch {
|
|
|
|
print("error encoding playlists: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshTags() {
|
2020-03-07 18:51:52 +00:00
|
|
|
self.isRefreshingTags = true
|
|
|
|
|
2020-03-06 23:36:51 +00:00
|
|
|
let api = TagApi.getTags
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
|
|
|
|
guard let data = response.data else {
|
|
|
|
fatalError("error getting tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let json = try? JSON(data: data) else {
|
|
|
|
fatalError("error parsing reponse")
|
|
|
|
}
|
|
|
|
|
|
|
|
let tags = json["tags"].arrayValue
|
|
|
|
|
|
|
|
// update state
|
|
|
|
self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
|
2020-03-07 18:51:52 +00:00
|
|
|
self.isRefreshingTags = false
|
|
|
|
|
2020-03-06 23:36:51 +00:00
|
|
|
let encoder = JSONEncoder()
|
|
|
|
do {
|
2020-03-20 00:08:18 +00:00
|
|
|
UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags")
|
2020-03-06 23:36:51 +00:00
|
|
|
} catch {
|
|
|
|
print("error encoding tags: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadUserDefaults() -> LiveUser {
|
|
|
|
let defaults = UserDefaults.standard
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
|
|
|
|
let _strPlaylists = defaults.string(forKey: "playlists")
|
|
|
|
let _strTags = defaults.string(forKey: "tags")
|
2020-04-25 15:44:54 +01:00
|
|
|
loggedIn = defaults.bool(forKey: "loggedIn")
|
2020-03-06 23:36:51 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
if let _strPlaylists = _strPlaylists {
|
2020-03-20 00:08:18 +00:00
|
|
|
if _strPlaylists.count > 0 {
|
|
|
|
self.playlists = (try decoder.decode([Playlist].self, from: _strPlaylists.data(using: .utf8)!)).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
}
|
2020-03-06 23:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let _strTags = _strTags {
|
2020-03-20 00:08:18 +00:00
|
|
|
if _strTags.count > 0 {
|
|
|
|
self.tags = (try decoder.decode([Tag].self, from: _strTags.data(using: .utf8)!)).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
}
|
2020-03-06 23:36:51 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
print("error decoding: \(error)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|