Skip to content

Commit 1ab99a6

Browse files
authored
Merge pull request #393 from LePips/improve-image-views
2 parents e72bae1 + 0d69ae5 commit 1ab99a6

File tree

49 files changed

+320
-152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+320
-152
lines changed

Shared/Coordinators/MainCoordinator/iOSMainCoordinator.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ final class MainCoordinator: NavigationCoordinatable {
4545
barAppearance.tintColor = UIColor(Color.jellyfinPurple)
4646

4747
// Notification setup for state
48-
let nc = SwiftfinNotificationCenter.main
49-
nc.addObserver(self, selector: #selector(didLogIn), name: SwiftfinNotificationCenter.Keys.didSignIn, object: nil)
50-
nc.addObserver(self, selector: #selector(didLogOut), name: SwiftfinNotificationCenter.Keys.didSignOut, object: nil)
51-
nc.addObserver(self, selector: #selector(processDeepLink), name: SwiftfinNotificationCenter.Keys.processDeepLink, object: nil)
52-
nc.addObserver(self, selector: #selector(didChangeServerCurrentURI),
53-
name: SwiftfinNotificationCenter.Keys.didChangeServerCurrentURI, object: nil)
48+
Notifications[.didSignIn].subscribe(self, selector: #selector(didSignIn))
49+
Notifications[.didSignOut].subscribe(self, selector: #selector(didSignOut))
50+
Notifications[.processDeepLink].subscribe(self, selector: #selector(processDeepLink(_:)))
51+
Notifications[.didChangeServerCurrentURI].subscribe(self, selector: #selector(didChangeServerCurrentURI(_:)))
5452

5553
Defaults.publisher(.appAppearance)
5654
.sink { _ in
@@ -60,13 +58,13 @@ final class MainCoordinator: NavigationCoordinatable {
6058
}
6159

6260
@objc
63-
func didLogIn() {
61+
func didSignIn() {
6462
LogManager.shared.log.info("Received `didSignIn` from SwiftfinNotificationCenter.")
6563
root(\.mainTab)
6664
}
6765

6866
@objc
69-
func didLogOut() {
67+
func didSignOut() {
7068
LogManager.shared.log.info("Received `didSignOut` from SwiftfinNotificationCenter.")
7169
root(\.serverList)
7270
}

Shared/Coordinators/MainCoordinator/tvOSMainCoordinator.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,18 @@ final class MainCoordinator: NavigationCoordinatable {
4040
DataLoader.sharedUrlCache.diskCapacity = 1000 * 1024 * 1024 // 1000MB disk
4141

4242
// Notification setup for state
43-
let nc = SwiftfinNotificationCenter.main
44-
nc.addObserver(self, selector: #selector(didLogIn), name: SwiftfinNotificationCenter.Keys.didSignIn, object: nil)
45-
nc.addObserver(self, selector: #selector(didLogOut), name: SwiftfinNotificationCenter.Keys.didSignOut, object: nil)
43+
Notifications[.didSignIn].subscribe(self, selector: #selector(didSignIn))
44+
Notifications[.didSignOut].subscribe(self, selector: #selector(didSignOut))
4645
}
4746

4847
@objc
49-
func didLogIn() {
48+
func didSignIn() {
5049
LogManager.shared.log.info("Received `didSignIn` from NSNotificationCenter.")
5150
root(\.mainTab)
5251
}
5352

5453
@objc
55-
func didLogOut() {
54+
func didSignOut() {
5655
LogManager.shared.log.info("Received `didSignOut` from NSNotificationCenter.")
5756
root(\.serverList)
5857
}

Shared/Singleton/SessionManager.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ final class SessionManager {
240240
Defaults[.lastServerUserID] = user.id
241241

242242
currentLogin = (server: currentServer.state, user: currentUser.state)
243-
SwiftfinNotificationCenter.main.post(name: SwiftfinNotificationCenter.Keys.didSignIn, object: nil)
243+
Notifications[.didSignIn].post()
244244
})
245245
.map { _, user, _ in
246246
user.state
@@ -255,7 +255,7 @@ final class SessionManager {
255255
Defaults[.lastServerUserID] = user.id
256256
setAuthHeader(with: user.accessToken)
257257
currentLogin = (server: server, user: user)
258-
SwiftfinNotificationCenter.main.post(name: SwiftfinNotificationCenter.Keys.didSignIn, object: nil)
258+
Notifications[.didSignIn].post()
259259
}
260260

261261
// MARK: logout
@@ -265,7 +265,7 @@ final class SessionManager {
265265
JellyfinAPI.basePath = ""
266266
setAuthHeader(with: "")
267267
Defaults[.lastServerUserID] = nil
268-
SwiftfinNotificationCenter.main.post(name: SwiftfinNotificationCenter.Keys.didSignOut, object: nil)
268+
Notifications[.didSignOut].post()
269269
}
270270

271271
// MARK: purge
@@ -278,7 +278,7 @@ final class SessionManager {
278278
delete(server: server)
279279
}
280280

281-
SwiftfinNotificationCenter.main.post(name: SwiftfinNotificationCenter.Keys.didPurge, object: nil)
281+
Notifications[.didPurge].post()
282282
}
283283

284284
// MARK: delete user

Shared/Singleton/SwiftfinNotificationCenter.swift

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,63 @@
88

99
import Foundation
1010

11-
enum SwiftfinNotificationCenter {
11+
class SwiftfinNotification {
12+
13+
private let notificationName: Notification.Name
14+
15+
fileprivate init(_ notificationName: Notification.Name) {
16+
self.notificationName = notificationName
17+
}
18+
19+
func post(object: Any? = nil) {
20+
Notifications.main.post(name: notificationName, object: object)
21+
}
22+
23+
func subscribe(_ observer: Any, selector: Selector) {
24+
Notifications.main.addObserver(observer, selector: selector, name: notificationName, object: nil)
25+
}
26+
27+
func unsubscribe(_ observer: Any) {
28+
Notifications.main.removeObserver(self, name: notificationName, object: nil)
29+
}
30+
}
31+
32+
enum Notifications {
1233

1334
static let main: NotificationCenter = {
1435
NotificationCenter()
1536
}()
1637

17-
enum Keys {
18-
static let didSignIn = Notification.Name("didSignIn")
19-
static let didSignOut = Notification.Name("didSignOut")
20-
static let processDeepLink = Notification.Name("processDeepLink")
21-
static let didPurge = Notification.Name("didPurge")
22-
static let didChangeServerCurrentURI = Notification.Name("didChangeCurrentLoginURI")
38+
final class Key {
39+
public typealias NotificationKey = Notifications.Key
40+
41+
public let key: String
42+
public let underlyingNotification: SwiftfinNotification
2343

24-
// Send with an item id to check if current item for item views
25-
static let didSendStopReport = Notification.Name("didSendStopReport")
44+
public init(_ key: String) {
45+
self.key = key
46+
self.underlyingNotification = SwiftfinNotification(Notification.Name(key))
47+
}
2648
}
49+
50+
static subscript(key: Key) -> SwiftfinNotification {
51+
key.underlyingNotification
52+
}
53+
54+
static func unsubscribe(_ observer: Any) {
55+
main.removeObserver(observer)
56+
}
57+
}
58+
59+
extension Notifications.Key {
60+
61+
static let didSignIn = NotificationKey("didSignIn")
62+
static let didSignOut = NotificationKey("didSignOut")
63+
static let processDeepLink = NotificationKey("processDeepLink")
64+
static let didPurge = NotificationKey("didPurge")
65+
static let didChangeServerCurrentURI = NotificationKey("didChangeCurrentLoginURI")
66+
static let toggleOfflineMode = NotificationKey("toggleOfflineMode")
67+
static let didDeleteOfflineItem = NotificationKey("didDeleteOfflineItem")
68+
static let didAddDownload = NotificationKey("didAddDownload")
69+
static let didSendStopReport = NotificationKey("didSendStopReport")
2770
}

Shared/ViewModels/HomeViewModel.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ final class HomeViewModel: ViewModel {
3434
// Nov. 6, 2021
3535
// This is a workaround since Stinsen doesn't have the ability to rebuild a root at the time of writing.
3636
// See ServerDetailViewModel.swift for feature request issue
37-
let nc = SwiftfinNotificationCenter.main
38-
nc.addObserver(self, selector: #selector(didSignIn), name: SwiftfinNotificationCenter.Keys.didSignIn, object: nil)
39-
nc.addObserver(self, selector: #selector(didSignOut), name: SwiftfinNotificationCenter.Keys.didSignOut, object: nil)
37+
Notifications[.didSignIn].subscribe(self, selector: #selector(didSignIn))
38+
Notifications[.didSignOut].subscribe(self, selector: #selector(didSignOut))
4039
}
4140

4241
@objc

Shared/ViewModels/ItemViewModel/ItemViewModel.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ class ItemViewModel: ViewModel {
5555

5656
getSimilarItems()
5757

58-
SwiftfinNotificationCenter.main.addObserver(self,
59-
selector: #selector(receivedStopReport(_:)),
60-
name: SwiftfinNotificationCenter.Keys.didSendStopReport,
61-
object: nil)
58+
Notifications[.didSendStopReport].subscribe(self, selector: #selector(receivedStopReport(_:)))
6259

6360
refreshItemVideoPlayerViewModel(for: item)
6461
}
@@ -72,7 +69,7 @@ class ItemViewModel: ViewModel {
7269
} else {
7370
// Remove if necessary. Note that this cannot be in deinit as
7471
// holding as an observer won't allow the object to be deinit-ed
75-
SwiftfinNotificationCenter.main.removeObserver(self)
72+
Notifications.unsubscribe(self)
7673
}
7774
}
7875

Shared/ViewModels/ServerDetailViewModel.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class ServerDetailViewModel: ViewModel {
2525
} receiveValue: { newServerState in
2626
self.server = newServerState
2727

28-
let nc = SwiftfinNotificationCenter.main
29-
nc.post(name: SwiftfinNotificationCenter.Keys.didChangeServerCurrentURI, object: newServerState)
28+
Notifications[.didChangeServerCurrentURI].post(object: newServerState)
3029
}
3130
.store(in: &cancellables)
3231
}

Shared/ViewModels/ServerListViewModel.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class ServerListViewModel: ObservableObject {
2020
// This is a workaround since Stinsen doesn't have the ability to rebuild a root at the time of writing.
2121
// Feature request issue: https://github.com/rundfunk47/stinsen/issues/33
2222
// Go to each MainCoordinator and implement the rebuild of the root when receiving the notification
23-
let nc = SwiftfinNotificationCenter.main
24-
nc.addObserver(self, selector: #selector(didPurge), name: SwiftfinNotificationCenter.Keys.didPurge, object: nil)
23+
Notifications[.didPurge].subscribe(self, selector: #selector(didPurge))
2524
}
2625

2726
func fetchServers() {

Shared/ViewModels/UserListViewModel.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class UserListViewModel: ViewModel {
2121

2222
super.init()
2323

24-
let nc = SwiftfinNotificationCenter.main
25-
nc.addObserver(self, selector: #selector(didChangeCurrentLoginURI), name: SwiftfinNotificationCenter.Keys.didChangeServerCurrentURI,
26-
object: nil)
24+
Notifications[.didChangeServerCurrentURI].subscribe(self, selector: #selector(didChangeCurrentLoginURI(_:)))
2725
}
2826

2927
@objc

Shared/ViewModels/VideoPlayerViewModel/VideoPlayerViewModel.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,7 @@ extension VideoPlayerViewModel {
585585
self.handleAPIRequestError(completion: completion)
586586
} receiveValue: { _ in
587587
LogManager.shared.log.debug("Stop report sent for item: \(self.item.id ?? "No ID")")
588-
SwiftfinNotificationCenter.main.post(name: SwiftfinNotificationCenter.Keys.didSendStopReport,
589-
object: self.item.id)
588+
Notifications[.didSendStopReport].post(object: self.item.id)
590589
}
591590
.store(in: &cancellables)
592591
}

0 commit comments

Comments
 (0)