Skip to content

Commit

Permalink
fix: Mosaic error handling (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Nov 26, 2024
1 parent 1612adc commit dec96cc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
24 changes: 11 additions & 13 deletions Sources/Screens/Mosaic/MosaicCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
import SwiftUI
import VoltaserveCore

struct MosaicCreate: View {
struct MosaicCreate: View, ErrorPresentable, TokenDistributing {
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var mosaicStore = MosaicStore()
@Environment(\.dismiss) private var dismiss
@Environment(\.colorScheme) private var colorScheme
@State private var showError = false
@State private var errorTitle: String?
@State private var errorMessage: String?
@State private var isCreating = false
private let fileID: String

Expand Down Expand Up @@ -63,11 +60,7 @@ struct MosaicCreate: View {
}
}
}
.voErrorAlert(
isPresented: $showError,
title: mosaicStore.errorTitle,
message: mosaicStore.errorMessage
)
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
.onAppear {
mosaicStore.fileID = fileID
if let token = tokenStore.token {
Expand All @@ -80,7 +73,6 @@ struct MosaicCreate: View {
}
}
.presentationDetents([.fraction(0.35)])
.sync($mosaicStore.showError, with: $showError)
}

private func performCreate() {
Expand All @@ -91,15 +83,21 @@ struct MosaicCreate: View {
} success: {
dismiss()
} failure: { message in
errorTitle = "Error: Creating Mosaic"
errorMessage = message
showError = true
errorIsPresented = true
} anyways: {
isCreating = false
}
}

private func assignTokenToStores(_ token: VOToken.Value) {
// MARK: - ErrorPresentable

@State var errorIsPresented: Bool = false
@State var errorMessage: String?

// MARK: - TokenDistributing

func assignTokenToStores(_ token: VOToken.Value) {
mosaicStore.token = token
}
}
49 changes: 30 additions & 19 deletions Sources/Screens/Mosaic/MosaicSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
import SwiftUI
import VoltaserveCore

struct MosaicSettings: View {
struct MosaicSettings: View, ViewDataProvider, LoadStateProvider, TimerLifecycle, TokenDistributing, ErrorPresentable {
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var mosaicStore = MosaicStore()
@Environment(\.dismiss) private var dismiss
@Environment(\.colorScheme) private var colorScheme
@State private var showError = false
@State private var errorTitle: String?
@State private var errorMessage: String?
@State private var isCreating = false
@State private var isDeleting = false
private let file: VOFile.Entity
Expand Down Expand Up @@ -85,11 +82,7 @@ struct MosaicSettings: View {
}
}
}
.voErrorAlert(
isPresented: $showError,
title: mosaicStore.errorTitle,
message: mosaicStore.errorMessage
)
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
.onAppear {
mosaicStore.fileID = file.id
if let token = tokenStore.token {
Expand All @@ -108,7 +101,6 @@ struct MosaicSettings: View {
}
}
.presentationDetents([.fraction(UIDevice.current.userInterfaceIdiom == .pad ? 0.50 : 0.40)])
.sync($mosaicStore.showError, with: $showError)
}

private var canCreate: Bool {
Expand Down Expand Up @@ -137,9 +129,8 @@ struct MosaicSettings: View {
} success: {
dismiss()
} failure: { message in
errorTitle = "Error: Creating Mosaic"
errorMessage = message
showError = true
errorIsPresented = true
} anyways: {
isCreating = false
}
Expand All @@ -153,33 +144,53 @@ struct MosaicSettings: View {
} success: {
dismiss()
} failure: { message in
errorTitle = "Error: Deleting Mosaic"
errorMessage = message
showError = true
errorIsPresented = true
} anyways: {
isDeleting = false
}
}

private func onAppearOrChange() {
// MARK: - LoadStateProvider

var isLoading: Bool {
mosaicStore.infoIsLoading
}

var error: String? {
mosaicStore.infoError
}

// MARK: - ErrorPresentable

@State var errorIsPresented: Bool = false
@State var errorMessage: String?

// MARK: - ViewDataProvider

func onAppearOrChange() {
fetchData()
}

private func fetchData() {
func fetchData() {
if let snapshot = file.snapshot, snapshot.hasMosaic() {
mosaicStore.fetchInfo()
}
}

private func startTimers() {
// MARK: - TimerLifecycle

func startTimers() {
mosaicStore.startTimer()
}

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

private func assignTokenToStores(_ token: VOToken.Value) {
// MARK: - TokenDistributing

func assignTokenToStores(_ token: VOToken.Value) {
mosaicStore.token = token
}
}
13 changes: 7 additions & 6 deletions Sources/Screens/Mosaic/MosaicStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import VoltaserveCore

class MosaicStore: ObservableObject {
@Published var info: VOMosaic.Info?
@Published var showError = false
@Published var errorTitle: String?
@Published var errorMessage: String?
@Published var infoIsLoading: Bool = false
@Published var infoError: String?
private var mosaicClient: VOMosaic?
private var timer: Timer?
var fileID: String?
Expand All @@ -42,12 +41,14 @@ class MosaicStore: ObservableObject {
withErrorHandling {
info = try await self.fetchInfo()
return true
} before: {
self.infoIsLoading = true
} success: {
self.info = info
} failure: { message in
self.errorTitle = "Error: Fetching Mosaic Info"
self.errorMessage = message
self.showError = true
self.infoError = message
} anyways: {
self.infoIsLoading = false
}
}

Expand Down

0 comments on commit dec96cc

Please sign in to comment.