Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
LePips committed Sep 30, 2024
1 parent 97b1b61 commit 120118e
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 375 deletions.
10 changes: 4 additions & 6 deletions Shared/Coordinators/SettingsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class SettingsCoordinator: NavigationCoordinatable {
@Route(.modal)
var itemOverviewView = makeItemOverviewView
@Route(.push)
var scheduledTasks = makeScheduledTasks
var tasks = makeTasks
@Route(.push)
var editScheduledTask = makeEditScheduledTask
@Route(.push)
Expand Down Expand Up @@ -173,10 +173,8 @@ final class SettingsCoordinator: NavigationCoordinatable {
}

@ViewBuilder
func makeActiveDeviceDetails(viewModel: ActiveSessionsViewModel) -> some View {
ActiveDeviceDetailView(
viewModel: viewModel
)
func makeActiveDeviceDetails(box: BindingBox<SessionInfo?>) -> some View {
ActiveDeviceDetailView(box: box)
}

func makeItemOverviewView(item: BaseItemDto) -> NavigationViewCoordinator<BasicNavigationViewCoordinator> {
Expand All @@ -186,7 +184,7 @@ final class SettingsCoordinator: NavigationCoordinatable {
}

@ViewBuilder
func makeScheduledTasks() -> some View {
func makeTasks() -> some View {
ScheduledTasksView()
}

Expand Down
95 changes: 58 additions & 37 deletions Shared/ViewModels/ActiveSessionsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ import JellyfinAPI
import OrderedCollections
import SwiftUI

extension Binding where Value: Sequence {

func first(where predicate: @escaping (Value.Element) throws -> Bool) rethrows -> Binding<Value.Element?> {
Binding<Value.Element?> {
try? wrappedValue.first(where: predicate)
} set: { _, _ in

}
}
}

final class ActiveSessionsViewModel: ViewModel, Stateful {

// MARK: - Action
Expand All @@ -49,7 +38,7 @@ final class ActiveSessionsViewModel: ViewModel, Stateful {
@Published
final var backgroundStates: OrderedSet<BackgroundState> = []
@Published
final var sessions: OrderedSet<SessionInfo> = []
final var sessions: OrderedDictionary<String, BindingBox<SessionInfo?>> = [:]
@Published
final var state: State = .initial

Expand All @@ -72,13 +61,7 @@ final class ActiveSessionsViewModel: ViewModel, Stateful {
}

do {
let newSessions = try await self?.getSessions()

guard let self else { return }

await MainActor.run {
self.sessions = newSessions ?? []
}
try await self?.updateSessions()
} catch {
guard let self else { return }
await MainActor.run {
Expand All @@ -102,12 +85,11 @@ final class ActiveSessionsViewModel: ViewModel, Stateful {
}

do {
let newSessions = try await self?.getSessions()
try await self?.updateSessions()

guard let self else { return }

await MainActor.run {
self.sessions = newSessions ?? []
self.state = .content
}
} catch {
Expand All @@ -123,35 +105,74 @@ final class ActiveSessionsViewModel: ViewModel, Stateful {
}
}

private func getSessions() async throws -> OrderedSet<SessionInfo> {
private func updateSessions() async throws {
var parameters = Paths.GetSessionsParameters()
parameters.activeWithinSeconds = activeWithinSeconds
parameters.deviceID = deviceID

let request = Paths.getSessions(parameters: parameters)
let response = try await userSession.client.send(request)

let newSessions = response.value.sorted {
let isPlaying0 = $0.nowPlayingItem != nil
let isPlaying1 = $1.nowPlayingItem != nil
let removedSessionIDs = sessions.keys.filter { !response.value.map(\.id).contains($0) }

if isPlaying0 && !isPlaying1 {
return true
} else if !isPlaying0 && isPlaying1 {
return false
let existingIDs = sessions.keys
.filter {
response.value.map(\.id).contains($0)
}
let newSessions = response.value
.filter {
guard let id = $0.id else { return false }
return !sessions.keys.contains(id)
}
.map { s in
BindingBox<SessionInfo?>(
source: .init(
get: { s },
set: { _ in }
)
)
}

if $0.userName != $1.userName {
return ($0.userName ?? "") < ($1.userName ?? "")
await MainActor.run {
for id in removedSessionIDs {
let t = sessions[id]
sessions[id] = nil
t?.value = nil
}

if isPlaying0 && isPlaying1 {
return ($0.nowPlayingItem?.name ?? "") < ($1.nowPlayingItem?.name ?? "")
} else {
return ($0.lastActivityDate ?? Date.now) > ($1.lastActivityDate ?? Date.now)
for id in existingIDs {
sessions[id]?.value = response.value.first(where: { $0.id == id })
}
}

return OrderedSet(newSessions)
for session in newSessions {
guard let id = session.value?.id else { continue }

sessions[id] = session
}

sessions.sort { x, y in
let xs = x.value.value
let ys = y.value.value

let isPlaying0 = xs?.nowPlayingItem != nil
let isPlaying1 = ys?.nowPlayingItem != nil

if isPlaying0 && !isPlaying1 {
return true
} else if !isPlaying0 && isPlaying1 {
return false
}

if xs?.userName != ys?.userName {
return (xs?.userName ?? "") < (ys?.userName ?? "")
}

if isPlaying0 && isPlaying1 {
return (xs?.nowPlayingItem?.name ?? "") < (ys?.nowPlayingItem?.name ?? "")
} else {
return (xs?.lastActivityDate ?? Date.now) > (ys?.lastActivityDate ?? Date.now)
}
}
}
}
}
8 changes: 0 additions & 8 deletions Swiftfin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
4E9A24E92C82B79D0023DA83 /* EditCustomDeviceProfileCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EC1C8572C80332500E2879E /* EditCustomDeviceProfileCoordinator.swift */; };
4E9A24EB2C82B9ED0023DA83 /* CustomDeviceProfileCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9A24EA2C82B9ED0023DA83 /* CustomDeviceProfileCoordinator.swift */; };
4E9A24ED2C82BAFB0023DA83 /* EditCustomDeviceProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9A24EC2C82BAFB0023DA83 /* EditCustomDeviceProfileView.swift */; };
4EB140482C8E323A008691F3 /* ClientSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB140472C8E323A008691F3 /* ClientSection.swift */; };
4EB1404A2C8E33B2008691F3 /* ConnectionSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB140492C8E33B2008691F3 /* ConnectionSection.swift */; };
4EB1404C2C8E45B1008691F3 /* StreamSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB1404B2C8E45B1008691F3 /* StreamSection.swift */; };
4EB1A8CA2C9A766200F43898 /* ActiveDevicesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB1A8C92C9A765800F43898 /* ActiveDevicesView.swift */; };
4EB1A8CC2C9B1BA200F43898 /* ServerTaskButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB1A8CB2C9B1B9700F43898 /* ServerTaskButton.swift */; };
Expand Down Expand Up @@ -1038,8 +1036,6 @@
4E9A24E72C82B6190023DA83 /* CustomProfileButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomProfileButton.swift; sourceTree = "<group>"; };
4E9A24EA2C82B9ED0023DA83 /* CustomDeviceProfileCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomDeviceProfileCoordinator.swift; sourceTree = "<group>"; };
4E9A24EC2C82BAFB0023DA83 /* EditCustomDeviceProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditCustomDeviceProfileView.swift; sourceTree = "<group>"; };
4EB140472C8E323A008691F3 /* ClientSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientSection.swift; sourceTree = "<group>"; };
4EB140492C8E33B2008691F3 /* ConnectionSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionSection.swift; sourceTree = "<group>"; };
4EB1404B2C8E45B1008691F3 /* StreamSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamSection.swift; sourceTree = "<group>"; };
4EB1A8C92C9A765800F43898 /* ActiveDevicesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActiveDevicesView.swift; sourceTree = "<group>"; };
4EB1A8CB2C9B1B9700F43898 /* ServerTaskButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerTaskButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1936,8 +1932,6 @@
4EE141632C8B8DCE0045B661 /* Sections */ = {
isa = PBXGroup;
children = (
4EB140472C8E323A008691F3 /* ClientSection.swift */,
4EB140492C8E33B2008691F3 /* ConnectionSection.swift */,
4E63B9F12C8A5BEF00C25378 /* ContentSection.swift */,
4EE141682C8BABDF0045B661 /* ProgressSection.swift */,
);
Expand Down Expand Up @@ -4757,7 +4751,6 @@
E107BB9327880A8F00354E07 /* CollectionItemViewModel.swift in Sources */,
E129428828F0831F00796AC6 /* SplitTimestamp.swift in Sources */,
C46DD8E72A8FA77F0046A504 /* LiveBottomBarView.swift in Sources */,
4EB1404A2C8E33B2008691F3 /* ConnectionSection.swift in Sources */,
E11CEB8D28999B4A003E74C7 /* Font.swift in Sources */,
E139CC1F28EC83E400688DE2 /* Int.swift in Sources */,
E11895A9289383BC0042947B /* ScrollViewOffsetModifier.swift in Sources */,
Expand Down Expand Up @@ -4805,7 +4798,6 @@
E1D3044428D1991900587289 /* LibraryViewTypeToggle.swift in Sources */,
C45C36542A8B1F2C003DAE46 /* LiveVideoPlayerManager.swift in Sources */,
E1CB75822C80F66900217C76 /* VideoPlayerType+Swiftfin.swift in Sources */,
4EB140482C8E323A008691F3 /* ClientSection.swift in Sources */,
E148128B28C15526003B8787 /* ItemSortBy.swift in Sources */,
E10231412BCF8A3C009D71FC /* ChannelLibraryView.swift in Sources */,
E1F0204E26CCCA74001C1C3B /* VideoPlayerJumpLength.swift in Sources */,
Expand Down
Loading

0 comments on commit 120118e

Please sign in to comment.