Skip to content

Commit

Permalink
support split wide image (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraemoe authored May 11, 2024
1 parent 97e2f21 commit b96b0f9
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 99 deletions.
8 changes: 4 additions & 4 deletions LANreader.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 90;
CURRENT_PROJECT_VERSION = 91;
DEVELOPMENT_ASSET_PATHS = "\"LANreader/Preview Content\"";
DEVELOPMENT_TEAM = UUEBW58SA6;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -899,7 +899,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 90;
CURRENT_PROJECT_VERSION = 91;
DEVELOPMENT_ASSET_PATHS = "\"LANreader/Preview Content\"";
DEVELOPMENT_TEAM = UUEBW58SA6;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -978,7 +978,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 90;
CURRENT_PROJECT_VERSION = 91;
DEVELOPMENT_TEAM = UUEBW58SA6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Action/Info.plist;
Expand Down Expand Up @@ -1007,7 +1007,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 90;
CURRENT_PROJECT_VERSION = 91;
DEVELOPMENT_TEAM = UUEBW58SA6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Action/Info.plist;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-composable-architecture",
"state" : {
"revision" : "e6c0959044cbe2c934ac0ce9c3c4ead86350f44f",
"version" : "1.10.2"
"revision" : "024130da0e37121a6ab30f451da4bc462ee4a0ed",
"version" : "1.10.3"
}
},
{
Expand All @@ -132,8 +132,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-dependencies",
"state" : {
"revision" : "d3a5af3038a09add4d7682f66555d6212058a3c0",
"version" : "1.2.2"
"revision" : "350e1e119babe8525f9bd155b76640a5de270184",
"version" : "1.3.0"
}
},
{
Expand Down Expand Up @@ -168,8 +168,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax",
"state" : {
"revision" : "fa8f95c2d536d6620cc2f504ebe8a6167c9fc2dd",
"version" : "510.0.1"
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion LANreader/Common/ArchiveListV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ struct ArchiveListV2: View {
if store.lanraragiUrl.isEmpty == false &&
store.archives.isEmpty && store.loadOnAppear {
store.send(.load(true))
} else if !store.archives.isEmpty {
} else if !store.archivesToDisplay.isEmpty {
store.send(.refreshDisplayArchives)
}
}
Expand Down
2 changes: 2 additions & 0 deletions LANreader/Models/AppModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ struct SettingsKey {
static let compressImageThreshold = "settings.read.image.compress.threshold"
static let showOriginal = "settings.read.image.showOriginal"
static let fallbackReader = "settings.read.fallback"
static let splitWideImage = "settings.read.split.Image"
static let splitPiorityLeft = "settings.read.split.piorityLeft"

static let archiveListOrder = "settings.archive.list.order"
static let searchSort = "settings.search.sort"
Expand Down
97 changes: 60 additions & 37 deletions LANreader/Page/ArchiveReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import NotificationBannerSwift
@SharedReader(.appStorage(SettingsKey.readDirection)) var readDirection = ReadDirection.leftRight.rawValue
@SharedReader(.appStorage(SettingsKey.fallbackReader)) var fallbackReader = false
@SharedReader(.appStorage(SettingsKey.serverProgress)) var serverProgress = false
@SharedReader(.appStorage(SettingsKey.splitWideImage)) var splitImage = false
@SharedReader(.appStorage(SettingsKey.splitPiorityLeft)) var piorityLeft = false
@Shared var archive: ArchiveItem
@Shared var archiveThumbnail: Data?

var index: Int?
var indexString: String?
var sliderIndex: Double = 0
var pages: IdentifiedArrayOf<PageFeature.State> = []
var fromStart = false
Expand All @@ -27,8 +29,11 @@ import NotificationBannerSwift
var errorMessage = ""
var successMessage = ""

var fallbackIndex: Int {
index ?? 0
var fallbackIndexString: String {
indexString ?? ""
}
var currentIndex: Int? {
pages.index(id: indexString ?? "")
}

init(archive: Shared<ArchiveItem>, fromStart: Bool = false) {
Expand All @@ -52,9 +57,9 @@ import NotificationBannerSwift
case finishExtracting([String])
case toggleControlUi(Bool?)
case preload(Int)
case setIndex(Int)
case setIndexString(String)
case setSliderIndex(Double)
case updateProgress
case updateProgress(Int)
case setIsNew(Bool)
case setThumbnail
case setThumbnailData(Data)
Expand Down Expand Up @@ -92,19 +97,20 @@ import NotificationBannerSwift
case let .finishExtracting(pages):
let pageState = pages.enumerated().map { (index, page) in
let normalizedPage = String(page.dropFirst(2))
return PageFeature.State(id: index, pageId: normalizedPage)
return PageFeature.State(archiveId: state.archive.id, pageId: normalizedPage, pageNumber: index + 1)
}
state.pages.append(contentsOf: pageState)
state.extracting = false
let progress = state.archive.progress > 0 ? state.archive.progress - 1 : 0
state.index = state.fromStart ? 0 : progress
state.sliderIndex = Double(progress)
let pageIndexToShow = state.fromStart ? 0 : progress
state.sliderIndex = Double(pageIndexToShow)
state.indexString = state.pages[pageIndexToShow].id
state.controlUiHidden = true
return .none
case .loadProgress:
let progress = state.archive.progress > 0 ? state.archive.progress - 1 : 0
state.index = state.fromStart ? 0 : progress
state.sliderIndex = Double(progress)
state.indexString = state.pages[progress].id
state.controlUiHidden = true
return .none
case let .toggleControlUi(show):
Expand All @@ -115,28 +121,31 @@ import NotificationBannerSwift
}
return .none
case let .preload(index):
return .run(priority: .utility) { [totalPage = state.pages.count] send in
return .run(priority: .utility) { [state] send in
if index - 1 > 0 {
await send(.page(.element(id: index - 1, action: .load(false))))
let previousPageId = state.pages[index-1].id
await send(.page(.element(id: previousPageId, action: .load(false))))
}
if index + 1 < totalPage {
await send(.page(.element(id: index + 1, action: .load(false))))
if index + 1 < state.pages.count {
let nextPageId = state.pages[index+1].id
await send(.page(.element(id: nextPageId, action: .load(false))))
}
}
case let .setIndex(index):
state.index = index
case let .setIndexString(indexString):
state.indexString = indexString
return .none
case let .setSliderIndex(index):
state.sliderIndex = index
return .none
case .updateProgress:
let progress = (state.index ?? 0) + 1
state.archive.progress = progress
case let .updateProgress(pageNumber):
state.archive.progress = pageNumber
return .run(priority: .background) { [state] send in
if state.serverProgress {
_ = try await service.updateArchiveReadProgress(id: state.archive.id, progress: progress).value
_ = try await service.updateArchiveReadProgress(
id: state.archive.id, progress: pageNumber
).value
}
if progress > 1 && state.archive.isNew {
if pageNumber > 1 && state.archive.isNew {
_ = try await service.clearNewFlag(id: state.archive.id).value
await send(.setIsNew(false))
}
Expand All @@ -149,9 +158,9 @@ import NotificationBannerSwift
return .none
case .setThumbnail:
state.settingThumbnail = true
let index = (state.index ?? 0) + 1
guard let pageNumber = state.pages[id: state.indexString ?? ""]?.pageNumber else {return .none }
return .run { [id = state.archive.id] send in
_ = try await service.updateArchiveThumbnail(id: id, page: index).value
_ = try await service.updateArchiveThumbnail(id: id, page: pageNumber).value
let imageData = try await service.retrieveArchiveThumbnail(id: id).serializingData().value
await send(.setThumbnailData(imageData))
let successMessage = String(localized: "archive.thumbnail.set")
Expand All @@ -170,15 +179,15 @@ import NotificationBannerSwift
case let .tapAction(action):
switch action {
case PageControl.next.rawValue:
if let pageIndex = state.index {
if pageIndex < state.archive.pagecount - 1 {
state.index! += 1
if let pageIndex = state.currentIndex {
if pageIndex < state.pages.count - 1 {
state.indexString = state.pages[pageIndex + 1].id
}
}
case PageControl.previous.rawValue:
if let pageIndex = state.index {
if let pageIndex = state.currentIndex {
if pageIndex > 0 {
state.index! -= 1
state.indexString = state.pages[pageIndex - 1].id
}
}
case PageControl.navigation.rawValue:
Expand All @@ -196,6 +205,19 @@ import NotificationBannerSwift
return .none
case .binding:
return .none
case let .page(.element(id: id, action: .insertPage(mode))):
guard let current = state.pages[id: id] else { return .none }
let currentIndex = state.pages.index(id: id)!
state.pages.insert(
PageFeature.State(
archiveId: state.archive.id,
pageId: current.pageId,
pageNumber: current.pageNumber,
pageMode: mode
),
at: currentIndex + 1
)
return .none
case .page:
return .none
}
Expand Down Expand Up @@ -249,8 +271,6 @@ struct ArchiveReader: View {
.onAppear {
if store.pages.isEmpty {
store.send(.extractArchive)
} else {
store.send(.loadProgress)
}
if store.archive.extension == "rar" || store.archive.extension == "cbr" {
let banner = NotificationBanner(
Expand All @@ -261,11 +281,13 @@ struct ArchiveReader: View {
banner.show()
}
}
.onChange(of: store.index) { _, newValue in
if let index = newValue {
.onChange(of: store.indexString) { _, newValue in
if let id = newValue {
let index = store.pages.index(id: id) ?? 0
let pageNumber = store.pages[id: id]?.pageNumber ?? 1
store.send(.preload(index))
store.send(.setSliderIndex(Double(index)))
store.send(.updateProgress)
store.send(.updateProgress(pageNumber))
}
}
.onChange(of: store.errorMessage) {
Expand Down Expand Up @@ -313,7 +335,7 @@ struct ArchiveReader: View {
}
.scrollTargetLayout()
}
.scrollPosition(id: $store.index)
.scrollPosition(id: $store.indexString)
.onTapGesture {
store.send(.tapAction(PageControl.navigation.rawValue))
}
Expand All @@ -340,7 +362,7 @@ struct ArchiveReader: View {
.scrollTargetLayout()
}
.scrollTargetBehavior(.paging)
.scrollPosition(id: $store.index)
.scrollPosition(id: $store.indexString)
.onTapGesture { location in
if location.x < geometry.size.width / 3 {
store.send(.tapAction(store.tapLeft))
Expand All @@ -357,7 +379,7 @@ struct ArchiveReader: View {
store: StoreOf<ArchiveReaderFeature>,
geometry: GeometryProxy
) -> some View {
TabView(selection: $store.fallbackIndex.sending(\.setIndex)) {
TabView(selection: $store.fallbackIndexString.sending(\.setIndexString)) {
ForEach(
store.scope(
state: \.pages,
Expand Down Expand Up @@ -391,7 +413,7 @@ struct ArchiveReader: View {
Grid {
GridRow {
Button(action: {
store.send(.page(.element(id: store.index ?? 0, action: .load(true))))
store.send(.page(.element(id: store.indexString ?? "", action: .load(true))))
}, label: {
Image(systemName: "arrow.clockwise")
})
Expand All @@ -415,7 +437,8 @@ struct ArchiveReader: View {
step: 1
) { onSlider in
if !onSlider {
store.send(.setIndex(store.sliderIndex.int))
let indexString = store.pages[store.sliderIndex.int].id
store.send(.setIndexString(indexString))
}
}
.padding(.horizontal)
Expand Down
Loading

0 comments on commit b96b0f9

Please sign in to comment.