@@ -7,23 +7,19 @@ import Dispatch
7
7
8
8
class MainPageAdapter : SCDLatticePageAdapter {
9
9
10
- public var randomBooks : [ Book ] = [ Book ] ( )
10
+ public var randomBooks : [ Book ] = [ ]
11
11
12
- public let selectedBook : Book ?
13
-
14
- override init ( ) {
15
- // Getting the selectedBook from randomBooks for generating a random image link for heroImage in the main.page
16
- self . selectedBook = randomBooks. randomElement ( )
17
- }
12
+ public var selectedBook : Book ?
18
13
19
14
// page adapter initialization
20
15
override func load( _ path: String ) {
21
16
super. load ( path)
22
17
23
18
self . page? . onEnter. append (
24
- SCDWidgetsEnterEventHandler { _ in
25
- self . createRandomAdventuralBook ( )
19
+ SCDWidgetsEnterEventHandler { [ weak self ] _ in
20
+ self ? . showRandomBook ( )
26
21
} )
22
+
27
23
self . fetchAdventure ( )
28
24
29
25
self . fetchFantasy ( )
@@ -42,12 +38,6 @@ class MainPageAdapter: SCDLatticePageAdapter {
42
38
43
39
//guard let selected = searchPage.selectedBook else {return}
44
40
45
- CatalogManager . loadDataAsync (
46
- from: selectedBook? . volumeInfo. imageLinks. thumbnail ?? " no image " , queue: . main
47
- ) { [ weak self] data in
48
- self ? . heroImage. content = data
49
- }
50
-
51
41
self . ctrlListBookCatalog. elementProvider { ( genre: Genre , element) in
52
42
guard let viewCategory = element [ " viewCategory " , as: SCDWidgetsRowView . self] ,
53
43
let rowView = element [ " rowView " , as: SCDWidgetsRowView . self] ,
@@ -107,47 +97,60 @@ class MainPageAdapter: SCDLatticePageAdapter {
107
97
108
98
}
109
99
100
+ private func addRandomBooks( _ books: [ Book ] ) {
101
+ self . randomBooks. append ( contentsOf: books)
102
+ showRandomBook ( )
103
+ }
104
+
105
+ private func showRandomBook( ) {
106
+ if selectedBook != nil {
107
+ CatalogManager . loadDataAsync (
108
+ from: selectedBook? . volumeInfo. imageLinks. thumbnail ?? " no image " , queue: . main
109
+ ) { [ weak self] data in
110
+ guard let self = self else { return }
111
+
112
+ self . heroImage. content = data
113
+ self . heroImage. contentPriority = true
114
+ }
115
+ }
116
+
117
+ DispatchQueue . main. async {
118
+ self . selectedBook = self . randomBooks. randomElement ( )
119
+ }
120
+ }
121
+
110
122
private func fetchAdventure( ) {
111
123
CatalogManager . shared. fetchGenre ( with: " Adventure " , lbCategory: " Adventure " ) {
112
124
[ weak self] adventure in
113
125
self ? . ctrlListBookCatalog. items. append ( adventure)
126
+ self ? . addRandomBooks ( adventure. books)
114
127
}
115
128
}
116
129
117
130
private func fetchFantasy( ) {
118
131
CatalogManager . shared. fetchGenre ( with: " Fantasy " , lbCategory: " Fantasy " ) {
119
132
[ weak self] fantasy in
120
133
self ? . ctrlListBookCatalog. items. append ( fantasy)
134
+ self ? . addRandomBooks ( fantasy. books)
121
135
}
122
136
}
123
137
124
138
private func fetchHorror( ) {
125
139
CatalogManager . shared. fetchGenre ( with: " Horror " , lbCategory: " Horror " ) { [ weak self] horror in
126
140
self ? . ctrlListBookCatalog. items. append ( horror)
141
+ self ? . addRandomBooks ( horror. books)
127
142
}
128
143
}
129
144
130
145
private func fetchHealth( ) {
131
146
CatalogManager . shared. fetchGenre ( with: " Health " , lbCategory: " Health " ) { [ weak self] health in
132
147
self ? . ctrlListBookCatalog. items. append ( health)
133
- }
134
- }
135
-
136
- private func createRandomAdventuralBook( ) {
137
- APICaller . shared. getAdventurousBooks { [ weak self] result in
138
- switch result {
139
- case . success( let createRandomAdventural) :
140
- DispatchQueue . main. async {
141
- self ? . randomBooks. append ( contentsOf: createRandomAdventural)
142
- }
143
- case . failure( let error) :
144
- print ( error. localizedDescription)
145
- }
148
+ self ? . addRandomBooks ( health. books)
146
149
}
147
150
}
148
151
149
152
func goToPage( ) {
150
153
self . navigation? . go ( page: " search.page " )
151
154
}
152
155
153
- }
156
+ }
0 commit comments