Skip to content

Commit 5a64664

Browse files
authored
Merge pull request #181 from makinosp/develop
Develop
2 parents a208511 + b9aa7ed commit 5a64664

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

Harmonie.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

harmonie/Models/ApplicationError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ struct ApplicationError: LocalizedError {
2222
ApplicationError(text: "User is not set")
2323
}
2424
}
25+
26+
extension ApplicationError {
27+
init(_ error: Error) {
28+
text = error.localizedDescription
29+
}
30+
}

harmonie/Modifiers/View+ErrorAlertModifier.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,44 @@ extension View {
1414
}
1515
}
1616

17-
@MemberwiseInit
17+
@MainActor @MemberwiseInit
1818
private struct ErrorAlertModifier {
1919
@Environment(AppViewModel.self) var appVM: AppViewModel
2020
@Init(.internal, escaping: true, label: "_") private let action: () -> Void
21+
22+
typealias ErrorKeyPath<E> = ReferenceWritableKeyPath<AppViewModel, E?>
23+
24+
func isThrownError<E>(_ errorKeyPath: ErrorKeyPath<E>) -> Binding<Bool> where E: Error {
25+
Binding<Bool>(
26+
get: { appVM[keyPath: errorKeyPath] != nil },
27+
set: { isErrorActive in
28+
guard !isErrorActive else { return }
29+
appVM[keyPath: errorKeyPath] = nil
30+
}
31+
)
32+
}
2133
}
2234

2335
extension ErrorAlertModifier: ViewModifier {
2436
func body(content: Content) -> some View {
2537
@Bindable var appVM = appVM
2638
content
2739
.alert(
28-
isPresented: $appVM.isPresentedAlert,
40+
isPresented: isThrownError(\.vrckError),
2941
error: appVM.vrckError
3042
) { _ in
3143
Button("OK") {
3244
action()
33-
appVM.vrckError = nil
45+
}
46+
} message: { error in
47+
Text(error.failureReason ?? "Try again later.")
48+
}
49+
.alert(
50+
isPresented: isThrownError(\.applicationError),
51+
error: appVM.applicationError
52+
) { _ in
53+
Button("OK") {
54+
action()
3455
}
3556
} message: { error in
3657
Text(error.failureReason ?? "Try again later.")

harmonie/ViewModels/AppViewModel.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import VRCKit
1313
final class AppViewModel {
1414
var user: User?
1515
var step: Step = .initializing
16-
var isPresentedAlert = false
1716
var vrckError: VRCKitError?
17+
var applicationError: ApplicationError?
1818
var services: APIServiceUtil
1919
var verifyType: VerifyType?
2020
@ObservationIgnored var client: APIClient
@@ -128,9 +128,10 @@ final class AppViewModel {
128128
return
129129
}
130130
vrckError = error
131+
} else if let error = error as? ApplicationError {
132+
applicationError = error
131133
} else if !error.isCancelled {
132-
vrckError = .unexpected
134+
applicationError = ApplicationError(error)
133135
}
134-
isPresentedAlert = vrckError != nil
135136
}
136137
}

harmonie/Views/Favorite/FavoritesView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ struct FavoritesView: View {
155155
_ title: any StringProtocol,
156156
count: Int,
157157
max: Constants.MaxCountInFavoriteList
158-
) -> some View {
158+
) -> LabeledContent<some View, some View> {
159159
LabeledContent {
160160
Text("\(count.description) / \(max.description)")
161161
} label: {

0 commit comments

Comments
 (0)