Skip to content

Commit 26f157d

Browse files
committed
[SymplyE] show random book in main page
1 parent 83bb327 commit 26f157d

File tree

4 files changed

+106
-53
lines changed

4 files changed

+106
-53
lines changed

SimplyE/Sources/SimplyE/BookDetail.page.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BookDetailPageAdapter: SCDLatticePageAdapter {
3434
super.show(view: view, data: data)
3535

3636
if let book = data as? Book {
37-
print("show book: \(book.volumeInfo.title)")
37+
//print("show book: \(book.volumeInfo.title)")
3838

3939
self.book = book
4040

@@ -67,4 +67,4 @@ class BookDetailPageAdapter: SCDLatticePageAdapter {
6767
self.navigation?.go(page: "search.page")
6868
}
6969

70-
}
70+
}

SimplyE/Sources/SimplyE/main.page.svg

+21-21
Loading

SimplyE/Sources/SimplyE/main.page.swift

+32-29
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ import Dispatch
77

88
class MainPageAdapter: SCDLatticePageAdapter {
99

10-
public var randomBooks: [Book] = [Book]()
10+
public var randomBooks: [Book] = []
1111

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?
1813

1914
// page adapter initialization
2015
override func load(_ path: String) {
2116
super.load(path)
2217

2318
self.page?.onEnter.append(
24-
SCDWidgetsEnterEventHandler { _ in
25-
self.createRandomAdventuralBook()
19+
SCDWidgetsEnterEventHandler { [weak self] _ in
20+
self?.showRandomBook()
2621
})
22+
2723
self.fetchAdventure()
2824

2925
self.fetchFantasy()
@@ -42,12 +38,6 @@ class MainPageAdapter: SCDLatticePageAdapter {
4238

4339
//guard let selected = searchPage.selectedBook else {return}
4440

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-
5141
self.ctrlListBookCatalog.elementProvider { (genre: Genre, element) in
5242
guard let viewCategory = element["viewCategory", as: SCDWidgetsRowView.self],
5343
let rowView = element["rowView", as: SCDWidgetsRowView.self],
@@ -107,47 +97,60 @@ class MainPageAdapter: SCDLatticePageAdapter {
10797

10898
}
10999

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+
110122
private func fetchAdventure() {
111123
CatalogManager.shared.fetchGenre(with: "Adventure", lbCategory: "Adventure") {
112124
[weak self] adventure in
113125
self?.ctrlListBookCatalog.items.append(adventure)
126+
self?.addRandomBooks(adventure.books)
114127
}
115128
}
116129

117130
private func fetchFantasy() {
118131
CatalogManager.shared.fetchGenre(with: "Fantasy", lbCategory: "Fantasy") {
119132
[weak self] fantasy in
120133
self?.ctrlListBookCatalog.items.append(fantasy)
134+
self?.addRandomBooks(fantasy.books)
121135
}
122136
}
123137

124138
private func fetchHorror() {
125139
CatalogManager.shared.fetchGenre(with: "Horror", lbCategory: "Horror") { [weak self] horror in
126140
self?.ctrlListBookCatalog.items.append(horror)
141+
self?.addRandomBooks(horror.books)
127142
}
128143
}
129144

130145
private func fetchHealth() {
131146
CatalogManager.shared.fetchGenre(with: "Health", lbCategory: "Health") { [weak self] health in
132147
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)
146149
}
147150
}
148151

149152
func goToPage() {
150153
self.navigation?.go(page: "search.page")
151154
}
152155

153-
}
156+
}

0 commit comments

Comments
 (0)