Skip to content

Commit

Permalink
fix: auto load next page
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuz0u committed May 15, 2021
1 parent 39f199a commit adaadcf
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 37 deletions.
19 changes: 10 additions & 9 deletions EhPanda/App/Tools/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -744,16 +744,17 @@ extension Parser {
var current = 0
var maximum = 0

guard let link = doc.at_xpath("//table [@class='ptt']") else { return PageNumber() }
if let currentStr = link.at_xpath("//td [@class='ptds']")?.text {
if let range = currentStr.range(of: "-") {
current = (Int(String(currentStr.suffix(from: range.upperBound))) ?? 1) - 1
} else {
current = (Int(currentStr) ?? 1) - 1
}
guard let link = doc.at_xpath("//table [@class='ptt']"),
let currentStr = link.at_xpath("//td [@class='ptds']")?.text
else { return PageNumber() }

if let range = currentStr.range(of: "-") {
current = (Int(String(currentStr.suffix(from: range.upperBound))) ?? 1) - 1
} else {
current = (Int(currentStr) ?? 1) - 1
}
for ptbLink in link.xpath("//a") {
if let num = Int(ptbLink.text ?? "") {
for aLink in link.xpath("//a") {
if let num = Int(aLink.text ?? "") {
maximum = num
}
}
Expand Down
2 changes: 1 addition & 1 deletion EhPanda/DataFlow/AppAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ enum AppAction {
case fetchMangaItemReverse(detailURL: String)
case fetchMangaItemReverseDone(result: Result<Manga, AppError>)
case fetchSearchItems(keyword: String)
case fetchSearchItemsDone(result: Result<(PageNumber, [Manga]), AppError>)
case fetchSearchItemsDone(result: Result<(Keyword, PageNumber, [Manga]), AppError>)
case fetchMoreSearchItems(keyword: String)
case fetchMoreSearchItemsDone(result: Result<(Keyword, PageNumber, [Manga]), AppError>)
case fetchFrontpageItems
Expand Down
8 changes: 7 additions & 1 deletion EhPanda/DataFlow/AppCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ struct FetchSearchItemsCommand: AppCommand {
}
token.unseal()
} receiveValue: { mangas in
store.dispatch(.fetchSearchItemsDone(result: .success(mangas)))
store.dispatch(
.fetchSearchItemsDone(
result: .success(
(keyword, mangas.0, mangas.1)
)
)
)
}
.seal(in: token)
}
Expand Down
94 changes: 68 additions & 26 deletions EhPanda/DataFlow/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ final class Store: ObservableObject {
appState.settings.insertGreeting(greeting: greeting)
case .failure(let error):
if error == .parseFailed {
appState.settings.insertGreeting(greeting: Greeting())
var greeting = Greeting()
greeting.updateTime = Date()
appState.settings.insertGreeting(greeting: greeting)
}
print(error)
}
Expand Down Expand Up @@ -189,26 +191,32 @@ final class Store: ObservableObject {

switch result {
case .success(let mangas):
appState.homeInfo.searchCurrentPageNum = mangas.0.current
appState.homeInfo.searchPageNumMaximum = mangas.0.maximum
appState.homeInfo.searchCurrentPageNum = mangas.1.current
appState.homeInfo.searchPageNumMaximum = mangas.1.maximum

if mangas.1.isEmpty {
appState.homeInfo.searchNotFound = true
appState.homeInfo.searchItems = mangas.2
if mangas.2.isEmpty {
if mangas.1.current < mangas.1.maximum {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreSearchItems(keyword: mangas.0))
}
} else {
appState.homeInfo.searchNotFound = true
}
} else {
appState.homeInfo.searchItems = mangas.1
appState.cachedList.cache(mangas: mangas.1)
appState.cachedList.cache(mangas: mangas.2)
}
case .failure(let error):
print(error)
appState.homeInfo.searchLoadFailed = true
print(error)
}

case .fetchMoreSearchItems(let keyword):
appState.homeInfo.moreSearchLoadFailed = false

let currentNum = appState.homeInfo.searchCurrentPageNum
let maximumNum = appState.homeInfo.searchPageNumMaximum
if currentNum + 1 >= maximumNum { break }
if currentNum + 1 > maximumNum { break }

if appState.homeInfo.moreSearchLoading { break }
appState.homeInfo.moreSearchLoading = true
Expand Down Expand Up @@ -237,6 +245,8 @@ final class Store: ObservableObject {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreSearchItems(keyword: mangas.0))
}
} else if appState.homeInfo.searchItems?.isEmpty == true {
appState.homeInfo.searchNotFound = true
}
case .failure(let error):
appState.homeInfo.moreSearchLoadFailed = true
Expand All @@ -260,10 +270,16 @@ final class Store: ObservableObject {
appState.homeInfo.frontpageCurrentPageNum = mangas.0.current
appState.homeInfo.frontpagePageNumMaximum = mangas.0.maximum

appState.homeInfo.frontpageItems = mangas.1
if mangas.1.isEmpty {
appState.homeInfo.frontpageNotFound = true
if mangas.0.current < mangas.0.maximum {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreFrontpageItems)
}
} else {
appState.homeInfo.frontpageNotFound = true
}
} else {
appState.homeInfo.frontpageItems = mangas.1
appState.cachedList.cache(mangas: mangas.1)
}
case .failure(let error):
Expand All @@ -277,7 +293,7 @@ final class Store: ObservableObject {
if !didLogin || !isTokenMatched { break }
let currentNum = appState.homeInfo.frontpageCurrentPageNum
let maximumNum = appState.homeInfo.frontpagePageNumMaximum
if currentNum + 1 >= maximumNum { break }
if currentNum + 1 > maximumNum { break }

if appState.homeInfo.moreFrontpageLoading { break }
appState.homeInfo.moreFrontpageLoading = true
Expand All @@ -300,6 +316,8 @@ final class Store: ObservableObject {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreFrontpageItems)
}
} else if appState.homeInfo.frontpageItems?.isEmpty == true {
appState.homeInfo.frontpageNotFound = true
}
case .failure(let error):
appState.homeInfo.moreFrontpageLoadFailed = true
Expand Down Expand Up @@ -347,10 +365,16 @@ final class Store: ObservableObject {
appState.homeInfo.watchedCurrentPageNum = mangas.0.current
appState.homeInfo.watchedPageNumMaximum = mangas.0.maximum

appState.homeInfo.watchedItems = mangas.1
if mangas.1.isEmpty {
appState.homeInfo.watchedNotFound = true
if mangas.0.current < mangas.0.maximum {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreWatchedItems)
}
} else {
appState.homeInfo.watchedNotFound = true
}
} else {
appState.homeInfo.watchedItems = mangas.1
appState.cachedList.cache(mangas: mangas.1)
}
case .failure(let error):
Expand All @@ -363,7 +387,7 @@ final class Store: ObservableObject {

let currentNum = appState.homeInfo.watchedCurrentPageNum
let maximumNum = appState.homeInfo.watchedPageNumMaximum
if currentNum + 1 >= maximumNum { break }
if currentNum + 1 > maximumNum { break }

if appState.homeInfo.moreWatchedLoading { break }
appState.homeInfo.moreWatchedLoading = true
Expand All @@ -386,6 +410,8 @@ final class Store: ObservableObject {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreWatchedItems)
}
} else if appState.homeInfo.watchedItems?.isEmpty == true {
appState.homeInfo.watchedNotFound = true
}
case .failure(let error):
appState.homeInfo.moreWatchedLoadFailed = true
Expand All @@ -409,10 +435,16 @@ final class Store: ObservableObject {
appState.homeInfo.favoritesCurrentPageNum[carriedValue] = mangas.0.current
appState.homeInfo.favoritesPageNumMaximum[carriedValue] = mangas.0.maximum

appState.homeInfo.favoritesItems[carriedValue] = mangas.1
if mangas.1.isEmpty {
appState.homeInfo.favoritesNotFound[carriedValue] = true
if mangas.0.current < mangas.0.maximum {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreFavoritesItems(index: carriedValue))
}
} else {
appState.homeInfo.favoritesNotFound[carriedValue] = true
}
} else {
appState.homeInfo.favoritesItems[carriedValue] = mangas.1
appState.cachedList.cache(mangas: mangas.1)
}
case .failure(let error):
Expand Down Expand Up @@ -452,6 +484,8 @@ final class Store: ObservableObject {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreFavoritesItems(index: carriedValue))
}
} else if appState.homeInfo.favoritesItems[carriedValue]?.isEmpty == true {
appState.homeInfo.favoritesNotFound[carriedValue] = true
}
case .failure(let error):
appState.homeInfo.moreFavoritesLoading[carriedValue] = true
Expand Down Expand Up @@ -562,15 +596,21 @@ final class Store: ObservableObject {

switch result {
case .success(let mangas):
appState.detailInfo.replaceAssociatedItems(
depth: mangas.0,
keyword: mangas.1,
pageNum: mangas.2,
items: mangas.3
)
if mangas.3.isEmpty {
appState.detailInfo.associatedItemsNotFound = true
if mangas.2.current < mangas.2.maximum {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreAssociatedItems(depth: mangas.0, keyword: mangas.1))
}
} else {
appState.detailInfo.associatedItemsNotFound = true
}
} else {
appState.detailInfo.replaceAssociatedItems(
depth: mangas.0,
keyword: mangas.1,
pageNum: mangas.2,
items: mangas.3
)
appState.cachedList.cache(mangas: mangas.3)
}
case .failure(let error):
Expand All @@ -584,7 +624,7 @@ final class Store: ObservableObject {
guard appState.detailInfo.associatedItems.count >= depth + 1 else { break }
let currentNum = appState.detailInfo.associatedItems[depth].pageNum.current
let maximumNum = appState.detailInfo.associatedItems[depth].pageNum.maximum
if currentNum + 1 >= maximumNum { break }
if currentNum + 1 > maximumNum { break }

if appState.detailInfo.moreAssociatedItemsLoading { break }
appState.detailInfo.moreAssociatedItemsLoading = true
Expand Down Expand Up @@ -614,6 +654,8 @@ final class Store: ObservableObject {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.dispatch(.fetchMoreAssociatedItems(depth: mangas.0, keyword: mangas.1))
}
} else if appState.detailInfo.associatedItems.isEmpty {
appState.detailInfo.associatedItemsNotFound = true
}
case .failure(let error):
appState.detailInfo.moreAssociatedItemsLoadFailed = true
Expand Down Expand Up @@ -703,7 +745,7 @@ final class Store: ObservableObject {

let currentNum = detail.currentPageNum
let maximumNum = detail.pageNumMaximum
if currentNum + 1 >= maximumNum { break }
if currentNum + 1 > maximumNum { break }

if appState.contentInfo.moreMangaContentsLoading { break }
appState.contentInfo.moreMangaContentsLoading = true
Expand Down

0 comments on commit adaadcf

Please sign in to comment.