stremio/apps/ios/VMC/Data/SessionStore.swift
Jos Vooges | STH 252bce838b Add native VMC iOS client with VLC playback.
SwiftUI app against master API: link, profiles, browse, live TV, favorites sync, and TestFlight-ready assets.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 19:16:29 +02:00

68 lines
2.5 KiB
Swift

import Foundation
@MainActor
final class SessionStore: ObservableObject {
static let defaultApiBase = "https://master.vonas.nl"
@Published var refreshToken: String?
@Published var apiBase: String
@Published var profileId: String?
@Published var profileName: String?
@Published var profileAvatarId: String?
private let defaults = UserDefaults.standard
private enum Key {
static let token = "vmc.refresh_token"
static let api = "vmc.api_base"
static let profileId = "vmc.profile_id"
static let profileName = "vmc.profile_name"
static let profileAvatar = "vmc.profile_avatar"
}
init() {
refreshToken = defaults.string(forKey: Key.token)
apiBase = defaults.string(forKey: Key.api) ?? Self.defaultApiBase
profileId = defaults.string(forKey: Key.profileId)
profileName = defaults.string(forKey: Key.profileName)
profileAvatarId = defaults.string(forKey: Key.profileAvatar)
}
var isAuthed: Bool { !(refreshToken ?? "").isEmpty }
var hasProfile: Bool { !(profileId ?? "").isEmpty }
func setToken(_ token: String?) {
refreshToken = token
if let token, !token.isEmpty {
defaults.set(token, forKey: Key.token)
} else {
defaults.removeObject(forKey: Key.token)
setProfile(id: nil, name: nil, avatarId: nil)
}
}
func setApiBase(_ url: String) {
let clean = url.trimmingCharacters(in: .whitespacesAndNewlines)
.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
apiBase = clean.isEmpty ? Self.defaultApiBase : clean
defaults.set(apiBase, forKey: Key.api)
}
func setProfile(id: String?, name: String?, avatarId: String?) {
profileId = id
profileName = name
profileAvatarId = avatarId
if let id, !id.isEmpty {
defaults.set(id, forKey: Key.profileId)
if let name, !name.isEmpty { defaults.set(name, forKey: Key.profileName) }
else { defaults.removeObject(forKey: Key.profileName) }
if let avatarId, !avatarId.isEmpty { defaults.set(avatarId, forKey: Key.profileAvatar) }
else { defaults.removeObject(forKey: Key.profileAvatar) }
} else {
defaults.removeObject(forKey: Key.profileId)
defaults.removeObject(forKey: Key.profileName)
defaults.removeObject(forKey: Key.profileAvatar)
profileName = nil
profileAvatarId = nil
}
}
}