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: insights error handling #22

Merged
merged 7 commits into from
Nov 25, 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
128 changes: 76 additions & 52 deletions Sources/Screens/Insights/InsightsChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import Charts
import SwiftUI
import VoltaserveCore

struct InsightsChart: View {
struct InsightsChart: View, ViewDataProvider, LoadStateProvider, TimerLifecycle, TokenDistributing {
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var insightsStore = InsightsStore(pageSize: Constants.pageSize)
@Environment(\.dismiss) private var dismiss
@State private var showError = false
@Environment(\.colorScheme) private var colorScheme
private let fileID: String

init(_ fileID: String) {
Expand All @@ -25,54 +25,59 @@ struct InsightsChart: View {

var body: some View {
NavigationView {
if let entities = insightsStore.entities {
Group {
if entities.count < 5 {
Text("Not enough data to render the chart.")
} else {
Chart(entities) { entity in
SectorMark(
angle: .value(
Text(verbatim: entity.text),
entity.frequency
),
innerRadius: .ratio(0.6),
angularInset: 5
)
.foregroundStyle(
by: .value(
Text(verbatim: entity.text),
entity.text
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
if let entities = insightsStore.entities {
Group {
if entities.count < 5 {
Text("Not enough data to render the chart.")
} else {
Chart(entities) { entity in
SectorMark(
angle: .value(
Text(verbatim: entity.text),
entity.frequency
),
innerRadius: .ratio(0.65),
angularInset: 4
)
)
.cornerRadius(5)
.foregroundStyle(sectorMarkColor)
.annotation(position: .overlay) {
Text("\(entity.text) (\(entity.frequency))")
.font(.footnote)
.padding(.horizontal)
.frame(height: 20)
.background(Color(UIColor.systemBackground))
.clipShape(RoundedRectangle(cornerRadius: 10))
.overlay {
RoundedRectangle(cornerRadius: 10)
.stroke(sectorMarkColor, lineWidth: 1)
}
}
}
.chartLegend(.hidden)
.frame(maxWidth: 300, maxHeight: 300)
}
.modifierIfPad {
$0.frame(maxWidth: 360, maxHeight: 360)
}
.padding(VOMetrics.spacing2Xl)
}
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Insights")
.refreshable {
insightsStore.fetchEntityNextPage(replace: true)
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Done") {
dismiss()
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Insights")
.refreshable {
insightsStore.fetchEntityNextPage(replace: true)
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Done") {
dismiss()
}
}
}
}
} else {
ProgressView()
}
}
.voErrorAlert(
isPresented: $showError,
title: insightsStore.errorTitle,
message: insightsStore.errorMessage
)
.onAppear {
insightsStore.fileID = fileID
if let token = tokenStore.token {
Expand All @@ -85,30 +90,49 @@ struct InsightsChart: View {
insightsStore.clear()
stopTimers()
}
.sync($insightsStore.showError, with: $showError)
}

private func onAppearOrChange() {
private var sectorMarkColor: Color {
colorScheme == .dark ? .gray500 : .gray200
}

private enum Constants {
static let pageSize: Int = 5
}

// MARK: - LoadStateProvider

var isLoading: Bool {
insightsStore.entitiesIsLoadingFirstTime
}

var error: String? {
insightsStore.entitiesError
}

// MARK: - ViewDataProvider

func onAppearOrChange() {
fetchData()
}

private func fetchData() {
func fetchData() {
insightsStore.fetchEntityNextPage(replace: true)
}

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

func startTimers() {
insightsStore.startTimer()
}

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

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

private enum Constants {
static let pageSize: Int = 5
func assignTokenToStores(_ token: VOToken.Value) {
insightsStore.token = token
}
}
49 changes: 31 additions & 18 deletions Sources/Screens/Insights/InsightsCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
import SwiftUI
import VoltaserveCore

struct InsightsCreate: View {
struct InsightsCreate: View, ViewDataProvider, LoadStateProvider, TokenDistributing, FormValidatable, ErrorPresentable {
@EnvironmentObject private var tokenStore: TokenStore
@StateObject private var insightsStore = InsightsStore()
@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 language: VOInsights.Language?
private let fileID: String
Expand Down Expand Up @@ -75,11 +72,7 @@ struct InsightsCreate: View {
ProgressView()
}
}
.voErrorAlert(
isPresented: $showError,
title: insightsStore.errorTitle,
message: insightsStore.errorMessage
)
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
.onAppear {
insightsStore.fileID = fileID
if let token = tokenStore.token {
Expand All @@ -99,39 +92,59 @@ struct InsightsCreate: View {
}
}
.presentationDetents([.fraction(0.45)])
.sync($insightsStore.showError, with: $showError)
}

private func performCreate() {
guard let language else { return }
isCreating = true
withErrorHandling {
_ = try await insightsStore.create(languageID: language.id)
return true
} before: {
isCreating = true
} success: {
dismiss()
} failure: { message in
errorTitle = "Error: Creating Insights"
errorMessage = message
showError = true
errorIsPresented = true
} anyways: {
isCreating = false
}
}

private func isValid() -> Bool {
language != nil
// MARK: - ErrorPresentable

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

// MARK: - LoadStateProvider

var isLoading: Bool {
insightsStore.languagesIsLoadingFirstTime
}

var error: String? {
insightsStore.languagesError
}

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

func onAppearOrChange() {
fetchData()
}

private func fetchData() {
func fetchData() {
insightsStore.fetchLanguages()
}

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

func assignTokenToStores(_ token: VOToken.Value) {
insightsStore.token = token
}

// MARK: - FormValidatable

func isValid() -> Bool {
language != nil
}
}
Loading