Skip to content

Commit

Permalink
- Changed makeRoute to throw RoutableError instead of returning o…
Browse files Browse the repository at this point in the history
…ptional.
  • Loading branch information
boscojwho committed Sep 25, 2023
1 parent 59afaf7 commit 3840110
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 61 deletions.
28 changes: 10 additions & 18 deletions Mlem/Extensions/View - Handle Lemmy Links.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,21 @@ struct HandleLemmyLinkResolution<Path: AnyNavigationPath>: ViewModifier {
}

return await MainActor.run {
switch resolution {
case let .post(object):
if let route = Path.makeRoute(object) {
navigationPath.wrappedValue.append(route)
do {
switch resolution {
case let .post(object):
navigationPath.wrappedValue.append(try Path.makeRoute(object))
return true
} else {
return false
}
case let .person(object):
if let route = Path.makeRoute(object.person) {
navigationPath.wrappedValue.append(route)
case let .person(object):
navigationPath.wrappedValue.append(try Path.makeRoute(object.person))
return true
} else {
return false
}
case let .community(object):
if let route = Path.makeRoute(object) {
navigationPath.wrappedValue.append(route)
case let .community(object):
navigationPath.wrappedValue.append(try Path.makeRoute(object))
return true
} else {
case .comment:
return false
}
case .comment:
} catch {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Navigation/AnyNavigationPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protocol AnyNavigationPath {
associatedtype Route: Routable

/// Implementation should make a route that makes sense for the passed-in data value and can be appended to the navigation path.
static func makeRoute<V>(_ value: V) -> Route? where V: Hashable
static func makeRoute<V>(_ value: V) throws -> Route where V: Hashable

/// The number of elements in this path.
var count: Int { get }
Expand Down
11 changes: 7 additions & 4 deletions Mlem/Navigation/Routable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ protocol Routable: Hashable {

/// - Parameter value: A data type for a given navigation destination.
/// - Returns: `nil` if data value cannot be mapped to a navigation route.
static func makeRoute<V>(_ value: V) -> Self? where V: Hashable
static func makeRoute<V>(_ value: V) throws -> Self where V: Hashable

/// Generic error string
static var makeRouteErrorString: String { get }
}

enum RoutableError<V: Hashable>: LocalizedError {
case routeNotConfigured(value: V)
}

extension Routable {

/// Default implementation.
static func makeRoute<V>(_ value: V) -> Self? where V: Hashable {
static func makeRoute<V>(_ value: V) throws -> Self where V: Hashable {
switch value {
case let value as Self:
return value
default:
print(Self.makeRouteErrorString)
return nil
throw RoutableError.routeNotConfigured(value: value)
}
}

Expand Down
47 changes: 11 additions & 36 deletions Mlem/Navigation/Route/SettingsRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,24 @@ enum SettingsRoute: Routable {
case postPage(PostSettingsRoute)
case licensesPage(LicensesSettingsRoute)

// swiftlint:disable cyclomatic_complexity
static func makeRoute<V>(_ value: V) -> SettingsRoute? where V: Hashable {
static func makeRoute<V>(_ value: V) throws -> SettingsRoute where V: Hashable {
switch value {
case let value as Self:
return value
case let value as AboutSettingsRoute:
if let route = AboutSettingsRoute.makeRoute(value) {
return .aboutPage(route)
} else {
return nil
}
return try .aboutPage(AboutSettingsRoute.makeRoute(value))
case let value as AppearanceSettingsRoute:
if let route = AppearanceSettingsRoute.makeRoute(value) {
return .appearancePage(route)
} else {
return nil
}
return try .appearancePage(AppearanceSettingsRoute.makeRoute(value))
case let value as CommentSettingsRoute:
if let route = CommentSettingsRoute.makeRoute(value) {
return .commentPage(route)
} else {
return nil
}
return try .commentPage(CommentSettingsRoute.makeRoute(value))
case let value as PostSettingsRoute:
if let route = PostSettingsRoute.makeRoute(value) {
return .postPage(route)
} else {
return nil
}
return try .postPage(PostSettingsRoute.makeRoute(value))
case let value as LicensesSettingsRoute:
if let route = LicensesSettingsRoute.makeRoute(value) {
return .licensesPage(route)
} else {
return nil
}
return try .licensesPage(LicensesSettingsRoute.makeRoute(value))
default:
print(Self.makeRouteErrorString)
return nil
throw RoutableError.routeNotConfigured(value: value)
}
}
// swiftlint:enable cyclomatic_complexity
}

enum AppearanceSettingsRoute: Routable, Codable {
Expand All @@ -89,30 +66,28 @@ enum AboutSettingsRoute: Routable {
case eula(Document)
case licenses

static func makeRoute<V>(_ value: V) -> AboutSettingsRoute? where V: Hashable {
static func makeRoute<V>(_ value: V) throws -> AboutSettingsRoute where V: Hashable {
switch value {
case let value as Self:
return value
case let value as Document:
// return .privacyPolicy(value)
return .eula(value)
default:
print(Self.makeRouteErrorString)
return nil
throw RoutableError.routeNotConfigured(value: value)
}
}
}

enum LicensesSettingsRoute: Routable {
case licenseDocument(Document)

static func makeRoute<V>(_ value: V) -> LicensesSettingsRoute? where V: Hashable {
static func makeRoute<V>(_ value: V) throws -> LicensesSettingsRoute where V: Hashable {
switch value {
case let value as Document:
return .licenseDocument(value)
default:
print(Self.makeRouteErrorString)
return nil
throw RoutableError.routeNotConfigured(value: value)
}
}
}
4 changes: 2 additions & 2 deletions Mlem/Navigation/Router/NavigationRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ extension NavigationRouter: AnyNavigationPath {

typealias Route = RouteValue

static func makeRoute<V>(_ value: V) -> Route? where V: Hashable {
RouteValue.makeRoute(value) ?? nil
static func makeRoute<V>(_ value: V) throws -> Route where V: Hashable {
try RouteValue.makeRoute(value)
}

var count: Int {
Expand Down

0 comments on commit 3840110

Please sign in to comment.