Mixonomer-iOS/Mixonomer/Model/User.swift

212 lines
6.1 KiB
Swift
Raw Normal View History

//
// User.swift
2022-08-07 13:31:15 +01:00
// Mixonomer
//
// Created by Andy Pack on 18/02/2020.
// Copyright © 2020 Sarsoo. All rights reserved.
//
import UIKit
import SwiftyJSON
2022-11-23 23:39:58 +00:00
import OSLog
enum UserType: String, Decodable {
case user = "user"
case admin = "admin"
}
2022-12-09 08:51:42 +00:00
class User: Identifiable, Decodable, ObservableObject {
//MARK: Properties
var username: String
var email: String?
var type: UserType
var locked: Bool
var last_login: String
var last_keygen: String
var last_refreshed: String
var spotify_linked: Bool
2022-12-09 08:51:42 +00:00
@Published var notify: Bool {
didSet {
self.updateUser(updates: JSON(["notify": self.notify]))
}
}
@Published var notify_playlist_updates: Bool {
didSet {
self.updateUser(updates: JSON(["notify_playlist_updates": self.notify_playlist_updates]))
}
}
@Published var notify_tag_updates: Bool {
didSet {
self.updateUser(updates: JSON(["notify_tag_updates": self.notify_tag_updates]))
}
}
@Published var notify_admins: Bool {
didSet {
self.updateUser(updates: JSON(["notify_admins": self.notify_admins]))
}
}
@Published var lastfm_username: String {
2022-08-16 18:01:17 +01:00
didSet {
self.updateUser(updates: JSON(["lastfm_username": self.lastfm_username]))
}
}
//MARK: Initialization
init(username: String = "user",
email: String? = nil,
type: UserType = .user,
locked: Bool = false,
last_login: String = "",
last_keygen: String = "",
last_refreshed: String = "",
spotify_linked: Bool = true,
2022-12-09 08:51:42 +00:00
lastfm_username: String = "",
notify: Bool = false,
notify_playlist_updates: Bool = false,
notify_tag_updates: Bool = false,
notify_admins: Bool = false){
self.username = username
self.email = email
self.type = type
self.locked = locked
self.last_login = last_login
self.last_keygen = last_keygen
self.last_refreshed = last_refreshed
self.spotify_linked = spotify_linked
self.lastfm_username = lastfm_username
2022-12-09 08:51:42 +00:00
self.notify = notify
self.notify_playlist_updates = notify_playlist_updates
self.notify_tag_updates = notify_tag_updates
self.notify_admins = notify_admins
}
2022-12-09 08:51:42 +00:00
2022-08-16 18:01:17 +01:00
func updateUser(updates: JSON) {
let api = UserApi.updateUser(updates: updates)
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
2022-12-09 08:51:42 +00:00
if !NetworkHelper.check_network_response(response: response) {
2022-11-23 23:39:58 +00:00
Logger.net.error("error while updating user: \(updates)")
2022-08-16 18:01:17 +01:00
}
}
}
private enum CodingKeys: String, CodingKey {
case username
case email
case type
case locked
case last_login
case last_keygen
case last_refreshed
case spotify_linked
case lastfm_username
2022-12-09 08:51:42 +00:00
case notify
case notify_playlist_updates
case notify_tag_updates
case notify_admins
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
username = try container.decode(String.self, forKey: .username)
do{
email = try container.decode(String.self, forKey: .email)
}catch {
email = nil
}
type = try container.decode(UserType.self, forKey: .type)
locked = try container.decode(Bool.self, forKey: .locked)
last_login = try container.decode(String.self, forKey: .last_login)
2022-08-14 13:14:21 +01:00
do{
last_keygen = try container.decode(String.self, forKey: .last_keygen)
}catch {
last_keygen = ""
}
do{
last_refreshed = try container.decode(String.self, forKey: .last_refreshed)
}catch {
last_refreshed = ""
}
spotify_linked = try container.decode(Bool.self, forKey: .spotify_linked)
2022-08-14 13:14:21 +01:00
do{
lastfm_username = try container.decode(String.self, forKey: .lastfm_username)
}catch {
2022-12-09 08:51:42 +00:00
lastfm_username = ""
}
do{
notify = try container.decode(Bool.self, forKey: .notify)
}catch {
notify = false
}
do{
notify_playlist_updates = try container.decode(Bool.self, forKey: .notify_playlist_updates)
}catch {
notify_playlist_updates = false
}
do{
notify_tag_updates = try container.decode(Bool.self, forKey: .notify_tag_updates)
}catch {
notify_tag_updates = false
}
do{
notify_admins = try container.decode(Bool.self, forKey: .notify_admins)
}catch {
notify_admins = false
2022-08-14 13:14:21 +01:00
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.username, forKey: .username)
try container.encode(self.email, forKey: .email)
try container.encode(self.type.rawValue, forKey: .type)
try container.encode(self.locked, forKey: .locked)
try container.encode(self.last_login, forKey: .last_login)
try container.encode(self.last_keygen, forKey: .last_keygen)
try container.encode(self.last_refreshed, forKey: .last_refreshed)
try container.encode(self.spotify_linked, forKey: .spotify_linked)
try container.encode(self.lastfm_username, forKey: .lastfm_username)
2022-12-09 08:51:42 +00:00
try container.encode(self.notify, forKey: .notify)
try container.encode(self.notify_playlist_updates, forKey: .notify_playlist_updates)
try container.encode(self.notify_tag_updates, forKey: .notify_tag_updates)
try container.encode(self.notify_admins, forKey: .notify_admins)
}
static func get_null_user() -> User {
return User()
}
}