Update lastSignedInUserID defaults to use new UserSignInState enum (#1209)

* init

* projecgt file

* Update project.pbxproj

---------

Co-authored-by: Ethan Pippin <ethanpippin2343@gmail.com>
This commit is contained in:
Steve Byatt 2024-09-02 17:00:25 +10:00 committed by GitHub
parent a199d69a31
commit 58dfddeeca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 59 additions and 15 deletions

View File

@ -0,0 +1,33 @@
//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2024 Jellyfin & Jellyfin Contributors
//
import Defaults
import Foundation
enum UserSignInState: RawRepresentable, Codable, Defaults.Serializable, Equatable, Hashable {
case signedOut
case signedIn(userID: String)
var rawValue: String {
switch self {
case .signedOut:
""
case let .signedIn(userID):
userID
}
}
init?(rawValue: String) {
if rawValue.isEmpty {
self = .signedOut
} else {
self = .signedIn(userID: rawValue)
}
}
}

View File

@ -33,7 +33,12 @@ extension UserDefaults {
// the Defaults value should always be in sync with the latest user and what
// views properly expect. However, this feels like a hack and should be changed?
static var currentUserSuite: UserDefaults {
userSuite(id: Defaults[.lastSignedInUserID] ?? "default")
switch Defaults[.lastSignedInUserID] {
case .signedOut:
return userSuite(id: "default")
case let .signedIn(userID):
return userSuite(id: userID)
}
}
static func userSuite(id: String) -> UserDefaults {
@ -82,7 +87,7 @@ extension Defaults.Keys {
static let backgroundSignOutInterval: Key<TimeInterval> = AppKey("backgroundSignOutInterval", default: 3600)
static let backgroundTimeStamp: Key<Date> = AppKey("backgroundTimeStamp", default: Date.now)
static let lastSignedInUserID: Key<String?> = AppKey("lastSignedInUserID")
static let lastSignedInUserID: Key<UserSignInState> = AppKey("lastSignedInUserID", default: .signedOut)
static let selectUserDisplayType: Key<LibraryDisplayType> = AppKey("selectUserDisplayType", default: .grid)
static let selectUserServerSelection: Key<SelectUserServerSelection> = AppKey("selectUserServerSelection", default: .all)

View File

@ -42,13 +42,13 @@ final class UserSession {
extension Container {
var currentUserSession: Factory<UserSession?> {
self {
guard let lastUserID = Defaults[.lastSignedInUserID] else { return nil }
guard case let .signedIn(userId) = Defaults[.lastSignedInUserID] else { return nil }
guard let user = try? SwiftfinStore.dataStack.fetchOne(
From<UserModel>().where(\.$id == lastUserID)
From<UserModel>().where(\.$id == userId)
) else {
// had last user ID but no saved user
Defaults[.lastSignedInUserID] = nil
Defaults[.lastSignedInUserID] = .signedOut
return nil
}

View File

@ -109,7 +109,7 @@ final class SettingsViewModel: ViewModel {
}
func signOut() {
Defaults[.lastSignedInUserID] = nil
Defaults[.lastSignedInUserID] = .signedOut
Container.shared.currentUserSession.reset()
Notifications[.didSignOut].post()
}

View File

@ -66,7 +66,7 @@ struct SwiftfinApp: App {
let backgroundedInterval = Date.now.timeIntervalSince(Defaults[.backgroundTimeStamp])
if Defaults[.signOutOnBackground], backgroundedInterval > Defaults[.backgroundSignOutInterval] {
Defaults[.lastSignedInUserID] = nil
Defaults[.lastSignedInUserID] = .signedOut
Container.shared.currentUserSession.reset()
Notifications[.didSignOut].post()
}

View File

@ -305,7 +305,7 @@ struct SelectUserView: View {
self.error = eventError
self.isPresentingError = true
case let .signedIn(user):
Defaults[.lastSignedInUserID] = user.id
Defaults[.lastSignedInUserID] = .signedIn(userID: user.id)
Container.shared.currentUserSession.reset()
Notifications[.didSignIn].post()
}

View File

@ -171,7 +171,7 @@ struct UserSignInView: View {
case let .signedIn(user):
router.dismissCoordinator()
Defaults[.lastSignedInUserID] = user.id
Defaults[.lastSignedInUserID] = .signedIn(userID: user.id)
Container.shared.currentUserSession.reset()
Notifications[.didSignIn].post()
}

View File

@ -180,6 +180,8 @@
C46DD8EC2A8FB49A0046A504 /* LiveMainOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46DD8EB2A8FB49A0046A504 /* LiveMainOverlay.swift */; };
C46DD8EF2A8FB56E0046A504 /* LiveBottomBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46DD8EE2A8FB56E0046A504 /* LiveBottomBarView.swift */; };
C4E5081B2703F82A0045C9AB /* MediaView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4E508172703E8190045C9AB /* MediaView.swift */; };
DFB7C3DF2C7AA43A00CE7CDC /* UserSignInState.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFB7C3DE2C7AA42700CE7CDC /* UserSignInState.swift */; };
DFB7C3E02C7AA43A00CE7CDC /* UserSignInState.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFB7C3DE2C7AA42700CE7CDC /* UserSignInState.swift */; };
E1002B642793CEE800E47059 /* ChapterInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1002B632793CEE700E47059 /* ChapterInfo.swift */; };
E1002B652793CEE800E47059 /* ChapterInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1002B632793CEE700E47059 /* ChapterInfo.swift */; };
E1002B682793CFBA00E47059 /* Algorithms in Frameworks */ = {isa = PBXBuildFile; productRef = E1002B672793CFBA00E47059 /* Algorithms */; };
@ -1086,6 +1088,7 @@
C46DD8EB2A8FB49A0046A504 /* LiveMainOverlay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveMainOverlay.swift; sourceTree = "<group>"; };
C46DD8EE2A8FB56E0046A504 /* LiveBottomBarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveBottomBarView.swift; sourceTree = "<group>"; };
C4E508172703E8190045C9AB /* MediaView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaView.swift; sourceTree = "<group>"; };
DFB7C3DE2C7AA42700CE7CDC /* UserSignInState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserSignInState.swift; sourceTree = "<group>"; };
E1002B632793CEE700E47059 /* ChapterInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChapterInfo.swift; sourceTree = "<group>"; };
E10231292BCF8A08009D71FC /* iOSLiveTVCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = iOSLiveTVCoordinator.swift; sourceTree = "<group>"; };
E102312A2BCF8A08009D71FC /* tvOSLiveTVCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = tvOSLiveTVCoordinator.swift; sourceTree = "<group>"; };
@ -1853,6 +1856,7 @@
E129428F28F0BDC300796AC6 /* TimeStampType.swift */,
E1C8CE7B28FF015000DF5D7B /* TrailingTimestampType.swift */,
E1EA09682BED78BB004CDE76 /* UserAccessPolicy.swift */,
DFB7C3DE2C7AA42700CE7CDC /* UserSignInState.swift */,
E1D8429229340B8300D1041A /* Utilities.swift */,
E1BDF2E42951475300CC0294 /* VideoPlayerActionButton.swift */,
E1F0204D26CCCA74001C1C3B /* VideoPlayerJumpLength.swift */,
@ -4189,6 +4193,7 @@
E1E6C44529AECCF20064123F /* PlayNextItemActionButton.swift in Sources */,
6264E88D273850380081A12A /* Strings.swift in Sources */,
E1C926102887565C002A7A66 /* PlayButton.swift in Sources */,
DFB7C3E02C7AA43A00CE7CDC /* UserSignInState.swift in Sources */,
E1575E67293E77B5001665B1 /* OverlayType.swift in Sources */,
E1E9EFEA28C6B96500CC1F8B /* ServerButton.swift in Sources */,
E1575E65293E77B5001665B1 /* VideoPlayerJumpLength.swift in Sources */,
@ -4671,6 +4676,7 @@
E1D3043528D1763100587289 /* SeeAllButton.swift in Sources */,
4E73E2A62C41CFD3002D2A78 /* PlaybackBitrateTestSize.swift in Sources */,
E172D3B22BACA569007B4647 /* EpisodeContent.swift in Sources */,
DFB7C3DF2C7AA43A00CE7CDC /* UserSignInState.swift in Sources */,
E13F05EC28BC9000003499D2 /* LibraryDisplayType.swift in Sources */,
4E16FD572C01A32700110147 /* LetterPickerOrientation.swift in Sources */,
E1356E0329A730B200382563 /* SeparatorHStack.swift in Sources */,

View File

@ -1,5 +1,5 @@
{
"originHash" : "651194fc1966b57201a0de2cba27dc40798bbdf515febdc83f00d634d916fea4",
"originHash" : "54fc43873cff9b3db2ad273a82066d201e4ea59316a81526b530004e4d98b974",
"pins" : [
{
"identity" : "blurhashkit",

View File

@ -33,7 +33,7 @@ extension SwiftfinApp {
lastSignInUserIDCancellable = Task {
for await newValue in Defaults.updates(.lastSignedInUserID) {
if let _ = newValue {
if case .signedIn = newValue {
setUserDefaultsObservation()
} else {
setAppDefaultsObservation()

View File

@ -67,7 +67,7 @@ struct SwiftfinApp: App {
// don't keep last user id
if Defaults[.signOutOnClose] {
Defaults[.lastSignedInUserID] = nil
Defaults[.lastSignedInUserID] = .signedOut
}
}
@ -107,7 +107,7 @@ struct SwiftfinApp: App {
let backgroundedInterval = Date.now.timeIntervalSince(Defaults[.backgroundTimeStamp])
if Defaults[.signOutOnBackground], backgroundedInterval > Defaults[.backgroundSignOutInterval] {
Defaults[.lastSignedInUserID] = nil
Defaults[.lastSignedInUserID] = .signedOut
Container.shared.currentUserSession.reset()
Notifications[.didSignOut].post()
}

View File

@ -561,7 +561,7 @@ struct SelectUserView: View {
case let .signedIn(user):
UIDevice.feedback(.success)
Defaults[.lastSignedInUserID] = user.id
Defaults[.lastSignedInUserID] = .signedIn(userID: user.id)
Container.shared.currentUserSession.reset()
Notifications[.didSignIn].post()
}

View File

@ -73,7 +73,7 @@ struct UserSignInView: View {
case let .signedIn(user):
UIDevice.feedback(.success)
Defaults[.lastSignedInUserID] = user.id
Defaults[.lastSignedInUserID] = .signedIn(userID: user.id)
Container.shared.currentUserSession.reset()
Notifications[.didSignIn].post()
}