Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Home - Activity Indicator #1282

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Shared/SwiftfinStore/StoredValue/StoredValues+User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ extension StoredValues.Keys {
)
}

static var activeSessionIndicator: Key<Bool> {
CurrentUserKey(
"activeSessionIndicator",
domain: "activeSessionIndicator",
default: false
)
}

static var customDeviceProfiles: Key<[CustomDeviceProfile]> {
CurrentUserKey(
"customDeviceProfiles",
Expand Down
4 changes: 4 additions & 0 deletions Swiftfin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
4E0253BD2CBF0C06007EB9CD /* DeviceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E12F9152CBE9615006C217E /* DeviceType.swift */; };
4E0A8FFB2CAF74D20014B047 /* TaskCompletionStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E0A8FFA2CAF74CD0014B047 /* TaskCompletionStatus.swift */; };
4E0A8FFC2CAF74D20014B047 /* TaskCompletionStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E0A8FFA2CAF74CD0014B047 /* TaskCompletionStatus.swift */; };
4E10F7F32CC49BDE0032C4B7 /* ActiveSessionsIndicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E10F7F22CC49BCF0032C4B7 /* ActiveSessionsIndicator.swift */; };
4E11805F2CBF52380077A588 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5377CBF8263B596B003A4E83 /* Assets.xcassets */; };
4E12F9172CBE9619006C217E /* DeviceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E12F9152CBE9615006C217E /* DeviceType.swift */; };
4E16FD512C0183DB00110147 /* LetterPickerButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E16FD502C0183DB00110147 /* LetterPickerButton.swift */; };
Expand Down Expand Up @@ -1028,6 +1029,7 @@
/* Begin PBXFileReference section */
091B5A872683142E00D78B61 /* ServerDiscovery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerDiscovery.swift; sourceTree = "<group>"; };
4E0A8FFA2CAF74CD0014B047 /* TaskCompletionStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskCompletionStatus.swift; sourceTree = "<group>"; };
4E10F7F22CC49BCF0032C4B7 /* ActiveSessionsIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActiveSessionsIndicator.swift; sourceTree = "<group>"; };
4E12F9152CBE9615006C217E /* DeviceType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceType.swift; sourceTree = "<group>"; };
4E16FD502C0183DB00110147 /* LetterPickerButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LetterPickerButton.swift; sourceTree = "<group>"; };
4E16FD522C01840C00110147 /* LetterPickerBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LetterPickerBar.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2440,6 +2442,7 @@
53F866422687A45400DCD1D7 /* Components */ = {
isa = PBXGroup;
children = (
4E10F7F22CC49BCF0032C4B7 /* ActiveSessionsIndicator.swift */,
E1D8429429346C6400D1041A /* BasicStepper.swift */,
E133328C2953AE4B00EE76AB /* CircularProgressView.swift */,
E1A3E4CE2BB7E02B005C59F8 /* DelayedProgressView.swift */,
Expand Down Expand Up @@ -4932,6 +4935,7 @@
E13332912953B91000EE76AB /* DownloadTaskCoordinator.swift in Sources */,
E43918662AD5C8310045A18C /* OnScenePhaseChangedModifier.swift in Sources */,
E1579EA72B97DC1500A31CA1 /* Eventful.swift in Sources */,
4E10F7F32CC49BDE0032C4B7 /* ActiveSessionsIndicator.swift in Sources */,
E1B33ED128EB860A0073B0FD /* LargePlaybackButtons.swift in Sources */,
E1549664296CA2EF00C4EF88 /* SwiftfinStore.swift in Sources */,
E102313F2BCF8A3C009D71FC /* DetailedChannelView.swift in Sources */,
Expand Down
176 changes: 176 additions & 0 deletions Swiftfin/Components/ActiveSessionsIndicator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//
// 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 JellyfinAPI
import SwiftUI

struct ActiveSessionIndicator: View {
@ObservedObject
var viewModel = ActiveSessionsViewModel()

let action: () -> Void

// MARK: - View Model Update Timer

private let timer = Timer.publish(every: 60, on: .main, in: .common)
.autoconnect()

// MARK: - Spinner States

@State
private var isSpinning = false
@State
private var showSpinner = false

// MARK: - Session States

var activeSessions: [SessionInfo] {
viewModel.sessions.compactMap(\.value.value).filter {
$0.nowPlayingItem != nil
}
}

// MARK: - Do Active Sessions Exist

var isEnabled: Bool {
activeSessions.isNotEmpty
}

// MARK: - Initializer

init(action: @escaping () -> Void) {
self.action = action
self.viewModel.send(.getSessions)
}

// MARK: - Body

var body: some View {
Button(action: action) {
contentView
.onReceive(timer) { _ in
viewModel.send(.getSessions)
}
}
}

// MARK: - Content View

var contentView: some View {
JPKribs marked this conversation as resolved.
Show resolved Hide resolved
switch viewModel.state {
case .content, .initial:
AnyView(sessionsView)
default:
AnyView(errorView)
}
}

// MARK: - Sessions View

var sessionsView: some View {
HStack(alignment: .bottom) {
if isEnabled {
counterView
.offset(x: 5)
}
ZStack {
imageView
if showSpinner {
loadingSpinner
} else {
idleCircle
}
}
.onChange(of: viewModel.backgroundStates) { newState in
if newState.contains(.gettingSessions) {
showSpinner = true
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
if !viewModel.backgroundStates.contains(.gettingSessions) {
showSpinner = false
}
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
showSpinner = false
}
}
}
}
}

// MARK: - Image View

var imageView: some View {
Image(systemName: "waveform.path.ecg")
.resizable()
.scaledToFit()
.padding(4)
.frame(width: 25, height: 23)
.foregroundColor(.primary)
.background(
Circle()
.fill(isEnabled ? Color.accentColor : .secondary)
)
}

// MARK: - Error View

var errorView: some View {
Image(systemName: "exclamationmark.triangle")
.resizable()
.scaledToFit()
.padding(4)
.frame(width: 25, height: 25)
.foregroundColor(.black)
.background(
Circle()
.fill(.yellow)
)
}

// MARK: - Loading Spinner View

var loadingSpinner: some View {
Circle()
.trim(from: 0.25, to: 0.75)
.stroke(showSpinner ? (isEnabled ? Color.accentColor : .secondary) : Color.clear, lineWidth: 2)
.frame(width: 30, height: 30)
.rotationEffect(
Angle(degrees: isSpinning ? 360 : 0)
)
.animation(
.linear(duration: 1.5)
.repeatForever(autoreverses: false),
value: isSpinning
)
.onAppear {
isSpinning = true
}
.onDisappear {
isSpinning = false
}
}

// MARK: - Spacer Spinner View

var idleCircle: some View {
// This exists to ensure spacing so the image doesn't move when loading happens
Circle()
.stroke(Color.clear, lineWidth: 2)
.frame(width: 30, height: 30)
}

// MARK: - Counter View

var counterView: some View {
Text("\(activeSessions.count)")
.font(.headline)
.padding(0)
.foregroundStyle(isEnabled ? Color.accentColor : .secondary)
}
}
21 changes: 16 additions & 5 deletions Swiftfin/Views/HomeView/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import SwiftUI
// - indicated by snapping to the top
struct HomeView: View {

@StoredValue(.User.activeSessionIndicator)
private var activeSessionIndicator

@Default(.Customization.nextUpPosterType)
private var nextUpPosterType
@Default(.Customization.Home.showRecentlyAdded)
Expand Down Expand Up @@ -87,11 +90,19 @@ struct HomeView: View {
ProgressView()
}

SettingsBarButton(
server: viewModel.userSession.server,
user: viewModel.userSession.user
) {
mainRouter.route(to: \.settings)
HStack(spacing: 0) {
if activeSessionIndicator {
ActiveSessionIndicator {
print("This will take you to the Admin Dashboard once it's on its own coordinator...")
JPKribs marked this conversation as resolved.
Show resolved Hide resolved
}
}

SettingsBarButton(
server: viewModel.userSession.server,
user: viewModel.userSession.user
) {
mainRouter.route(to: \.settings)
}
}
}
.sinceLastDisappear { interval in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
//

import Defaults
import Factory
import SwiftUI

extension CustomizeViewsSettings {

struct HomeSection: View {

@Injected(\.currentUserSession)
private var userSession

@StoredValue(.User.activeSessionIndicator)
private var activeSessionIndicator

@Default(.Customization.Home.showRecentlyAdded)
private var showRecentlyAdded
@Default(.Customization.Home.maxNextUp)
Expand All @@ -23,6 +30,10 @@ extension CustomizeViewsSettings {
var body: some View {
Section(L10n.home) {

if userSession?.user.isAdministrator ?? false {
Toggle("Activity Indicator", isOn: $activeSessionIndicator)
JPKribs marked this conversation as resolved.
Show resolved Hide resolved
}

Toggle(L10n.showRecentlyAdded, isOn: $showRecentlyAdded)

Toggle(L10n.nextUpRewatch, isOn: $resumeNextUp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
//

import Defaults
import Factory
import SwiftUI

// TODO: will be entirely re-organized

struct CustomizeViewsSettings: View {

@Injected(\.currentUserSession)
private var userSesssion

@Default(.Customization.itemViewType)
private var itemViewType
@Default(.Customization.CinematicItemViewType.usePrimaryImage)
Expand Down
Loading