Skip to content

Commit

Permalink
feat: Searchable support & Remove login restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuz0u committed Jun 11, 2021
1 parent f2c9b32 commit 6006d0e
Show file tree
Hide file tree
Showing 25 changed files with 293 additions and 337 deletions.
19 changes: 3 additions & 16 deletions EhPanda/App/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,13 @@ struct Defaults {
struct URL {
// Domains
static var host: String {
if isTokenMatched {
return galleryType == .ehentai ? ehentai : exhentai
} else {
return durarara
}
galleryType == .ehentai ? ehentai : exhentai
}
static let ehentai = "https://e-hentai.org/"
static let exhentai = "https://exhentai.org/"
static let forum = "https://forums.e-hentai.org/"
static let login = merge([forum + index, loginAct])
static let magnet = "magnet:?xt=urn:btih:"
static let durarara = merge([ehentai, listCompact, nonh, fSearch + "parody:durarara$"])

// Functional Pages
static let tag = "tag/"
Expand Down Expand Up @@ -152,21 +147,13 @@ extension Defaults.URL {
)
}
static func frontpageList() -> String {
if isTokenMatched {
return merge([host, listCompact])
} else {
return durarara
}
merge([host, listCompact])
}
static func moreFrontpageList(pageNum: String, lastID: String) -> String {
merge([host, listCompact, page + pageNum, from + lastID])
}
static func popularList() -> String {
if isTokenMatched {
return merge([host + popular, listCompact])
} else {
return merge([ehentai, listCompact, nonh, fSearch + "parody:gintama$"])
}
merge([host + popular, listCompact])
}
static func watchedList() -> String {
merge([host + watched, listCompact])
Expand Down
4 changes: 0 additions & 4 deletions EhPanda/App/EhPandaApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ struct EhPandaApp: App {
init() {
configureWebImage()
clearImageCachesIfNeeded()
UIScrollView.appearance()
.keyboardDismissMode = .onDrag
UITableViewCell.appearance()
.selectedBackgroundView = UIView()
}
Expand Down Expand Up @@ -46,8 +44,6 @@ private extension EhPandaApp {

func onOpenURL(_ url: URL) {
switch url.host {
case "token":
setToken(with: url.pathComponents.last)
case "debugMode":
setDebugMode(with: url.pathComponents.last == "on")
default:
Expand Down
2 changes: 1 addition & 1 deletion EhPanda/App/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extension String {
func safeURL() -> URL {
isValidURL
? URL(string: self)!
: URL(string: Defaults.URL.durarara)!
: URL(string: Defaults.URL.ehentai)!
}

var isValidURL: Bool {
Expand Down
22 changes: 18 additions & 4 deletions EhPanda/App/Tools/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,14 @@ struct Parser {
}

// MARK: Content
static func parseImagePreContents(_ doc: HTMLDocument, pageCount: Int) throws -> [(Int, URL)] {
static func parseImagePreContents(_ doc: HTMLDocument, previewMode: String, pageCount: Int) throws -> [(Int, URL)] {
copyHTMLIfNeeded(doc.toHTML)
var imageDetailURLs = [(Int, URL)]()

let className = isTokenMatched ? "gdtl" : "gdtm"
guard let gdtNode = doc.at_xpath("//div [@id='gdt']")
else { throw AppError.parseFailed }

for (index, element) in gdtNode.xpath("//div [@class='\(className)']").enumerated() {

for (index, element) in gdtNode.xpath("//div [@class='\(previewMode)']").enumerated() {
guard let imageDetailStr = element.at_xpath("//a")?["href"],
let imageDetailURL = URL(string: imageDetailStr)
else { continue }
Expand All @@ -428,6 +426,22 @@ struct Parser {
return MangaContent(tag: tag, url: imageURL)
}

static func parsePreviewMode(_ doc: HTMLDocument) throws -> String {
guard let gdoNode = doc.at_xpath("//div [@id='gdo']"),
let gdo4Node = gdoNode.at_xpath("//div [@id='gdo4']")
else { return "gdtm" }

for link in gdo4Node.xpath("//div") {
if link.text == "Large",
["tha nosel", "ths nosel"]
.contains(link["class"])
{
return "gdtl"
}
}
return "gdtm"
}

// MARK: User
static func parseUserInfo(doc: HTMLDocument) throws -> User {
var displayName: String?
Expand Down
6 changes: 0 additions & 6 deletions EhPanda/App/Utility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,10 @@ func getStringWithComma(_ value: Int) -> String? {
// MARK: UserDefaults
let isDebugModeOn = UserDefaults.standard.bool(forKey: "debugModeOn")

let isTokenMatched = UserDefaults.standard.string(forKey: "token") == "r9vG3pcs2mT9MoWj2ZJR"

var pasteboardChangeCount: Int? {
UserDefaults.standard.integer(forKey: "PasteboardChangeCount")
}

func setToken(with token: String?) {
UserDefaults.standard.set(token, forKey: "token")
}

func setDebugMode(with debugModeOn: Bool) {
UserDefaults.standard.set(debugModeOn, forKey: "debugModeOn")
}
Expand Down
1 change: 0 additions & 1 deletion EhPanda/App/ja.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

// MARK: AlertView
"Loading..." = "読み込み中...";
"You need to login to use this app." = "ご利用にはログインが必要です";
"Login" = "ログイン";
"Your search didn't match any docs." = "お探しの情報が見つかりませんでした";
"Retry" = "やり直す";
Expand Down
1 change: 0 additions & 1 deletion EhPanda/App/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

// MARK: AlertView
"Loading..." = "加载中...";
"You need to login to use this app." = "请登录以使用这个App";
"Login" = "登录";
"Your search didn't match any docs." = "未能找到你需要的信息";
"Retry" = "重试";
Expand Down
1 change: 0 additions & 1 deletion EhPanda/App/zh-Hant.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

// MARK: AlertView
"Loading..." = "載入中...";
"You need to login to use this app." = "你需要登入才能使用這個App";
"Login" = "登入";
"Your search didn't match any docs." = "未能找到你需要的資訊";
"Retry" = "重試";
Expand Down
7 changes: 5 additions & 2 deletions EhPanda/DataFlow/AppAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ enum AppAction {
case initializeUser
case initializeFilter
case initializeSetting
case cleanDetailViewCommentContent
case cleanCommentViewCommentContent
case clearDetailViewCommentContent
case clearCommentViewCommentContent
case saveAspectBox(gid: String, box: [Int: CGFloat])
case saveReadingProgress(gid: String, tag: Int)
case updateDiskImageCacheSize(size: String)
case updateAppIconType(iconType: IconType)
case updateHistoryItems(gid: String)
case updateHistoryKeywords(text: String)
case clearHistoryKeywords
case updateSearchKeyword(text: String)
case updateViewControllersCount
case resetDownloadCommandResponse
case replaceMangaCommentJumpID(gid: String?)
Expand Down
28 changes: 28 additions & 0 deletions EhPanda/DataFlow/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ extension AppState {

@FileStorage(directory: .cachesDirectory, fileName: "historyList.json")
var historyItems: [String: Manga]?
@FileStorage(directory: .cachesDirectory, fileName: "historyKeywords.json")
var historyKeywords: [String]?

static func generateBoolDic(_ defaultValue: Bool = false) -> [Int: Bool] {
var tmp = [Int: Bool]()
Expand Down Expand Up @@ -193,6 +195,32 @@ extension AppState {
)
}
}
mutating func insertHistoryKeyword(text: String) {
guard !text.isEmpty else { return }
guard var historyKeywords = historyKeywords else {
historyKeywords = [text]
return
}

if let index = historyKeywords.firstIndex(of: text) {
if historyKeywords.last != text {
historyKeywords.remove(at: index)
historyKeywords.append(text)
}
} else {
historyKeywords.append(text)

let overflow = historyKeywords.count - 10

if overflow > 0 {
historyKeywords = Array(
historyKeywords.dropFirst(overflow)
)
}
}

self.historyKeywords = historyKeywords
}
}

// MARK: DetailInfo
Expand Down
19 changes: 8 additions & 11 deletions EhPanda/DataFlow/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ final class Store: ObservableObject {
case .updateHistoryItems(let gid):
let item = appState.cachedList.items?[gid]
appState.homeInfo.insertHistoryItem(manga: item)
case .updateHistoryKeywords(let text):
appState.homeInfo.insertHistoryKeyword(text: text)
case .clearHistoryKeywords:
appState.homeInfo.historyKeywords = nil
case .updateSearchKeyword(let text):
appState.homeInfo.searchKeyword = text
case .updateViewControllersCount:
appState.environment.viewControllersCount = viewControllersCount
case .resetDownloadCommandResponse:
Expand Down Expand Up @@ -101,14 +107,13 @@ final class Store: ObservableObject {
case .toggleCommentViewSheetNil:
appState.environment.commentViewSheetState = nil

case .cleanDetailViewCommentContent:
case .clearDetailViewCommentContent:
appState.detailInfo.commentContent = ""
case .cleanCommentViewCommentContent:
case .clearCommentViewCommentContent:
appState.commentInfo.commentContent = ""

// MARK: Fetch Data
case .fetchGreeting:
if !didLogin && isTokenMatched { break }
if appState.settings.greetingLoading { break }
appState.settings.greetingLoading = true

Expand All @@ -129,7 +134,6 @@ final class Store: ObservableObject {
}

case .fetchUserInfo(let uid):
if !didLogin && isTokenMatched { break }
if appState.settings.userInfoLoading { break }
appState.settings.userInfoLoading = true

Expand Down Expand Up @@ -160,7 +164,6 @@ final class Store: ObservableObject {
}

case .fetchMangaItemReverse(let detailURL):
if !didLogin || !isTokenMatched { break }
appState.environment.mangaItemReverseLoadFailed = false

if appState.environment.mangaItemReverseLoading { break }
Expand All @@ -180,7 +183,6 @@ final class Store: ObservableObject {
}

case .fetchSearchItems(let keyword):
if !didLogin && isTokenMatched { break }
appState.homeInfo.searchNotFound = false
appState.homeInfo.searchLoadFailed = false

Expand Down Expand Up @@ -258,7 +260,6 @@ final class Store: ObservableObject {
}

case .fetchFrontpageItems:
if !didLogin && isTokenMatched { break }
appState.homeInfo.frontpageNotFound = false
appState.homeInfo.frontpageLoadFailed = false

Expand Down Expand Up @@ -294,7 +295,6 @@ final class Store: ObservableObject {
case .fetchMoreFrontpageItems:
appState.homeInfo.moreFrontpageLoadFailed = false

if !didLogin || !isTokenMatched { break }
let currentNum = appState.homeInfo.frontpageCurrentPageNum
let maximumNum = appState.homeInfo.frontpagePageNumMaximum
if currentNum + 1 > maximumNum { break }
Expand Down Expand Up @@ -329,7 +329,6 @@ final class Store: ObservableObject {
}

case .fetchPopularItems:
if !didLogin && isTokenMatched { break }
appState.homeInfo.popularNotFound = false
appState.homeInfo.popularLoadFailed = false

Expand All @@ -353,7 +352,6 @@ final class Store: ObservableObject {
}

case .fetchWatchedItems:
if !didLogin || !isTokenMatched { break }
appState.homeInfo.watchedNotFound = false
appState.homeInfo.watchedLoadFailed = false

Expand Down Expand Up @@ -423,7 +421,6 @@ final class Store: ObservableObject {
}

case .fetchFavoritesItems(let favIndex):
if !didLogin || !isTokenMatched { break }
appState.homeInfo.favoritesNotFound[favIndex] = false
appState.homeInfo.favoritesLoadFailed[favIndex] = false

Expand Down
23 changes: 21 additions & 2 deletions EhPanda/Models/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ struct Filter: Codable {
var minRating: Int = 2

var pageRangeActivated = false
var pageLowerBound: String = ""
var pageUpperBound: String = ""
@NumString var pageLowerBound: String = ""
@NumString var pageUpperBound: String = ""

var disableLanguage = false
var disableUploader = false
Expand All @@ -60,3 +60,22 @@ struct AssociatedCategory: Codable {
category.color
}
}

@propertyWrapper
struct NumString: Codable {
private var value = ""
var wrappedValue: String {
get { value }
set {
if let num = Int(newValue) {
value = String(num)
} else if newValue.isEmpty {
value = newValue
}
}
}

init(wrappedValue: String) {
self.wrappedValue = wrappedValue
}
}
2 changes: 1 addition & 1 deletion EhPanda/Models/Manga.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct MangaPreview: Identifiable, Codable {
}

struct MangaAlterData: Identifiable, Codable {
var id: Data { data }
var id = UUID().uuidString

let data: Data
}
Expand Down
7 changes: 6 additions & 1 deletion EhPanda/Network/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,12 @@ struct MangaContentsRequest {
.tryMap { try Kanna.HTML(html: $0.data, encoding: .utf8) }
.tryMap { try (
Parser.parsePageNum($0),
Parser.parseImagePreContents($0, pageCount: pageCount)
Parser.parseImagePreContents(
$0,
previewMode:
Parser.parsePreviewMode($0),
pageCount: pageCount
)
) }
.mapError(mapAppError)
.eraseToAnyPublisher()
Expand Down
4 changes: 2 additions & 2 deletions EhPanda/View/Detail/CommentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private extension CommentView {
}
}
func onLinkTap(_ link: URL) {
if isValidDetailURL(url: link) && isTokenMatched {
if isValidDetailURL(url: link) {
let gid = link.pathComponents[2]
if cachedList.hasCached(gid: gid) {
replaceMangaCommentJumpID(gid: gid)
Expand Down Expand Up @@ -182,7 +182,7 @@ private extension CommentView {

func postComment() {
store.dispatch(.comment(gid: gid, content: commentContent))
store.dispatch(.cleanCommentViewCommentContent)
store.dispatch(.clearCommentViewCommentContent)
}
func fetchMangaWithDetailURL(_ detailURL: String) {
store.dispatch(.fetchMangaItemReverse(detailURL: detailURL))
Expand Down
Loading

0 comments on commit 6006d0e

Please sign in to comment.