Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: file browser and Mosaic viewer minor issues #40

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/Screens/Browser/BrowserStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BrowserStore: ObservableObject {
@Published var folder: VOFile.Entity?
@Published var folderIsLoading: Bool = false
@Published var folderError: String?
@Published var query: VOFile.Query?
@Published var query: VOFile.Query = .init(type: .folder)
private var list: VOFile.List?
private var cancellables = Set<AnyCancellable>()
private var timer: Timer?
Expand All @@ -44,7 +44,11 @@ class BrowserStore: ObservableObject {
.debounce(for: .seconds(1), scheduler: RunLoop.main)
.removeDuplicates()
.sink {
self.query = .init(text: $0, type: .folder)
if $0.isEmpty {
self.query = .init(type: .folder)
} else {
self.query = .init(text: $0, type: .folder)
}
}
.store(in: &cancellables)
}
Expand Down
183 changes: 94 additions & 89 deletions Sources/Screens/Viewer/Mosaic/ViewerMosaic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,111 +25,116 @@ struct ViewerMosaic: View {
}

var body: some View {
if file.type == .file,
let snapshot = file.snapshot,
let download = snapshot.preview,
let fileExtension = download.fileExtension, fileExtension.isImage(), snapshot.mosaic != nil
{
GeometryReader { geometry in
let visibleRect = CGRect(
origin: CGPoint(x: -dragOffset.width, y: -dragOffset.height),
size: geometry.size
)
ZStack {
if let zoomLevel = viewerMosaicStore.zoomLevel, !viewerMosaicStore.grid.isEmpty {
ForEach(0..<zoomLevel.rows, id: \.self) { row in
ForEach(0..<zoomLevel.cols, id: \.self) { col in
let size = viewerMosaicStore.sizeForCell(row: row, col: col)
let position = viewerMosaicStore.positionForCell(row: row, col: col)
let frame = viewerMosaicStore.frameForCellAt(position: position, size: size)
Group {
if file.type == .file,
let snapshot = file.snapshot,
let download = snapshot.preview,
let fileExtension = download.fileExtension, fileExtension.isImage(), snapshot.mosaic != nil
{
GeometryReader { geometry in
let visibleRect = CGRect(
origin: CGPoint(x: -dragOffset.width, y: -dragOffset.height),
size: geometry.size
)
ZStack {
if let zoomLevel = viewerMosaicStore.zoomLevel, !viewerMosaicStore.grid.isEmpty {
ForEach(0..<zoomLevel.rows, id: \.self) { row in
ForEach(0..<zoomLevel.cols, id: \.self) { col in
let size = viewerMosaicStore.sizeForCell(row: row, col: col)
let position = viewerMosaicStore.positionForCell(row: row, col: col)
let frame = viewerMosaicStore.frameForCellAt(position: position, size: size)

// Check if the cell is within the visible bounds or the surrounding buffer
if visibleRect.insetBy(
dx: -CGFloat(Constants.extraTilesToLoad) * size.width,
dy: -CGFloat(Constants.extraTilesToLoad) * size.height
).intersects(frame) {
if let image = viewerMosaicStore.grid[row][col] {
Image(uiImage: image)
.resizable()
.frame(width: size.width, height: size.height)
.position(
x: position.x + dragOffset.width,
y: position.y + dragOffset.height
)
} else {
Rectangle()
.fill(Color.black)
.frame(width: size.width, height: size.height)
.position(
x: position.x + dragOffset.width,
y: position.y + dragOffset.height
)
.onAppear {
viewerMosaicStore.loadImageForCell(file.id, row: row, column: col)
}
// Check if the cell is within the visible bounds or the surrounding buffer
if visibleRect.insetBy(
dx: -CGFloat(Constants.extraTilesToLoad) * size.width,
dy: -CGFloat(Constants.extraTilesToLoad) * size.height
).intersects(frame) {
if let image = viewerMosaicStore.grid[row][col] {
Image(uiImage: image)
.resizable()
.frame(width: size.width, height: size.height)
.position(
x: position.x + dragOffset.width,
y: position.y + dragOffset.height
)
} else {
Rectangle()
.fill(Color.black)
.frame(width: size.width, height: size.height)
.position(
x: position.x + dragOffset.width,
y: position.y + dragOffset.height
)
.onAppear {
viewerMosaicStore.loadImageForCell(file.id, row: row, column: col)
}
}
}
}
}
} else {
Rectangle()
.fill(Color.gray)
.frame(width: geometry.size.width, height: geometry.size.height)
}
} else {
Rectangle()
.fill(Color.gray)
.frame(width: geometry.size.width, height: geometry.size.height)
}
}
.clipped()
.gesture(
DragGesture()
.onChanged { value in
dragOffset = CGSize(
width: lastDragOffset.width + value.translation.width,
height: lastDragOffset.height + value.translation.height
)
}
.onEnded { _ in
lastDragOffset = dragOffset
viewerMosaicStore.unloadImagesOutsideRect(
visibleRect,
extraTilesToLoad: Constants.extraTilesToLoad
)
}
)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Menu {
if let zoomLevels = viewerMosaicStore.info?.metadata.zoomLevels {
ForEach(zoomLevels, id: \.index) { zoomLevel in
Button(
action: {
resetMosaicPosition()
viewerMosaicStore.selectZoomLevel(zoomLevel)
},
label: {
Text("\(Int(zoomLevel.scaleDownPercentage))%")
})
.clipped()
.gesture(
DragGesture()
.onChanged { value in
dragOffset = CGSize(
width: lastDragOffset.width + value.translation.width,
height: lastDragOffset.height + value.translation.height
)
}
.onEnded { _ in
lastDragOffset = dragOffset
viewerMosaicStore.unloadImagesOutsideRect(
visibleRect,
extraTilesToLoad: Constants.extraTilesToLoad
)
}
)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Menu {
if let zoomLevels = viewerMosaicStore.info?.metadata.zoomLevels {
ForEach(zoomLevels, id: \.index) { zoomLevel in
Button(
action: {
resetMosaicPosition()
viewerMosaicStore.selectZoomLevel(zoomLevel)
},
label: {
Text("\(Int(zoomLevel.scaleDownPercentage))%")
})
}
}
} label: {
Image(systemName: "ellipsis.circle")
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
}
.onAppear {
if let token = tokenStore.token {
assignTokenToStores(token)
Task {
try await viewerMosaicStore.loadMosaic(file.id)
.onAppear {
if let token = tokenStore.token {
assignTokenToStores(token)
Task {
try await viewerMosaicStore.loadMosaic(file.id)
}
}
}
}
.onChange(of: tokenStore.token) { _, newToken in
if let newToken {
assignTokenToStores(newToken)
.onChange(of: tokenStore.token) { _, newToken in
if let newToken {
assignTokenToStores(newToken)
}
}
}
}
}
.modifierIfPhone {
$0.edgesIgnoringSafeArea(.horizontal)
}
}

private func assignTokenToStores(_ token: VOToken.Value) {
Expand Down