Skip to content

Commit

Permalink
Merge pull request #34 from shinrenpan/develop
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
shinrenpan authored Nov 4, 2024
2 parents 9c68b85 + 52cf03b commit d6a1a31
Show file tree
Hide file tree
Showing 42 changed files with 283 additions and 251 deletions.
2 changes: 1 addition & 1 deletion Sources/Extensions/AnyCodable+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import AnyCodable
import UIKit

extension AnyCodable {
extension AnyCodable: @unchecked @retroactive Sendable {
subscript(index: Int) -> AnyCodable {
anyArray?[safe: index] ?? AnyCodable(NSNull())
}
Expand Down
18 changes: 16 additions & 2 deletions Sources/Extensions/Kingfisher+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@
//
// Created by Joe Pan on 2024/10/28.
//
//

import UIKit
import Kingfisher

extension Kingfisher.ImageCache {
func asyncCleanDiskCache() async {
await withCheckedContinuation { continuation in
ImageCache.default.clearDiskCache {
clearDiskCache {
continuation.resume(returning: ())
}
}
}
}

extension KingfisherManager {
func asyncRetrieveImage(with resource: Resource, options: KingfisherOptionsInfo? = nil) async throws -> KFCrossPlatformImage {
try await withCheckedThrowingContinuation { continuation in
retrieveImage(with: resource, options: options) { result in
switch result {
case let .success(value):
continuation.resume(returning: value.image)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}
}
10 changes: 3 additions & 7 deletions Sources/Javascripts/Update.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ list.each(function() {
comic.id = $(this).find('a').eq(0).attr('href').split('/').filter(Boolean).pop();
// title
comic.title = $(this).find('a').eq(0).attr('title');
// 圖片
// 圖片有載入是 scr, 未載入前是 data-src
comic.cover = $(this).find('img').eq(0).attr('src') || $(this).find('img').eq(0).attr('data-src');
// 更新進度
comic.note = $(this).find('.tt').eq(0).text();
// 最後更新時間
date = $(this).find('em').eq(0).text();
timestamp = Date.parse(date);
comic.lastUpdate = (timestamp + length) / 1000

// Detail
var detail = new Object();
// 圖片有載入是 scr, 未載入前是 data-src
detail.cover = $(this).find('img').eq(0).attr('src') || $(this).find('img').eq(0).attr('data-src');
comic.detail = detail
results.push(comic);

length--;
});
results;
8 changes: 4 additions & 4 deletions Sources/MVVVR/Detail/DetailModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ extension Detail {
let title: String
let author: String
let description: String?
let cover: String?
let coverURI: String
let favorited: Bool

init(comic: Comic) {
init(comic: Database.Comic) {
self.title = comic.title
self.author = comic.detail?.author ?? "Unknown"
self.description = comic.detail?.desc
self.cover = comic.detail?.cover
self.coverURI = comic.cover
self.favorited = comic.favorited
}
}
Expand All @@ -60,7 +60,7 @@ extension Detail {
hasher.combine(id)
}

init(episode: Comic.Episode, selected: Bool) {
init(episode: Database.Episode, selected: Bool) {
self.id = episode.id
self.title = episode.title
self.selected = selected
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/Detail/DetailRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension Detail {
final class Router {
@MainActor final class Router {
weak var vc: ViewController?

// MARK: - Public
Expand Down
1 change: 1 addition & 0 deletions Sources/MVVVR/Detail/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extension Detail {
_ = withObservationTracking {
vm.state
} onChange: { [weak self] in
guard let self else { return }
Task { @MainActor [weak self] in
guard let self else { return }
if viewIfLoaded?.window == nil { return }
Expand Down
8 changes: 4 additions & 4 deletions Sources/MVVVR/Detail/DetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import WebParser

extension Detail {
@Observable final class ViewModel {
@MainActor @Observable final class ViewModel {
let comicId: String
private(set) var state = State.none
private let parser: Parser
Expand Down Expand Up @@ -62,7 +62,7 @@ extension Detail {
throw ParserError.timeout
}

let result = try await parser.result()
let result = try await parser.anyResult()
await handleLoadRemote(comic: comic, result: result)
actionLoadData()
}
Expand All @@ -81,14 +81,14 @@ extension Detail {

// MARK: - Handle Action Result

private func handleLoadRemote(comic: Comic, result: Any) async {
private func handleLoadRemote(comic: Database.Comic, result: Any) async {
let data = AnyCodable(result)
comic.detail?.author = data["author"].string ?? ""
comic.detail?.desc = data["desc"].string ?? ""

let array = data["episodes"].anyArray ?? []

let episodes: [Comic.Episode] = array.compactMap {
let episodes: [Database.Episode] = array.compactMap {
guard let id = $0["id"].string, !id.isEmpty else {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/Detail/DetailViewOutlet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension Detail {
final class ViewOutlet {
@MainActor final class ViewOutlet {
let mainView = UIView(frame: .zero)
.setup(\.translatesAutoresizingMaskIntoConstraints, value: false)
.setup(\.backgroundColor, value: .white)
Expand Down
18 changes: 8 additions & 10 deletions Sources/MVVVR/Detail/DetailViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ extension Detail {
authorLabel.text = comic.author
descLabel.text = comic.description

if let cover = comic.cover {
coverView.kf.setImage(
with: URL(string: "https:\(cover)"),
options: [
.processor(DownsamplingImageProcessor(size: coverView.frame.size)),
.scaleFactor(UIScreen.main.scale),
.cacheOriginalImage,
]
)
}
coverView.kf.setImage(
with: URL(string: "https:\(comic.coverURI)"),
options: [
.processor(DownsamplingImageProcessor(size: coverView.frame.size)),
.scaleFactor(UIScreen.main.scale),
.cacheOriginalImage,
]
)
}

// MARK: - Setup Something
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/EpisodePicker/EpisodePickerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension EpisodePicker {
hasher.combine(id)
}

init(epidose: Comic.Episode, selected: Bool) {
init(epidose: Database.Episode, selected: Bool) {
self.id = epidose.id
self.title = epidose.title
self.selected = selected
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/EpisodePicker/EpisodePickerRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension EpisodePicker {
final class Router {
@MainActor final class Router {
weak var vc: ViewController?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extension EpisodePicker {
_ = withObservationTracking {
vm.state
} onChange: { [weak self] in
guard let self else { return }
Task { @MainActor [weak self] in
guard let self else { return }
if viewIfLoaded?.window == nil { return }
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/EpisodePicker/EpisodePickerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Observation
import UIKit

extension EpisodePicker {
@Observable final class ViewModel {
@MainActor @Observable final class ViewModel {
private(set) var state = State.none
private let comicId: String
private let epidoseId: String
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/EpisodePicker/EpisodePickerViewOutlet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension EpisodePicker {
final class ViewOutlet {
@MainActor final class ViewOutlet {
let mainView = UIView(frame: .zero)
.setup(\.translatesAutoresizingMaskIntoConstraints, value: false)
.setup(\.backgroundColor, value: .white)
Expand Down
4 changes: 2 additions & 2 deletions Sources/MVVVR/Favorite/FavoriteModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ extension Favorite {
hasher.combine(id)
}

init(comic: Comic) {
init(comic: Database.Comic) {
self.id = comic.id
self.title = comic.title
self.coverURI = comic.detail?.cover ?? ""
self.coverURI = comic.cover
self.lastUpdate = comic.lastUpdate
self.hasNew = comic.hasNew
self.note = comic.note
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/Favorite/FavoriteRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension Favorite {
final class Router {
@MainActor final class Router {
weak var vc: ViewController?

// MARK: - Public
Expand Down
1 change: 1 addition & 0 deletions Sources/MVVVR/Favorite/FavoriteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extension Favorite {
_ = withObservationTracking {
vm.state
} onChange: { [weak self] in
guard let self else { return }
Task { @MainActor [weak self] in
guard let self else { return }
if viewIfLoaded?.window == nil { return }
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/Favorite/FavoriteViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Observation
import UIKit

extension Favorite {
@Observable final class ViewModel {
@MainActor @Observable final class ViewModel {
private(set) var state = State.none

// MARK: - Public
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/Favorite/FavoriteViewOutlet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension Favorite {
final class ViewOutlet {
@MainActor final class ViewOutlet {
let mainView = UIView(frame: .zero)
.setup(\.translatesAutoresizingMaskIntoConstraints, value: false)

Expand Down
4 changes: 2 additions & 2 deletions Sources/MVVVR/History/HistoryModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ extension History {
hasher.combine(id)
}

init(comic: Comic) {
init(comic: Database.Comic) {
self.id = comic.id
self.title = comic.title
self.coverURI = comic.detail?.cover ?? ""
self.coverURI = comic.cover
self.favorited = comic.favorited
self.lastUpdate = comic.lastUpdate
self.hasNew = comic.hasNew
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/History/HistoryRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension History {
final class Router {
@MainActor final class Router {
weak var vc: ViewController?

// MARK: - Public
Expand Down
1 change: 1 addition & 0 deletions Sources/MVVVR/History/HistoryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extension History {
_ = withObservationTracking {
vm.state
} onChange: { [weak self] in
guard let self else { return }
Task { @MainActor [weak self] in
guard let self else { return }
if viewIfLoaded?.window == nil { return }
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/History/HistoryViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Observation
import UIKit

extension History {
@Observable final class ViewModel {
@MainActor @Observable final class ViewModel {
private(set) var state = State.none

// MARK: - Public
Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/History/HistoryViewOutlet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension History {
final class ViewOutlet {
@MainActor final class ViewOutlet {
let mainView = UIView(frame: .zero)
.setup(\.translatesAutoresizingMaskIntoConstraints, value: false)

Expand Down
2 changes: 1 addition & 1 deletion Sources/MVVVR/Reader/ReaderRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import UIKit

extension Reader {
final class Router {
@MainActor final class Router {
weak var vc: ViewController?

// MARK: - Public
Expand Down
1 change: 1 addition & 0 deletions Sources/MVVVR/Reader/ReaderViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ extension Reader {
_ = withObservationTracking {
vm.state
} onChange: { [weak self] in
guard let self else { return }
Task { @MainActor [weak self] in
guard let self else { return }
if viewIfLoaded?.window == nil { return }
Expand Down
19 changes: 7 additions & 12 deletions Sources/MVVVR/Reader/ReaderViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import WebParser

extension Reader {
@Observable final class ViewModel {
@MainActor @Observable final class ViewModel {
let comicId: String
private(set) var episodeId: String
private(set) var state = State.none
Expand Down Expand Up @@ -52,9 +52,8 @@ extension Reader {
let isFavorited = await ComicWorker.shared.getComic(id: comicId)?.favorited ?? false
state = .checkoutFavorited(response: .init(isFavorited: isFavorited))

let result = try await parser.result()
let images = try await makeImagesWithParser(result: result)
imageDatas = images.compactMap { .init(uri: $0.uri) }
let result = try await parser.anyResult()
imageDatas = try await makeImagesWithParser(result: result)

if imageDatas.isEmpty {
state = .dataLoadFail(response: .init(error: .empty))
Expand Down Expand Up @@ -111,14 +110,10 @@ extension Reader {

// MARK: - Make Something

private func makeImagesWithParser(result: Any) async throws -> [Comic.ImageData] {
private func makeImagesWithParser(result: Any) async throws -> [ImageData] {
let array = AnyCodable(result).anyArray ?? []

let result: [Comic.ImageData] = array.compactMap {
guard let index = $0["index"].int else {
return nil
}

let result: [ImageData] = array.compactMap {
guard let uri = $0["uri"].string, !uri.isEmpty else {
return nil
}
Expand All @@ -127,7 +122,7 @@ extension Reader {
return nil
}

return .init(index: index, uri: uriDecode)
return .init(uri: uriDecode)
}

if result.isEmpty {
Expand All @@ -148,7 +143,7 @@ extension Reader {

// MARK: - Get Something

private func getCurrentEpisode() async -> Comic.Episode? {
private func getCurrentEpisode() async -> Database.Episode? {
let episodes = await ComicWorker.shared.getEpisodes(comicId: comicId)
return episodes.first(where: { $0.id == episodeId })
}
Expand Down
Loading

0 comments on commit d6a1a31

Please sign in to comment.