gitbay/Repos/LoadState.swift
25 lines · 817 bytes
1import Foundation
2
3/// One screen's loading lifecycle. `empty` is a first-class state, not a
4/// failure — exit 3 lands here, and so does a list with nothing in it.
5nonisolated enum LoadState<Value: Sendable>: Sendable {
6 case loading
7 case loaded(Value)
8 case empty(String)
9 case failed(String)
10
11 var value: Value? {
12 if case .loaded(let value) = self { return value }
13 return nil
14 }
15}
16
17extension LoadState {
18 /// Map any thrown error onto the state a screen should show.
19 static func from(_ error: any Error) -> LoadState {
20 guard let error = error as? GitbayError else {
21 return .failed(GitbayError.transport(error).userFacingMessage)
22 }
23 return error.isEmptyState ? .empty(error.userFacingMessage) : .failed(error.userFacingMessage)
24 }
25}