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
|
2022-08-08 21:59:28 +01:00
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
self.checkNetworkResponse(response: response)
|
|
|
|
|
|
|
|
switch response.response?.statusCode {
|
|
|
|
case 200, 201:
|
2020-03-06 23:36:51 +00:00
|
|
|
|
2022-08-08 21:59:28 +01:00
|
|
|
guard let data = response.data else {
|
|
|
|
fatalError("error getting playlists")
|
|
|
|
}
|
2020-03-06 23:36:51 +00:00
|
|
|
|
2022-08-08 21:59:28 +01:00
|
|
|
guard let json = try? JSON(data: data) else {
|
|
|
|
fatalError("error parsing reponse")
|
|
|
|
}
|
|
|
|
|
|
|
|
let playlists = json["playlists"].arrayValue
|
2020-03-06 23:36:51 +00:00
|
|
|
|
2022-08-08 21:59:28 +01:00
|
|
|
// update state
|
|
|
|
self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
|
|
|
|
self.isRefreshingPlaylists = false
|
|
|
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
do {
|
|
|
|
UserDefaults.standard.set(String(data: try encoder.encode(playlists), encoding: .utf8), forKey: "playlists")
|
|
|
|
} catch {
|
|
|
|
print("error encoding playlists: \(error)")
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
case _:
|
|
|
|
break
|
2020-03-06 23:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-08-09 17:42:01 +01:00
|
|
|
|
|
|
|
self.checkNetworkResponse(response: response)
|
|
|
|
|
|
|
|
switch response.response?.statusCode {
|
|
|
|
case 200, 201:
|
2020-03-06 23:36:51 +00:00
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
guard let data = response.data else {
|
|
|
|
fatalError("error getting tags")
|
|
|
|
}
|
2020-03-06 23:36:51 +00:00
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
guard let json = try? JSON(data: data) else {
|
|
|
|
fatalError("error parsing reponse")
|
|
|
|
}
|
|
|
|
|
|
|
|
let tags = json["tags"].arrayValue
|
2020-03-06 23:36:51 +00:00
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
// update state
|
|
|
|
self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
|
|
|
|
self.isRefreshingTags = false
|
|
|
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
do {
|
|
|
|
UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags")
|
|
|
|
} catch {
|
|
|
|
print("error encoding tags: \(error)")
|
|
|
|
}
|
|
|
|
|
|
|
|
case _:
|
|
|
|
break
|
2020-03-06 23:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
func checkNetworkResponse(response: AFDataResponse<Any>) {
|
|
|
|
|
|
|
|
switch response.response?.statusCode {
|
|
|
|
case 401:
|
|
|
|
self.loggedIn = false
|
|
|
|
case _:
|
|
|
|
print("error making request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 23:36:51 +00:00
|
|
|
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
|
|
|
}
|