@@ -19,15 +19,19 @@ class JellyfinCoordinator: Coordinator, AlertPresenter {
19
19
private let jellyfinConnectionService : JellyfinConnectionService
20
20
private var disposeBag = Set < AnyCancellable > ( )
21
21
private var dismissing = false
22
-
23
- init ( flow: BPCoordinatorPresentationFlow , singleFileDownloadService: SingleFileDownloadService , jellyfinConnectionService: JellyfinConnectionService ) {
22
+
23
+ init (
24
+ flow: BPCoordinatorPresentationFlow ,
25
+ singleFileDownloadService: SingleFileDownloadService ,
26
+ jellyfinConnectionService: JellyfinConnectionService
27
+ ) {
24
28
self . flow = flow
25
29
self . singleFileDownloadService = singleFileDownloadService
26
30
self . jellyfinConnectionService = jellyfinConnectionService
27
-
31
+
28
32
bindObservers ( )
29
33
}
30
-
34
+
31
35
func bindObservers( ) {
32
36
singleFileDownloadService. eventsPublisher. sink { [ weak self] event in
33
37
switch event {
@@ -46,24 +50,31 @@ class JellyfinCoordinator: Coordinator, AlertPresenter {
46
50
}
47
51
. store ( in: & disposeBag)
48
52
}
49
-
53
+
50
54
func start( ) {
51
55
let connectionVC = createJellyfinLoginScreen ( )
52
56
flow. startPresentation ( connectionVC, animated: true )
53
-
57
+
54
58
tryShowLibraryView ( )
55
59
}
56
60
57
61
private func createJellyfinLoginScreen( ) -> UIViewController {
58
62
let viewModel = JellyfinConnectionViewModel ( jellyfinConnectionService: jellyfinConnectionService)
59
-
63
+
60
64
viewModel. coordinator = self
61
- viewModel. onTransition = { [ viewModel ] route in
65
+ viewModel. onTransition = { route in
62
66
switch route {
63
67
case . cancel:
64
- viewModel. dismiss ( )
65
- case . signInFinished( let url, let userID, let accessToken) :
66
- self . handleSignInFinished ( url: url, userID: userID, accessToken: accessToken, connectionViewModel: viewModel)
68
+ self . flow. finishPresentation ( animated: true )
69
+ case . signInFinished( let url, let userID, let userName, let accessToken, let serverName, let saveToKeychain) :
70
+ self . handleSignInFinished (
71
+ url: url,
72
+ userID: userID,
73
+ userName: userName,
74
+ accessToken: accessToken,
75
+ serverName: serverName,
76
+ saveToKeychain: saveToKeychain
77
+ )
67
78
case . signOut:
68
79
self . jellyfinConnectionService. deleteConnection ( )
69
80
case . showLibrary:
@@ -72,16 +83,22 @@ class JellyfinCoordinator: Coordinator, AlertPresenter {
72
83
self . showAlert ( content)
73
84
}
74
85
}
75
-
86
+
76
87
let vc = UIHostingController ( rootView: JellyfinConnectionView ( viewModel: viewModel) )
77
88
return vc
78
89
}
79
-
80
- private func createJellyfinLibraryScreen( withLibraryName libraryName: String , userID: String , client: JellyfinClient ) -> UIViewController {
81
- let viewModel = JellyfinLibraryViewModel ( data: . topLevel( libraryName: libraryName, userID: userID) ,
82
- apiClient: client,
83
- singleFileDownloadService: singleFileDownloadService)
84
-
90
+
91
+ private func createJellyfinLibraryScreen(
92
+ withLibraryName libraryName: String ,
93
+ userID: String ,
94
+ client: JellyfinClient
95
+ ) -> UIViewController {
96
+ let viewModel = JellyfinLibraryViewModel (
97
+ data: . topLevel( libraryName: libraryName, userID: userID) ,
98
+ apiClient: client,
99
+ singleFileDownloadService: singleFileDownloadService
100
+ )
101
+
85
102
viewModel. onTransition = { route in
86
103
switch route {
87
104
case . done:
@@ -95,26 +112,38 @@ class JellyfinCoordinator: Coordinator, AlertPresenter {
95
112
return vc
96
113
}
97
114
98
- private func handleSignInFinished( url: URL , userID: String , accessToken: String , connectionViewModel viewModel: JellyfinConnectionViewModel ) {
99
- let connectionData = JellyfinConnectionData ( url: url,
100
- serverName: viewModel. form. serverName ?? " " ,
101
- userID: userID,
102
- userName: viewModel. form. username,
103
- accessToken: accessToken)
104
- jellyfinConnectionService. setConnection ( connectionData, saveToKeychain: viewModel. form. rememberMe)
105
-
115
+ private func handleSignInFinished(
116
+ url: URL ,
117
+ userID: String ,
118
+ userName: String ,
119
+ accessToken: String ,
120
+ serverName: String ,
121
+ saveToKeychain: Bool
122
+ ) {
123
+ let connectionData = JellyfinConnectionData (
124
+ url: url,
125
+ serverName: serverName,
126
+ userID: userID,
127
+ userName: userName,
128
+ accessToken: accessToken
129
+ )
130
+ jellyfinConnectionService. setConnection ( connectionData, saveToKeychain: saveToKeychain)
131
+
106
132
self . tryShowLibraryView ( )
107
133
}
108
134
109
135
private func tryShowLibraryView( ) {
110
136
guard let connectionData = jellyfinConnectionService. connection,
111
- let apiClient = jellyfinConnectionService. createClient ( ) else {
137
+ let apiClient = jellyfinConnectionService. createClient ( )
138
+ else {
112
139
return
113
140
}
114
-
115
- let libraryVC = self . createJellyfinLibraryScreen ( withLibraryName: connectionData. serverName,
116
- userID: connectionData. userID,
117
- client: apiClient)
141
+
142
+ let libraryVC = self . createJellyfinLibraryScreen (
143
+ withLibraryName: connectionData. serverName,
144
+ userID: connectionData. userID,
145
+ client: apiClient
146
+ )
118
147
self . flow. pushViewController ( libraryVC, animated: true )
119
148
}
120
149
}
0 commit comments