Skip to content

Commit

Permalink
fix: file error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Nov 24, 2024
1 parent 6a101f5 commit f56074c
Show file tree
Hide file tree
Showing 25 changed files with 328 additions and 310 deletions.
8 changes: 4 additions & 4 deletions Sources/Screens/Browser/BrowserStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ class BrowserStore: ObservableObject {
}
}
}
if let current = self.folder {
if self.folder != nil {
Task {
let file = try await self.fetchFolder()
if let file {
let folder = try await self.fetchFolder()
if let folder {
DispatchQueue.main.async {
self.folder = file
self.folder = folder
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/Screens/File/FileGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import SwiftUI
import VoltaserveCore

struct FileGrid: View {
struct FileGrid: View, ListItemScrollable {
@ObservedObject private var fileStore: FileStore
@ObservedObject private var workspaceStore: WorkspaceStore
@State private var tappedItem: VOFile.Entity?
Expand Down Expand Up @@ -71,8 +71,10 @@ struct FileGrid: View {
}
}
}

// MARK: - ListItemScrollable

private func onListItemAppear(_ id: String) {
func onListItemAppear(_ id: String) {
if fileStore.isEntityThreshold(id) {
fileStore.fetchNextPage()
}
Expand Down
223 changes: 120 additions & 103 deletions Sources/Screens/File/FileInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import SwiftUI
import VoltaserveCore

struct FileInfo: View {
struct FileInfo: View, ViewDataProvider, LoadStateProvider, TimerLifecycle, TokenDistributing {
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var fileStore = FileStore()
@Environment(\.dismiss) private var dismiss
@State private var showError = false
private let file: VOFile.Entity

init(_ file: VOFile.Entity) {
Expand All @@ -24,103 +23,111 @@ struct FileInfo: View {

var body: some View {
NavigationView {
Form {
Section(header: VOSectionHeader("Properties")) {
NavigationLink {
Form {
Text(file.name)
}
.navigationTitle(file.name)
} label: {
HStack {
Text("Name")
Spacer()
Text(file.name)
.lineLimit(1)
.truncationMode(.middle)
.foregroundStyle(.secondary)
}
}
HStack {
Text("Type")
Spacer()
Text(fileType)
.foregroundStyle(.secondary)
}
if let fileExtension = file.snapshot?.original.fileExtension {
HStack {
Text("Extension")
Spacer()
VOColorBadge(fileExtension, color: .gray300, style: .fill)
}
}
HStack {
Text("Permission")
Spacer()
VOPermissionBadge(file.permission)
}
}
if let image = file.snapshot?.original.image {
Section(header: VOSectionHeader("Image")) {
HStack {
Text("Dimensions")
Spacer()
Text("\(image.width)x\(image.height)")
.foregroundStyle(.secondary)
}
}
}
if let document = file.snapshot?.original.document {
Section(header: VOSectionHeader("Document")) {
if let pages = document.pages {
VStack {
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
Form {
Section(header: VOSectionHeader("Properties")) {
NavigationLink {
Form {
Text(file.name)
}
.navigationTitle(file.name)
} label: {
HStack {
Text("Name")
Spacer()
Text(file.name)
.lineLimit(1)
.truncationMode(.middle)
.foregroundStyle(.secondary)
}
}
HStack {
Text("Pages")
Text("Type")
Spacer()
Text("\(pages.count)")
Text(fileType)
.foregroundStyle(.secondary)
}
if let fileExtension = file.snapshot?.original.fileExtension {
HStack {
Text("Extension")
Spacer()
VOColorBadge(fileExtension, color: .gray300, style: .fill)
}
}
HStack {
Text("Permission")
Spacer()
VOPermissionBadge(file.permission)
}
}
}
}
if file.type == .folder {
HStack {
Text("Item Count")
Spacer()
if let itemCount = fileStore.itemCount {
Text("\(itemCount)")
.foregroundStyle(.secondary)
} else {
Text("Calculating…")
.foregroundStyle(.secondary)
if let image = file.snapshot?.original.image {
Section(header: VOSectionHeader("Image")) {
HStack {
Text("Dimensions")
Spacer()
Text("\(image.width)x\(image.height)")
.foregroundStyle(.secondary)
}
}
}
}
}
Section(header: VOSectionHeader("Storage")) {
VStack(alignment: .leading) {
if let storageUsage = fileStore.storageUsage {
Text("\(storageUsage.bytes.prettyBytes()) of \(storageUsage.maxBytes.prettyBytes()) used")
ProgressView(value: Double(storageUsage.percentage) / 100.0)
} else {
Text("Calculating…")
ProgressView()
if let document = file.snapshot?.original.document {
Section(header: VOSectionHeader("Document")) {
if let pages = document.pages {
HStack {
Text("Pages")
Spacer()
Text("\(pages.count)")
.foregroundStyle(.secondary)
}
}
}
}
}
}
Section(header: VOSectionHeader("Time")) {
if let createTime = file.createTime.date?.pretty {
HStack {
Text("Create time")
Spacer()
Text(createTime)
.foregroundStyle(.secondary)
if file.type == .folder {
HStack {
Text("Item Count")
Spacer()
if let itemCount = fileStore.itemCount {
Text("\(itemCount)")
.foregroundStyle(.secondary)
} else {
Text("Calculating…")
.foregroundStyle(.secondary)
}
}
}
}
if let updateTime = file.updateTime?.date?.pretty {
HStack {
Text("Update time")
Spacer()
Text(updateTime)
.foregroundStyle(.secondary)
Section(header: VOSectionHeader("Storage")) {
VStack(alignment: .leading) {
if let storageUsage = fileStore.storageUsage {
Text("\(storageUsage.bytes.prettyBytes()) of \(storageUsage.maxBytes.prettyBytes()) used")
ProgressView(value: Double(storageUsage.percentage) / 100.0)
} else {
Text("Calculating…")
ProgressView()
}
}
}
Section(header: VOSectionHeader("Time")) {
if let createTime = file.createTime.date?.pretty {
HStack {
Text("Create time")
Spacer()
Text(createTime)
.foregroundStyle(.secondary)
}
}
if let updateTime = file.updateTime?.date?.pretty {
HStack {
Text("Update time")
Spacer()
Text(updateTime)
.foregroundStyle(.secondary)
}
}
}
}
}
Expand All @@ -135,13 +142,8 @@ struct FileInfo: View {
}
}
}
.voErrorAlert(
isPresented: $showError,
title: fileStore.errorTitle,
message: fileStore.errorMessage
)
.onAppear {
fileStore.current = file
fileStore.file = file
if let token = tokenStore.token {
assignTokenToStores(token)
startTimers()
Expand All @@ -152,7 +154,6 @@ struct FileInfo: View {
fileStore.clear()
stopTimers()
}
.sync($fileStore.showError, with: $showError)
}

private var fileType: String {
Expand All @@ -161,27 +162,43 @@ struct FileInfo: View {
case .folder: "Folder"
}
}

// MARK: - LoadStateProvider

var isLoading: Bool {
fileStore.storageUsageIsLoading || fileStore.itemCountIsLoading
}

var error: String? {
fileStore.storageUsageError ?? fileStore.itemCountError
}

// MARK: - ViewDataProvider

private func onAppearOrChange() {
func onAppearOrChange() {
fetchData()
}

private func fetchData() {
func fetchData() {
fileStore.fetchStorageUsage()
if file.type == .folder {
fileStore.fetchItemCount()
}
}

// MARK: - TimerLifecycle

private func startTimers() {
func startTimers() {
fileStore.startTimer()
}

private func stopTimers() {
func stopTimers() {
fileStore.stopTimer()
}

// MARK: - TokenDistributing

private func assignTokenToStores(_ token: VOToken.Value) {
func assignTokenToStores(_ token: VOToken.Value) {
fileStore.token = token
}
}
10 changes: 5 additions & 5 deletions Sources/Screens/File/FileList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import SwiftUI
import VoltaserveCore

struct FileList: View {
struct FileList: View, ListItemScrollable {
@ObservedObject private var fileStore: FileStore
@ObservedObject private var workspaceStore: WorkspaceStore
@State private var selection = Set<String>()
@State private var tappedItem: VOFile.Entity?

init(fileStore: FileStore, workspaceStore: WorkspaceStore) {
Expand All @@ -24,7 +23,7 @@ struct FileList: View {

var body: some View {
if let entities = fileStore.entities {
List(selection: $selection) {
List(selection: $fileStore.selection) {
ForEach(entities, id: \.id) { file in
if file.type == .file {
Button {
Expand Down Expand Up @@ -56,11 +55,12 @@ struct FileList: View {
.navigationDestination(item: $tappedItem) {
Viewer($0)
}
.sync($fileStore.selection, with: $selection)
}
}

// MARK: - ListItemScrollable

private func onListItemAppear(_ id: String) {
func onListItemAppear(_ id: String) {
if fileStore.isEntityThreshold(id) {
fileStore.fetchNextPage()
}
Expand Down
Loading

0 comments on commit f56074c

Please sign in to comment.